[6443] | 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 Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 18 | |
---|
[6799] | 19 | #include "hover.h" |
---|
[6443] | 20 | |
---|
| 21 | #include "weapons/weapon_manager.h" |
---|
| 22 | #include "weapons/test_gun.h" |
---|
| 23 | #include "weapons/turret.h" |
---|
| 24 | #include "weapons/cannon.h" |
---|
| 25 | |
---|
| 26 | #include "factory.h" |
---|
| 27 | #include "key_mapper.h" |
---|
| 28 | #include "event_handler.h" |
---|
| 29 | #include "state.h" |
---|
| 30 | |
---|
| 31 | #include "graphics_engine.h" |
---|
| 32 | |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
[6799] | 35 | CREATE_FACTORY(Hover, CL_HOVER); |
---|
[6443] | 36 | |
---|
| 37 | /** |
---|
[6799] | 38 | * destructs the hover, deletes alocated memory |
---|
[6443] | 39 | */ |
---|
[6799] | 40 | Hover::~Hover () |
---|
[6986] | 41 | { |
---|
| 42 | this->setPlayer(NULL); |
---|
| 43 | } |
---|
[6443] | 44 | |
---|
| 45 | /** |
---|
[6799] | 46 | * loads a Hover information from a specified file. |
---|
| 47 | * @param fileName the name of the File to load the hover from (absolute path) |
---|
[6443] | 48 | */ |
---|
[6799] | 49 | Hover::Hover(const char* fileName) |
---|
[6443] | 50 | { |
---|
| 51 | this->init(); |
---|
| 52 | TiXmlDocument doc(fileName); |
---|
| 53 | |
---|
| 54 | if(!doc.LoadFile()) |
---|
| 55 | { |
---|
[6799] | 56 | PRINTF(2)("Loading file %s failed for Hover.\n", fileName); |
---|
[6443] | 57 | return; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | this->loadParams(doc.RootElement()); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * creates a new Spaceship from Xml Data |
---|
| 65 | * @param root the xml element containing spaceship data |
---|
| 66 | |
---|
| 67 | @todo add more parameters to load |
---|
| 68 | */ |
---|
[6799] | 69 | Hover::Hover(const TiXmlElement* root) |
---|
[6443] | 70 | { |
---|
| 71 | this->init(); |
---|
| 72 | if (root != NULL) |
---|
| 73 | this->loadParams(root); |
---|
| 74 | |
---|
| 75 | //weapons: |
---|
| 76 | Weapon* wpRight = new TestGun(0); |
---|
| 77 | wpRight->setName("testGun Right"); |
---|
| 78 | Weapon* wpLeft = new TestGun(1); |
---|
| 79 | wpLeft->setName("testGun Left"); |
---|
[6811] | 80 | Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER)); |
---|
[6443] | 81 | |
---|
| 82 | cannon->setName("BFG"); |
---|
| 83 | |
---|
| 84 | this->addWeapon(wpLeft, 1, 0); |
---|
| 85 | this->addWeapon(wpRight,1 ,1); |
---|
[6803] | 86 | this->addWeapon(cannon, 0, 2); |
---|
[6443] | 87 | |
---|
| 88 | this->getWeaponManager()->changeWeaponConfig(1); |
---|
| 89 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
[6800] | 90 | |
---|
| 91 | this->loadModel("models/ships/hoverglider_mainbody.obj"); |
---|
[6443] | 92 | } |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | /** |
---|
[6799] | 96 | * initializes a Hover |
---|
[6443] | 97 | */ |
---|
[6799] | 98 | void Hover::init() |
---|
[6443] | 99 | { |
---|
[6805] | 100 | // this->setRelDir(Quaternion(M_PI, Vector(1,0,0))); |
---|
[6799] | 101 | this->setClassID(CL_HOVER, "Hover"); |
---|
[6443] | 102 | |
---|
[6807] | 103 | this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3); |
---|
| 104 | this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4); |
---|
| 105 | this->loadModel("models/ships/rotor.obj", .45f, 5); |
---|
[6443] | 106 | |
---|
[6805] | 107 | bForward = bBackward = bLeft = bRight = bAscend = bDescend = false; |
---|
[6807] | 108 | mouseSensitivity = 0.005; |
---|
[6443] | 109 | |
---|
[6806] | 110 | this->rotorSpeed = 1000.0f; |
---|
| 111 | this->rotorCycle = 0.0f; |
---|
[6807] | 112 | this->cameraLook = 0.0f; |
---|
| 113 | this->rotation = 0.0f; |
---|
[6999] | 114 | this->acceleration = 25.0f; |
---|
| 115 | this->airFriction = 3.0f; |
---|
[6799] | 116 | |
---|
[6806] | 117 | // camera - issue |
---|
[6800] | 118 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6806] | 119 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6880] | 120 | //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT); |
---|
[6724] | 121 | this->cameraNode.setParent(this); |
---|
[6799] | 122 | |
---|
[6724] | 123 | // rotors |
---|
[6803] | 124 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT ); |
---|
| 125 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6801] | 126 | this->wingNodeLeft.setParent(this); |
---|
| 127 | this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0); |
---|
[6800] | 128 | this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6801] | 129 | this->rotorNodeLeft.setParent(&this->wingNodeLeft); |
---|
| 130 | this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3); |
---|
[6799] | 131 | |
---|
[6801] | 132 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6803] | 133 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6800] | 134 | this->wingNodeRight.setParent(this); |
---|
[6801] | 135 | this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0); |
---|
| 136 | this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6800] | 137 | this->rotorNodeRight.setParent(&this->wingNodeRight); |
---|
[6801] | 138 | this->rotorNodeRight.setRelCoor(0, 1.0, 2.3); |
---|
[6443] | 139 | |
---|
[6803] | 140 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 141 | |
---|
[6800] | 142 | |
---|
[6443] | 143 | //add events to the eventlist |
---|
[6997] | 144 | registerEvent(KeyMapper::PEV_FORWARD); |
---|
| 145 | registerEvent(KeyMapper::PEV_BACKWARD); |
---|
[6637] | 146 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 147 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
[6998] | 148 | registerEvent(KeyMapper::PEV_UP); |
---|
| 149 | registerEvent(KeyMapper::PEV_DOWN); |
---|
[6443] | 150 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 151 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 152 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
| 153 | registerEvent(EV_MOUSE_MOTION); |
---|
| 154 | |
---|
| 155 | |
---|
[6803] | 156 | // WEAPON_MANAGER configuration |
---|
| 157 | this->getWeaponManager()->setSlotCount(5); |
---|
| 158 | |
---|
| 159 | this->getWeaponManager()->setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft); |
---|
[6443] | 160 | this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 161 | |
---|
[6803] | 162 | this->getWeaponManager()->setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight); |
---|
[6443] | 163 | this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL); |
---|
| 164 | |
---|
[6803] | 165 | this->getWeaponManager()->setSlotPosition(2, Vector(-1.63, .809, -.003)); |
---|
| 166 | this->getWeaponManager()->setSlotCapability(2, WTYPE_HEAVY); |
---|
[6443] | 167 | |
---|
[6803] | 168 | /// TODO: THESE ARE TOO MUCH |
---|
| 169 | this->getWeaponManager()->setSlotPosition(3, Vector(-1.63, .678, -.652)); |
---|
| 170 | this->getWeaponManager()->setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0))); |
---|
[6443] | 171 | |
---|
[6803] | 172 | this->getWeaponManager()->setSlotPosition(4, Vector(-1.63, .678, .652)); |
---|
| 173 | this->getWeaponManager()->setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0))); |
---|
[6443] | 174 | |
---|
[6807] | 175 | this->cameraNode.setRelCoor(1,5,0); |
---|
| 176 | this->getWeaponManager()->getFixedTarget()->setParent(&this->cameraNode); |
---|
| 177 | this->getWeaponManager()->getFixedTarget()->setRelCoor(1000,0,0); |
---|
[6443] | 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
[6799] | 181 | * loads the Settings of a Hover from an XML-element. |
---|
[6443] | 182 | * @param root the XML-element to load the Spaceship's properties from |
---|
| 183 | */ |
---|
[6799] | 184 | void Hover::loadParams(const TiXmlElement* root) |
---|
[6443] | 185 | { |
---|
[6512] | 186 | WorldEntity::loadParams(root); |
---|
[6443] | 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
[6799] | 190 | void Hover::enter() |
---|
[6443] | 191 | { |
---|
| 192 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true); |
---|
| 193 | |
---|
[7014] | 194 | State::getCameraNode()->setParentSoft(&this->cameraNode); |
---|
| 195 | State::getCameraNode()->setRelCoorSoft(-10, 0,0); |
---|
| 196 | State::getCameraTargetNode()->setParentSoft(&this->cameraNode); |
---|
[6443] | 197 | } |
---|
| 198 | |
---|
[6799] | 199 | void Hover::leave() |
---|
[6443] | 200 | { |
---|
| 201 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 202 | this->detachCamera(); |
---|
| 203 | |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | /** |
---|
[6799] | 208 | * effect that occurs after the Hover is spawned |
---|
[6443] | 209 | */ |
---|
[6799] | 210 | void Hover::postSpawn () |
---|
[6443] | 211 | { |
---|
| 212 | //setCollision(new CollisionCluster(1.0, Vector(0,0,0))); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
[6799] | 216 | * the action occuring if the hover left the game |
---|
[6443] | 217 | */ |
---|
[6799] | 218 | void Hover::leftWorld () |
---|
[6443] | 219 | {} |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * this function is called, when two entities collide |
---|
| 223 | * @param entity: the world entity with whom it collides |
---|
| 224 | * |
---|
| 225 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
| 226 | */ |
---|
[6799] | 227 | void Hover::collidesWith(WorldEntity* entity, const Vector& location) |
---|
[7072] | 228 | { |
---|
| 229 | Playable::collidesWith(entity, location); |
---|
| 230 | } |
---|
[6443] | 231 | |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | /** |
---|
| 235 | * the function called for each passing timeSnap |
---|
| 236 | * @param time The timespan passed since last update |
---|
| 237 | */ |
---|
[6804] | 238 | void Hover::tick (float dt) |
---|
[6443] | 239 | { |
---|
[6804] | 240 | Playable::tick(dt); |
---|
| 241 | |
---|
[6443] | 242 | // spaceship controlled movement |
---|
[6805] | 243 | this->movement(dt); |
---|
[6806] | 244 | this->rotorCycle += this->rotorSpeed * dt; |
---|
[6443] | 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * calculate the velocity |
---|
| 249 | * @param time the timeslice since the last frame |
---|
| 250 | */ |
---|
[6806] | 251 | void Hover::movement (float dt) |
---|
[6443] | 252 | { |
---|
| 253 | Vector accel(0.0, 0.0, 0.0); |
---|
[6879] | 254 | float rotSpeed = .3; |
---|
[6443] | 255 | |
---|
[6814] | 256 | if( this->bForward ) |
---|
| 257 | { |
---|
[6879] | 258 | accel += Vector(this->acceleration, 0, 0); |
---|
[6443] | 259 | } |
---|
| 260 | |
---|
[6814] | 261 | if( this->bBackward ) |
---|
| 262 | { |
---|
[6879] | 263 | accel -= Vector(this->acceleration, 0, 0); |
---|
[6443] | 264 | } |
---|
[6814] | 265 | if( this->bLeft) |
---|
| 266 | { |
---|
[6879] | 267 | accel -= Vector(0, 0, this->acceleration); |
---|
[6443] | 268 | } |
---|
[6807] | 269 | |
---|
[6814] | 270 | if( this->bRight) |
---|
| 271 | { |
---|
[6879] | 272 | accel += Vector(0, 0, this->acceleration); |
---|
[6443] | 273 | } |
---|
| 274 | |
---|
[6814] | 275 | if (this->bAscend ) |
---|
| 276 | { |
---|
[6879] | 277 | accel += Vector(0, this->acceleration, 0); |
---|
[6443] | 278 | } |
---|
[6814] | 279 | if (this->bDescend ) |
---|
| 280 | { |
---|
[6879] | 281 | accel -= Vector(0, this->acceleration, 0); |
---|
[6807] | 282 | } |
---|
[6443] | 283 | |
---|
[6999] | 284 | Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration); |
---|
[6814] | 285 | |
---|
[6879] | 286 | // this is the air friction (necessary for a smooth control) |
---|
[6999] | 287 | Vector damping = (this->velocity * this->airFriction); |
---|
| 288 | |
---|
| 289 | |
---|
| 290 | this->velocity += (accelerationDir - damping)* dt; |
---|
| 291 | |
---|
| 292 | this->shiftCoor (this->velocity * dt); |
---|
[6879] | 293 | this->rotation = 0.0f; |
---|
| 294 | |
---|
[6880] | 295 | this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5); |
---|
[6805] | 296 | |
---|
[6999] | 297 | this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .03 +this->rotation, Vector(1,0,0)), 5); |
---|
| 298 | this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .05+this->rotation + cameraLook, Vector(0,0,1)), 5); |
---|
[6805] | 299 | |
---|
[6999] | 300 | this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .03 +this->rotation, Vector(1,0,0)), 5); |
---|
| 301 | this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.05 -this->rotation + cameraLook, Vector(0,0,1)), 5); |
---|
[6443] | 302 | } |
---|
| 303 | |
---|
| 304 | |
---|
[6799] | 305 | void Hover::draw() const |
---|
[6443] | 306 | { |
---|
[6801] | 307 | Vector tmpRot; |
---|
[6443] | 308 | WorldEntity::draw(); |
---|
[6799] | 309 | |
---|
[6801] | 310 | glPushMatrix(); |
---|
| 311 | /// LEFT SIDE |
---|
| 312 | glTranslatef (this->wingNodeLeft.getAbsCoor ().x, |
---|
| 313 | this->wingNodeLeft.getAbsCoor ().y, |
---|
| 314 | this->wingNodeLeft.getAbsCoor ().z); |
---|
| 315 | tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis(); |
---|
| 316 | glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 317 | this->getModel(3)->draw(); |
---|
| 318 | glPopMatrix (); |
---|
[6443] | 319 | |
---|
[6801] | 320 | glPushMatrix(); |
---|
| 321 | glTranslatef (this->rotorNodeLeft.getAbsCoor ().x, |
---|
| 322 | this->rotorNodeLeft.getAbsCoor ().y, |
---|
| 323 | this->rotorNodeLeft.getAbsCoor ().z); |
---|
| 324 | tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis(); |
---|
| 325 | glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 326 | this->getModel(4)->draw(); |
---|
[6806] | 327 | glTranslatef(0,-1,0); |
---|
| 328 | glRotatef(this->rotorCycle, 0,1,0); |
---|
| 329 | this->getModel(5)->draw(); |
---|
[6801] | 330 | glPopMatrix (); |
---|
[6724] | 331 | |
---|
[6801] | 332 | /// RIGHT SIDE |
---|
| 333 | glPushMatrix(); |
---|
| 334 | glTranslatef (this->wingNodeRight.getAbsCoor ().x, |
---|
| 335 | this->wingNodeRight.getAbsCoor ().y, |
---|
| 336 | this->wingNodeRight.getAbsCoor ().z); |
---|
| 337 | tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis(); |
---|
| 338 | glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 339 | glScalef(1,1,-1); |
---|
| 340 | this->getModel(3)->draw(); |
---|
| 341 | glPopMatrix (); |
---|
[6724] | 342 | |
---|
[6801] | 343 | glPushMatrix(); |
---|
| 344 | glTranslatef (this->rotorNodeRight.getAbsCoor ().x, |
---|
| 345 | this->rotorNodeRight.getAbsCoor ().y, |
---|
| 346 | this->rotorNodeRight.getAbsCoor ().z); |
---|
| 347 | tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis(); |
---|
| 348 | glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 349 | glScalef(1,1,-1); |
---|
| 350 | this->getModel(4)->draw(); |
---|
[6806] | 351 | glTranslatef(0,-1,0); |
---|
| 352 | glRotatef(this->rotorCycle, 0,1,0); |
---|
| 353 | this->getModel(5)->draw(); |
---|
[6801] | 354 | glPopMatrix (); |
---|
[6443] | 355 | } |
---|
| 356 | |
---|
| 357 | /** |
---|
| 358 | * @todo switch statement ?? |
---|
| 359 | */ |
---|
[6799] | 360 | void Hover::process(const Event &event) |
---|
[6443] | 361 | { |
---|
[6804] | 362 | Playable::process(event); |
---|
[6443] | 363 | |
---|
[6637] | 364 | if( event.type == KeyMapper::PEV_LEFT) |
---|
[6805] | 365 | this->bLeft = event.bPressed; |
---|
[6637] | 366 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[6805] | 367 | this->bRight = event.bPressed; |
---|
[6998] | 368 | else if( event.type == KeyMapper::PEV_UP) |
---|
[6443] | 369 | this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6998] | 370 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
[6443] | 371 | this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6997] | 372 | else if( event.type == KeyMapper::PEV_FORWARD) |
---|
[6805] | 373 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6997] | 374 | else if( event.type == KeyMapper::PEV_BACKWARD) |
---|
[6805] | 375 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6443] | 376 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 377 | { |
---|
[6807] | 378 | float xMouse, yMouse; |
---|
| 379 | xMouse = event.xRel*mouseSensitivity; |
---|
| 380 | yMouse = event.yRel*mouseSensitivity; |
---|
[6443] | 381 | |
---|
[6805] | 382 | // rotate the Player around the y-axis |
---|
[6807] | 383 | this->direction *= Quaternion(-M_PI/4.0*xMouse, Vector(0,1,0)); |
---|
| 384 | this->rotation += xMouse; |
---|
[6799] | 385 | |
---|
[6807] | 386 | this->cameraLook += yMouse; |
---|
[6805] | 387 | // rotate the Camera around the z-axis |
---|
[6807] | 388 | if (cameraLook > M_PI_4) |
---|
| 389 | cameraLook = M_PI_4; |
---|
[6880] | 390 | else if (cameraLook < -M_PI_4) |
---|
| 391 | cameraLook = -M_PI_4; |
---|
| 392 | //this->cameraNode.setRelDirSoft(this->direction,10); |
---|
[6805] | 393 | } |
---|
[6443] | 394 | } |
---|