[6868] | 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 |
---|
| 13 | co-programmer: Silvan Nellen |
---|
| 14 | |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
| 19 | #include "executor/executor.h" |
---|
| 20 | #include "space_ship.h" |
---|
| 21 | |
---|
| 22 | #include "objModel.h" |
---|
| 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 "dot_emitter.h" |
---|
| 31 | #include "sprite_particles.h" |
---|
| 32 | |
---|
| 33 | #include "factory.h" |
---|
| 34 | #include "key_mapper.h" |
---|
| 35 | #include "event_handler.h" |
---|
| 36 | |
---|
| 37 | #include "network_game_manager.h" |
---|
| 38 | |
---|
| 39 | #include "power_ups/weapon_power_up.h" |
---|
| 40 | #include "power_ups/param_power_up.h" |
---|
| 41 | |
---|
| 42 | #include "graphics_engine.h" |
---|
| 43 | |
---|
| 44 | #include "plane.h" |
---|
| 45 | |
---|
| 46 | #include "state.h" |
---|
| 47 | #include "player.h" |
---|
| 48 | |
---|
[7056] | 49 | #include "load_param.h" |
---|
[6868] | 50 | |
---|
[7056] | 51 | |
---|
[6868] | 52 | // #include "lib/gui/gl_gui/glgui_bar.h" |
---|
| 53 | // #include "lib/gui/gl_gui/glgui_pushbutton.h" |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | using namespace std; |
---|
| 57 | |
---|
| 58 | CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP); |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * destructs the spaceship, deletes alocated memory |
---|
| 63 | */ |
---|
| 64 | SpaceShip::~SpaceShip () |
---|
| 65 | { |
---|
[6986] | 66 | this->setPlayer(NULL); |
---|
[6868] | 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * loads a Spaceships information from a specified file. |
---|
| 71 | * @param fileName the name of the File to load the spaceship from (absolute path) |
---|
| 72 | */ |
---|
| 73 | SpaceShip::SpaceShip(const char* fileName) |
---|
| 74 | { |
---|
| 75 | this->init(); |
---|
| 76 | TiXmlDocument doc(fileName); |
---|
| 77 | |
---|
| 78 | if(!doc.LoadFile()) |
---|
| 79 | { |
---|
| 80 | PRINTF(2)("Loading file %s failed for spaceship.\n", fileName); |
---|
| 81 | return; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | this->loadParams(doc.RootElement()); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * creates a new Spaceship from Xml Data |
---|
| 89 | * @param root the xml element containing spaceship data |
---|
| 90 | |
---|
| 91 | @todo add more parameters to load |
---|
| 92 | */ |
---|
| 93 | SpaceShip::SpaceShip(const TiXmlElement* root) |
---|
| 94 | { |
---|
| 95 | this->init(); |
---|
| 96 | if (root != NULL) |
---|
| 97 | this->loadParams(root); |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | //this->loadModel("models/ships/reap_#.obj"); |
---|
[6994] | 101 | //TODO HACK this is only for network multiplayer games. |
---|
[6959] | 102 | if( this->getOwner()%2 == 0) |
---|
| 103 | { |
---|
| 104 | this->loadModel("models/ships/reap_#.obj"); |
---|
| 105 | this->toList(OM_GROUP_00); |
---|
| 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
| 109 | this->loadModel( "models/ships/fighter.obj" ); |
---|
| 110 | this->toList(OM_GROUP_01); |
---|
| 111 | } |
---|
[6868] | 112 | } |
---|
| 113 | |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * initializes a Spaceship |
---|
| 119 | */ |
---|
| 120 | void SpaceShip::init() |
---|
| 121 | { |
---|
| 122 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
| 123 | this->setClassID(CL_SPACE_SHIP, "SpaceShip"); |
---|
| 124 | |
---|
| 125 | PRINTF(4)("SPACESHIP INIT\n"); |
---|
| 126 | |
---|
| 127 | //weapons: |
---|
| 128 | Weapon* wpRight = new TestGun(0); |
---|
| 129 | wpRight->setName("testGun Right"); |
---|
| 130 | Weapon* wpLeft = new TestGun(1); |
---|
| 131 | wpLeft->setName("testGun Left"); |
---|
[7087] | 132 | //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
[6868] | 133 | |
---|
[7087] | 134 | //cannon->setName("BFG"); |
---|
[6868] | 135 | |
---|
| 136 | this->addWeapon(wpLeft, 1, 0); |
---|
| 137 | this->addWeapon(wpRight,1 ,1); |
---|
[7087] | 138 | //this->addWeapon(cannon, 0, 6); |
---|
[6868] | 139 | |
---|
| 140 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
| 141 | |
---|
| 142 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
| 143 | |
---|
| 144 | xMouse = yMouse = 0; |
---|
| 145 | yInvert = 1; |
---|
[7138] | 146 | mouseSensitivity = 0.01; |
---|
| 147 | airViscosity = 0.8; |
---|
[6868] | 148 | controlVelocityX = 25; |
---|
[7141] | 149 | controlVelocityY = 100; |
---|
| 150 | shipInertia = M_PI_2; |
---|
[7138] | 151 | maxRudderAmplitude = 0.6 ; |
---|
[6868] | 152 | // cycle = 0.0; |
---|
| 153 | |
---|
| 154 | this->setHealthMax(100); |
---|
| 155 | this->setHealth(80); |
---|
| 156 | |
---|
[6947] | 157 | travelSpeed = 0.0; |
---|
[6868] | 158 | acceleration = 3; |
---|
| 159 | this->velocity = this->getAbsDirX()*travelSpeed; |
---|
| 160 | this->mouseDir = this->getAbsDir(); |
---|
| 161 | this->pitchDir = this->getAbsDir(); |
---|
| 162 | |
---|
| 163 | // GLGuiButton* button = new GLGuiPushButton(); |
---|
| 164 | // button->show(); |
---|
| 165 | // button->setLabel("orxonox"); |
---|
| 166 | // button->setBindNode(this); |
---|
| 167 | // GLGuiBar* bar = new GLGuiBar(); |
---|
| 168 | // bar->show(); |
---|
| 169 | // bar->setValue(7.0); |
---|
| 170 | // bar->setMaximum(10); |
---|
| 171 | // bar->setSize2D( 20, 100); |
---|
| 172 | // bar->setAbsCoor2D( 10, 200); |
---|
| 173 | |
---|
| 174 | //add events to the eventlist |
---|
[6997] | 175 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
| 176 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
[6868] | 177 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 178 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
[7138] | 179 | |
---|
[6868] | 180 | //registerEvent(SDLK_e); |
---|
[7138] | 181 | //registerEvent(SDLK_e); |
---|
| 182 | //registerEvent(SDLK_e); |
---|
| 183 | //registerEvent(SDLK_e); |
---|
| 184 | |
---|
[6868] | 185 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 186 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 187 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
| 188 | //registerEvent(SDLK_PAGEUP); |
---|
| 189 | //registerEvent(SDLK_PAGEDOWN); |
---|
| 190 | registerEvent(EV_MOUSE_MOTION); |
---|
| 191 | |
---|
| 192 | this->getWeaponManager()->setSlotCount(7); |
---|
| 193 | |
---|
| 194 | this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0)); |
---|
| 195 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 196 | |
---|
| 197 | this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0)); |
---|
| 198 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 199 | |
---|
| 200 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5)); |
---|
| 201 | this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0))); |
---|
| 202 | |
---|
| 203 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5)); |
---|
| 204 | this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0))); |
---|
| 205 | |
---|
| 206 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5)); |
---|
| 207 | this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0))); |
---|
| 208 | |
---|
| 209 | this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5)); |
---|
| 210 | this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0))); |
---|
| 211 | // |
---|
| 212 | this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0)); |
---|
| 213 | this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 214 | // |
---|
| 215 | // this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0)); |
---|
| 216 | // this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0))); |
---|
| 217 | // |
---|
| 218 | // this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0)); |
---|
| 219 | // this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));: |
---|
| 220 | |
---|
| 221 | this->getWeaponManager()->getFixedTarget()->setParent(this); |
---|
| 222 | this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0); |
---|
| 223 | |
---|
| 224 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 225 | |
---|
| 226 | this->burstEmitter = new DotEmitter(200, 0.0, .01); |
---|
| 227 | this->burstEmitter->setParent(this); |
---|
| 228 | this->burstEmitter->setRelCoor(-1, .5, 0); |
---|
| 229 | this->burstEmitter->setName("SpaceShip_Burst_emitter"); |
---|
| 230 | |
---|
| 231 | this->burstSystem = new SpriteParticles(1000); |
---|
| 232 | this->burstSystem->addEmitter(this->burstEmitter); |
---|
| 233 | this->burstSystem->setName("SpaceShip_Burst_System"); |
---|
| 234 | ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png"); |
---|
| 235 | this->burstSystem->setLifeSpan(1.0, .3); |
---|
| 236 | this->burstSystem->setRadius(0.0, 1.0); |
---|
| 237 | this->burstSystem->setRadius(0.05, 1.0); |
---|
| 238 | this->burstSystem->setRadius(.5, .8); |
---|
| 239 | this->burstSystem->setRadius(1.0, 0); |
---|
| 240 | this->burstSystem->setColor(0.0, .7,.7,1,.7); |
---|
| 241 | this->burstSystem->setColor(0.2, 0,0,0.8,.5); |
---|
| 242 | this->burstSystem->setColor(0.5, .5,.5,.8,.8); |
---|
| 243 | this->burstSystem->setColor(1.0, .8,.8,.8,.0); |
---|
| 244 | |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * loads the Settings of a SpaceShip from an XML-element. |
---|
| 250 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 251 | */ |
---|
| 252 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
| 253 | { |
---|
| 254 | Playable::loadParams(root); |
---|
| 255 | } |
---|
| 256 | |
---|
[7092] | 257 | void SpaceShip::setAbsDirPlay(const Quaternion& quat) |
---|
[7056] | 258 | { |
---|
[7095] | 259 | this->mouseDir = quat; |
---|
[7056] | 260 | } |
---|
[6868] | 261 | |
---|
[7056] | 262 | |
---|
[7085] | 263 | void SpaceShip::reset() |
---|
| 264 | { |
---|
| 265 | bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false; |
---|
[7056] | 266 | |
---|
[7085] | 267 | xMouse = yMouse = 0; |
---|
| 268 | |
---|
| 269 | this->setHealth(80); |
---|
| 270 | this->velocity = Vector(0.0, 0.0, 0.0); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
[6868] | 274 | void SpaceShip::enter() |
---|
| 275 | { |
---|
| 276 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
| 277 | this->attachCamera(); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | void SpaceShip::leave() |
---|
| 281 | { |
---|
| 282 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 283 | this->detachCamera(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | /** |
---|
| 288 | * effect that occurs after the SpaceShip is spawned |
---|
| 289 | */ |
---|
| 290 | void SpaceShip::postSpawn () |
---|
| 291 | { |
---|
| 292 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * the action occuring if the spaceship left the game |
---|
| 297 | */ |
---|
| 298 | void SpaceShip::leftWorld () |
---|
| 299 | {} |
---|
| 300 | |
---|
| 301 | WorldEntity* ref = NULL; |
---|
| 302 | /** |
---|
| 303 | * this function is called, when two entities collide |
---|
| 304 | * @param entity: the world entity with whom it collides |
---|
| 305 | * |
---|
| 306 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 307 | */ |
---|
| 308 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 309 | { |
---|
| 310 | Playable::collidesWith(entity, location); |
---|
[6959] | 311 | |
---|
| 312 | if( entity->isA(CL_PROJECTILE) && entity != ref) |
---|
| 313 | { |
---|
| 314 | if ( isServer() ) |
---|
| 315 | { |
---|
| 316 | networkCollisionList.push_back( entity->getHealth() ); |
---|
| 317 | doCollideNetwork( entity->getHealth() ); |
---|
[6868] | 318 | } |
---|
[6959] | 319 | } |
---|
[6868] | 320 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /** |
---|
| 324 | * draws the spaceship after transforming it. |
---|
| 325 | */ |
---|
| 326 | void SpaceShip::draw () const |
---|
| 327 | { |
---|
| 328 | WorldEntity::draw(); |
---|
| 329 | this->getWeaponManager()->draw(); |
---|
| 330 | |
---|
| 331 | //this->debug(0); |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | /** |
---|
| 335 | * the function called for each passing timeSnap |
---|
| 336 | * @param time The timespan passed since last update |
---|
| 337 | */ |
---|
| 338 | void SpaceShip::tick (float time) |
---|
| 339 | { |
---|
| 340 | Playable::tick(time); |
---|
[7146] | 341 | |
---|
| 342 | if( ( xMouse != 0 |
---|
[7138] | 343 | || yMouse != 0 ) |
---|
[7146] | 344 | && this->getOwner() == this->getHostID() ) |
---|
[6868] | 345 | { |
---|
| 346 | if (xMouse > controlVelocityX) xMouse = controlVelocityX; |
---|
| 347 | else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; |
---|
| 348 | if (yMouse > controlVelocityY) yMouse = controlVelocityY; |
---|
| 349 | else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; |
---|
| 350 | |
---|
| 351 | pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); |
---|
| 352 | |
---|
[7146] | 353 | Quaternion correctionQuat = (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); |
---|
| 354 | if( fabsf((mouseDir*correctionQuat).distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping' |
---|
| 355 | { |
---|
| 356 | mouseDir *= correctionQuat; |
---|
| 357 | } |
---|
| 358 | else |
---|
| 359 | { |
---|
[7147] | 360 | mouseDir = Quaternion::quatSlerp(mouseDir,mouseDir*correctionQuat,fabsf(time)); |
---|
[7146] | 361 | } |
---|
[6868] | 362 | xMouse = yMouse = 0; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | |
---|
[7146] | 366 | |
---|
[6868] | 367 | // if( this != State::getPlayer()->getControllable()) |
---|
| 368 | // return; |
---|
| 369 | |
---|
| 370 | // spaceship controlled movement |
---|
[6959] | 371 | if (this->getOwner() == this->getHostID()) |
---|
| 372 | this->calculateVelocity(time); |
---|
[6868] | 373 | |
---|
[6959] | 374 | |
---|
[6868] | 375 | Vector move = velocity*time; |
---|
| 376 | |
---|
| 377 | //orient the velocity in the direction of the spaceship. |
---|
| 378 | travelSpeed = velocity.len(); |
---|
| 379 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
| 380 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
| 381 | this->burstEmitter->setEmissionRate(travelSpeed); |
---|
| 382 | this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1); |
---|
| 383 | |
---|
| 384 | //orient the spaceship in direction of the mouse |
---|
[7138] | 385 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir,fabsf(time)*shipInertia); |
---|
[6868] | 386 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
| 387 | this->setAbsDir( rotQuat); |
---|
| 388 | //this->setAbsDirSoft(mouseDir,5); |
---|
| 389 | |
---|
| 390 | // this is the air friction (necessary for a smooth control) |
---|
| 391 | if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; |
---|
| 392 | else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; |
---|
| 393 | |
---|
| 394 | //other physics (gravity) |
---|
| 395 | //if(travelSpeed < 120) |
---|
| 396 | //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; |
---|
| 397 | |
---|
| 398 | //hoover effect |
---|
| 399 | //cycle += time; |
---|
| 400 | //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
| 401 | |
---|
| 402 | //readjust |
---|
| 403 | //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
| 404 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
| 405 | |
---|
| 406 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
| 407 | |
---|
| 408 | this->shiftCoor(move); |
---|
| 409 | |
---|
| 410 | |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | /** |
---|
| 414 | * calculate the velocity |
---|
| 415 | * @param time the timeslice since the last frame |
---|
| 416 | */ |
---|
| 417 | void SpaceShip::calculateVelocity (float time) |
---|
| 418 | { |
---|
| 419 | Vector accel(0.0, 0.0, 0.0); |
---|
| 420 | /* |
---|
| 421 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 422 | */ |
---|
| 423 | //float rotVal = 0.0; |
---|
| 424 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
| 425 | /* calculate the direction in which the craft is heading */ |
---|
| 426 | |
---|
| 427 | //Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
| 428 | |
---|
| 429 | if( this->bUp ) |
---|
| 430 | { |
---|
| 431 | //this->shiftCoor(this->getAbsDirX()); |
---|
| 432 | //accel += (this->getAbsDirX())*2; |
---|
| 433 | accel += (this->getAbsDirX())*acceleration; |
---|
| 434 | |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | if( this->bDown ) |
---|
| 438 | { |
---|
| 439 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
| 440 | //accel -= (this->getAbsDirX())*2; |
---|
| 441 | //if(velocity.len() > 50) |
---|
| 442 | accel -= (this->getAbsDirX())*0.5*acceleration; |
---|
| 443 | |
---|
| 444 | |
---|
| 445 | |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
| 449 | { |
---|
| 450 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 451 | // accel -= rightDirection; |
---|
| 452 | //velocityDir.normalize(); |
---|
| 453 | //rot +=Vector(1,0,0); |
---|
| 454 | //rotVal -= .4; |
---|
| 455 | } |
---|
| 456 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 457 | { |
---|
| 458 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 459 | |
---|
| 460 | // accel += rightDirection; |
---|
| 461 | //velocityDir.normalize(); |
---|
| 462 | //rot += Vector(1,0,0); |
---|
| 463 | //rotVal += .4; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | |
---|
| 467 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
| 468 | { |
---|
[7138] | 469 | if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping' |
---|
[6868] | 470 | mouseDir *= Quaternion(-time*2, Vector(1,0,0)); |
---|
| 471 | // accel -= rightDirection; |
---|
| 472 | //velocityDir.normalize(); |
---|
| 473 | //rot +=Vector(1,0,0); |
---|
| 474 | //rotVal -= .4; |
---|
| 475 | } |
---|
| 476 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
| 477 | { |
---|
[7138] | 478 | if( fabsf(mouseDir.distance(this->getAbsDir())) < maxRudderAmplitude )//prevent 'jumping' |
---|
[6868] | 479 | mouseDir *= Quaternion(time*2, Vector(1,0,0)); |
---|
| 480 | |
---|
| 481 | // accel += rightDirection; |
---|
| 482 | //velocityDir.normalize(); |
---|
| 483 | //rot += Vector(1,0,0); |
---|
| 484 | //rotVal += .4; |
---|
| 485 | } |
---|
| 486 | if (this->bAscend ) |
---|
| 487 | { |
---|
| 488 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 489 | |
---|
| 490 | // accel += upDirection; |
---|
| 491 | //velocityDir.normalize(); |
---|
| 492 | //rot += Vector(0,0,1); |
---|
| 493 | //rotVal += .4; |
---|
| 494 | } |
---|
| 495 | if (this->bDescend ) |
---|
| 496 | { |
---|
| 497 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 498 | |
---|
| 499 | // accel -= upDirection; |
---|
| 500 | //velocityDir.normalize(); |
---|
| 501 | //rot += Vector(0,0,1); |
---|
| 502 | //rotVal -= .4; |
---|
| 503 | } |
---|
| 504 | |
---|
[6959] | 505 | velocity += accel*time*10; |
---|
[6868] | 506 | //rot.normalize(); |
---|
| 507 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | * @todo switch statement ?? |
---|
| 512 | */ |
---|
| 513 | void SpaceShip::process(const Event &event) |
---|
| 514 | { |
---|
| 515 | Playable::process(event); |
---|
| 516 | |
---|
| 517 | if( event.type == KeyMapper::PEV_LEFT) |
---|
| 518 | this->bRollL = event.bPressed; |
---|
| 519 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
| 520 | this->bRollR = event.bPressed; |
---|
[6997] | 521 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
[6868] | 522 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6997] | 523 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
[6868] | 524 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 525 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 526 | { |
---|
| 527 | this->xMouse += event.xRel; |
---|
| 528 | this->yMouse += event.yRel; |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | |
---|
| 533 | #define MASK_bUp 1 |
---|
| 534 | #define MASK_bDown 2 |
---|
| 535 | #define MASK_bLeft 4 |
---|
| 536 | #define MASK_bRight 8 |
---|
| 537 | #define MASK_bAscend 16 |
---|
| 538 | #define MASK_bDescend 32 |
---|
| 539 | #define MASK_bRollL 64 |
---|
| 540 | #define MASK_bRollR 128 |
---|
| 541 | |
---|
| 542 | #define DATA_state 1 |
---|
| 543 | #define DATA_flags 2 |
---|
| 544 | #define DATA_mouse 3 |
---|
| 545 | #define DATA_sync 4 |
---|
| 546 | #define DATA_velocity 5 |
---|
| 547 | #define DATA_playables 6 |
---|
[6959] | 548 | #define DATA_collision 7 |
---|
[6868] | 549 | |
---|
| 550 | int SpaceShip::writeBytes( const byte * data, int length, int sender ) |
---|
| 551 | { |
---|
| 552 | SYNCHELP_READ_BEGIN(); |
---|
| 553 | |
---|
| 554 | byte b; |
---|
| 555 | |
---|
| 556 | while ( SYNCHELP_READ_REMAINING()>0 ) |
---|
| 557 | { |
---|
| 558 | SYNCHELP_READ_BYTE( b, NWT_SS_B ); |
---|
| 559 | |
---|
| 560 | if ( b == DATA_state ) |
---|
| 561 | { |
---|
| 562 | setRequestedSync( false ); |
---|
| 563 | setIsOutOfSync( false ); |
---|
| 564 | SYNCHELP_READ_FKT( WorldEntity::writeState, NWT_SS_WE_STATE ); |
---|
| 565 | |
---|
| 566 | continue; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | if ( b == DATA_flags ) |
---|
| 570 | { |
---|
| 571 | if ( this->getOwner() != this->getHostID() ) |
---|
| 572 | { |
---|
| 573 | byte flags = 0; |
---|
| 574 | SYNCHELP_READ_BYTE( flags, NWT_SS_FLAGS ); |
---|
| 575 | |
---|
| 576 | bUp = (flags & MASK_bUp) != 0; |
---|
| 577 | bDown = (flags & MASK_bDown) != 0; |
---|
| 578 | bLeft = (flags & MASK_bLeft) != 0; |
---|
| 579 | bRight = (flags & MASK_bRight) != 0; |
---|
| 580 | bAscend = (flags & MASK_bAscend) != 0; |
---|
| 581 | bDescend = (flags & MASK_bDescend) != 0; |
---|
| 582 | bRollL = (flags & MASK_bRollL) != 0; |
---|
| 583 | bRollR = (flags & MASK_bRollR) != 0; |
---|
[6959] | 584 | |
---|
[6868] | 585 | } |
---|
| 586 | else |
---|
| 587 | assert(false); |
---|
| 588 | |
---|
| 589 | continue; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | if ( b == DATA_mouse ) |
---|
| 593 | { |
---|
| 594 | if ( this->getOwner() != this->getHostID() ) |
---|
| 595 | { |
---|
| 596 | SYNCHELP_READ_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
| 597 | SYNCHELP_READ_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
| 598 | SYNCHELP_READ_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
| 599 | SYNCHELP_READ_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
| 600 | } |
---|
| 601 | else |
---|
| 602 | assert(false); |
---|
| 603 | |
---|
| 604 | continue; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | if ( b == DATA_sync ) |
---|
| 608 | { |
---|
| 609 | if ( this->getOwner() != this->getHostID() ) |
---|
| 610 | { |
---|
| 611 | SYNCHELP_READ_FKT( PNode::writeSync, NWT_SS_PN_SYNC ); |
---|
| 612 | } |
---|
| 613 | else |
---|
| 614 | assert(false); |
---|
| 615 | |
---|
| 616 | continue; |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | if ( b == DATA_velocity ) |
---|
| 620 | { |
---|
| 621 | if ( this->getOwner() != this->getHostID() ) |
---|
| 622 | { |
---|
| 623 | SYNCHELP_READ_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
| 624 | SYNCHELP_READ_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
| 625 | SYNCHELP_READ_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
[6959] | 626 | } |
---|
[6868] | 627 | else |
---|
| 628 | assert(false); |
---|
[6959] | 629 | |
---|
[6868] | 630 | continue; |
---|
| 631 | } |
---|
[6959] | 632 | |
---|
[6868] | 633 | if ( b == DATA_playables ) |
---|
| 634 | { |
---|
| 635 | if ( this->getOwner() != this->getHostID() ) |
---|
| 636 | { |
---|
| 637 | SYNCHELP_READ_FKT( Playable::writeSync, NWT_SS_PL_SYNC ); |
---|
| 638 | } |
---|
| 639 | else |
---|
| 640 | assert(false); |
---|
| 641 | } |
---|
[6959] | 642 | |
---|
| 643 | if ( b == DATA_collision ) |
---|
| 644 | { |
---|
| 645 | int n; |
---|
| 646 | float energy; |
---|
| 647 | SYNCHELP_READ_INT( n, NWT_SS_CO_N ); |
---|
| 648 | |
---|
| 649 | for ( int i = 0; i<n; i++ ) |
---|
| 650 | { |
---|
| 651 | SYNCHELP_READ_FLOAT( energy, NWT_SS_CO_CLID ); |
---|
| 652 | doCollideNetwork( energy ); |
---|
| 653 | } |
---|
| 654 | } |
---|
[6868] | 655 | } |
---|
| 656 | |
---|
| 657 | return SYNCHELP_READ_N; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | |
---|
| 661 | |
---|
| 662 | int SpaceShip::readBytes( byte * data, int maxLength, int * reciever ) |
---|
| 663 | { |
---|
| 664 | SYNCHELP_WRITE_BEGIN(); |
---|
| 665 | |
---|
| 666 | if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
| 667 | { |
---|
| 668 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
| 669 | setRequestedSync( true ); |
---|
| 670 | PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID()); |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | int rec = this->getRequestSync(); |
---|
| 674 | if ( rec > 0 ) |
---|
| 675 | { |
---|
| 676 | *reciever = rec; |
---|
| 677 | |
---|
| 678 | PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec); |
---|
| 679 | |
---|
| 680 | SYNCHELP_WRITE_BYTE( (byte)DATA_state, NWT_SS_B ); |
---|
| 681 | |
---|
| 682 | SYNCHELP_WRITE_FKT( WorldEntity::readState, NWT_SS_WE_STATE ); |
---|
| 683 | |
---|
| 684 | return SYNCHELP_WRITE_N; |
---|
| 685 | } |
---|
| 686 | |
---|
[6959] | 687 | *reciever = 0 - this->getOwner(); |
---|
| 688 | //TODO: implement with SYNCHELP_WRITE_SENT() |
---|
[6868] | 689 | bool sentSomething = false; |
---|
| 690 | |
---|
[6959] | 691 | if ( PNode::needsReadSync() && ( this->getHostID()==0 || this->getOwner() == this->getHostID() ) ) |
---|
[6868] | 692 | { |
---|
[6959] | 693 | PRINTF(0)("sending PNode::readSync\n"); |
---|
[6868] | 694 | SYNCHELP_WRITE_BYTE( DATA_sync, NWT_SS_B ); |
---|
| 695 | SYNCHELP_WRITE_FKT( PNode::readSync, NWT_SS_PN_SYNC ); |
---|
[6959] | 696 | sentSomething = true; |
---|
[6868] | 697 | } |
---|
| 698 | |
---|
| 699 | if ( this->getHostID()==0 || this->getHostID()==this->getOwner() ) |
---|
| 700 | { |
---|
| 701 | byte mask = 0; |
---|
| 702 | |
---|
| 703 | if ( bUp ) |
---|
| 704 | mask |= MASK_bUp; |
---|
| 705 | if ( bDown ) |
---|
| 706 | mask |= MASK_bDown; |
---|
| 707 | if ( bLeft ) |
---|
| 708 | mask |= MASK_bLeft; |
---|
| 709 | if ( bRight ) |
---|
| 710 | mask |= MASK_bRight; |
---|
| 711 | if ( bAscend ) |
---|
| 712 | mask |= MASK_bAscend; |
---|
| 713 | if ( bRollL ) |
---|
| 714 | mask |= MASK_bRollL; |
---|
| 715 | if ( bRollR ) |
---|
| 716 | mask |= MASK_bRollR; |
---|
| 717 | |
---|
| 718 | |
---|
| 719 | if ( mask != oldMask ) |
---|
| 720 | { |
---|
| 721 | oldMask = mask; |
---|
[6959] | 722 | PRINTF(0)("sending mask\n"); |
---|
[6868] | 723 | sentSomething = true; |
---|
| 724 | SYNCHELP_WRITE_BYTE( DATA_flags, NWT_SS_B ); |
---|
| 725 | SYNCHELP_WRITE_BYTE( mask, NWT_SS_FLAGS ); |
---|
| 726 | } |
---|
| 727 | #define __OFFSET_MDIR_W 0.01 |
---|
| 728 | #define __OFFSET_MDIR_V 0.01 |
---|
| 729 | if ( fabs( oldMouseDir.w - mouseDir.w ) > __OFFSET_MDIR_W || |
---|
| 730 | fabs( oldMouseDir.v.x - mouseDir.v.x ) > __OFFSET_MDIR_V || |
---|
| 731 | fabs( oldMouseDir.v.y - mouseDir.v.y ) > __OFFSET_MDIR_V || |
---|
| 732 | fabs( oldMouseDir.v.z - mouseDir.v.z ) > __OFFSET_MDIR_V ) |
---|
| 733 | { |
---|
| 734 | oldMouseDir = mouseDir; |
---|
| 735 | |
---|
| 736 | SYNCHELP_WRITE_BYTE( DATA_mouse, NWT_SS_B ); |
---|
| 737 | PRINTF(0)("SENDING mousedir\n"); |
---|
| 738 | sentSomething = true; |
---|
| 739 | SYNCHELP_WRITE_FLOAT( mouseDir.w, NWT_SS_MOUSEDIRW ); |
---|
| 740 | SYNCHELP_WRITE_FLOAT( mouseDir.v.x, NWT_SS_MOUSEDIRX ); |
---|
| 741 | SYNCHELP_WRITE_FLOAT( mouseDir.v.y, NWT_SS_MOUSEDIRY ); |
---|
| 742 | SYNCHELP_WRITE_FLOAT( mouseDir.v.z, NWT_SS_MOUSEDIRZ ); |
---|
| 743 | } |
---|
| 744 | #define __OFFSET_VEL 0.05 |
---|
[6959] | 745 | if ( fabs( oldVelocity.x - velocity.x ) > __OFFSET_VEL*fabs(oldVelocity.x)+0.1 || |
---|
| 746 | fabs( oldVelocity.y - velocity.y ) > __OFFSET_VEL*fabs(oldVelocity.y)+0.1 || |
---|
| 747 | fabs( oldVelocity.z - velocity.z ) > __OFFSET_VEL*fabs(oldVelocity.z)+0.1 ) |
---|
[6868] | 748 | { |
---|
[6959] | 749 | oldVelocity.x = velocity.x; |
---|
| 750 | oldVelocity.y = velocity.y; |
---|
| 751 | oldVelocity.z = velocity.z; |
---|
| 752 | PRINTF(0)("SENDING velocity\n"); |
---|
| 753 | sentSomething = true; |
---|
| 754 | SYNCHELP_WRITE_BYTE( DATA_velocity, NWT_SS_B ); |
---|
| 755 | SYNCHELP_WRITE_FLOAT( velocity.x, NWT_SS_VELX ); |
---|
| 756 | SYNCHELP_WRITE_FLOAT( velocity.y, NWT_SS_VELY ); |
---|
| 757 | SYNCHELP_WRITE_FLOAT( velocity.z, NWT_SS_VELZ ); |
---|
[6868] | 758 | } |
---|
[6959] | 759 | |
---|
[6994] | 760 | while ( Playable::needsReadSync() ) |
---|
[6868] | 761 | { |
---|
| 762 | sentSomething = true; |
---|
[6959] | 763 | PRINTF(0)("SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC )\n"); |
---|
[6868] | 764 | SYNCHELP_WRITE_BYTE( DATA_playables, NWT_SS_B ); |
---|
| 765 | SYNCHELP_WRITE_FKT( Playable::readSync, NWT_SS_PL_SYNC ); |
---|
| 766 | } |
---|
| 767 | |
---|
| 768 | } |
---|
[6959] | 769 | |
---|
[6868] | 770 | if ( !sentSomething ) |
---|
[6959] | 771 | { |
---|
| 772 | *reciever = 0; |
---|
[6868] | 773 | |
---|
[6959] | 774 | if ( networkCollisionList.size()>0 ) |
---|
| 775 | { |
---|
| 776 | SYNCHELP_WRITE_BYTE( DATA_collision, NWT_SS_B ); |
---|
| 777 | |
---|
| 778 | SYNCHELP_WRITE_INT( networkCollisionList.size(), NWT_SS_CO_N ); |
---|
| 779 | |
---|
| 780 | for ( std::list<float>::iterator it = networkCollisionList.begin(); it!=networkCollisionList.end(); it++ ) |
---|
| 781 | { |
---|
| 782 | SYNCHELP_WRITE_FLOAT( *it, NWT_SS_CO_CLID ); |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | networkCollisionList.clear(); |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
[6868] | 789 | return SYNCHELP_WRITE_N; |
---|
| 790 | } |
---|
| 791 | |
---|
[7101] | 792 | |
---|
[6959] | 793 | void SpaceShip::doCollideNetwork( float energy ) |
---|
| 794 | { |
---|
| 795 | this->decreaseHealth( energy ); |
---|
[6966] | 796 | if( this->getHealth() <= 0) |
---|
| 797 | { |
---|
| 798 | this->die(); |
---|
| 799 | } |
---|
[6959] | 800 | } |
---|
[6868] | 801 | |
---|
[6959] | 802 | |
---|
| 803 | |
---|