| 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 "test_gun.h" |
|---|
| 26 | #include "turret.h" |
|---|
| 27 | #include "world.h" |
|---|
| 28 | |
|---|
| 29 | #include "list.h" |
|---|
| 30 | |
|---|
| 31 | #include "event_handler.h" |
|---|
| 32 | |
|---|
| 33 | #include "event.h" |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | using namespace std; |
|---|
| 37 | |
|---|
| 38 | CREATE_FACTORY(Player); |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * creates a new Player |
|---|
| 42 | * @param isFree if the player is free |
|---|
| 43 | */ |
|---|
| 44 | Player::Player() |
|---|
| 45 | { |
|---|
| 46 | this->init(); |
|---|
| 47 | |
|---|
| 48 | //weapons: |
|---|
| 49 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
|---|
| 50 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
|---|
| 51 | |
|---|
| 52 | this->weaponMan->addWeapon(wpRight); |
|---|
| 53 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG1, WM_SLOT1); |
|---|
| 54 | // this->weaponMan->addWeapon(wpRight, WM_CONFIG2); |
|---|
| 55 | // this->weaponMan->addWeapon(wpLeft, WM_CONFIG2); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * destructs the player, deletes alocated memory |
|---|
| 61 | */ |
|---|
| 62 | Player::~Player () |
|---|
| 63 | { |
|---|
| 64 | /* do not delete the weapons, they are contained in the pnode tree |
|---|
| 65 | and will be deleted there. |
|---|
| 66 | this only frees the memory allocated to save the list. |
|---|
| 67 | */ |
|---|
| 68 | delete this->weaponMan; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * creates a new Player from Xml Data |
|---|
| 73 | * @param root the xml element containing player data |
|---|
| 74 | |
|---|
| 75 | @todo add more parameters to load |
|---|
| 76 | */ |
|---|
| 77 | Player::Player(const TiXmlElement* root) |
|---|
| 78 | { |
|---|
| 79 | this->init(); |
|---|
| 80 | this->loadParams(root); |
|---|
| 81 | |
|---|
| 82 | //weapons: |
|---|
| 83 | Weapon* wpRight = new TestGun(this->weaponMan, 0); |
|---|
| 84 | wpRight->setName("testGun Right"); |
|---|
| 85 | Weapon* wpLeft = new TestGun(this->weaponMan, 1); |
|---|
| 86 | wpLeft->setName("testGun Left"); |
|---|
| 87 | |
|---|
| 88 | Weapon* turret = new Turret(this->weaponMan); |
|---|
| 89 | Weapon* turret2 = new Turret(this->weaponMan); |
|---|
| 90 | this->weaponMan->addWeapon(wpLeft, 1, 0); |
|---|
| 91 | this->weaponMan->addWeapon(wpRight,1 ,1); |
|---|
| 92 | this->weaponMan->addWeapon(turret, 2, 2); |
|---|
| 93 | this->weaponMan->addWeapon(turret2, 2, 3); |
|---|
| 94 | this->weaponMan->addWeapon(wpLeft, 3, 0); |
|---|
| 95 | this->weaponMan->addWeapon(wpRight,3 ,1); |
|---|
| 96 | |
|---|
| 97 | this->weaponMan->changeWeaponConfig(0); |
|---|
| 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 | acceleration = 10.0; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | this->weaponMan = new WeaponManager(this); |
|---|
| 116 | this->weaponMan->setSlotCount(4); |
|---|
| 117 | this->weaponMan->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
|---|
| 118 | this->weaponMan->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
|---|
| 119 | this->weaponMan->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
|---|
| 120 | this->weaponMan->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
|---|
| 121 | this->weaponMan->setSlotPosition(3, Vector(-1.5, .5, .5)); |
|---|
| 122 | this->weaponMan->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * loads the Settings of a Player from an XML-element. |
|---|
| 128 | * @param root the XML-element to load the Player's properties from |
|---|
| 129 | */ |
|---|
| 130 | void Player::loadParams(const TiXmlElement* root) |
|---|
| 131 | { |
|---|
| 132 | static_cast<WorldEntity*>(this)->loadParams(root); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | * adds a weapon to the weapon list of player |
|---|
| 141 | * @param weapon to add |
|---|
| 142 | */ |
|---|
| 143 | void Player::addWeapon(Weapon* weapon) |
|---|
| 144 | { |
|---|
| 145 | this->weaponMan->addWeapon(weapon); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * removes a weapon from the player |
|---|
| 151 | * @param weapon to remove |
|---|
| 152 | */ |
|---|
| 153 | void Player::removeWeapon(Weapon* weapon) |
|---|
| 154 | { |
|---|
| 155 | this->weaponMan->removeWeapon(weapon); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * effect that occurs after the player is spawned |
|---|
| 161 | */ |
|---|
| 162 | void Player::postSpawn () |
|---|
| 163 | { |
|---|
| 164 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * the action occuring if the player left the game |
|---|
| 170 | */ |
|---|
| 171 | void Player::leftWorld () |
|---|
| 172 | {} |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * if the player is hit, call this function |
|---|
| 178 | * @param weapon hit by this weapon |
|---|
| 179 | * @param loc ?? |
|---|
| 180 | */ |
|---|
| 181 | void Player::hit (WorldEntity* weapon, Vector* loc) |
|---|
| 182 | { |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Collision with another Entity has this effect |
|---|
| 188 | * @param other the other colider |
|---|
| 189 | * @param ownhitflags ?? |
|---|
| 190 | * @param otherhitflags ?? |
|---|
| 191 | */ |
|---|
| 192 | void Player::collide (WorldEntity* other, Uint32 ownhitflags, Uint32 otherhitflags) |
|---|
| 193 | { |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * draws the player after transforming him. |
|---|
| 199 | */ |
|---|
| 200 | void Player::draw () |
|---|
| 201 | { |
|---|
| 202 | glMatrixMode(GL_MODELVIEW); |
|---|
| 203 | glPushMatrix(); |
|---|
| 204 | float matrix[4][4]; |
|---|
| 205 | |
|---|
| 206 | /* translate */ |
|---|
| 207 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 208 | this->getAbsCoor ().y, |
|---|
| 209 | this->getAbsCoor ().z); |
|---|
| 210 | /* rotate */ |
|---|
| 211 | this->getAbsDir ().matrix (matrix); |
|---|
| 212 | glMultMatrixf((float*)matrix); |
|---|
| 213 | |
|---|
| 214 | this->model->draw(); |
|---|
| 215 | glPopMatrix(); |
|---|
| 216 | |
|---|
| 217 | this->weaponMan->draw(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * the function called for each passing timeSnap |
|---|
| 223 | * @param time The timespan passed since last update |
|---|
| 224 | */ |
|---|
| 225 | void Player::tick (float time) |
|---|
| 226 | { |
|---|
| 227 | // player controlled movement |
|---|
| 228 | this->move(time); |
|---|
| 229 | |
|---|
| 230 | this->weaponMan->tick(time); |
|---|
| 231 | // weapon system manipulation |
|---|
| 232 | this->weaponAction(); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * action if player moves |
|---|
| 238 | * @param time the timeslice since the last frame |
|---|
| 239 | */ |
|---|
| 240 | void Player::move (float time) |
|---|
| 241 | { |
|---|
| 242 | Vector accel(0.0, 0.0, 0.0); |
|---|
| 243 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
|---|
| 244 | /* calculate the direction in which the craft is heading */ |
|---|
| 245 | Vector direction (1.0, 0.0, 0.0); |
|---|
| 246 | //direction = this->absDirection.apply (direction); |
|---|
| 247 | Vector orthDirection (0.0, 0.0, 1.0); |
|---|
| 248 | //orthDirection = orthDirection.cross (direction); |
|---|
| 249 | |
|---|
| 250 | if( this->bUp && this->getRelCoor().x < 20) |
|---|
| 251 | accel = accel+(direction*acceleration); |
|---|
| 252 | if( this->bDown && this->getRelCoor().x > -5) |
|---|
| 253 | accel = accel -(direction*acceleration); |
|---|
| 254 | if( this->bLeft && TrackManager::getInstance()->getWidth() > -this->getRelCoor().z*2) |
|---|
| 255 | accel = accel - (orthDirection*acceleration); |
|---|
| 256 | if( this->bRight && TrackManager::getInstance()->getWidth() > this->getRelCoor().z*2) |
|---|
| 257 | accel = accel + (orthDirection*acceleration); |
|---|
| 258 | if( this->bAscend ) { /* FIXME */ } |
|---|
| 259 | if( this->bDescend) {/* FIXME */} /* @todo up and down player movement */ |
|---|
| 260 | |
|---|
| 261 | Vector move = accel * time; |
|---|
| 262 | this->shiftCoor (move); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | /** |
|---|
| 267 | * weapon manipulation by the player |
|---|
| 268 | */ |
|---|
| 269 | void Player::weaponAction() |
|---|
| 270 | { |
|---|
| 271 | if( this->bFire) |
|---|
| 272 | { |
|---|
| 273 | this->weaponMan->fire(); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * @todo switch statement ?? |
|---|
| 279 | */ |
|---|
| 280 | void Player::process(const Event &event) |
|---|
| 281 | { |
|---|
| 282 | if( event.type == KeyMapper::PEV_UP) |
|---|
| 283 | this->bUp = event.bPressed; |
|---|
| 284 | else if( event.type == KeyMapper::PEV_DOWN) |
|---|
| 285 | this->bDown = event.bPressed; |
|---|
| 286 | else if( event.type == KeyMapper::PEV_RIGHT) |
|---|
| 287 | this->bRight= event.bPressed; |
|---|
| 288 | else if( event.type == KeyMapper::PEV_LEFT) |
|---|
| 289 | this->bLeft = event.bPressed; |
|---|
| 290 | else if( event.type == KeyMapper::PEV_FIRE1) |
|---|
| 291 | this->bFire = event.bPressed; |
|---|
| 292 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
|---|
| 293 | this->weaponMan->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
|---|
| 294 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
|---|
| 295 | this->weaponMan->previousWeaponConfig(); |
|---|
| 296 | } |
|---|