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