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