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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Dominik Solenicki |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "DivisionController.h" |
---|
30 | |
---|
31 | |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | |
---|
35 | RegisterClass(DivisionController); |
---|
36 | |
---|
37 | //Leaders share the fact that they have Wingmans |
---|
38 | DivisionController::DivisionController(Context* context) : LeaderController(context) |
---|
39 | { |
---|
40 | RegisterObject(DivisionController); |
---|
41 | |
---|
42 | this->setFormationMode(FormationMode::DIAMOND); |
---|
43 | this->target_ = 0; |
---|
44 | this->myFollower_ = 0; |
---|
45 | this->myWingman_ = 0; |
---|
46 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DivisionController::action, this))); |
---|
47 | this->rank_ = Rank::DIVISIONLEADER; |
---|
48 | |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | DivisionController::~DivisionController() |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
57 | { |
---|
58 | SUPER(DivisionController, XMLPort, xmlelement, mode); |
---|
59 | |
---|
60 | //XMLPortParam(DivisionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void DivisionController::tick(float dt) |
---|
65 | { |
---|
66 | if (!this->isActive()) |
---|
67 | return; |
---|
68 | if (this->bHasTargetPosition_) |
---|
69 | { |
---|
70 | this->moveToTargetPosition(dt); |
---|
71 | } |
---|
72 | else if (this->bLookAtTarget_) |
---|
73 | { |
---|
74 | this->lookAtTarget(dt); |
---|
75 | } |
---|
76 | if (bShooting_) |
---|
77 | { |
---|
78 | this->doFire(); |
---|
79 | } |
---|
80 | |
---|
81 | SUPER(DivisionController, tick, dt); |
---|
82 | |
---|
83 | } |
---|
84 | void DivisionController::action() |
---|
85 | { |
---|
86 | |
---|
87 | |
---|
88 | if (this->action_ == Action::FIGHT) |
---|
89 | { |
---|
90 | if (!this->hasTarget()) |
---|
91 | { |
---|
92 | //----find a target---- |
---|
93 | this->setClosestTarget(); |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | //----fly in formation if far enough---- |
---|
98 | Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); |
---|
99 | if (diffVector.length() > 3000) |
---|
100 | { |
---|
101 | this->setTargetPositionOfWingman(); |
---|
102 | this->setTargetPositionOfFollower(); |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | //----wingmans shall support the fire of their leaders---- |
---|
107 | if (this->myWingman_) |
---|
108 | { |
---|
109 | this->myWingman_->setAction (Action::FIGHT, this->target_); |
---|
110 | } |
---|
111 | if (this->myFollower_) |
---|
112 | { |
---|
113 | this->myFollower_->setAction (Action::FIGHT); |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|
119 | if (this->hasTarget()) |
---|
120 | { |
---|
121 | //----choose where to go---- |
---|
122 | this->maneuver(); |
---|
123 | //----fire if you can---- |
---|
124 | this->bShooting_ = this->canFire(); |
---|
125 | } |
---|
126 | |
---|
127 | } |
---|
128 | else if (this->action_ == Action::FLY) |
---|
129 | { |
---|
130 | this->setTargetPositionOfWingman(); |
---|
131 | this->setTargetPositionOfFollower(); |
---|
132 | } |
---|
133 | else if (this->action_ == Action::PROTECT) |
---|
134 | { |
---|
135 | if (!this->getProtect()) |
---|
136 | { |
---|
137 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
138 | { |
---|
139 | if ((*itP)->getName() == this->protectName_) |
---|
140 | { |
---|
141 | this->setProtect (static_cast<ControllableEntity*>(*itP)); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | /* if (this->myWingman_) |
---|
148 | this->myWingman_->setAction (Action::PROTECT, this->getProtect()); |
---|
149 | if (this->myFollower_) |
---|
150 | this->myFollower_->setAction (Action::PROTECT, this->getProtect()); |
---|
151 | */ |
---|
152 | Vector3* targetRelativePosition; |
---|
153 | |
---|
154 | targetRelativePosition = new Vector3 (0, 0, 500); |
---|
155 | |
---|
156 | Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + |
---|
157 | (this->getProtect()->getWorldOrientation()* (*targetRelativePosition))); |
---|
158 | this->setTargetPosition(targetAbsolutePosition); |
---|
159 | |
---|
160 | this->setTargetPositionOfWingman(); |
---|
161 | this->setTargetPositionOfFollower(); |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | void DivisionController::setTargetPositionOfWingman() |
---|
171 | { |
---|
172 | if (!this->myWingman_) |
---|
173 | return; |
---|
174 | Vector3* targetRelativePositionOfWingman; |
---|
175 | switch (this->formationMode_){ |
---|
176 | case FormationMode::WALL: |
---|
177 | { |
---|
178 | targetRelativePositionOfWingman = new Vector3 (400, 0, 0); |
---|
179 | break; |
---|
180 | } |
---|
181 | case FormationMode::FINGER4: |
---|
182 | { |
---|
183 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
---|
184 | break; |
---|
185 | } |
---|
186 | |
---|
187 | case FormationMode::DIAMOND: |
---|
188 | { |
---|
189 | targetRelativePositionOfWingman = new Vector3 (400, 0, 200); |
---|
190 | break; |
---|
191 | } |
---|
192 | } |
---|
193 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
194 | |
---|
195 | Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
---|
196 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
---|
197 | |
---|
198 | myWingman_->setAction( Action::FLY, targetAbsolutePositionOfWingman, orient); |
---|
199 | |
---|
200 | } |
---|
201 | void DivisionController::setTargetPositionOfFollower() |
---|
202 | { |
---|
203 | if (!this->myFollower_) |
---|
204 | return; |
---|
205 | this->myFollower_->setFormationMode(this->formationMode_); |
---|
206 | |
---|
207 | Vector3* targetRelativePositionOfFollower; |
---|
208 | switch (this->formationMode_){ |
---|
209 | case FormationMode::WALL: |
---|
210 | { |
---|
211 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 0); |
---|
212 | break; |
---|
213 | } |
---|
214 | case FormationMode::FINGER4: |
---|
215 | { |
---|
216 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
---|
217 | break; |
---|
218 | } |
---|
219 | |
---|
220 | case FormationMode::DIAMOND: |
---|
221 | { |
---|
222 | targetRelativePositionOfFollower = new Vector3 (-400, 0, 200); |
---|
223 | break; |
---|
224 | } |
---|
225 | } |
---|
226 | Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
227 | |
---|
228 | Vector3 targetAbsolutePositionOfFollower = ((this->getControllableEntity()->getWorldPosition()) + |
---|
229 | (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfFollower))); |
---|
230 | |
---|
231 | myFollower_->setAction ( Action::FLY, targetAbsolutePositionOfFollower, orient ); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | bool DivisionController::setWingman(CommonController* cwingman) |
---|
236 | { |
---|
237 | |
---|
238 | WeakPtr<WingmanController> wingman = orxonox_cast<WingmanController*>(cwingman); |
---|
239 | if (!this->myWingman_) |
---|
240 | { |
---|
241 | this->myWingman_ = wingman; |
---|
242 | return true; |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | return false; |
---|
247 | } |
---|
248 | |
---|
249 | } |
---|
250 | bool DivisionController::setFollower(LeaderController* myFollower) |
---|
251 | { |
---|
252 | if (!this->myFollower_) |
---|
253 | { |
---|
254 | this->myFollower_ = myFollower; |
---|
255 | return true; |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | return false; |
---|
260 | } |
---|
261 | } |
---|
262 | bool DivisionController::hasWingman() |
---|
263 | { |
---|
264 | if (this->myWingman_) |
---|
265 | return true; |
---|
266 | else |
---|
267 | return false; |
---|
268 | } |
---|
269 | bool DivisionController::hasFollower() |
---|
270 | { |
---|
271 | if (this->myFollower_) |
---|
272 | return true; |
---|
273 | else |
---|
274 | return false; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | } |
---|