Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/spacecraft_2d.cc @ 9198

Last change on this file since 9198 was 9198, checked in by bensch, 18 years ago

dealing damage

File size: 15.0 KB
Line 
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 "util/loading/load_param.h"
28#include "key_mapper.h"
29#include "state.h"
30
31#include "graphics_engine.h"
32#include "dot_emitter.h"
33#include "sprite_particles.h"
34
35#include "debug.h"
36
37#include "script_class.h"
38
39
40CREATE_FACTORY(Spacecraft2D, CL_SPACECRAFT_2D);
41
42
43CREATE_SCRIPTABLE_CLASS(Spacecraft2D, CL_SPACECRAFT_2D,
44                        addMethod("hasPlayer", ExecutorLua0ret<Playable,bool>(&Playable::hasPlayer))
45                        //Coordinates
46                        ->addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
47                        ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
48                        ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
49                        ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
50                        ->addMethod("setAirFriction", ExecutorLua1<Spacecraft2D, float>(&Spacecraft2D::setAirFriction))
51                       );
52
53
54/**
55 * @brief loads a Spacecraft2D information from a specified file.
56 * @param fileName the name of the File to load the spacecraft_2d from (absolute path)
57 */
58Spacecraft2D::Spacecraft2D(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 Spacecraft2D.\n", fileName.c_str());
66    return;
67  }
68
69  this->loadParams(doc.RootElement());
70}
71
72/**
73 * @brief 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*/
78Spacecraft2D::Spacecraft2D(const TiXmlElement* root)
79{
80  this->init();
81  if (root != NULL)
82    this->loadParams(root);
83
84
85
86  //weapons:
87  Weapon* wpRight = dynamic_cast<Weapon*>(Factory::fabricate(CL_LASER_CANNON));
88  wpRight->setName("testGun Right");
89  Weapon* wpLeft = dynamic_cast<Weapon*>(Factory::fabricate(CL_LASER_CANNON));
90  wpLeft->setName("testGun Left");
91  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER));
92
93  //  cannon->setName("BFG");
94
95  this->addWeapon(wpLeft, 1, 0);
96  this->addWeapon(wpRight,1 ,1);
97  //this->addWeapon(cannon, 0, 2);
98
99  this->getWeaponManager().changeWeaponConfig(1);
100  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
101}
102
103
104/**
105 * @brief destructs the spacecraft_2d, deletes alocated memory
106 */
107Spacecraft2D::~Spacecraft2D ()
108{
109  this->setPlayer(NULL);
110  delete this->toTravelHeight;
111}
112
113
114/**
115 * @brief initializes a Spacecraft2D
116 */
117void Spacecraft2D::init()
118{
119  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
120  this->setClassID(CL_SPACECRAFT_2D, "Spacecraft2D");
121
122  this->setSupportedPlaymodes(Playable::Full3D | Playable::Horizontal );
123
124  bForward = bBackward = bLeft = bRight = false;
125  mouseSensitivity = 0.005;
126
127  this->cameraLook = 0.0f;
128  this->rotation = 0.0f;
129  this->acceleration = 20.0f;
130  this->airFriction = 0.0f;
131
132
133  this->setHealthMax(1000);
134  this->setHealth(1000);
135  this->setDamage(100.0f);
136
137
138
139  /// 2D-MODE
140  this->toTravelHeight = NULL;
141  this->travelSpeed = 0.0f;
142  this->travelNode = new PNode();
143
144  this->loadModel("models/ships/mantawing.obj", 5.0f);
145
146  // camera - issue
147  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
148  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
149  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
150  //this->cameraNode.setParent(this);
151
152  // PARTICLES
153  this->burstEmitter = new DotEmitter(200, 5.0, .01);
154  this->burstEmitter->setParent(this);
155  this->burstEmitter->setRelCoor(0, -0.7, 0);
156  this->burstEmitter->setRelDir(Quaternion(-M_PI, Vector(0,0,1)));
157  this->burstEmitter->setName("Spacecraft2D_Burst_emitter_Left");
158
159  this->burstSystem = new SpriteParticles(1000);
160  this->burstSystem->addEmitter(this->burstEmitter);
161  this->burstSystem->setName("SpaceShip_Burst_System");
162  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
163  this->burstSystem->setLifeSpan(1.0, .3);
164  this->burstSystem->setRadius(0.0, 1.5);
165  this->burstSystem->setRadius(0.05, 1.8);
166  this->burstSystem->setRadius(.5, .8);
167  this->burstSystem->setRadius(1.0, 0);
168  this->burstSystem->setColor(0.0, .7,.7,1,.5);
169  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
170  this->burstSystem->setColor(0.5, .5,.5,.8,.3);
171  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
172
173
174  //add events to the eventlist of the Playable
175  this->registerEvent(KeyMapper::PEV_FORWARD);
176  this->registerEvent(KeyMapper::PEV_BACKWARD);
177  this->registerEvent(KeyMapper::PEV_LEFT);
178  this->registerEvent(KeyMapper::PEV_RIGHT);
179  this->registerEvent(KeyMapper::PEV_FIRE1);
180  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
181  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
182  this->registerEvent(EV_MOUSE_MOTION);
183
184  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
185
186  // WEAPON_MANAGER configuration
187  this->getWeaponManager().setSlotCount(5);
188
189  this->getWeaponManager().setSlotPosition(0, Vector(1.843, -0.335, 2.029) * 5.0);
190  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
191
192  this->getWeaponManager().setSlotPosition(1, Vector(1.843, -0.335, -2.029) * 5.0);
193  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
194
195  this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003));
196  this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY);
197
198  /// TODO: THESE ARE TOO MUCH
199  this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652));
200  this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
201
202  this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652));
203  this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
204
205  this->cameraNode.setRelCoor(1,5,0);
206  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
207  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
208
209  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
210  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
211  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
212  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
213  //registerVar( new SynchronizeableQuaternion( &direction, &direction, "direction", PERMISSION_OWNER ) );
214  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
215  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
216
217
218
219}
220
221/**
222 * @brief loads the Settings of a Spacecraft2D from an XML-element.
223 * @param root the XML-element to load the Spaceship's properties from
224 */
225void Spacecraft2D::loadParams(const TiXmlElement* root)
226{
227  Playable::loadParams(root);
228
229  LoadParam(root, "travel-speed", this, Spacecraft2D, setTravelSpeed);
230  LoadParam(root, "travel-height", this, Spacecraft2D, setTravelHeight);
231  LoadParam(root, "travel-distance", this, Spacecraft2D, setTravelDistance);
232}
233
234void Spacecraft2D::setPlayDirection(const Quaternion& rot, float speed)
235{
236  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
237}
238
239void Spacecraft2D::setTravelSpeed(float travelSpeed)
240{
241  this->travelSpeed = travelSpeed;
242}
243
244
245void Spacecraft2D::setTravelHeight(float travelHeight)
246{
247  if (this->toTravelHeight == NULL)
248    this->toTravelHeight = new float;
249  *this->toTravelHeight = travelHeight;
250}
251
252
253void Spacecraft2D::setTravelDistance(const Vector2D& distance)
254{
255  this->travelDistance = distance;
256}
257
258void Spacecraft2D::setTravelDistance(float x, float y)
259{
260  this->setTravelDistance(Vector2D(x, y));
261}
262
263
264
265void Spacecraft2D::enter()
266{
267  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
268  this->setPlaymode(this->getPlaymode());
269}
270
271void Spacecraft2D::leave()
272{
273  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
274  this->detachCamera();
275
276}
277
278
279void Spacecraft2D::enterPlaymode(Playable::Playmode playmode)
280{
281  switch(playmode)
282  {
283    case Playable::Full3D:
284      if (State::getCameraNode != NULL)
285      {
286        Vector absCoor = this->getAbsCoor();
287        this->setParent(PNode::getNullParent());
288        this->setAbsCoor(absCoor);
289        State::getCameraNode()->setParentSoft(&this->cameraNode);
290        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
291        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
292        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
293
294      }
295      break;
296
297
298    case Playable::Horizontal:
299      if (State::getCameraNode != NULL)
300      {
301        this->debugNode(1);
302        this->travelNode->debugNode(1);
303
304        this->travelNode->setAbsCoor(this->getAbsCoor());
305        this->travelNode->updateNode(0.01f);
306
307        this->setParent(this->travelNode);
308        this->setRelCoor(0,0,0);
309
310        State::getCameraNode()->setParentSoft(this->travelNode);
311        State::getCameraNode()->setRelCoorSoft(-3, 100,0);
312        State::getCameraTargetNode()->setParentSoft(this->travelNode);
313        State::getCameraTargetNode()->setRelCoorSoft(5,0,1);
314
315
316        this->debugNode(1);
317        this->travelNode->debugNode(1);
318      }
319      break;
320
321    default:
322      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassName());
323  }
324}
325
326
327
328/**
329 * @brief effect that occurs after the Spacecraft2D is spawned
330*/
331void Spacecraft2D::postSpawn ()
332{
333  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
334}
335
336/**
337 * @brief the action occuring if the spacecraft_2d left the game
338*/
339void Spacecraft2D::leftWorld ()
340{}
341
342/**
343 * @brief this function is called, when two entities collide
344 * @param entity: the world entity with whom it collides
345 *
346 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
347 */
348void Spacecraft2D::collidesWith(WorldEntity* entity, const Vector& location)
349{
350  Playable::collidesWith(entity, location);
351}
352
353
354
355/**
356 * @brief the function called for each passing timeSnap
357 * @param time The timespan passed since last update
358*/
359void Spacecraft2D::tick (float dt)
360{
361  //  this->debugNode(1);
362  Playable::tick(dt);
363
364  // spaceship controlled movement
365  this->movement(dt);
366
367  // TRYING TO FIX PNode.
368  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
369  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
370}
371
372/**
373 * @brief calculate the velocity
374 * @param time the timeslice since the last frame
375*/
376void Spacecraft2D::movement (float dt)
377{
378  Vector accel(0.0, 0.0, 0.0);
379
380  if( this->bForward )
381  {
382    accel += Vector(this->acceleration, 0, 0);
383  }
384
385  if( this->bBackward )
386  {
387    accel -= Vector(this->acceleration, 0, 0);
388  }
389  if( this->bLeft)
390  {
391    accel -= Vector(0, 0, this->acceleration);
392  }
393
394  if( this->bRight)
395  {
396    accel += Vector(0, 0, this->acceleration);
397  }
398
399  switch(this->getPlaymode())
400  {
401    case Playable::Full3D:
402      {
403        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
404
405        // this is the air friction (necessary for a smooth control)
406        Vector damping = (this->velocity * this->airFriction);
407
408
409        this->velocity += (accelerationDir - damping)* dt;
410        this->shiftCoor (this->velocity * dt);
411
412        // limit the maximum rotation speed.
413        if (this->rotation != 0.0f)
414        {
415          float maxRot = 10.0 * dt;
416          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
417          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
418          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
419
420          this->rotation = 0.0f;
421        }
422
423        this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
424      }
425      break;
426
427    case Playable::Horizontal:
428      {
429
430        if (this->toTravelHeight != NULL)
431        {
432          this->travelNode->shiftCoor(Vector(0, (*toTravelHeight - this->travelNode->getAbsCoor().y) * dt * 10.0, 0));
433          if (fabsf(this->travelNode->getAbsCoor().y - *this->toTravelHeight) < .1)
434          {
435            delete this->toTravelHeight;
436            this->toTravelHeight = NULL;
437          }
438        }
439        this->travelNode->shiftCoor(Vector(this->travelSpeed * dt, 0, 0));
440
441        accel.y = 0.0;
442
443        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
444        accelerationDir.y = 0.0;
445
446        // this is the air friction (necessary for a smooth control)
447        Vector damping = (this->velocity * this->airFriction);
448
449
450        this->velocity += (accelerationDir - damping)* dt;
451
452        if (this->getRelCoor().z > this->travelDistance.y && velocity.z > 0.0)
453          this->velocity.z = 0.0f;
454        if (this->getRelCoor().z < -this->travelDistance.y && velocity.z < 0.0)
455          this->velocity.z = 0.0f;
456
457        if (this->getRelCoor().x > this->travelDistance.x && velocity.x > 0.0)
458          this->velocity.x = 0.0f;
459        if (this->getRelCoor().x < -this->travelDistance.x && velocity.x < 0.0)
460          this->velocity.x = 0.0f;
461
462
463        this->shiftCoor (this->velocity * dt);
464        if (accel.z == 0)
465          this->setRelDirSoft(Quaternion(0, Vector(0,0,0)), 5.0f);
466        else
467          this->setRelDirSoft(Quaternion(this->velocity.z * .004, Vector(1,0,0)), 4.5f);
468      }
469      break;
470
471    default:
472      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassName());
473  }
474}
475
476
477void Spacecraft2D::draw() const
478{
479  WorldEntity::draw();
480}
481
482/**
483 * @todo switch statement ??
484 */
485void Spacecraft2D::process(const Event &event)
486{
487  Playable::process(event);
488
489  if( event.type == KeyMapper::PEV_LEFT)
490    this->bLeft = event.bPressed;
491  else if( event.type == KeyMapper::PEV_RIGHT)
492    this->bRight = event.bPressed;
493  else if( event.type == KeyMapper::PEV_FORWARD)
494    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
495  else if( event.type == KeyMapper::PEV_BACKWARD)
496    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
497  else if( event.type == EV_MOUSE_MOTION)
498  {
499
500
501
502    if (this->getPlaymode() == Playable::Full3D)
503    {
504      float xMouse, yMouse;
505      xMouse = event.xRel*mouseSensitivity;
506      yMouse = event.yRel*mouseSensitivity;
507
508      // rotate the Player around the y-axis
509      this->rotation += xMouse;
510
511      this->cameraLook += yMouse;
512      // rotate the Camera around the z-axis
513      if (cameraLook > M_PI_4)
514        cameraLook = M_PI_4;
515      else if (cameraLook < -M_PI_4)
516        cameraLook = -M_PI_4;
517    }
518  }
519}
Note: See TracBrowser for help on using the repository browser.