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