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