1 | |
---|
2 | #include "PacmanBrown.h" |
---|
3 | |
---|
4 | #include "core/CoreIncludes.h" |
---|
5 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
6 | |
---|
7 | |
---|
8 | namespace orxonox{ |
---|
9 | |
---|
10 | RegisterClass(PacmanBrown); |
---|
11 | |
---|
12 | PacmanBrown::PacmanBrown(Context* context) : PacmanGhost(context){ |
---|
13 | |
---|
14 | RegisterObject(PacmanBrown); |
---|
15 | |
---|
16 | this->target_x=0; |
---|
17 | this->target_z=15; |
---|
18 | this->lastPlayerPassedPoint=Vector3(0,0,0); |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | @brief |
---|
24 | Method for creating a ghost through XML. |
---|
25 | */ |
---|
26 | void PacmanBrown::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
27 | { |
---|
28 | SUPER(PacmanBrown, XMLPort, xmlelement, mode); |
---|
29 | } |
---|
30 | |
---|
31 | |
---|
32 | int PacmanBrown::absoluteDistance(Vector3 pos1, Vector3 pos2){ |
---|
33 | |
---|
34 | |
---|
35 | Vector3 diffVector; |
---|
36 | diffVector.x=pos2.x-pos1.x; |
---|
37 | diffVector.y=pos2.y-pos1.y; //should always be 0 |
---|
38 | diffVector.z=pos2.z-pos1.z; |
---|
39 | int result = sqrt((diffVector.x)*(diffVector.x)+(diffVector.z)*(diffVector.z)); |
---|
40 | return result; |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | void PacmanBrown::tick(float dt) |
---|
45 | { |
---|
46 | SUPER(PacmanGhost, tick, dt); |
---|
47 | |
---|
48 | this->actuelposition = this->getPosition(); |
---|
49 | |
---|
50 | |
---|
51 | for(int u=0; u < 67; u++){//always check if player passed a point |
---|
52 | if(jeanfindpos(this->getPlayerPos(), possibleposition[u])){ |
---|
53 | this->lastPlayerPassedPoint=possibleposition[u]; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | //Stop, if target arrived |
---|
59 | if((abs(this->actuelposition.x - this->target_x)<0.5) && (abs(this->actuelposition.z - this->target_z)<0.5)){ |
---|
60 | this->ismoving = false; |
---|
61 | } |
---|
62 | |
---|
63 | //Move, if ghost hasn't arrived yet |
---|
64 | if(this->ismoving){ |
---|
65 | if(!(abs(this->actuelposition.z-target_z)<0.5)) { |
---|
66 | velocity = Vector3(0,0,-sgn(this->actuelposition.z-this->target_z)); |
---|
67 | move(dt, actuelposition, velocity); |
---|
68 | } |
---|
69 | if(!(abs(this->actuelposition.x-target_x)<0.5)){ |
---|
70 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,0); |
---|
71 | move(dt, actuelposition, velocity); |
---|
72 | } |
---|
73 | } |
---|
74 | else if(this->lastPlayerPassedPoint==Vector3(0,0,0)){ |
---|
75 | //as long as the player has not started the game, |
---|
76 | //i.e. lastPlayerPastPoint is (0,0,0), brown pacman |
---|
77 | //cannot possibly move, because it needs the position |
---|
78 | //of the player to move accordingly |
---|
79 | |
---|
80 | this->ismoving=false; |
---|
81 | } |
---|
82 | |
---|
83 | //Check on which position the ghost has arrived and set new target |
---|
84 | else{ |
---|
85 | while(lockmove){}; |
---|
86 | lockmove = true; |
---|
87 | |
---|
88 | //do brown behavior |
---|
89 | //put everything needed here |
---|
90 | |
---|
91 | Vector3 brownPos=Vector3(this->target_x, 10, this->target_z); |
---|
92 | |
---|
93 | |
---|
94 | if(this->isFleeing==true){ |
---|
95 | if(findpos(brownPos, Vector3(-215,10,-195))){ |
---|
96 | this->isFleeing=false; |
---|
97 | } |
---|
98 | else{ |
---|
99 | if(findpos(this->lastPlayerPassedPoint, Vector3(-215,10,-195))){ |
---|
100 | Vector3 nextMoveP = getShortestPath(brownPos, Vector3(-215,10,-195)); |
---|
101 | this->setNewTargetGhost(nextMoveP); |
---|
102 | } |
---|
103 | else{ |
---|
104 | Vector3 nextMoveP = getShortestPath(brownPos, Vector3(-215,10,-195), this->lastPlayerPassedPoint); |
---|
105 | this->setNewTargetGhost(nextMoveP); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|
110 | else { |
---|
111 | Vector3 arrayPlaNeig[4]; |
---|
112 | |
---|
113 | findNeighboorPositions(this->lastPlayerPassedPoint, arrayPlaNeig, possibleposition); |
---|
114 | if(isAdjacentToPlayerLastPastPoint(brownPos, arrayPlaNeig)){ |
---|
115 | this->isFleeing=true; |
---|
116 | } |
---|
117 | else{ |
---|
118 | Vector3 nextMove = getShortestPath(brownPos, this->lastPlayerPassedPoint); |
---|
119 | setNewTargetGhost(nextMove); |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | lockmove=false; //NEVER FORGET THIS ONE !!! |
---|
126 | |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | bool PacmanBrown::isAdjacentToPlayerLastPastPoint(Vector3 pacmanBrownPos, Vector3 arrayForNeighborPositions[]){ |
---|
132 | //return true if brownPacman is on an adjacent position to the last |
---|
133 | //point visited by the player. return false otherwise. |
---|
134 | |
---|
135 | for(int i =0; i < 4; i++){ |
---|
136 | if(arrayForNeighborPositions[i]!=Vector3(0,0,0)){ |
---|
137 | if(findpos(pacmanBrownPos, arrayForNeighborPositions[i])){ |
---|
138 | return true; |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | return false; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | } |
---|
148 | |
---|