[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); |
---|
| 99 | |
---|
| 100 | //weapons: |
---|
| 101 | Weapon* wpRight = new TestGun(0); |
---|
| 102 | wpRight->setName("testGun Right"); |
---|
| 103 | Weapon* wpLeft = new TestGun(1); |
---|
| 104 | wpLeft->setName("testGun Left"); |
---|
[5982] | 105 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON)); |
---|
[5907] | 106 | |
---|
| 107 | cannon->setName("BFG"); |
---|
| 108 | |
---|
[6443] | 109 | this->addWeapon(wpLeft, 1, 0); |
---|
| 110 | this->addWeapon(wpRight,1 ,1); |
---|
| 111 | this->addWeapon(cannon, 0, 6); |
---|
[5907] | 112 | |
---|
| 113 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
| 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 | |
---|
[6547] | 127 | EventHandler::getInstance()->grabEvents(true); |
---|
[5907] | 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; |
---|
| 137 | shipInertia = 0.5 ; |
---|
| 138 | // cycle = 0.0; |
---|
[5907] | 139 | |
---|
[6430] | 140 | this->setMaxEnergy(100); |
---|
| 141 | this->setEnergy(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); |
---|
[5907] | 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * loads the Settings of a SpaceShip from an XML-element. |
---|
| 211 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 212 | */ |
---|
| 213 | void SpaceShip::loadParams(const TiXmlElement* root) |
---|
| 214 | { |
---|
[6512] | 215 | WorldEntity::loadParams(root); |
---|
[5907] | 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
[6222] | 219 | void SpaceShip::enter() |
---|
| 220 | { |
---|
| 221 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
[6241] | 222 | this->attachCamera(); |
---|
[6222] | 223 | } |
---|
| 224 | |
---|
| 225 | void SpaceShip::leave() |
---|
| 226 | { |
---|
| 227 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
[6241] | 228 | this->detachCamera(); |
---|
[6222] | 229 | } |
---|
| 230 | |
---|
[5907] | 231 | |
---|
| 232 | /** |
---|
| 233 | * effect that occurs after the SpaceShip is spawned |
---|
| 234 | */ |
---|
| 235 | void SpaceShip::postSpawn () |
---|
| 236 | { |
---|
| 237 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * the action occuring if the spaceship left the game |
---|
| 242 | */ |
---|
| 243 | void SpaceShip::leftWorld () |
---|
| 244 | {} |
---|
| 245 | |
---|
| 246 | WorldEntity* ref = NULL; |
---|
| 247 | /** |
---|
| 248 | * this function is called, when two entities collide |
---|
| 249 | * @param entity: the world entity with whom it collides |
---|
| 250 | * |
---|
| 251 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 252 | */ |
---|
| 253 | void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location) |
---|
| 254 | { |
---|
[6436] | 255 | Playable::collidesWith(entity, location); |
---|
[5907] | 256 | if (entity->isA(CL_TURRET_POWER_UP) && entity != ref) |
---|
| 257 | { |
---|
| 258 | this->ADDWEAPON(); |
---|
| 259 | ref = entity; |
---|
| 260 | } |
---|
| 261 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * draws the spaceship after transforming it. |
---|
| 266 | */ |
---|
| 267 | void SpaceShip::draw () const |
---|
| 268 | { |
---|
[6281] | 269 | WorldEntity::draw(); |
---|
[5907] | 270 | this->getWeaponManager()->draw(); |
---|
| 271 | |
---|
| 272 | //this->debug(0); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * the function called for each passing timeSnap |
---|
| 277 | * @param time The timespan passed since last update |
---|
| 278 | */ |
---|
| 279 | void SpaceShip::tick (float time) |
---|
| 280 | { |
---|
[6634] | 281 | this->getWeaponManager()->tick(time); |
---|
| 282 | // weapon system manipulation |
---|
| 283 | this->weaponAction(); |
---|
[6679] | 284 | |
---|
[6637] | 285 | if( xMouse != 0 || yMouse != 0) |
---|
| 286 | { |
---|
| 287 | if (xMouse > controlVelocityX) xMouse = controlVelocityX; |
---|
| 288 | else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX; |
---|
| 289 | if (yMouse > controlVelocityY) yMouse = controlVelocityY; |
---|
| 290 | else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY; |
---|
[6679] | 291 | |
---|
[6637] | 292 | pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0))); |
---|
[6679] | 293 | |
---|
[6637] | 294 | mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir); |
---|
| 295 | xMouse = yMouse = 0; |
---|
| 296 | } |
---|
[6034] | 297 | |
---|
[6679] | 298 | |
---|
[6634] | 299 | // if( this != State::getPlayer()->getControllable()) |
---|
| 300 | // return; |
---|
| 301 | |
---|
[5907] | 302 | // spaceship controlled movement |
---|
| 303 | this->calculateVelocity(time); |
---|
| 304 | |
---|
[6637] | 305 | Vector move = velocity*time; |
---|
[6679] | 306 | |
---|
[6034] | 307 | //orient the velocity in the direction of the spaceship. |
---|
| 308 | travelSpeed = velocity.len(); |
---|
| 309 | velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity; |
---|
| 310 | velocity = (velocity.getNormalized())*travelSpeed; |
---|
[6222] | 311 | |
---|
[6162] | 312 | //orient the spaceship in direction of the mouse |
---|
[6637] | 313 | rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*shipInertia); |
---|
[6241] | 314 | if (this->getAbsDir().distance(rotQuat) > 0.00000000000001) |
---|
[6162] | 315 | this->setAbsDir( rotQuat); |
---|
| 316 | //this->setAbsDirSoft(mouseDir,5); |
---|
[6222] | 317 | |
---|
[5978] | 318 | // this is the air friction (necessary for a smooth control) |
---|
[6637] | 319 | if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001; |
---|
| 320 | else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001; |
---|
[6679] | 321 | |
---|
[6637] | 322 | //other physics (gravity) |
---|
[6692] | 323 | //if(travelSpeed < 120) |
---|
| 324 | //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time; |
---|
[6679] | 325 | |
---|
[6034] | 326 | //hoover effect |
---|
[6634] | 327 | //cycle += time; |
---|
| 328 | //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02); |
---|
[5982] | 329 | |
---|
[6034] | 330 | //readjust |
---|
[6426] | 331 | //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0))); |
---|
[6034] | 332 | //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0))); |
---|
| 333 | |
---|
[5978] | 334 | //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2); |
---|
[5907] | 335 | |
---|
[6637] | 336 | this->shiftCoor(move); |
---|
[5907] | 337 | |
---|
[6634] | 338 | |
---|
[5907] | 339 | } |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | * calculate the velocity |
---|
| 343 | * @param time the timeslice since the last frame |
---|
| 344 | */ |
---|
| 345 | void SpaceShip::calculateVelocity (float time) |
---|
| 346 | { |
---|
| 347 | Vector accel(0.0, 0.0, 0.0); |
---|
[6162] | 348 | /* |
---|
| 349 | Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter |
---|
| 350 | */ |
---|
[5907] | 351 | //float rotVal = 0.0; |
---|
[5912] | 352 | /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */ |
---|
[5907] | 353 | /* calculate the direction in which the craft is heading */ |
---|
| 354 | |
---|
[6637] | 355 | //Plane plane(Vector(0,1,0), Vector(0,0,0)); |
---|
[6222] | 356 | |
---|
[5907] | 357 | if( this->bUp ) |
---|
| 358 | { |
---|
[5978] | 359 | //this->shiftCoor(this->getAbsDirX()); |
---|
[6637] | 360 | //accel += (this->getAbsDirX())*2; |
---|
| 361 | accel += (this->getAbsDirX())*acceleration; |
---|
[6222] | 362 | |
---|
[5907] | 363 | } |
---|
| 364 | |
---|
| 365 | if( this->bDown ) |
---|
[5908] | 366 | { |
---|
[5978] | 367 | //this->shiftCoor((this->getAbsDirX())*-1); |
---|
[6637] | 368 | //accel -= (this->getAbsDirX())*2; |
---|
| 369 | accel -= (this->getAbsDirX())*0.5*acceleration; |
---|
[5907] | 370 | } |
---|
| 371 | |
---|
[6034] | 372 | if( this->bLeft/* > -this->getRelCoor().z*2*/) |
---|
[5907] | 373 | { |
---|
[5912] | 374 | this->shiftDir(Quaternion(time, Vector(0,1,0))); |
---|
| 375 | // accel -= rightDirection; |
---|
[5908] | 376 | //velocityDir.normalize(); |
---|
[5907] | 377 | //rot +=Vector(1,0,0); |
---|
| 378 | //rotVal -= .4; |
---|
| 379 | } |
---|
| 380 | if( this->bRight /* > this->getRelCoor().z*2*/) |
---|
| 381 | { |
---|
[5912] | 382 | this->shiftDir(Quaternion(-time, Vector(0,1,0))); |
---|
| 383 | |
---|
| 384 | // accel += rightDirection; |
---|
[5908] | 385 | //velocityDir.normalize(); |
---|
[5907] | 386 | //rot += Vector(1,0,0); |
---|
| 387 | //rotVal += .4; |
---|
| 388 | } |
---|
[5982] | 389 | |
---|
[6034] | 390 | |
---|
[5978] | 391 | if( this->bRollL /* > -this->getRelCoor().z*2*/) |
---|
| 392 | { |
---|
[6637] | 393 | mouseDir *= Quaternion(-time*2, Vector(1,0,0)); |
---|
[5978] | 394 | // accel -= rightDirection; |
---|
| 395 | //velocityDir.normalize(); |
---|
| 396 | //rot +=Vector(1,0,0); |
---|
| 397 | //rotVal -= .4; |
---|
| 398 | } |
---|
| 399 | if( this->bRollR /* > this->getRelCoor().z*2*/) |
---|
| 400 | { |
---|
[6637] | 401 | mouseDir *= Quaternion(time*2, Vector(1,0,0)); |
---|
[5978] | 402 | |
---|
| 403 | // accel += rightDirection; |
---|
| 404 | //velocityDir.normalize(); |
---|
| 405 | //rot += Vector(1,0,0); |
---|
| 406 | //rotVal += .4; |
---|
| 407 | } |
---|
[5907] | 408 | if (this->bAscend ) |
---|
| 409 | { |
---|
[5912] | 410 | this->shiftDir(Quaternion(time, Vector(0,0,1))); |
---|
| 411 | |
---|
| 412 | // accel += upDirection; |
---|
[5908] | 413 | //velocityDir.normalize(); |
---|
[5907] | 414 | //rot += Vector(0,0,1); |
---|
| 415 | //rotVal += .4; |
---|
| 416 | } |
---|
| 417 | if (this->bDescend ) |
---|
| 418 | { |
---|
[5912] | 419 | this->shiftDir(Quaternion(-time, Vector(0,0,1))); |
---|
| 420 | |
---|
| 421 | // accel -= upDirection; |
---|
[5908] | 422 | //velocityDir.normalize(); |
---|
[5907] | 423 | //rot += Vector(0,0,1); |
---|
| 424 | //rotVal -= .4; |
---|
| 425 | } |
---|
| 426 | |
---|
[5978] | 427 | velocity += accel; |
---|
[5907] | 428 | //rot.normalize(); |
---|
| 429 | //this->setRelDirSoft(Quaternion(rotVal, rot), 5); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | * weapon manipulation by the player |
---|
| 434 | */ |
---|
| 435 | void SpaceShip::weaponAction() |
---|
| 436 | { |
---|
| 437 | if( this->bFire) |
---|
| 438 | { |
---|
| 439 | this->getWeaponManager()->fire(); |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | /** |
---|
| 444 | * @todo switch statement ?? |
---|
| 445 | */ |
---|
| 446 | void SpaceShip::process(const Event &event) |
---|
| 447 | { |
---|
[6637] | 448 | if( event.type == KeyMapper::PEV_LEFT) |
---|
[5978] | 449 | this->bRollL = event.bPressed; |
---|
[6637] | 450 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[5978] | 451 | this->bRollR = event.bPressed; |
---|
[5907] | 452 | else if( event.type == KeyMapper::PEV_FIRE1) |
---|
| 453 | this->bFire = event.bPressed; |
---|
| 454 | else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed) |
---|
[6443] | 455 | { |
---|
[6444] | 456 | this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange; |
---|
[6443] | 457 | } |
---|
[5907] | 458 | else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed) |
---|
[6444] | 459 | this->previousWeaponConfig(); |
---|
[6637] | 460 | else if( event.type == KeyMapper::PEV_UP) |
---|
[5978] | 461 | this->bUp = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6637] | 462 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
[5978] | 463 | this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
| 464 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 465 | { |
---|
[6637] | 466 | this->xMouse += event.xRel; |
---|
| 467 | this->yMouse += event.yRel; |
---|
[5978] | 468 | } |
---|
[5907] | 469 | } |
---|
| 470 | |
---|
| 471 | #include "weapons/aiming_turret.h" |
---|
| 472 | // FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG |
---|
| 473 | void SpaceShip::ADDWEAPON() |
---|
| 474 | { |
---|
| 475 | Weapon* turret = NULL; |
---|
| 476 | |
---|
[6306] | 477 | if ((float)rand()/RAND_MAX < .9) |
---|
[5907] | 478 | { |
---|
| 479 | //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET)) |
---|
| 480 | { |
---|
| 481 | turret = new Turret(); |
---|
[6443] | 482 | this->addWeapon(turret, 2); |
---|
[5907] | 483 | this->getWeaponManager()->changeWeaponConfig(2); |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | else |
---|
| 487 | { |
---|
| 488 | //if (this->getWeaponManager()->hasFreeSlot(3)) |
---|
| 489 | { |
---|
[6426] | 490 | turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET)); |
---|
| 491 | if (turret != NULL) |
---|
[6443] | 492 | this->addWeapon(turret, 3); |
---|
[5907] | 493 | |
---|
| 494 | this->getWeaponManager()->changeWeaponConfig(3); |
---|
| 495 | } |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | if(turret != NULL) |
---|
| 499 | { |
---|
| 500 | turret->setName("Turret"); |
---|
| 501 | turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1); |
---|
| 502 | } |
---|
| 503 | } |
---|
[6424] | 504 | |
---|
[6498] | 505 | #define MASK_bUp 1 |
---|
| 506 | #define MASK_bDown 2 |
---|
| 507 | #define MASK_bLeft 4 |
---|
| 508 | #define MASK_bRight 8 |
---|
| 509 | #define MASK_bAscend 16 |
---|
| 510 | #define MASK_bDescend 32 |
---|
| 511 | #define MASK_bFire 64 |
---|
| 512 | #define MASK_bRollL 128 |
---|
| 513 | #define MASK_bRollR 256 |
---|
[6424] | 514 | |
---|
[6498] | 515 | #define DATA_state 1 |
---|
| 516 | #define DATA_flags 2 |
---|
| 517 | #define DATA_mouse 3 |
---|
| 518 | |
---|
[6424] | 519 | int SpaceShip::writeBytes( const byte * data, int length, int sender ) |
---|
| 520 | { |
---|
| 521 | SYNCHELP_READ_BEGIN(); |
---|
| 522 | |
---|
[6498] | 523 | byte b; |
---|
| 524 | SYNCHELP_READ_BYTE( b ); |
---|
[6424] | 525 | |
---|
[6634] | 526 | if ( b == DATA_state /*&& (this->getHostID()!=this->getOwner() || sender==0)*/ ) |
---|
[6498] | 527 | { |
---|
[6634] | 528 | PRINTF(0)("GOT STATE %d\n", this->getUniqueID()); |
---|
[6498] | 529 | setRequestedSync( false ); |
---|
| 530 | setIsOutOfSync( false ); |
---|
| 531 | SYNCHELP_READ_FKT( WorldEntity::writeState ); |
---|
[6634] | 532 | //SYNCHELP_READ_FLOAT( cycle ); |
---|
| 533 | |
---|
| 534 | return SYNCHELP_READ_N; |
---|
[6498] | 535 | } |
---|
| 536 | |
---|
[6634] | 537 | |
---|
| 538 | if ( b == DATA_flags /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
[6498] | 539 | { |
---|
[6634] | 540 | int flags = 0; |
---|
[6498] | 541 | SYNCHELP_READ_INT( flags ); |
---|
| 542 | |
---|
[6634] | 543 | bUp = (flags & MASK_bUp) != 0; |
---|
| 544 | bDown = (flags & MASK_bDown) != 0; |
---|
| 545 | bLeft = (flags & MASK_bLeft) != 0; |
---|
| 546 | bRight = (flags & MASK_bRight) != 0; |
---|
| 547 | bAscend = (flags & MASK_bAscend) != 0; |
---|
| 548 | bDescend = (flags & MASK_bDescend) != 0; |
---|
| 549 | bFire = (flags & MASK_bFire) != 0; |
---|
| 550 | bRollL = (flags & MASK_bRollL) != 0; |
---|
| 551 | bRollR = (flags & MASK_bRollR) != 0; |
---|
| 552 | |
---|
[6498] | 553 | } |
---|
| 554 | |
---|
[6634] | 555 | /*if ( b == DATA_mouse && this->getHostID()!=this->getOwner() ) |
---|
[6498] | 556 | { |
---|
| 557 | SYNCHELP_READ_FLOAT( xMouse ); |
---|
| 558 | SYNCHELP_READ_FLOAT( yMouse ); |
---|
| 559 | SYNCHELP_READ_FLOAT( mouseSensitivity ); |
---|
| 560 | SYNCHELP_READ_FLOAT( cycle ); |
---|
[6634] | 561 | }*/ |
---|
[6498] | 562 | |
---|
[6634] | 563 | if ( this->getOwner() != this->getHostID() ) |
---|
| 564 | SYNCHELP_READ_FKT( PNode::writeSync ); |
---|
| 565 | |
---|
[6424] | 566 | return SYNCHELP_READ_N; |
---|
| 567 | } |
---|
| 568 | |
---|
[6498] | 569 | |
---|
| 570 | |
---|
[6424] | 571 | int SpaceShip::readBytes( byte * data, int maxLength, int * reciever ) |
---|
| 572 | { |
---|
[6498] | 573 | SYNCHELP_WRITE_BEGIN(); |
---|
| 574 | |
---|
[6634] | 575 | if ( isOutOfSync() && !requestedSync() /*&& this->getHostID()!=this->getOwner()*/ ) |
---|
[6424] | 576 | { |
---|
| 577 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
| 578 | setRequestedSync( true ); |
---|
[6634] | 579 | PRINTF(0)("REQUESTED STATE %d\n", this->getUniqueID()); |
---|
[6424] | 580 | } |
---|
| 581 | |
---|
| 582 | int rec = this->getRequestSync(); |
---|
| 583 | if ( rec > 0 ) |
---|
| 584 | { |
---|
| 585 | *reciever = rec; |
---|
| 586 | |
---|
[6634] | 587 | PRINTF(0)("SEND STATE %d %d\n", this->getUniqueID(), rec); |
---|
| 588 | |
---|
[6498] | 589 | SYNCHELP_WRITE_BYTE( (byte)DATA_state ); |
---|
[6424] | 590 | |
---|
| 591 | SYNCHELP_WRITE_FKT( WorldEntity::readState ); |
---|
[6634] | 592 | //SYNCHELP_WRITE_FLOAT( cycle ); |
---|
[6424] | 593 | |
---|
| 594 | return SYNCHELP_WRITE_N; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | *reciever = 0; |
---|
[6498] | 598 | |
---|
| 599 | if ( this->getHostID()==this->getOwner() ) |
---|
| 600 | { |
---|
| 601 | int mask = 0; |
---|
| 602 | |
---|
| 603 | if ( bUp ) |
---|
| 604 | mask |= MASK_bUp; |
---|
| 605 | if ( bDown ) |
---|
| 606 | mask |= MASK_bDown; |
---|
| 607 | if ( bLeft ) |
---|
| 608 | mask |= MASK_bLeft; |
---|
| 609 | if ( bRight ) |
---|
| 610 | mask |= MASK_bRight; |
---|
| 611 | if ( bAscend ) |
---|
| 612 | mask |= MASK_bAscend; |
---|
| 613 | if ( bFire ) |
---|
| 614 | mask |= MASK_bFire; |
---|
| 615 | if ( bRollL ) |
---|
| 616 | mask |= MASK_bRollL; |
---|
| 617 | if ( bRollR ) |
---|
| 618 | mask |= MASK_bRollR; |
---|
| 619 | |
---|
| 620 | |
---|
[6634] | 621 | //static float oldxMouse = xMouse + 1.0; |
---|
| 622 | //static float oldyMouse = yMouse + 1.0; |
---|
| 623 | |
---|
[6498] | 624 | if ( mask != oldMask ) |
---|
| 625 | { |
---|
| 626 | oldMask = mask; |
---|
| 627 | SYNCHELP_WRITE_BYTE( DATA_flags ); |
---|
| 628 | SYNCHELP_WRITE_INT( mask ); |
---|
| 629 | } |
---|
[6634] | 630 | else |
---|
| 631 | { |
---|
| 632 | SYNCHELP_WRITE_BYTE( 0 ); |
---|
| 633 | } |
---|
[6498] | 634 | |
---|
[6634] | 635 | /*if ( oldxMouse != xMouse || oldyMouse != yMouse ) |
---|
[6498] | 636 | { |
---|
| 637 | oldxMouse = xMouse; |
---|
| 638 | oldyMouse = yMouse; |
---|
| 639 | SYNCHELP_WRITE_BYTE( DATA_mouse ); |
---|
| 640 | SYNCHELP_WRITE_FLOAT( xMouse ); |
---|
| 641 | SYNCHELP_WRITE_FLOAT( yMouse ); |
---|
| 642 | SYNCHELP_WRITE_FLOAT( mouseSensitivity ); |
---|
| 643 | SYNCHELP_WRITE_FLOAT( cycle ); |
---|
[6634] | 644 | }*/ |
---|
[6498] | 645 | } |
---|
[6634] | 646 | else |
---|
| 647 | { |
---|
| 648 | SYNCHELP_WRITE_BYTE( 0 ); |
---|
| 649 | } |
---|
[6498] | 650 | |
---|
[6634] | 651 | if ( this->getOwner() == this->getHostID() ) |
---|
| 652 | SYNCHELP_WRITE_FKT( PNode::readSync ); |
---|
| 653 | |
---|
[6498] | 654 | return SYNCHELP_WRITE_N; |
---|
[6424] | 655 | } |
---|