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