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