[5837] | 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: |
---|
[5878] | 12 | main-programmer: Benjamin Knecht |
---|
| 13 | co-programmer: ... |
---|
[5837] | 14 | |
---|
| 15 | */ |
---|
| 16 | |
---|
[5878] | 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
| 19 | #include "executor/executor.h" |
---|
| 20 | #include "space_ship.h" |
---|
| 21 | |
---|
| 22 | #include "objModel.h" |
---|
| 23 | #include "resource_manager.h" |
---|
| 24 | |
---|
| 25 | #include "weapons/weapon_manager.h" |
---|
| 26 | #include "weapons/test_gun.h" |
---|
| 27 | #include "weapons/turret.h" |
---|
| 28 | #include "weapons/cannon.h" |
---|
| 29 | |
---|
[5880] | 30 | #include "factory.h" |
---|
| 31 | #include "key_mapper.h" |
---|
[5878] | 32 | |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP); |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * creates the controlable Spaceship |
---|
| 39 | */ |
---|
| 40 | SpaceShip::SpaceShip() |
---|
| 41 | { |
---|
[5881] | 42 | this->init(); |
---|
[5878] | 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * destructs the spaceship, deletes alocated memory |
---|
| 47 | */ |
---|
| 48 | SpaceShip::~SpaceShip () |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * loads a Spaceships information from a specified file. |
---|
| 54 | * @param fileName the name of the File to load the spaceship from (absolute path) |
---|
| 55 | */ |
---|
| 56 | SpaceShip::SpaceShip(const char* fileName) |
---|
| 57 | { |
---|
| 58 | this->init(); |
---|
| 59 | TiXmlDocument doc(fileName); |
---|
| 60 | |
---|
| 61 | if(!doc.LoadFile()) |
---|
| 62 | { |
---|
| 63 | PRINTF(2)("Loading file %s failed for spaceship.\n", fileName); |
---|
| 64 | return; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | this->loadParams(doc.RootElement()); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * creates a new Spaceship from Xml Data |
---|
| 72 | * @param root the xml element containing spaceship data |
---|
| 73 | |
---|
| 74 | @todo add more parameters to load |
---|
| 75 | */ |
---|
| 76 | SpaceShip::SpaceShip(const TiXmlElement* root) |
---|
| 77 | { |
---|
| 78 | this->init(); |
---|
| 79 | if (root != NULL) |
---|
| 80 | this->loadParams(root); |
---|
| 81 | |
---|
| 82 | //weapons: |
---|
| 83 | Weapon* wpRight = new TestGun(0); |
---|
| 84 | wpRight->setName("testGun Right"); |
---|
| 85 | Weapon* wpLeft = new TestGun(1); |
---|
| 86 | wpLeft->setName("testGun Left"); |
---|
| 87 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(CL_CANNON)); |
---|
| 88 | |
---|
| 89 | cannon->setName("BFG"); |
---|
| 90 | |
---|
[5880] | 91 | this->getWeaponManager()->addWeapon(wpLeft, 1, 0); |
---|
| 92 | this->getWeaponManager()->addWeapon(wpRight,1 ,1); |
---|
| 93 | this->getWeaponManager()->addWeapon(cannon, 0, 6); |
---|
[5878] | 94 | |
---|
[5880] | 95 | //this->getWeaponManager()->addWeapon(turret, 3, 0); |
---|
[5878] | 96 | |
---|
[5880] | 97 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
[5878] | 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | /** |
---|
| 102 | * initializes a Spaceship |
---|
| 103 | */ |
---|
| 104 | void SpaceShip::init() |
---|
| 105 | { |
---|
| 106 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 107 | this->setClassID(CL_SPACE_SHIP, "SpaceShip"); |
---|
| 108 | |
---|
| 109 | PRINTF(4)("SPACESHIP INIT\n"); |
---|
[5897] | 110 | |
---|
| 111 | |
---|
[5878] | 112 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
| 113 | bFire = false; |
---|
| 114 | |
---|
[5897] | 115 | travelSpeed = 15.0; |
---|
| 116 | this->velocity = Vector(0.0,0.0,0.0); |
---|
| 117 | |
---|
[5878] | 118 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 119 | // button->show(); |
---|
| 120 | // button->setLabel("orxonox"); |
---|
| 121 | // button->setBindNode(this); |
---|
| 122 | |
---|
[5893] | 123 | //add events to the eventlist |
---|
[5890] | 124 | registerEvent(KeyMapper::PEV_UP); |
---|
| 125 | registerEvent(KeyMapper::PEV_DOWN); |
---|
| 126 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 127 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
| 128 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 129 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 130 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
| 131 | registerEvent(SDLK_PAGEUP); |
---|
[5894] | 132 | registerEvent(SDLK_PAGEDOWN); |
---|
[5890] | 133 | |
---|
[5880] | 134 | this->getWeaponManager()->setSlotCount(7); |
---|
[5878] | 135 | |
---|
[5880] | 136 | this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 137 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[5878] | 138 | |
---|
[5880] | 139 | this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
| 140 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[5878] | 141 | |
---|
[5880] | 142 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 143 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
[5878] | 144 | |
---|
[5880] | 145 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 146 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
[5878] | 147 | |
---|
[5880] | 148 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 149 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
[5878] | 150 | |
---|
[5880] | 151 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 152 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
[5878] | 153 | // |
---|
[5880] | 154 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 155 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
[5878] | 156 | // |
---|
[5880] | 157 | // this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); |
---|
| 158 | // this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); |
---|
[5878] | 159 | // |
---|
[5880] | 160 | // this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); |
---|
| 161 | // this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: |
---|
[5878] | 162 | |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * loads the Settings of a SpaceShip from an XML-element. |
---|
| 167 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 168 | */ |
---|
| 169 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
| 170 | { |
---|
| 171 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * adds a weapon to the weapon list of the spaceship |
---|
| 177 | * @param weapon to add |
---|
| 178 | */ |
---|
| 179 | void SpaceShip::addWeapon(Weapon* weapon) |
---|
| 180 | { |
---|
[5880] | 181 | this->getWeaponManager()->addWeapon(weapon); |
---|
[5878] | 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * removes a weapon from the spaceship |
---|
| 187 | * @param weapon to remove |
---|
| 188 | */ |
---|
| 189 | void SpaceShip::removeWeapon(Weapon* weapon) |
---|
| 190 | { |
---|
[5880] | 191 | this->getWeaponManager()->removeWeapon(weapon); |
---|
[5878] | 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * effect that occurs after the SpaceShip is spawned |
---|
| 196 | */ |
---|
| 197 | void SpaceShip::postSpawn () |
---|
| 198 | { |
---|
| 199 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * the action occuring if the spaceship left the game |
---|
| 204 | */ |
---|
| 205 | void SpaceShip::leftWorld () |
---|
| 206 | {} |
---|
| 207 | |
---|
| 208 | WorldEntity* ref = NULL; |
---|
| 209 | /** |
---|
| 210 | * this function is called, when two entities collide |
---|
| 211 | * @param entity: the world entity with whom it collides |
---|
| 212 | * |
---|
| 213 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 214 | */ |
---|
| 215 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 216 | { |
---|
| 217 | if (entity->isA(CL_TURRET_POWER_UP) && entity != ref) |
---|
| 218 | { |
---|
| 219 | this->ADDWEAPON(); |
---|
| 220 | ref = entity; |
---|
| 221 | } |
---|
| 222 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * draws the spaceship after transforming it. |
---|
| 227 | */ |
---|
| 228 | void SpaceShip::draw () const |
---|
| 229 | { |
---|
| 230 | glMatrixMode(GL_MODELVIEW); |
---|
| 231 | glPushMatrix(); |
---|
| 232 | /* translate */ |
---|
| 233 | glTranslatef (this->getAbsCoor ().x, |
---|
| 234 | this->getAbsCoor ().y, |
---|
| 235 | this->getAbsCoor ().z); |
---|
| 236 | /* rotate */ |
---|
| 237 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 238 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 239 | this->model->draw(); |
---|
| 240 | glPopMatrix(); |
---|
| 241 | |
---|
[5880] | 242 | this->getWeaponManager()->draw(); |
---|
[5878] | 243 | |
---|
| 244 | //this->debug(0); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * the function called for each passing timeSnap |
---|
| 249 | * @param time The timespan passed since last update |
---|
| 250 | */ |
---|
| 251 | void SpaceShip::tick (float time) |
---|
| 252 | { |
---|
| 253 | // spaceship controlled movement |
---|
[5893] | 254 | this->calculateVelocity(time); |
---|
[5878] | 255 | |
---|
[5893] | 256 | Vector move = (velocity)*time; |
---|
| 257 | |
---|
[5897] | 258 | if( velocity.len() != 0 ) |
---|
| 259 | { |
---|
| 260 | this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); |
---|
| 261 | } |
---|
| 262 | |
---|
[5893] | 263 | this->shiftCoor (move); |
---|
| 264 | |
---|
[5880] | 265 | this->getWeaponManager()->tick(time); |
---|
[5878] | 266 | // weapon system manipulation |
---|
| 267 | this->weaponAction(); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
[5893] | 271 | * calculate the velocity |
---|
[5878] | 272 | * @param time the timeslice since the last frame |
---|
| 273 | */ |
---|
[5893] | 274 | void SpaceShip::calculateVelocity (float time) |
---|
[5878] | 275 | { |
---|
| 276 | Vector accel(0.0, 0.0, 0.0); |
---|
[5897] | 277 | //Vector rot(0.0, 0.0, 0.0); |
---|
| 278 | //float rotVal = 0.0; |
---|
[5878] | 279 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 280 | /* calculate the direction in which the craft is heading */ |
---|
| 281 | Vector direction (1.0, 0.0, 0.0); |
---|
| 282 | //direction = this->absDirection.apply (direction); |
---|
[5897] | 283 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
[5878] | 284 | //orthDirection = orthDirection.cross (direction); |
---|
| 285 | |
---|
| 286 | |
---|
[5897] | 287 | if( this->bUp ) |
---|
| 288 | travelSpeed++; |
---|
| 289 | if( this->bDown ) |
---|
| 290 | travelSpeed--; |
---|
| 291 | |
---|
| 292 | |
---|
[5883] | 293 | if( this->bLeft /* > -this->getRelCoor().z*2*/) |
---|
[5878] | 294 | { |
---|
| 295 | accel -=(orthDirection); |
---|
[5897] | 296 | //rot +=Vector(1,0,0); |
---|
| 297 | //rotVal -= .4; |
---|
[5878] | 298 | } |
---|
[5883] | 299 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
[5878] | 300 | { |
---|
| 301 | accel += orthDirection; |
---|
[5897] | 302 | //rot += Vector(1,0,0); |
---|
| 303 | //rotVal += .4; |
---|
[5878] | 304 | } |
---|
| 305 | if (this->bAscend ) |
---|
| 306 | { |
---|
| 307 | accel += Vector(0,1,0); |
---|
[5897] | 308 | //rot += Vector(0,0,1); |
---|
| 309 | //rotVal += .4; |
---|
[5878] | 310 | } |
---|
| 311 | if (this->bDescend ) |
---|
| 312 | { |
---|
| 313 | accel -= Vector(0,1,0); |
---|
[5897] | 314 | //rot += Vector(0,0,1); |
---|
| 315 | //rotVal -= .4; |
---|
[5878] | 316 | } |
---|
| 317 | |
---|
[5893] | 318 | velocity += accel*time; |
---|
[5897] | 319 | velocity = (velocity.getNormalized() ) *travelSpeed; |
---|
| 320 | //rot.normalize(); |
---|
| 321 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
[5878] | 322 | } |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | * weapon manipulation by the player |
---|
| 326 | */ |
---|
| 327 | void SpaceShip::weaponAction() |
---|
| 328 | { |
---|
| 329 | if( this->bFire) |
---|
| 330 | { |
---|
[5880] | 331 | this->getWeaponManager()->fire(); |
---|
[5878] | 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
| 336 | * @todo switch statement ?? |
---|
| 337 | */ |
---|
| 338 | void SpaceShip::process(const Event &event) |
---|
| 339 | { |
---|
| 340 | if( event.type == KeyMapper::PEV_UP) |
---|
| 341 | this->bUp = event.bPressed; |
---|
| 342 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
| 343 | this->bDown = event.bPressed; |
---|
| 344 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
| 345 | this->bRight= event.bPressed; |
---|
| 346 | else if( event.type == KeyMapper::PEV_LEFT) |
---|
| 347 | this->bLeft = event.bPressed; |
---|
| 348 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 349 | this->bFire = event.bPressed; |
---|
| 350 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
[5880] | 351 | this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
[5878] | 352 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
[5880] | 353 | this->getWeaponManager()->previousWeaponConfig(); |
---|
[5878] | 354 | |
---|
| 355 | else if( event.type == SDLK_PAGEUP) |
---|
| 356 | this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 357 | else if( event.type == SDLK_PAGEDOWN) |
---|
| 358 | this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | #include "weapons/aiming_turret.h" |
---|
| 362 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
| 363 | void SpaceShip::ADDWEAPON() |
---|
| 364 | { |
---|
| 365 | Weapon* turret = NULL; |
---|
| 366 | |
---|
| 367 | if ((float)rand()/RAND_MAX < .1) |
---|
| 368 | { |
---|
[5880] | 369 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
[5878] | 370 | { |
---|
| 371 | turret = new Turret(); |
---|
[5880] | 372 | this->getWeaponManager()->addWeapon(turret, 2); |
---|
| 373 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
[5878] | 374 | } |
---|
| 375 | } |
---|
| 376 | else |
---|
| 377 | { |
---|
[5880] | 378 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
[5878] | 379 | { |
---|
| 380 | turret = new AimingTurret(); |
---|
[5880] | 381 | this->getWeaponManager()->addWeapon(turret, 3); |
---|
[5878] | 382 | |
---|
[5880] | 383 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
[5878] | 384 | } |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | if(turret != NULL) |
---|
| 388 | { |
---|
| 389 | turret->setName("Turret"); |
---|
| 390 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
| 391 | } |
---|
| 392 | } |
---|