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