1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Oli Scheuss |
---|
24 | * Co-authors: |
---|
25 | * Damian 'Mozork' Frick |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "PacmanGhost.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
33 | |
---|
34 | namespace orxonox |
---|
35 | { |
---|
36 | RegisterClass(PacmanGhost); |
---|
37 | |
---|
38 | /** |
---|
39 | @brief |
---|
40 | Constructor. Registers the object and initializes some default values. |
---|
41 | @param creator |
---|
42 | The creator of this object. |
---|
43 | */ |
---|
44 | PacmanGhost::PacmanGhost(Context* context) : ControllableEntity(context) |
---|
45 | { |
---|
46 | RegisterObject(PacmanGhost); |
---|
47 | |
---|
48 | this->velocity = Vector3(0, 0, 0); |
---|
49 | |
---|
50 | this->setCollisionType(CollisionType::Dynamic); |
---|
51 | |
---|
52 | this->resetposition = Vector3(0,20,245); //Set Default start position |
---|
53 | |
---|
54 | this->actuelposition = this->getPosition(); |
---|
55 | |
---|
56 | this->target_x = actuelposition.x; |
---|
57 | this->target_z = actuelposition.z; |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | @brief |
---|
63 | Destructor. Destroys controller, if present. |
---|
64 | */ |
---|
65 | PacmanGhost::~PacmanGhost() |
---|
66 | { |
---|
67 | // Deletes the controller if the object was initialized and the pointer to the controller is not NULL. |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | @brief |
---|
72 | Method for creating a AutonomousDrone through XML. |
---|
73 | */ |
---|
74 | void PacmanGhost::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
75 | { |
---|
76 | SUPER(PacmanGhost, XMLPort, xmlelement, mode); |
---|
77 | |
---|
78 | XMLPortParam(PacmanGhost, "resetposition", setResetPosition, getResetPosition, xmlelement, mode); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | Vector3 possibleposition[] = {Vector3(0,10,245),Vector3(215,10,245),Vector3(215,10,220)}; |
---|
84 | |
---|
85 | /** |
---|
86 | @brief |
---|
87 | Defines which actions the AutonomousDrone has to take in each tick. |
---|
88 | @param dt |
---|
89 | The length of the tick. |
---|
90 | */ |
---|
91 | void PacmanGhost::tick(float dt) |
---|
92 | { |
---|
93 | SUPER(PacmanGhost, tick, dt); |
---|
94 | |
---|
95 | //setorientation |
---|
96 | |
---|
97 | this->actuelposition = this->getPosition(); |
---|
98 | |
---|
99 | //Stop, if target arrived |
---|
100 | if((abs(this->actuelposition.x - this->target_x)<0.1) && (abs(this->actuelposition.z - this->target_z)<0.1)){ |
---|
101 | this->ismoving = false; |
---|
102 | } |
---|
103 | |
---|
104 | //Move, if ghost hasn't arrived yet |
---|
105 | if(this->ismoving){ |
---|
106 | if(!(abs(this->actuelposition.z-target_z)<0.1)) { |
---|
107 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,-sgn(this->actuelposition.z-this->target_z)); |
---|
108 | move(dt, actuelposition, velocity); |
---|
109 | } |
---|
110 | if(!(abs(this->actuelposition.x-target_x)<0.1)){ |
---|
111 | velocity = Vector3(-sgn(this->actuelposition.x-this->target_x),0,-sgn(this->actuelposition.z-this->target_z)); |
---|
112 | move(dt, actuelposition, velocity); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | //Check on which position ghost has arrived and set new target |
---|
117 | else{ |
---|
118 | if((abs(this->actuelposition.x - possibleposition[0].x)<0.1) && (abs(this->actuelposition.z - possibleposition[0].z)<0.1)){ |
---|
119 | decision = rand()%1; |
---|
120 | switch(decision){ |
---|
121 | case 0: |
---|
122 | this->target_x = possibleposition[1].x; |
---|
123 | this->target_z = possibleposition[1].z; |
---|
124 | this->ismoving = true; |
---|
125 | } |
---|
126 | |
---|
127 | } |
---|
128 | else if((abs(actuelposition.x - possibleposition[1].x)<0.1) && (abs(actuelposition.z - possibleposition[1].z)<0.1)){ |
---|
129 | decision = rand()%2; |
---|
130 | switch(decision){ |
---|
131 | case 0: |
---|
132 | this->target_x = possibleposition[0].x; |
---|
133 | this->target_z = possibleposition[0].z; |
---|
134 | this->ismoving = true; |
---|
135 | break; |
---|
136 | case 1: |
---|
137 | this->target_x = possibleposition[2].x; |
---|
138 | this->target_z = possibleposition[2].z; |
---|
139 | this->ismoving = true; |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | if((abs(this->actuelposition.x - possibleposition[2].x)<0.1) && (abs(this->actuelposition.z - possibleposition[2].z)<0.1)){ |
---|
144 | decision = rand()%1; |
---|
145 | switch(decision){ |
---|
146 | case 0: |
---|
147 | this->target_x = possibleposition[1].x; |
---|
148 | this->target_z = possibleposition[1].z; |
---|
149 | this->ismoving = true; |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | else{ |
---|
155 | } //End of Position table |
---|
156 | } |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void PacmanGhost::move(float dt, Vector3 actuelposition, Vector3 velocity){ |
---|
162 | this->setPosition(Vector3(actuelposition.x+20*velocity.x*dt,10,actuelposition.z+20*velocity.z*dt)); |
---|
163 | } |
---|
164 | |
---|
165 | void PacmanGhost::resetGhost(){ |
---|
166 | this->setPosition(resetposition); |
---|
167 | } |
---|
168 | } |
---|