| 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_PLAYER | 
|---|
| 17 |  | 
|---|
| 18 | #include "player.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "track_manager.h" | 
|---|
| 21 | #include "objModel.h" | 
|---|
| 22 | #include "resource_manager.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "weapon_manager.h" | 
|---|
| 25 | #include "test_gun.h" | 
|---|
| 26 | #include "world.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "list.h" | 
|---|
| 29 |  | 
|---|
| 30 | #include "event_handler.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "event.h" | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | CREATE_FACTORY(Player); | 
|---|
| 38 |  | 
|---|
| 39 | /** | 
|---|
| 40 |  * creates a new Player | 
|---|
| 41 |  * @param isFree if the player is free | 
|---|
| 42 | */ | 
|---|
| 43 | Player::Player() | 
|---|
| 44 | { | 
|---|
| 45 |   this->init(); | 
|---|
| 46 |  | 
|---|
| 47 |   //weapons: | 
|---|
| 48 |   Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); | 
|---|
| 49 |   Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); | 
|---|
| 50 |  | 
|---|
| 51 |   this->weaponMan->addWeapon(wpRight, WM_CONFIG0, WM_SLOT0); | 
|---|
| 52 | //  this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1); | 
|---|
| 53 | //  this->weaponMan->addWeapon(wpRight, WM_CONFIG2); | 
|---|
| 54 | //  this->weaponMan->addWeapon(wpLeft, WM_CONFIG2); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 |  *  destructs the player, deletes alocated memory | 
|---|
| 60 | */ | 
|---|
| 61 | Player::~Player () | 
|---|
| 62 | { | 
|---|
| 63 |   /* do not delete the weapons, they are contained in the pnode tree | 
|---|
| 64 |      and will be deleted there. | 
|---|
| 65 |      this only frees the memory allocated to save the list. | 
|---|
| 66 |   */ | 
|---|
| 67 |   delete this->weaponMan; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | /** | 
|---|
| 71 |  *  creates a new Player from Xml Data | 
|---|
| 72 |  * @param root the xml element containing player data | 
|---|
| 73 |  | 
|---|
| 74 |    @todo add more parameters to load | 
|---|
| 75 | */ | 
|---|
| 76 | Player::Player(const TiXmlElement* root) | 
|---|
| 77 | { | 
|---|
| 78 |   this->init(); | 
|---|
| 79 |   this->loadParams(root); | 
|---|
| 80 |  | 
|---|
| 81 |   //weapons: | 
|---|
| 82 |   Weapon* wpRight = new TestGun(this, Vector(-2.6, 0.1, 3.0), Quaternion(), 0); | 
|---|
| 83 |   Weapon* wpLeft = new TestGun(this, Vector(-2.6, 0.1, -3.0), Quaternion(), 1); | 
|---|
| 84 |  | 
|---|
| 85 |   this->weaponMan->addWeapon(wpRight, WM_CONFIG0, WM_SLOT0); | 
|---|
| 86 |   this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1); | 
|---|
| 87 |   this->weaponMan->addWeapon(wpRight, WM_CONFIG2); | 
|---|
| 88 |   this->weaponMan->addWeapon(wpLeft, WM_CONFIG2); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | /** | 
|---|
| 92 |  * initializes a Player | 
|---|
| 93 |  */ | 
|---|
| 94 | void Player::init() | 
|---|
| 95 | { | 
|---|
| 96 |   this->setClassID(CL_PLAYER, "Player"); | 
|---|
| 97 |  | 
|---|
| 98 |   this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); | 
|---|
| 99 |   travelSpeed = 15.0; | 
|---|
| 100 |   velocity = new Vector(); | 
|---|
| 101 |   bUp = bDown = bLeft = bRight = bAscend = bDescend = false; | 
|---|
| 102 |   bFire = false; | 
|---|
| 103 |   this->bWeaponChange = false; | 
|---|
| 104 |   acceleration = 10.0; | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 |   this->weaponMan = new WeaponManager(2); | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 |  * loads the Settings of a Player from an XML-element. | 
|---|
| 113 |  * @param root the XML-element to load the Player's properties from | 
|---|
| 114 |  */ | 
|---|
| 115 | void Player::loadParams(const TiXmlElement* root) | 
|---|
| 116 | { | 
|---|
| 117 |   static_cast<WorldEntity*>(this)->loadParams(root); | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | /** | 
|---|
| 125 |  * adds a weapon to the weapon list of player | 
|---|
| 126 |  * @param weapon to add | 
|---|
| 127 | */ | 
|---|
| 128 | void Player::addWeapon(Weapon* weapon) | 
|---|
| 129 | { | 
|---|
| 130 |   this->weaponMan->addWeapon(weapon); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 |  *  removes a weapon from the player | 
|---|
| 136 |  * @param weapon to remove | 
|---|
| 137 | */ | 
|---|
| 138 | void Player::removeWeapon(Weapon* weapon) | 
|---|
| 139 | { | 
|---|
| 140 |   this->weaponMan->removeWeapon(weapon); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | /** | 
|---|
| 145 |  *  effect that occurs after the player is spawned | 
|---|
| 146 | */ | 
|---|
| 147 | void Player::postSpawn () | 
|---|
| 148 | { | 
|---|
| 149 |   //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | /** | 
|---|
| 154 |  *  the action occuring if the player left the game | 
|---|
| 155 | */ | 
|---|
| 156 | void Player::leftWorld () | 
|---|
| 157 | {} | 
|---|
| 158 |  | 
|---|
| 159 |  | 
|---|
| 160 |  | 
|---|
| 161 | /** | 
|---|
| 162 |  *  if the player is hit, call this function | 
|---|
| 163 |  * @param weapon hit by this weapon | 
|---|
| 164 |  * @param loc ?? | 
|---|
| 165 | */ | 
|---|
| 166 | void Player::hit (WorldEntity* weapon, Vector* loc) | 
|---|
| 167 | { | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 |  | 
|---|
| 171 | /** | 
|---|
| 172 |   *  Collision with another Entity has this effect | 
|---|
| 173 |   * @param other the other colider | 
|---|
| 174 |   * @param ownhitflags ?? | 
|---|
| 175 |   * @param otherhitflags ?? | 
|---|
| 176 | */ | 
|---|
| 177 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) | 
|---|
| 178 | { | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 |  | 
|---|
| 182 | /** | 
|---|
| 183 |  *  draws the player after transforming him. | 
|---|
| 184 | */ | 
|---|
| 185 | void Player::draw () | 
|---|
| 186 | { | 
|---|
| 187 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 188 |   glPushMatrix(); | 
|---|
| 189 |   float matrix[4][4]; | 
|---|
| 190 |  | 
|---|
| 191 |   /* translate */ | 
|---|
| 192 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 193 |                 this->getAbsCoor ().y, | 
|---|
| 194 |                 this->getAbsCoor ().z); | 
|---|
| 195 |   /* rotate */ | 
|---|
| 196 |   this->getAbsDir ().matrix (matrix); | 
|---|
| 197 |   glMultMatrixf((float*)matrix); | 
|---|
| 198 |  | 
|---|
| 199 |   this->model->draw(); | 
|---|
| 200 |   glPopMatrix(); | 
|---|
| 201 |  | 
|---|
| 202 |   this->weaponMan->draw(); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | /** | 
|---|
| 207 |  *  the function called for each passing timeSnap | 
|---|
| 208 |  * @param time The timespan passed since last update | 
|---|
| 209 | */ | 
|---|
| 210 | void Player::tick (float time) | 
|---|
| 211 | { | 
|---|
| 212 |   // player controlled movement | 
|---|
| 213 |   this->move(time); | 
|---|
| 214 |  | 
|---|
| 215 |   this->weaponMan->tick(time); | 
|---|
| 216 |   // weapon system manipulation | 
|---|
| 217 |   this->weaponAction(); | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 |  | 
|---|
| 221 | /** | 
|---|
| 222 |  *  action if player moves | 
|---|
| 223 |  * @param time the timeslice since the last frame | 
|---|
| 224 | */ | 
|---|
| 225 | void Player::move (float time) | 
|---|
| 226 | { | 
|---|
| 227 |   Vector accel(0.0, 0.0, 0.0); | 
|---|
| 228 |   /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ | 
|---|
| 229 |   /* calculate the direction in which the craft is heading  */ | 
|---|
| 230 |   Vector direction (1.0, 0.0, 0.0); | 
|---|
| 231 |   //direction = this->absDirection.apply (direction); | 
|---|
| 232 |   Vector orthDirection (0.0, 0.0, 1.0); | 
|---|
| 233 |   //orthDirection = orthDirection.cross (direction); | 
|---|
| 234 |  | 
|---|
| 235 |   if( this->bUp && this->getRelCoor().x < 20) | 
|---|
| 236 |     accel = accel+(direction*acceleration); | 
|---|
| 237 |   if( this->bDown && this->getRelCoor().x > -5) | 
|---|
| 238 |     accel = accel -(direction*acceleration); | 
|---|
| 239 |   if( this->bLeft &&  TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) | 
|---|
| 240 |     accel = accel - (orthDirection*acceleration); | 
|---|
| 241 |   if( this->bRight &&  TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) | 
|---|
| 242 |     accel = accel + (orthDirection*acceleration); | 
|---|
| 243 |   if( this->bAscend ) { /* FIXME */ } | 
|---|
| 244 |   if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */ | 
|---|
| 245 |  | 
|---|
| 246 |   Vector move = accel * time; | 
|---|
| 247 |   this->shiftCoor (move); | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 |  | 
|---|
| 251 | /** | 
|---|
| 252 |  * weapon manipulation by the player | 
|---|
| 253 | */ | 
|---|
| 254 | void Player::weaponAction() | 
|---|
| 255 | { | 
|---|
| 256 |   if( this->bFire) | 
|---|
| 257 |     { | 
|---|
| 258 |       this->weaponMan->fire(); | 
|---|
| 259 |     } | 
|---|
| 260 |   if( this->bWeaponChange) | 
|---|
| 261 |     { | 
|---|
| 262 |       this->weaponMan->nextWeaponConf(); | 
|---|
| 263 |       this->bWeaponChange = false; | 
|---|
| 264 |     } | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | /** | 
|---|
| 268 |  * @todo switch statement ?? | 
|---|
| 269 |  */ | 
|---|
| 270 | void Player::process(const Event &event) | 
|---|
| 271 | { | 
|---|
| 272 |   if( event.type == KeyMapper::PEV_UP) | 
|---|
| 273 |       this->bUp = event.bPressed; | 
|---|
| 274 |   else if( event.type == KeyMapper::PEV_DOWN) | 
|---|
| 275 |       this->bDown = event.bPressed; | 
|---|
| 276 |   else if( event.type == KeyMapper::PEV_RIGHT) | 
|---|
| 277 |       this->bRight= event.bPressed; | 
|---|
| 278 |   else if( event.type == KeyMapper::PEV_LEFT) | 
|---|
| 279 |       this->bLeft = event.bPressed; | 
|---|
| 280 |   else if( event.type == KeyMapper::PEV_FIRE1) | 
|---|
| 281 |       this->bFire = event.bPressed; | 
|---|
| 282 |   else if( event.type == KeyMapper::PEV_NEXT_WEAPON) | 
|---|
| 283 |       if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; | 
|---|
| 284 |  | 
|---|
| 285 | } | 
|---|