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