[5907] | 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: Benjamin Knecht |
---|
[6034] | 13 | co-programmer: Silvan Nellen |
---|
[5907] | 14 | |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
| 19 | #include "executor/executor.h" |
---|
| 20 | #include "space_ship.h" |
---|
| 21 | |
---|
[6034] | 22 | #include "objModel.h" |
---|
[5907] | 23 | #include "resource_manager.h" |
---|
| 24 | |
---|
| 25 | #include "weapons/weapon_manager.h" |
---|
| 26 | #include "weapons/test_gun.h" |
---|
| 27 | #include "weapons/turret.h" |
---|
| 28 | #include "weapons/cannon.h" |
---|
| 29 | |
---|
| 30 | #include "factory.h" |
---|
| 31 | #include "key_mapper.h" |
---|
[5978] | 32 | #include "event_handler.h" |
---|
[5907] | 33 | |
---|
[6424] | 34 | #include "network_game_manager.h" |
---|
| 35 | |
---|
[6243] | 36 | #include "power_ups/weapon_power_up.h" |
---|
| 37 | #include "power_ups/param_power_up.h" |
---|
| 38 | |
---|
[5978] | 39 | #include "graphics_engine.h" |
---|
| 40 | |
---|
[6617] | 41 | #include "plane.h" |
---|
[6287] | 42 | |
---|
[6634] | 43 | #include "state.h" |
---|
| 44 | #include "player.h" |
---|
[6287] | 45 | |
---|
[6634] | 46 | |
---|
[6306] | 47 | // #include "lib/gui/gl_gui/glgui_bar.h" |
---|
| 48 | // #include "lib/gui/gl_gui/glgui_pushbutton.h" |
---|
[6287] | 49 | |
---|
| 50 | |
---|
[5907] | 51 | using namespace std; |
---|
| 52 | |
---|
| 53 | CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP); |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * creates the controlable Spaceship |
---|
| 57 | */ |
---|
| 58 | SpaceShip::SpaceShip() |
---|
| 59 | { |
---|
| 60 | this->init(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * destructs the spaceship, deletes alocated memory |
---|
| 65 | */ |
---|
| 66 | SpaceShip::~SpaceShip () |
---|
| 67 | { |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * loads a Spaceships information from a specified file. |
---|
| 72 | * @param fileName the name of the File to load the spaceship from (absolute path) |
---|
| 73 | */ |
---|
| 74 | SpaceShip::SpaceShip(const char* fileName) |
---|
| 75 | { |
---|
| 76 | this->init(); |
---|
| 77 | TiXmlDocument doc(fileName); |
---|
| 78 | |
---|
| 79 | if(!doc.LoadFile()) |
---|
| 80 | { |
---|
| 81 | PRINTF(2)("Loading file %s failed for spaceship.\n", fileName); |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | this->loadParams(doc.RootElement()); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * creates a new Spaceship from Xml Data |
---|
| 90 | * @param root the xml element containing spaceship data |
---|
| 91 | |
---|
| 92 | @todo add more parameters to load |
---|
| 93 | */ |
---|
| 94 | SpaceShip::SpaceShip(const TiXmlElement* root) |
---|
| 95 | { |
---|
| 96 | this->init(); |
---|
| 97 | if (root != NULL) |
---|
| 98 | this->loadParams(root); |
---|
[6695] | 99 | else |
---|
| 100 | { |
---|
| 101 | this->loadModel("models/ships/reap_#.obj"); |
---|
| 102 | } |
---|
[5907] | 103 | |
---|
[6695] | 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * initializes a Spaceship |
---|
| 109 | */ |
---|
| 110 | void SpaceShip::init() |
---|
| 111 | { |
---|
| 112 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 113 | this->setClassID(CL_SPACE_SHIP, "SpaceShip"); |
---|
| 114 | |
---|
| 115 | PRINTF(4)("SPACESHIP INIT\n"); |
---|
| 116 | |
---|
[5907] | 117 | //weapons: |
---|
| 118 | Weapon* wpRight = new TestGun(0); |
---|
| 119 | wpRight->setName("testGun Right"); |
---|
| 120 | Weapon* wpLeft = new TestGun(1); |
---|
| 121 | wpLeft->setName("testGun Left"); |
---|
[5982] | 122 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
[5907] | 123 | |
---|
| 124 | cannon->setName("BFG"); |
---|
| 125 | |
---|
[6443] | 126 | this->addWeapon(wpLeft, 1, 0); |
---|
| 127 | this->addWeapon(wpRight,1 ,1); |
---|
| 128 | this->addWeapon(cannon, 0, 6); |
---|
[5907] | 129 | |
---|
| 130 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
| 131 | |
---|
[6547] | 132 | EventHandler::getInstance()->grabEvents(true); |
---|
[5907] | 133 | |
---|
[5978] | 134 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
[5907] | 135 | bFire = false; |
---|
[5978] | 136 | xMouse = yMouse = 0; |
---|
[6637] | 137 | yInvert = 1; |
---|
[5978] | 138 | mouseSensitivity = 0.001; |
---|
[6692] | 139 | airViscosity = 0.05; |
---|
[6637] | 140 | controlVelocityX = 25; |
---|
| 141 | controlVelocityY = 150; |
---|
| 142 | shipInertia = 0.5 ; |
---|
| 143 | // cycle = 0.0; |
---|
[5907] | 144 | |
---|
[6430] | 145 | this->setMaxEnergy(100); |
---|
| 146 | this->setEnergy(80); |
---|
[5982] | 147 | |
---|
[6637] | 148 | travelSpeed = 40.0; |
---|
| 149 | acceleration = 3; |
---|
| 150 | this->velocity = this->getAbsDirX()*travelSpeed; |
---|
[6162] | 151 | this->mouseDir = this->getAbsDir(); |
---|
[6637] | 152 | this->pitchDir = this->getAbsDir(); |
---|
[5907] | 153 | |
---|
[6306] | 154 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 155 | // button->show(); |
---|
| 156 | // button->setLabel("orxonox"); |
---|
| 157 | // button->setBindNode(this); |
---|
| 158 | // GLGuiBar* bar = new GLGuiBar(); |
---|
| 159 | // bar->show(); |
---|
| 160 | // bar->setValue(7.0); |
---|
| 161 | // bar->setMaximum(10); |
---|
| 162 | // bar->setSize2D( 20, 100); |
---|
| 163 | // bar->setAbsCoor2D( 10, 200); |
---|
[6300] | 164 | |
---|
[5907] | 165 | //add events to the eventlist |
---|
[6637] | 166 | registerEvent(KeyMapper::PEV_UP); |
---|
| 167 | registerEvent(KeyMapper::PEV_DOWN); |
---|
| 168 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 169 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
| 170 | //registerEvent(SDLK_q); |
---|
| 171 | //registerEvent(SDLK_e); |
---|
[5907] | 172 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 173 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 174 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
[6637] | 175 | //registerEvent(SDLK_PAGEUP); |
---|
| 176 | //registerEvent(SDLK_PAGEDOWN); |
---|
[5978] | 177 | registerEvent(EV_MOUSE_MOTION); |
---|
[5907] | 178 | |
---|
| 179 | this->getWeaponManager()->setSlotCount(7); |
---|
| 180 | |
---|
| 181 | this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 182 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 183 | |
---|
| 184 | this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
| 185 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 186 | |
---|
| 187 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 188 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
| 189 | |
---|
| 190 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 191 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 192 | |
---|
| 193 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 194 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 195 | |
---|
| 196 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 197 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 198 | // |
---|
| 199 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 200 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 201 | // |
---|
| 202 | // this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); |
---|
| 203 | // this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); |
---|
| 204 | // |
---|
| 205 | // this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); |
---|
| 206 | // this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: |
---|
| 207 | |
---|
[6162] | 208 | this->getWeaponManager()->getFixedTarget()->setParent(this); |
---|
| 209 | this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 210 | |
---|
[6222] | 211 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
[5907] | 212 | } |
---|
| 213 | |
---|
[6695] | 214 | |
---|
[5907] | 215 | /** |
---|
| 216 | * loads the Settings of a SpaceShip from an XML-element. |
---|
| 217 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 218 | */ |
---|
| 219 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
| 220 | { |
---|
[6512] | 221 | WorldEntity::loadParams(root); |
---|
[5907] | 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
[6222] | 225 | void SpaceShip::enter() |
---|
| 226 | { |
---|
| 227 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
[6241] | 228 | this->attachCamera(); |
---|
[6222] | 229 | } |
---|
| 230 | |
---|
| 231 | void SpaceShip::leave() |
---|
| 232 | { |
---|
| 233 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
[6241] | 234 | this->detachCamera(); |
---|
[6222] | 235 | } |
---|
| 236 | |
---|
[5907] | 237 | |
---|
| 238 | /** |
---|
| 239 | * effect that occurs after the SpaceShip is spawned |
---|
| 240 | */ |
---|
| 241 | void SpaceShip::postSpawn () |
---|
| 242 | { |
---|
| 243 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * the action occuring if the spaceship left the game |
---|
| 248 | */ |
---|
| 249 | void SpaceShip::leftWorld () |
---|
| 250 | {} |
---|
| 251 | |
---|
| 252 | WorldEntity* ref = NULL; |
---|
| 253 | /** |
---|
| 254 | * this function is called, when two entities collide |
---|
| 255 | * @param entity: the world entity with whom it collides |
---|
| 256 | * |
---|
| 257 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 258 | */ |
---|
| 259 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 260 | { |
---|
[6436] | 261 | Playable::collidesWith(entity, location); |
---|
[5907] | 262 | if (entity->isA(CL_TURRET_POWER_UP) && entity != ref) |
---|
| 263 | { |
---|
| 264 | this->ADDWEAPON(); |
---|
| 265 | ref = entity; |
---|
| 266 | } |
---|
| 267 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * draws the spaceship after transforming it. |
---|
| 272 | */ |
---|
| 273 | void SpaceShip::draw () const |
---|
| 274 | { |
---|
[6281] | 275 | WorldEntity::draw(); |
---|
[5907] | 276 | this->getWeaponManager()->draw(); |
---|
| 277 | |
---|
| 278 | //this->debug(0); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | /** |
---|
| 282 | * the function called for each passing timeSnap |
---|
| 283 | * @param time The timespan passed since last update |
---|
| 284 | */ |
---|
| 285 | void SpaceShip::tick (float time) |
---|
| 286 | { |
---|
[6634] | 287 | this->getWeaponManager()->tick(time); |
---|
| 288 | // weapon system manipulation |
---|
| 289 | this->weaponAction(); |
---|
[6679] | 290 | |
---|
[6637] | 291 | if( xMouse != 0 || yMouse != 0) |
---|
| 292 | { |
---|
| 293 | if (xMouse > controlVelocityX) xMouse = controlVelocityX; |
---|
| 294 | else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; |
---|
| 295 | if (yMouse > controlVelocityY) yMouse = controlVelocityY; |
---|
| 296 | else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; |
---|
[6679] | 297 | |
---|
[6637] | 298 | pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); |
---|
[6679] | 299 | |
---|
[6637] | 300 | mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); |
---|
| 301 | xMouse = yMouse = 0; |
---|
| 302 | } |
---|
[6034] | 303 | |
---|
[6679] | 304 | |
---|
[6634] | 305 | // if( this != State::getPlayer()->getControllable()) |
---|
| 306 | // return; |
---|
| 307 | |
---|
[5907] | 308 | // spaceship controlled movement |
---|
| 309 | this->calculateVelocity(time); |
---|
| 310 | |
---|
[6637] | 311 | Vector move = velocity*time; |
---|
[6679] | 312 | |
---|
[6034] | 313 | //orient the velocity in the direction of the spaceship. |
---|
| 314 | travelSpeed = velocity.len(); |
---|
| 315 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
| 316 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
[6222] | 317 | |
---|
[6162] | 318 | //orient the spaceship in direction of the mouse |
---|
[6637] | 319 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*shipInertia); |
---|
[6241] | 320 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
[6162] | 321 | this->setAbsDir( rotQuat); |
---|
| 322 | //this->setAbsDirSoft(mouseDir,5); |
---|
[6222] | 323 | |
---|
[5978] | 324 | // this is the air friction (necessary for a smooth control) |
---|
[6637] | 325 | if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; |
---|
| 326 | else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; |
---|
[6679] | 327 | |
---|
[6637] | 328 | //other physics (gravity) |
---|
[6692] | 329 | //if(travelSpeed < 120) |
---|
| 330 | //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; |
---|
[6679] | 331 | |
---|
[6034] | 332 | //hoover effect |
---|
[6634] | 333 | //cycle += time; |
---|
| 334 | //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
[5982] | 335 | |
---|
[6034] | 336 | //readjust |
---|
[6426] | 337 | //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
[6034] | 338 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
| 339 | |
---|
[5978] | 340 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
[5907] | 341 | |
---|
[6637] | 342 | this->shiftCoor(move); |
---|
[5907] | 343 | |
---|
[6634] | 344 | |
---|
[5907] | 345 | } |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * calculate the velocity |
---|
| 349 | * @param time the timeslice since the last frame |
---|
| 350 | */ |
---|
| 351 | void SpaceShip::calculateVelocity (float time) |
---|
| 352 | { |
---|
| 353 | Vector accel(0.0, 0.0, 0.0); |
---|
[6162] | 354 | /* |
---|
| 355 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 356 | */ |
---|
[5907] | 357 | //float rotVal = 0.0; |
---|
[5912] | 358 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
[5907] | 359 | /* calculate the direction in which the craft is heading */ |
---|
| 360 | |
---|
[6637] | 361 | //Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
[6222] | 362 | |
---|
[5907] | 363 | if( this->bUp ) |
---|
| 364 | { |
---|
[5978] | 365 | //this->shiftCoor(this->getAbsDirX()); |
---|
[6637] | 366 | //accel += (this->getAbsDirX())*2; |
---|
| 367 | accel += (this->getAbsDirX())*acceleration; |
---|
[6222] | 368 | |
---|
[5907] | 369 | } |
---|
| 370 | |
---|
| 371 | if( this->bDown ) |
---|
[5908] | 372 | { |
---|
[5978] | 373 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
[6637] | 374 | //accel -= (this->getAbsDirX())*2; |
---|
| 375 | accel -= (this->getAbsDirX())*0.5*acceleration; |
---|
[5907] | 376 | } |
---|
| 377 | |
---|
[6034] | 378 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
[5907] | 379 | { |
---|
[5912] | 380 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 381 | // accel -= rightDirection; |
---|
[5908] | 382 | //velocityDir.normalize(); |
---|
[5907] | 383 | //rot +=Vector(1,0,0); |
---|
| 384 | //rotVal -= .4; |
---|
| 385 | } |
---|
| 386 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 387 | { |
---|
[5912] | 388 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 389 | |
---|
| 390 | // accel += rightDirection; |
---|
[5908] | 391 | //velocityDir.normalize(); |
---|
[5907] | 392 | //rot += Vector(1,0,0); |
---|
| 393 | //rotVal += .4; |
---|
| 394 | } |
---|
[5982] | 395 | |
---|
[6034] | 396 | |
---|
[5978] | 397 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
| 398 | { |
---|
[6637] | 399 | mouseDir *= Quaternion(-time*2, Vector(1,0,0)); |
---|
[5978] | 400 | // accel -= rightDirection; |
---|
| 401 | //velocityDir.normalize(); |
---|
| 402 | //rot +=Vector(1,0,0); |
---|
| 403 | //rotVal -= .4; |
---|
| 404 | } |
---|
| 405 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
| 406 | { |
---|
[6637] | 407 | mouseDir *= Quaternion(time*2, Vector(1,0,0)); |
---|
[5978] | 408 | |
---|
| 409 | // accel += rightDirection; |
---|
| 410 | //velocityDir.normalize(); |
---|
| 411 | //rot += Vector(1,0,0); |
---|
| 412 | //rotVal += .4; |
---|
| 413 | } |
---|
[5907] | 414 | if (this->bAscend ) |
---|
| 415 | { |
---|
[5912] | 416 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 417 | |
---|
| 418 | // accel += upDirection; |
---|
[5908] | 419 | //velocityDir.normalize(); |
---|
[5907] | 420 | //rot += Vector(0,0,1); |
---|
| 421 | //rotVal += .4; |
---|
| 422 | } |
---|
| 423 | if (this->bDescend ) |
---|
| 424 | { |
---|
[5912] | 425 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 426 | |
---|
| 427 | // accel -= upDirection; |
---|
[5908] | 428 | //velocityDir.normalize(); |
---|
[5907] | 429 | //rot += Vector(0,0,1); |
---|
| 430 | //rotVal -= .4; |
---|
| 431 | } |
---|
| 432 | |
---|
[5978] | 433 | velocity += accel; |
---|
[5907] | 434 | //rot.normalize(); |
---|
| 435 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | /** |
---|
| 439 | * weapon manipulation by the player |
---|
| 440 | */ |
---|
| 441 | void SpaceShip::weaponAction() |
---|
| 442 | { |
---|
| 443 | if( this->bFire) |
---|
| 444 | { |
---|
| 445 | this->getWeaponManager()->fire(); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | /** |
---|
| 450 | * @todo switch statement ?? |
---|
| 451 | */ |
---|
| 452 | void SpaceShip::process(const Event &event) |
---|
| 453 | { |
---|
[6637] | 454 | if( event.type == KeyMapper::PEV_LEFT) |
---|
[5978] | 455 | this->bRollL = event.bPressed; |
---|
[6637] | 456 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[5978] | 457 | this->bRollR = event.bPressed; |
---|
[5907] | 458 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 459 | this->bFire = event.bPressed; |
---|
| 460 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
[6443] | 461 | { |
---|
[6444] | 462 | this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
[6443] | 463 | } |
---|
[5907] | 464 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
[6444] | 465 | this->previousWeaponConfig(); |
---|
[6637] | 466 | else if( event.type == KeyMapper::PEV_UP) |
---|
[5978] | 467 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6637] | 468 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
[5978] | 469 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 470 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 471 | { |
---|
[6637] | 472 | this->xMouse += event.xRel; |
---|
| 473 | this->yMouse += event.yRel; |
---|
[5978] | 474 | } |
---|
[5907] | 475 | } |
---|
| 476 | |
---|
| 477 | #include "weapons/aiming_turret.h" |
---|
| 478 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
| 479 | void SpaceShip::ADDWEAPON() |
---|
| 480 | { |
---|
| 481 | Weapon* turret = NULL; |
---|
| 482 | |
---|
[6306] | 483 | if ((float)rand()/RAND_MAX < .9) |
---|
[5907] | 484 | { |
---|
| 485 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
| 486 | { |
---|
| 487 | turret = new Turret(); |
---|
[6443] | 488 | this->addWeapon(turret, 2); |
---|
[5907] | 489 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | else |
---|
| 493 | { |
---|
| 494 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
| 495 | { |
---|
[6426] | 496 | turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET)); |
---|
| 497 | if (turret != NULL) |
---|
[6443] | 498 | this->addWeapon(turret, 3); |
---|
[5907] | 499 | |
---|
| 500 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
| 501 | } |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | if(turret != NULL) |
---|
| 505 | { |
---|
| 506 | turret->setName("Turret"); |
---|
| 507 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
| 508 | } |
---|
| 509 | } |
---|
[6424] | 510 | |
---|
[6498] | 511 | #define MASK_bUp 1 |
---|
| 512 | #define MASK_bDown 2 |
---|
| 513 | #define MASK_bLeft 4 |
---|
| 514 | #define MASK_bRight 8 |
---|
| 515 | #define MASK_bAscend 16 |
---|
| 516 | #define MASK_bDescend 32 |
---|
| 517 | #define MASK_bFire 64 |
---|
| 518 | #define MASK_bRollL 128 |
---|
| 519 | #define MASK_bRollR 256 |
---|
[6424] | 520 | |
---|
[6498] | 521 | #define DATA_state 1 |
---|
| 522 | #define DATA_flags 2 |
---|
| 523 | #define DATA_mouse 3 |
---|
| 524 | |
---|
[6424] | 525 | int SpaceShip::writeBytes( const byte * data, int length, int sender ) |
---|
| 526 | { |
---|
| 527 | SYNCHELP_READ_BEGIN(); |
---|
| 528 | |
---|
[6498] | 529 | byte b; |
---|
| 530 | SYNCHELP_READ_BYTE( b ); |
---|
[6424] | 531 | |
---|
[6634] | 532 | if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ ) |
---|
[6498] | 533 | { |
---|
[6634] | 534 | PRINTF(0)("GOT STATE %d\n", this->getUniqueID()); |
---|
[6498] | 535 | setRequestedSync( false ); |
---|
| 536 | setIsOutOfSync( false ); |
---|
| 537 | SYNCHELP_READ_FKT( WorldEntity::writeState ); |
---|
[6634] | 538 | //SYNCHELP_READ_FLOAT( cycle ); |
---|
| 539 | |
---|
| 540 | return SYNCHELP_READ_N; |
---|
[6498] | 541 | } |
---|
| 542 | |
---|
[6634] | 543 | |
---|
| 544 | if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
[6498] | 545 | { |
---|
[6634] | 546 | int flags = 0; |
---|
[6498] | 547 | SYNCHELP_READ_INT( flags ); |
---|
| 548 | |
---|
[6634] | 549 | bUp = (flags & MASK_bUp) != 0; |
---|
| 550 | bDown = (flags & MASK_bDown) != 0; |
---|
| 551 | bLeft = (flags & MASK_bLeft) != 0; |
---|
| 552 | bRight = (flags & MASK_bRight) != 0; |
---|
| 553 | bAscend = (flags & MASK_bAscend) != 0; |
---|
| 554 | bDescend = (flags & MASK_bDescend) != 0; |
---|
| 555 | bFire = (flags & MASK_bFire) != 0; |
---|
| 556 | bRollL = (flags & MASK_bRollL) != 0; |
---|
| 557 | bRollR = (flags & MASK_bRollR) != 0; |
---|
| 558 | |
---|
[6498] | 559 | } |
---|
| 560 | |
---|
[6634] | 561 | /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() ) |
---|
[6498] | 562 | { |
---|
| 563 | SYNCHELP_READ_FLOAT( xMouse ); |
---|
| 564 | SYNCHELP_READ_FLOAT( yMouse ); |
---|
| 565 | SYNCHELP_READ_FLOAT( mouseSensitivity ); |
---|
| 566 | SYNCHELP_READ_FLOAT( cycle ); |
---|
[6634] | 567 | }*/ |
---|
[6498] | 568 | |
---|
[6634] | 569 | if ( this->getOwner() != this->getHostID() ) |
---|
| 570 | SYNCHELP_READ_FKT( PNode::writeSync ); |
---|
| 571 | |
---|
[6424] | 572 | return SYNCHELP_READ_N; |
---|
| 573 | } |
---|
| 574 | |
---|
[6498] | 575 | |
---|
| 576 | |
---|
[6424] | 577 | int SpaceShip::readBytes( byte * data, int maxLength, int * reciever ) |
---|
| 578 | { |
---|
[6498] | 579 | SYNCHELP_WRITE_BEGIN(); |
---|
| 580 | |
---|
[6634] | 581 | if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
[6424] | 582 | { |
---|
| 583 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
| 584 | setRequestedSync( true ); |
---|
[6634] | 585 | PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID()); |
---|
[6424] | 586 | } |
---|
| 587 | |
---|
| 588 | int rec = this->getRequestSync(); |
---|
| 589 | if ( rec > 0 ) |
---|
| 590 | { |
---|
| 591 | *reciever = rec; |
---|
| 592 | |
---|
[6634] | 593 | PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec); |
---|
| 594 | |
---|
[6498] | 595 | SYNCHELP_WRITE_BYTE( (byte)DATA_state ); |
---|
[6424] | 596 | |
---|
| 597 | SYNCHELP_WRITE_FKT( WorldEntity::readState ); |
---|
[6634] | 598 | //SYNCHELP_WRITE_FLOAT( cycle ); |
---|
[6424] | 599 | |
---|
| 600 | return SYNCHELP_WRITE_N; |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | *reciever = 0; |
---|
[6498] | 604 | |
---|
| 605 | if ( this->getHostID()==this->getOwner() ) |
---|
| 606 | { |
---|
| 607 | int mask = 0; |
---|
| 608 | |
---|
| 609 | if ( bUp ) |
---|
| 610 | mask |= MASK_bUp; |
---|
| 611 | if ( bDown ) |
---|
| 612 | mask |= MASK_bDown; |
---|
| 613 | if ( bLeft ) |
---|
| 614 | mask |= MASK_bLeft; |
---|
| 615 | if ( bRight ) |
---|
| 616 | mask |= MASK_bRight; |
---|
| 617 | if ( bAscend ) |
---|
| 618 | mask |= MASK_bAscend; |
---|
| 619 | if ( bFire ) |
---|
| 620 | mask |= MASK_bFire; |
---|
| 621 | if ( bRollL ) |
---|
| 622 | mask |= MASK_bRollL; |
---|
| 623 | if ( bRollR ) |
---|
| 624 | mask |= MASK_bRollR; |
---|
| 625 | |
---|
| 626 | |
---|
[6634] | 627 | //static float oldxMouse = xMouse + 1.0; |
---|
| 628 | //static float oldyMouse = yMouse + 1.0; |
---|
| 629 | |
---|
[6498] | 630 | if ( mask != oldMask ) |
---|
| 631 | { |
---|
| 632 | oldMask = mask; |
---|
| 633 | SYNCHELP_WRITE_BYTE( DATA_flags ); |
---|
| 634 | SYNCHELP_WRITE_INT( mask ); |
---|
| 635 | } |
---|
[6634] | 636 | else |
---|
| 637 | { |
---|
| 638 | SYNCHELP_WRITE_BYTE( 0 ); |
---|
| 639 | } |
---|
[6498] | 640 | |
---|
[6634] | 641 | /*if ( oldxMouse != xMouse || oldyMouse != yMouse ) |
---|
[6498] | 642 | { |
---|
| 643 | oldxMouse = xMouse; |
---|
| 644 | oldyMouse = yMouse; |
---|
| 645 | SYNCHELP_WRITE_BYTE( DATA_mouse ); |
---|
| 646 | SYNCHELP_WRITE_FLOAT( xMouse ); |
---|
| 647 | SYNCHELP_WRITE_FLOAT( yMouse ); |
---|
| 648 | SYNCHELP_WRITE_FLOAT( mouseSensitivity ); |
---|
| 649 | SYNCHELP_WRITE_FLOAT( cycle ); |
---|
[6634] | 650 | }*/ |
---|
[6498] | 651 | } |
---|
[6634] | 652 | else |
---|
| 653 | { |
---|
| 654 | SYNCHELP_WRITE_BYTE( 0 ); |
---|
| 655 | } |
---|
[6498] | 656 | |
---|
[6634] | 657 | if ( this->getOwner() == this->getHostID() ) |
---|
| 658 | SYNCHELP_WRITE_FKT( PNode::readSync ); |
---|
| 659 | |
---|
[6498] | 660 | return SYNCHELP_WRITE_N; |
---|
[6424] | 661 | } |
---|