[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(); |
---|
| 85 | this->loadParams(root); |
---|
[4597] | 86 | |
---|
[4010] | 87 | //weapons: |
---|
[4955] | 88 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
---|
[4953] | 89 | wpRight->setName("testGun Right"); |
---|
[4955] | 90 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
---|
[4953] | 91 | wpLeft->setName("testGun Left"); |
---|
[4592] | 92 | |
---|
[4963] | 93 | Weapon* turret = new Turret(this->weaponMan); |
---|
[4969] | 94 | Weapon* turret2 = new Turret(this->weaponMan); |
---|
[4953] | 95 | this->weaponMan->addWeapon(wpLeft, 1, 0); |
---|
| 96 | this->weaponMan->addWeapon(wpRight,1 ,1); |
---|
[4967] | 97 | this->weaponMan->addWeapon(turret, 2, 2); |
---|
[4969] | 98 | this->weaponMan->addWeapon(turret2, 2, 3); |
---|
[4967] | 99 | this->weaponMan->addWeapon(wpLeft, 3, 0); |
---|
| 100 | this->weaponMan->addWeapon(wpRight,3 ,1); |
---|
[4953] | 101 | |
---|
| 102 | this->weaponMan->changeWeaponConfig(0); |
---|
[4010] | 103 | } |
---|
[3583] | 104 | |
---|
[4780] | 105 | /** |
---|
[4975] | 106 | * destructs the player, deletes alocated memory |
---|
| 107 | */ |
---|
| 108 | Player::~Player () |
---|
| 109 | { |
---|
| 110 | /* do not delete the weapons, they are contained in the pnode tree |
---|
| 111 | and will be deleted there. |
---|
| 112 | this only frees the memory allocated to save the list. |
---|
| 113 | */ |
---|
| 114 | delete this->weaponMan; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
[4780] | 118 | * initializes a Player |
---|
| 119 | */ |
---|
| 120 | void Player::init() |
---|
| 121 | { |
---|
| 122 | this->setClassID(CL_PLAYER, "Player"); |
---|
[4404] | 123 | |
---|
[4951] | 124 | // this->model = (Model*)ResourceManager::getInstance()->load("models/reaplow.obj", OBJ, RP_CAMPAIGN); |
---|
[4780] | 125 | travelSpeed = 15.0; |
---|
| 126 | velocity = new Vector(); |
---|
| 127 | bUp = bDown = bLeft = bRight = bAscend = bDescend = false; |
---|
| 128 | bFire = false; |
---|
| 129 | acceleration = 10.0; |
---|
[4834] | 130 | |
---|
| 131 | |
---|
[4953] | 132 | this->weaponMan = new WeaponManager(this); |
---|
[4969] | 133 | this->weaponMan->setSlotCount(4); |
---|
[4953] | 134 | this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 135 | this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
[4969] | 136 | this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 137 | this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
| 138 | this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 139 | this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
[4780] | 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
[3583] | 143 | /** |
---|
[4885] | 144 | * loads the Settings of a Player from an XML-element. |
---|
[4780] | 145 | * @param root the XML-element to load the Player's properties from |
---|
| 146 | */ |
---|
| 147 | void Player::loadParams(const TiXmlElement* root) |
---|
| 148 | { |
---|
| 149 | static_cast<WorldEntity*>(this)->loadParams(root); |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | |
---|
| 156 | /** |
---|
[4885] | 157 | * adds a weapon to the weapon list of player |
---|
[4836] | 158 | * @param weapon to add |
---|
[3583] | 159 | */ |
---|
| 160 | void Player::addWeapon(Weapon* weapon) |
---|
| 161 | { |
---|
[3878] | 162 | this->weaponMan->addWeapon(weapon); |
---|
[3471] | 163 | } |
---|
| 164 | |
---|
[3583] | 165 | |
---|
[3471] | 166 | /** |
---|
[4836] | 167 | * removes a weapon from the player |
---|
| 168 | * @param weapon to remove |
---|
[3583] | 169 | */ |
---|
| 170 | void Player::removeWeapon(Weapon* weapon) |
---|
| 171 | { |
---|
[3878] | 172 | this->weaponMan->removeWeapon(weapon); |
---|
[3583] | 173 | } |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | /** |
---|
[4836] | 177 | * effect that occurs after the player is spawned |
---|
[3471] | 178 | */ |
---|
| 179 | void Player::postSpawn () |
---|
| 180 | { |
---|
[3474] | 181 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
[3471] | 182 | } |
---|
| 183 | |
---|
[3584] | 184 | |
---|
[3471] | 185 | /** |
---|
[4836] | 186 | * the action occuring if the player left the game |
---|
[3471] | 187 | */ |
---|
[3584] | 188 | void Player::leftWorld () |
---|
| 189 | {} |
---|
[3471] | 190 | |
---|
[3584] | 191 | |
---|
| 192 | |
---|
[3471] | 193 | /** |
---|
[4836] | 194 | * if the player is hit, call this function |
---|
| 195 | * @param weapon hit by this weapon |
---|
| 196 | * @param loc ?? |
---|
[3471] | 197 | */ |
---|
[3578] | 198 | void Player::hit (WorldEntity* weapon, Vector* loc) |
---|
[3471] | 199 | { |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
[4592] | 203 | /** |
---|
[4836] | 204 | * Collision with another Entity has this effect |
---|
| 205 | * @param other the other colider |
---|
| 206 | * @param ownhitflags ?? |
---|
| 207 | * @param otherhitflags ?? |
---|
[3471] | 208 | */ |
---|
| 209 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
---|
| 210 | { |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | |
---|
| 214 | /** |
---|
[4836] | 215 | * draws the player after transforming him. |
---|
[3471] | 216 | */ |
---|
| 217 | void Player::draw () |
---|
[4592] | 218 | { |
---|
[3471] | 219 | glMatrixMode(GL_MODELVIEW); |
---|
[3526] | 220 | glPushMatrix(); |
---|
[3471] | 221 | float matrix[4][4]; |
---|
[4592] | 222 | |
---|
[3471] | 223 | /* translate */ |
---|
[4592] | 224 | glTranslatef (this->getAbsCoor ().x, |
---|
| 225 | this->getAbsCoor ().y, |
---|
| 226 | this->getAbsCoor ().z); |
---|
[3471] | 227 | /* rotate */ |
---|
| 228 | this->getAbsDir ().matrix (matrix); |
---|
| 229 | glMultMatrixf((float*)matrix); |
---|
[4592] | 230 | |
---|
[3471] | 231 | this->model->draw(); |
---|
[3526] | 232 | glPopMatrix(); |
---|
[3750] | 233 | |
---|
[3877] | 234 | this->weaponMan->draw(); |
---|
[3471] | 235 | } |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | /** |
---|
[4836] | 239 | * the function called for each passing timeSnap |
---|
| 240 | * @param time The timespan passed since last update |
---|
[3471] | 241 | */ |
---|
[3584] | 242 | void Player::tick (float time) |
---|
[3471] | 243 | { |
---|
[4885] | 244 | // player controlled movement |
---|
| 245 | this->move(time); |
---|
[4592] | 246 | |
---|
[3877] | 247 | this->weaponMan->tick(time); |
---|
[3584] | 248 | // weapon system manipulation |
---|
[4885] | 249 | this->weaponAction(); |
---|
[3471] | 250 | } |
---|
| 251 | |
---|
[3584] | 252 | |
---|
[3471] | 253 | /** |
---|
[4836] | 254 | * action if player moves |
---|
| 255 | * @param time the timeslice since the last frame |
---|
[3471] | 256 | */ |
---|
| 257 | void Player::move (float time) |
---|
| 258 | { |
---|
| 259 | Vector accel(0.0, 0.0, 0.0); |
---|
| 260 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 261 | /* calculate the direction in which the craft is heading */ |
---|
| 262 | Vector direction (1.0, 0.0, 0.0); |
---|
| 263 | //direction = this->absDirection.apply (direction); |
---|
| 264 | Vector orthDirection (0.0, 0.0, 1.0); |
---|
| 265 | //orthDirection = orthDirection.cross (direction); |
---|
| 266 | |
---|
[3966] | 267 | if( this->bUp && this->getRelCoor().x < 20) |
---|
[3596] | 268 | accel = accel+(direction*acceleration); |
---|
[3966] | 269 | if( this->bDown && this->getRelCoor().x > -5) |
---|
[4412] | 270 | accel = accel -(direction*acceleration); |
---|
[3966] | 271 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
---|
[4592] | 272 | accel = accel - (orthDirection*acceleration); |
---|
[3966] | 273 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
---|
[4413] | 274 | accel = accel + (orthDirection*acceleration); |
---|
[4885] | 275 | if( this->bAscend ) { /* FIXME */ } |
---|
[4836] | 276 | if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */ |
---|
[3471] | 277 | |
---|
| 278 | Vector move = accel * time; |
---|
[3811] | 279 | this->shiftCoor (move); |
---|
[3471] | 280 | } |
---|
[3584] | 281 | |
---|
| 282 | |
---|
| 283 | /** |
---|
[4885] | 284 | * weapon manipulation by the player |
---|
[3584] | 285 | */ |
---|
[4885] | 286 | void Player::weaponAction() |
---|
[3584] | 287 | { |
---|
[3618] | 288 | if( this->bFire) |
---|
[3584] | 289 | { |
---|
[3875] | 290 | this->weaponMan->fire(); |
---|
[3584] | 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | /** |
---|
[4781] | 295 | * @todo switch statement ?? |
---|
| 296 | */ |
---|
[4404] | 297 | void Player::process(const Event &event) |
---|
| 298 | { |
---|
[4409] | 299 | if( event.type == KeyMapper::PEV_UP) |
---|
| 300 | this->bUp = event.bPressed; |
---|
| 301 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
| 302 | this->bDown = event.bPressed; |
---|
| 303 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[4413] | 304 | this->bRight= event.bPressed; |
---|
[4409] | 305 | else if( event.type == KeyMapper::PEV_LEFT) |
---|
[4413] | 306 | this->bLeft = event.bPressed; |
---|
[4412] | 307 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
[4885] | 308 | this->bFire = event.bPressed; |
---|
[4952] | 309 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
[4954] | 310 | this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
[4952] | 311 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
| 312 | this->weaponMan->previousWeaponConfig(); |
---|
[4404] | 313 | } |
---|