[4592] | 1 | /* |
---|
[3471] | 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" |
---|
[4826] | 23 | |
---|
| 24 | #include "weapon_manager.h" |
---|
[3620] | 25 | #include "test_gun.h" |
---|
[4963] | 26 | #include "turret.h" |
---|
[3620] | 27 | #include "world.h" |
---|
[3471] | 28 | |
---|
[3620] | 29 | #include "list.h" |
---|
| 30 | |
---|
[4404] | 31 | #include "event_handler.h" |
---|
[4389] | 32 | |
---|
[4404] | 33 | #include "event.h" |
---|
[4287] | 34 | |
---|
[4780] | 35 | |
---|
[3471] | 36 | using namespace std; |
---|
| 37 | |
---|
[4010] | 38 | CREATE_FACTORY(Player); |
---|
| 39 | |
---|
[3471] | 40 | /** |
---|
[4885] | 41 | * creates a new Player |
---|
[4836] | 42 | * @param isFree if the player is free |
---|
[3471] | 43 | */ |
---|
[4762] | 44 | Player::Player() |
---|
[3471] | 45 | { |
---|
[4780] | 46 | this->init(); |
---|
[4597] | 47 | |
---|
[3620] | 48 | //weapons: |
---|
[4955] | 49 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
---|
| 50 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
---|
[4592] | 51 | |
---|
[4951] | 52 | this->weaponMan->addWeapon(wpRight); |
---|
[4910] | 53 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1); |
---|
| 54 | // this->weaponMan->addWeapon(wpRight, WM_CONFIG2); |
---|
| 55 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG2); |
---|
[3471] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
[4975] | 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) |
---|
[3471] | 63 | { |
---|
[4975] | 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()); |
---|
[3583] | 74 | } |
---|
[3566] | 75 | |
---|
[4010] | 76 | /** |
---|
[4836] | 77 | * creates a new Player from Xml Data |
---|
| 78 | * @param root the xml element containing player data |
---|
[4592] | 79 | |
---|
[4836] | 80 | @todo add more parameters to load |
---|
[4010] | 81 | */ |
---|
[4780] | 82 | Player::Player(const TiXmlElement* root) |
---|
[4010] | 83 | { |
---|
[4780] | 84 | this->init(); |
---|
[5155] | 85 | if (root != NULL) |
---|
| 86 | this->loadParams(root); |
---|
[4597] | 87 | |
---|
[4010] | 88 | //weapons: |
---|
[4955] | 89 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
---|
[4953] | 90 | wpRight->setName("testGun Right"); |
---|
[4955] | 91 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
---|
[4953] | 92 | wpLeft->setName("testGun Left"); |
---|
[4592] | 93 | |
---|
[5064] | 94 | Weapon* turret1 = new Turret(this->weaponMan); |
---|
| 95 | turret1->setName("Turret1"); |
---|
| 96 | turret1->setStateDuration(WS_SHOOTING, .2); |
---|
[4969] | 97 | Weapon* turret2 = new Turret(this->weaponMan); |
---|
[5064] | 98 | turret2->setName("Turret2"); |
---|
| 99 | turret2->setStateDuration(WS_SHOOTING, .3); |
---|
| 100 | Weapon* turret3 = new Turret(this->weaponMan); |
---|
| 101 | turret3->setName("Turret3"); |
---|
| 102 | turret3->setStateDuration(WS_SHOOTING, .17); |
---|
| 103 | |
---|
| 104 | Weapon* turret4 = new Turret(this->weaponMan); |
---|
| 105 | turret4->setName("Turret4"); |
---|
| 106 | turret4->setStateDuration(WS_SHOOTING, .3); |
---|
| 107 | |
---|
| 108 | |
---|
[4953] | 109 | this->weaponMan->addWeapon(wpLeft, 1, 0); |
---|
| 110 | this->weaponMan->addWeapon(wpRight,1 ,1); |
---|
[5064] | 111 | this->weaponMan->addWeapon(turret1, 2, 2); |
---|
[4969] | 112 | this->weaponMan->addWeapon(turret2, 2, 3); |
---|
[5064] | 113 | this->weaponMan->addWeapon(turret3, 2, 4); |
---|
| 114 | this->weaponMan->addWeapon(turret4, 2, 5); |
---|
| 115 | |
---|
[5041] | 116 | //this->weaponMan->addWeapon(turret, 3, 0); |
---|
[4953] | 117 | |
---|
| 118 | this->weaponMan->changeWeaponConfig(0); |
---|
[4010] | 119 | } |
---|
[3583] | 120 | |
---|
[4780] | 121 | /** |
---|
[4975] | 122 | * destructs the player, deletes alocated memory |
---|
| 123 | */ |
---|
| 124 | Player::~Player () |
---|
| 125 | { |
---|
| 126 | /* do not delete the weapons, they are contained in the pnode tree |
---|
| 127 | and will be deleted there. |
---|
| 128 | this only frees the memory allocated to save the list. |
---|
| 129 | */ |
---|
| 130 | delete this->weaponMan; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
[4780] | 134 | * initializes a Player |
---|
| 135 | */ |
---|
| 136 | void Player::init() |
---|
| 137 | { |
---|
[5037] | 138 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
[4780] | 139 | this->setClassID(CL_PLAYER, "Player"); |
---|
[5144] | 140 | |
---|
| 141 | PRINTF(1)("PLAYER INIT\n"); |
---|
[4780] | 142 | travelSpeed = 15.0; |
---|
| 143 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
| 144 | bFire = false; |
---|
| 145 | acceleration = 10.0; |
---|
[4834] | 146 | |
---|
| 147 | |
---|
[4953] | 148 | this->weaponMan = new WeaponManager(this); |
---|
[5064] | 149 | this->weaponMan->setSlotCount(6); |
---|
| 150 | |
---|
[4953] | 151 | this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
[5064] | 152 | |
---|
[4953] | 153 | this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
[5064] | 154 | |
---|
[4969] | 155 | this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 156 | this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
[5064] | 157 | |
---|
[4969] | 158 | this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 159 | this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
[5064] | 160 | |
---|
| 161 | this->weaponMan->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 162 | this->weaponMan->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 163 | |
---|
| 164 | this->weaponMan->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 165 | this->weaponMan->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 166 | |
---|
[4780] | 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
[3583] | 170 | /** |
---|
[4885] | 171 | * loads the Settings of a Player from an XML-element. |
---|
[4780] | 172 | * @param root the XML-element to load the Player's properties from |
---|
| 173 | */ |
---|
| 174 | void Player::loadParams(const TiXmlElement* root) |
---|
| 175 | { |
---|
| 176 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
[4885] | 183 | * adds a weapon to the weapon list of player |
---|
[4836] | 184 | * @param weapon to add |
---|
[3583] | 185 | */ |
---|
| 186 | void Player::addWeapon(Weapon* weapon) |
---|
| 187 | { |
---|
[3878] | 188 | this->weaponMan->addWeapon(weapon); |
---|
[3471] | 189 | } |
---|
| 190 | |
---|
[3583] | 191 | |
---|
[3471] | 192 | /** |
---|
[4836] | 193 | * removes a weapon from the player |
---|
| 194 | * @param weapon to remove |
---|
[3583] | 195 | */ |
---|
| 196 | void Player::removeWeapon(Weapon* weapon) |
---|
| 197 | { |
---|
[3878] | 198 | this->weaponMan->removeWeapon(weapon); |
---|
[3583] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | /** |
---|
[4836] | 203 | * effect that occurs after the player is spawned |
---|
[3471] | 204 | */ |
---|
| 205 | void Player::postSpawn () |
---|
| 206 | { |
---|
[3474] | 207 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
[3471] | 208 | } |
---|
| 209 | |
---|
[3584] | 210 | |
---|
[3471] | 211 | /** |
---|
[4836] | 212 | * the action occuring if the player left the game |
---|
[3471] | 213 | */ |
---|
[3584] | 214 | void Player::leftWorld () |
---|
| 215 | {} |
---|
[3471] | 216 | |
---|
[3584] | 217 | |
---|
| 218 | |
---|
[3471] | 219 | /** |
---|
[4836] | 220 | * if the player is hit, call this function |
---|
| 221 | * @param weapon hit by this weapon |
---|
| 222 | * @param loc ?? |
---|
[3471] | 223 | */ |
---|
[3578] | 224 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
[3471] | 225 | { |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
[4592] | 229 | /** |
---|
[4836] | 230 | * Collision with another Entity has this effect |
---|
| 231 | * @param other the other colider |
---|
| 232 | * @param ownhitflags ?? |
---|
| 233 | * @param otherhitflags ?? |
---|
[3471] | 234 | */ |
---|
| 235 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
| 236 | { |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | /** |
---|
[4836] | 241 | * draws the player after transforming him. |
---|
[3471] | 242 | */ |
---|
| 243 | void Player::draw () |
---|
[4592] | 244 | { |
---|
[3471] | 245 | glMatrixMode(GL_MODELVIEW); |
---|
[3526] | 246 | glPushMatrix(); |
---|
[3471] | 247 | /* translate */ |
---|
[4592] | 248 | glTranslatef (this->getAbsCoor ().x, |
---|
| 249 | this->getAbsCoor ().y, |
---|
| 250 | this->getAbsCoor ().z); |
---|
[3471] | 251 | /* rotate */ |
---|
[4998] | 252 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
| 253 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
[3471] | 254 | this->model->draw(); |
---|
[3526] | 255 | glPopMatrix(); |
---|
[3750] | 256 | |
---|
[3877] | 257 | this->weaponMan->draw(); |
---|
[4994] | 258 | |
---|
| 259 | //this->debug(0); |
---|
[3471] | 260 | } |
---|
| 261 | |
---|
| 262 | |
---|
| 263 | /** |
---|
[4836] | 264 | * the function called for each passing timeSnap |
---|
| 265 | * @param time The timespan passed since last update |
---|
[3471] | 266 | */ |
---|
[3584] | 267 | void Player::tick (float time) |
---|
[3471] | 268 | { |
---|
[4885] | 269 | // player controlled movement |
---|
| 270 | this->move(time); |
---|
[4592] | 271 | |
---|
[3877] | 272 | this->weaponMan->tick(time); |
---|
[3584] | 273 | // weapon system manipulation |
---|
[4885] | 274 | this->weaponAction(); |
---|
[3471] | 275 | } |
---|
| 276 | |
---|
[3584] | 277 | |
---|
[3471] | 278 | /** |
---|
[4836] | 279 | * action if player moves |
---|
| 280 | * @param time the timeslice since the last frame |
---|
[3471] | 281 | */ |
---|
| 282 | void Player::move (float time) |
---|
| 283 | { |
---|
| 284 | Vector accel(0.0, 0.0, 0.0); |
---|
| 285 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 286 | /* calculate the direction in which the craft is heading */ |
---|
| 287 | Vector direction (1.0, 0.0, 0.0); |
---|
| 288 | //direction = this->absDirection.apply (direction); |
---|
| 289 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
| 290 | //orthDirection = orthDirection.cross (direction); |
---|
| 291 | |
---|
[3966] | 292 | if( this->bUp && this->getRelCoor().x < 20) |
---|
[3596] | 293 | accel = accel+(direction*acceleration); |
---|
[3966] | 294 | if( this->bDown && this->getRelCoor().x > -5) |
---|
[4412] | 295 | accel = accel -(direction*acceleration); |
---|
[3966] | 296 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
[4592] | 297 | accel = accel - (orthDirection*acceleration); |
---|
[3966] | 298 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
[4413] | 299 | accel = accel + (orthDirection*acceleration); |
---|
[4885] | 300 | if( this->bAscend ) { /* FIXME */ } |
---|
[4836] | 301 | if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */ |
---|
[3471] | 302 | |
---|
| 303 | Vector move = accel * time; |
---|
[3811] | 304 | this->shiftCoor (move); |
---|
[3471] | 305 | } |
---|
[3584] | 306 | |
---|
| 307 | |
---|
| 308 | /** |
---|
[4885] | 309 | * weapon manipulation by the player |
---|
[3584] | 310 | */ |
---|
[4885] | 311 | void Player::weaponAction() |
---|
[3584] | 312 | { |
---|
[3618] | 313 | if( this->bFire) |
---|
[3584] | 314 | { |
---|
[3875] | 315 | this->weaponMan->fire(); |
---|
[3584] | 316 | } |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
[4781] | 320 | * @todo switch statement ?? |
---|
| 321 | */ |
---|
[4404] | 322 | void Player::process(const Event &event) |
---|
| 323 | { |
---|
[4409] | 324 | if( event.type == KeyMapper::PEV_UP) |
---|
| 325 | this->bUp = event.bPressed; |
---|
| 326 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
| 327 | this->bDown = event.bPressed; |
---|
| 328 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[4413] | 329 | this->bRight= event.bPressed; |
---|
[4409] | 330 | else if( event.type == KeyMapper::PEV_LEFT) |
---|
[4413] | 331 | this->bLeft = event.bPressed; |
---|
[4412] | 332 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
[4885] | 333 | this->bFire = event.bPressed; |
---|
[4952] | 334 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
[4954] | 335 | this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
[4952] | 336 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 337 | this->weaponMan->previousWeaponConfig(); |
---|
[4404] | 338 | } |
---|