1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
18 | |
---|
19 | #include "hover.h" |
---|
20 | |
---|
21 | #include "weapons/weapon_manager.h" |
---|
22 | #include "weapons/test_gun.h" |
---|
23 | #include "weapons/turret.h" |
---|
24 | #include "weapons/cannon.h" |
---|
25 | |
---|
26 | #include "util/loading/factory.h" |
---|
27 | //#include "util/loading/resource_manager.h" |
---|
28 | |
---|
29 | #include "key_mapper.h" |
---|
30 | #include "state.h" |
---|
31 | |
---|
32 | #include "graphics_engine.h" |
---|
33 | |
---|
34 | #include "debug.h" |
---|
35 | |
---|
36 | CREATE_FACTORY(Hover, CL_HOVER); |
---|
37 | |
---|
38 | #include "script_class.h" |
---|
39 | CREATE_SCRIPTABLE_CLASS(Hover, CL_HOVER, |
---|
40 | addMethod("hasPlayer", ExecutorLua0ret<Playable,bool>(&Playable::hasPlayer)) |
---|
41 | //Coordinates |
---|
42 | ->addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor)) |
---|
43 | ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX)) |
---|
44 | ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY)) |
---|
45 | ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ)) |
---|
46 | ); |
---|
47 | |
---|
48 | /** |
---|
49 | * destructs the hover, deletes alocated memory |
---|
50 | */ |
---|
51 | Hover::~Hover () |
---|
52 | { |
---|
53 | this->setPlayer(NULL); |
---|
54 | |
---|
55 | //if (this->hoverBuffer != NULL) |
---|
56 | // ResourceManager::getInstance()->unload(this->hoverBuffer); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * loads a Hover information from a specified file. |
---|
61 | * @param fileName the name of the File to load the hover from (absolute path) |
---|
62 | */ |
---|
63 | Hover::Hover(const std::string& fileName) |
---|
64 | { |
---|
65 | this->init(); |
---|
66 | TiXmlDocument doc(fileName); |
---|
67 | |
---|
68 | if(!doc.LoadFile()) |
---|
69 | { |
---|
70 | PRINTF(2)("Loading file %s failed for Hover.\n", fileName.c_str()); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | this->loadParams(doc.RootElement()); |
---|
75 | |
---|
76 | //load sound |
---|
77 | //if (this->hoverBuffer != NULL) |
---|
78 | // ResourceManager::getInstance()->unload(this->hoverBuffer); |
---|
79 | //this->hoverBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/engine/hover.wav", WAV); |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * creates a new Spaceship from Xml Data |
---|
85 | * @param root the xml element containing spaceship data |
---|
86 | |
---|
87 | @todo add more parameters to load |
---|
88 | */ |
---|
89 | Hover::Hover(const TiXmlElement* root) |
---|
90 | { |
---|
91 | this->init(); |
---|
92 | if (root != NULL) |
---|
93 | this->loadParams(root); |
---|
94 | |
---|
95 | //weapons: |
---|
96 | Weapon* wpRight = new TestGun(0); |
---|
97 | wpRight->setName("testGun Right"); |
---|
98 | Weapon* wpLeft = new TestGun(1); |
---|
99 | wpLeft->setName("testGun Left"); |
---|
100 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER)); |
---|
101 | |
---|
102 | cannon->setName("BFG"); |
---|
103 | |
---|
104 | this->addWeapon(wpLeft, 1, 0); |
---|
105 | this->addWeapon(wpRight,1 ,1); |
---|
106 | this->addWeapon(cannon, 0, 2); |
---|
107 | |
---|
108 | this->getWeaponManager().changeWeaponConfig(1); |
---|
109 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
110 | |
---|
111 | this->loadModel("models/ships/hoverglider_mainbody.obj"); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * initializes a Hover |
---|
117 | */ |
---|
118 | void Hover::init() |
---|
119 | { |
---|
120 | |
---|
121 | //this->hitEntity = NULL; |
---|
122 | //this->hoverBuffer = NULL; |
---|
123 | |
---|
124 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
125 | this->setClassID(CL_HOVER, "Hover"); |
---|
126 | this->toReflectionList(); |
---|
127 | |
---|
128 | this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3); |
---|
129 | this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4); |
---|
130 | this->loadModel("models/ships/rotor.obj", .45f, 5); |
---|
131 | |
---|
132 | bForward = bBackward = bLeft = bRight = bAscend = bDescend = false; |
---|
133 | mouseSensitivity = 0.005; |
---|
134 | |
---|
135 | this->rotorSpeed = 1000.0f; |
---|
136 | this->rotorCycle = 0.0f; |
---|
137 | this->cameraLook = 0.0f; |
---|
138 | this->rotation = 0.0f; |
---|
139 | this->acceleration = 15.0f; |
---|
140 | this->airFriction = 3.0f; |
---|
141 | |
---|
142 | // camera - issue |
---|
143 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
144 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
145 | //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT); |
---|
146 | this->cameraNode.setParent(this); |
---|
147 | |
---|
148 | // rotors |
---|
149 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT ); |
---|
150 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
151 | this->wingNodeLeft.setParent(this); |
---|
152 | this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0); |
---|
153 | this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
154 | this->rotorNodeLeft.setParent(&this->wingNodeLeft); |
---|
155 | this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3); |
---|
156 | |
---|
157 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
158 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
159 | this->wingNodeRight.setParent(this); |
---|
160 | this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0); |
---|
161 | this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
162 | this->rotorNodeRight.setParent(&this->wingNodeRight); |
---|
163 | this->rotorNodeRight.setRelCoor(0, 1.0, 2.3); |
---|
164 | |
---|
165 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
166 | |
---|
167 | |
---|
168 | //add events to the eventlist |
---|
169 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
170 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
171 | registerEvent(KeyMapper::PEV_LEFT); |
---|
172 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
173 | registerEvent(KeyMapper::PEV_UP); |
---|
174 | registerEvent(KeyMapper::PEV_DOWN); |
---|
175 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
176 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
177 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
178 | registerEvent(EV_MOUSE_MOTION); |
---|
179 | |
---|
180 | |
---|
181 | // WEAPON_MANAGER configuration |
---|
182 | this->getWeaponManager().setSlotCount(5); |
---|
183 | |
---|
184 | this->getWeaponManager().setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft); |
---|
185 | this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
186 | |
---|
187 | this->getWeaponManager().setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight); |
---|
188 | this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
189 | |
---|
190 | this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003)); |
---|
191 | this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY); |
---|
192 | |
---|
193 | /// TODO: THESE ARE TOO MUCH |
---|
194 | this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652)); |
---|
195 | this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0))); |
---|
196 | |
---|
197 | this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652)); |
---|
198 | this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0))); |
---|
199 | |
---|
200 | this->cameraNode.setRelCoor(1,5,0); |
---|
201 | this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode); |
---|
202 | this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0); |
---|
203 | |
---|
204 | // NETWORK THINGS |
---|
205 | |
---|
206 | registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) ); |
---|
207 | registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) ); |
---|
208 | registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) ); |
---|
209 | registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) ); |
---|
210 | registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) ); |
---|
211 | registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) ); |
---|
212 | |
---|
213 | registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) ); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * loads the Settings of a Hover from an XML-element. |
---|
218 | * @param root the XML-element to load the Spaceship's properties from |
---|
219 | */ |
---|
220 | void Hover::loadParams(const TiXmlElement* root) |
---|
221 | { |
---|
222 | WorldEntity::loadParams(root); |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void Hover::enter() |
---|
227 | { |
---|
228 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true); |
---|
229 | |
---|
230 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
---|
231 | State::getCameraNode()->setRelCoorSoft(-10, 0,0); |
---|
232 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
---|
233 | |
---|
234 | //this->soundSource.play(this->hoverBuffer, 0.3f, true); |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | void Hover::leave() |
---|
239 | { |
---|
240 | dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false); |
---|
241 | this->detachCamera(); |
---|
242 | |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | /** |
---|
247 | * effect that occurs after the Hover is spawned |
---|
248 | */ |
---|
249 | void Hover::postSpawn () |
---|
250 | { |
---|
251 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * the action occuring if the hover left the game |
---|
256 | */ |
---|
257 | void Hover::leftWorld () |
---|
258 | {} |
---|
259 | |
---|
260 | /** |
---|
261 | * this function is called, when two entities collide |
---|
262 | * @param entity: the world entity with whom it collides |
---|
263 | * |
---|
264 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
265 | */ |
---|
266 | void Hover::collidesWith(WorldEntity* entity, const Vector& location) |
---|
267 | { |
---|
268 | Playable::collidesWith(entity, location); |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | /** |
---|
274 | * the function called for each passing timeSnap |
---|
275 | * @param time The timespan passed since last update |
---|
276 | */ |
---|
277 | void Hover::tick (float dt) |
---|
278 | { |
---|
279 | Playable::tick(dt); |
---|
280 | |
---|
281 | // spaceship controlled movement |
---|
282 | this->movement(dt); |
---|
283 | this->rotorCycle += this->rotorSpeed * dt; |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * calculate the velocity |
---|
288 | * @param time the timeslice since the last frame |
---|
289 | */ |
---|
290 | void Hover::movement (float dt) |
---|
291 | { |
---|
292 | Vector accel(0.0, 0.0, 0.0); |
---|
293 | |
---|
294 | if( this->bForward ) |
---|
295 | { |
---|
296 | accel += Vector(this->acceleration, 0, 0); |
---|
297 | } |
---|
298 | |
---|
299 | if( this->bBackward ) |
---|
300 | { |
---|
301 | accel -= Vector(this->acceleration, 0, 0); |
---|
302 | } |
---|
303 | if( this->bLeft) |
---|
304 | { |
---|
305 | accel -= Vector(0, 0, this->acceleration); |
---|
306 | } |
---|
307 | |
---|
308 | if( this->bRight) |
---|
309 | { |
---|
310 | accel += Vector(0, 0, this->acceleration); |
---|
311 | } |
---|
312 | |
---|
313 | if (this->bAscend ) |
---|
314 | { |
---|
315 | accel += Vector(0, this->acceleration, 0); |
---|
316 | } |
---|
317 | if (this->bDescend ) |
---|
318 | { |
---|
319 | accel -= Vector(0, this->acceleration, 0); |
---|
320 | } |
---|
321 | |
---|
322 | Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration); |
---|
323 | |
---|
324 | // this is the air friction (necessary for a smooth control) |
---|
325 | Vector damping = (this->velocity * this->airFriction); |
---|
326 | |
---|
327 | |
---|
328 | this->velocity += (accelerationDir - damping)* dt; |
---|
329 | |
---|
330 | this->shiftCoor (this->velocity * dt); |
---|
331 | |
---|
332 | // limit the maximum rotation speed. |
---|
333 | if (this->rotation != 0.0f) |
---|
334 | { |
---|
335 | float maxRot = 10.0 * dt; |
---|
336 | if (unlikely(this->rotation > maxRot)) this->rotation = maxRot; |
---|
337 | if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot; |
---|
338 | this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0)); |
---|
339 | this->rotation = 0.0f; |
---|
340 | } |
---|
341 | |
---|
342 | this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5); |
---|
343 | |
---|
344 | this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .03 +this->rotation, Vector(1,0,0)), 5); |
---|
345 | this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .05+this->rotation + cameraLook, Vector(0,0,1)), 5); |
---|
346 | |
---|
347 | this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .03 +this->rotation, Vector(1,0,0)), 5); |
---|
348 | this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.05 -this->rotation + cameraLook, Vector(0,0,1)), 5); |
---|
349 | } |
---|
350 | |
---|
351 | |
---|
352 | void Hover::draw() const |
---|
353 | { |
---|
354 | Vector tmpRot; |
---|
355 | WorldEntity::draw(); |
---|
356 | |
---|
357 | glPushMatrix(); |
---|
358 | /// LEFT SIDE |
---|
359 | glTranslatef (this->wingNodeLeft.getAbsCoor ().x, |
---|
360 | this->wingNodeLeft.getAbsCoor ().y, |
---|
361 | this->wingNodeLeft.getAbsCoor ().z); |
---|
362 | tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis(); |
---|
363 | glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
364 | this->getModel(3)->draw(); |
---|
365 | glPopMatrix (); |
---|
366 | |
---|
367 | glPushMatrix(); |
---|
368 | glTranslatef (this->rotorNodeLeft.getAbsCoor ().x, |
---|
369 | this->rotorNodeLeft.getAbsCoor ().y, |
---|
370 | this->rotorNodeLeft.getAbsCoor ().z); |
---|
371 | tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis(); |
---|
372 | glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
373 | this->getModel(4)->draw(); |
---|
374 | glTranslatef(0,-1,0); |
---|
375 | glRotatef(this->rotorCycle, 0,1,0); |
---|
376 | this->getModel(5)->draw(); |
---|
377 | glPopMatrix (); |
---|
378 | |
---|
379 | /// RIGHT SIDE |
---|
380 | glPushMatrix(); |
---|
381 | glTranslatef (this->wingNodeRight.getAbsCoor ().x, |
---|
382 | this->wingNodeRight.getAbsCoor ().y, |
---|
383 | this->wingNodeRight.getAbsCoor ().z); |
---|
384 | tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis(); |
---|
385 | glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
386 | glScalef(1,1,-1); |
---|
387 | this->getModel(3)->draw(); |
---|
388 | glPopMatrix (); |
---|
389 | |
---|
390 | glPushMatrix(); |
---|
391 | glTranslatef (this->rotorNodeRight.getAbsCoor ().x, |
---|
392 | this->rotorNodeRight.getAbsCoor ().y, |
---|
393 | this->rotorNodeRight.getAbsCoor ().z); |
---|
394 | tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis(); |
---|
395 | glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
396 | glScalef(1,1,-1); |
---|
397 | this->getModel(4)->draw(); |
---|
398 | glTranslatef(0,-1,0); |
---|
399 | glRotatef(this->rotorCycle, 0,1,0); |
---|
400 | this->getModel(5)->draw(); |
---|
401 | glPopMatrix (); |
---|
402 | } |
---|
403 | |
---|
404 | /** |
---|
405 | * @todo switch statement ?? |
---|
406 | */ |
---|
407 | void Hover::process(const Event &event) |
---|
408 | { |
---|
409 | Playable::process(event); |
---|
410 | |
---|
411 | if( event.type == KeyMapper::PEV_LEFT) |
---|
412 | this->bLeft = event.bPressed; |
---|
413 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
414 | this->bRight = event.bPressed; |
---|
415 | else if( event.type == KeyMapper::PEV_UP) |
---|
416 | this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
417 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
418 | this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
419 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
420 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
421 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
422 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
423 | else if( event.type == EV_MOUSE_MOTION) |
---|
424 | { |
---|
425 | float xMouse, yMouse; |
---|
426 | xMouse = event.xRel*mouseSensitivity; |
---|
427 | yMouse = event.yRel*mouseSensitivity; |
---|
428 | |
---|
429 | // rotate the Player around the y-axis |
---|
430 | this->rotation += xMouse; |
---|
431 | |
---|
432 | this->cameraLook += yMouse; |
---|
433 | // rotate the Camera around the z-axis |
---|
434 | if (cameraLook > M_PI_4) |
---|
435 | cameraLook = M_PI_4; |
---|
436 | else if (cameraLook < -M_PI_4) |
---|
437 | cameraLook = -M_PI_4; |
---|
438 | //this->cameraNode.setRelDirSoft(this->direction,10); |
---|
439 | } |
---|
440 | } |
---|