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