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