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