[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 () |
---|
[6871] | 41 | { |
---|
[6443] | 42 | |
---|
[6871] | 43 | } |
---|
| 44 | |
---|
[6443] | 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; |
---|
[6799] | 114 | |
---|
[6806] | 115 | // camera - issue |
---|
[6800] | 116 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6806] | 117 | this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6724] | 118 | this->cameraNode.setParent(this); |
---|
[6799] | 119 | |
---|
[6724] | 120 | // rotors |
---|
[6803] | 121 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT ); |
---|
| 122 | this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6801] | 123 | this->wingNodeLeft.setParent(this); |
---|
| 124 | this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0); |
---|
[6800] | 125 | this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6801] | 126 | this->rotorNodeLeft.setParent(&this->wingNodeLeft); |
---|
| 127 | this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3); |
---|
[6799] | 128 | |
---|
[6801] | 129 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6803] | 130 | this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE); |
---|
[6800] | 131 | this->wingNodeRight.setParent(this); |
---|
[6801] | 132 | this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0); |
---|
| 133 | this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
[6800] | 134 | this->rotorNodeRight.setParent(&this->wingNodeRight); |
---|
[6801] | 135 | this->rotorNodeRight.setRelCoor(0, 1.0, 2.3); |
---|
[6443] | 136 | |
---|
[6803] | 137 | dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false); |
---|
| 138 | |
---|
[6800] | 139 | |
---|
[6443] | 140 | //add events to the eventlist |
---|
[6637] | 141 | registerEvent(KeyMapper::PEV_UP); |
---|
| 142 | registerEvent(KeyMapper::PEV_DOWN); |
---|
| 143 | registerEvent(KeyMapper::PEV_LEFT); |
---|
| 144 | registerEvent(KeyMapper::PEV_RIGHT); |
---|
| 145 | //registerEvent(SDLK_q); |
---|
[6443] | 146 | registerEvent(SDLK_e); |
---|
| 147 | registerEvent(SDLK_c); |
---|
| 148 | registerEvent(KeyMapper::PEV_FIRE1); |
---|
| 149 | registerEvent(KeyMapper::PEV_NEXT_WEAPON); |
---|
| 150 | registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON); |
---|
[6637] | 151 | //registerEvent(SDLK_PAGEUP); |
---|
| 152 | //registerEvent(SDLK_PAGEDOWN); |
---|
[6443] | 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 | |
---|
[6871] | 194 | State::getCamera()->setParentSoft(&this->cameraNode); |
---|
| 195 | State::getCamera()->setRelCoorSoft(-10, 0,0); |
---|
| 196 | State::getCameraTarget()->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) |
---|
[6807] | 228 | {} |
---|
[6443] | 229 | |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * the function called for each passing timeSnap |
---|
| 234 | * @param time The timespan passed since last update |
---|
| 235 | */ |
---|
[6804] | 236 | void Hover::tick (float dt) |
---|
[6443] | 237 | { |
---|
[6804] | 238 | Playable::tick(dt); |
---|
| 239 | |
---|
[6443] | 240 | // spaceship controlled movement |
---|
[6805] | 241 | this->movement(dt); |
---|
[6806] | 242 | this->rotorCycle += this->rotorSpeed * dt; |
---|
[6443] | 243 | |
---|
[6804] | 244 | Vector move = (velocity)*dt; |
---|
[6443] | 245 | |
---|
| 246 | // this is the air friction (necessary for a smooth control) |
---|
[6807] | 247 | if(velocity.len() != 0) |
---|
| 248 | velocity -= velocity*0.1; |
---|
| 249 | this->shiftCoor (move); |
---|
[6443] | 250 | |
---|
[6807] | 251 | this->rotation = 0.0f; |
---|
[6443] | 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * calculate the velocity |
---|
| 256 | * @param time the timeslice since the last frame |
---|
| 257 | */ |
---|
[6806] | 258 | void Hover::movement (float dt) |
---|
[6443] | 259 | { |
---|
| 260 | Vector accel(0.0, 0.0, 0.0); |
---|
[6805] | 261 | float rotVal = .3; |
---|
[6443] | 262 | |
---|
[6814] | 263 | if( this->bForward ) |
---|
| 264 | { |
---|
[6805] | 265 | accel += Vector(rotVal, 0, 0); |
---|
[6443] | 266 | } |
---|
| 267 | |
---|
[6814] | 268 | if( this->bBackward ) |
---|
| 269 | { |
---|
[6807] | 270 | accel -= Vector(rotVal,0,0); |
---|
[6443] | 271 | } |
---|
[6814] | 272 | if( this->bLeft) |
---|
| 273 | { |
---|
[6805] | 274 | accel -= Vector(0,0,rotVal); |
---|
[6443] | 275 | } |
---|
[6807] | 276 | |
---|
[6814] | 277 | if( this->bRight) |
---|
| 278 | { |
---|
[6805] | 279 | accel += Vector(0,0,rotVal); |
---|
[6443] | 280 | } |
---|
| 281 | |
---|
[6814] | 282 | if (this->bAscend ) |
---|
| 283 | { |
---|
[6805] | 284 | accel += Vector(0,rotVal,0); |
---|
[6443] | 285 | } |
---|
[6814] | 286 | if (this->bDescend ) |
---|
| 287 | { |
---|
| 288 | accel -= Vector(0,rotVal,0); |
---|
[6807] | 289 | } |
---|
[6443] | 290 | |
---|
[6814] | 291 | Vector tmp = this->getAbsDir().apply(accel * 30.0); |
---|
[6824] | 292 | tmp.y = accel.y * 1000.0 * dt; |
---|
[6814] | 293 | velocity += tmp; |
---|
[6443] | 294 | |
---|
[6814] | 295 | |
---|
[6805] | 296 | this->setRelDirSoft(this->direction * Quaternion(-accel.x, Vector(0,0,1)) * Quaternion(accel.z, Vector(1,0,0)), 5); |
---|
| 297 | |
---|
[6807] | 298 | this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z+this->rotation, Vector(1,0,0)), 10); |
---|
| 299 | this->rotorNodeLeft.setRelDirSoft(Quaternion(-2.0*accel.x+this->rotation, Vector(0,0,1)), 10); |
---|
[6805] | 300 | |
---|
[6807] | 301 | this->wingNodeRight.setRelDirSoft(Quaternion(accel.z+this->rotation, Vector(1,0,0)), 10); |
---|
| 302 | this->rotorNodeRight.setRelDirSoft(Quaternion(-2.0*accel.x-this->rotation, Vector(0,0,1)), 10); |
---|
[6443] | 303 | } |
---|
| 304 | |
---|
| 305 | |
---|
[6799] | 306 | void Hover::draw() const |
---|
[6443] | 307 | { |
---|
[6801] | 308 | Vector tmpRot; |
---|
[6443] | 309 | WorldEntity::draw(); |
---|
[6799] | 310 | |
---|
[6801] | 311 | glPushMatrix(); |
---|
| 312 | /// LEFT SIDE |
---|
| 313 | glTranslatef (this->wingNodeLeft.getAbsCoor ().x, |
---|
| 314 | this->wingNodeLeft.getAbsCoor ().y, |
---|
| 315 | this->wingNodeLeft.getAbsCoor ().z); |
---|
| 316 | tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis(); |
---|
| 317 | glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 318 | this->getModel(3)->draw(); |
---|
| 319 | glPopMatrix (); |
---|
[6443] | 320 | |
---|
[6801] | 321 | glPushMatrix(); |
---|
| 322 | glTranslatef (this->rotorNodeLeft.getAbsCoor ().x, |
---|
| 323 | this->rotorNodeLeft.getAbsCoor ().y, |
---|
| 324 | this->rotorNodeLeft.getAbsCoor ().z); |
---|
| 325 | tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis(); |
---|
| 326 | glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 327 | this->getModel(4)->draw(); |
---|
[6806] | 328 | glTranslatef(0,-1,0); |
---|
| 329 | glRotatef(this->rotorCycle, 0,1,0); |
---|
| 330 | this->getModel(5)->draw(); |
---|
[6801] | 331 | glPopMatrix (); |
---|
[6724] | 332 | |
---|
[6801] | 333 | /// RIGHT SIDE |
---|
| 334 | glPushMatrix(); |
---|
| 335 | glTranslatef (this->wingNodeRight.getAbsCoor ().x, |
---|
| 336 | this->wingNodeRight.getAbsCoor ().y, |
---|
| 337 | this->wingNodeRight.getAbsCoor ().z); |
---|
| 338 | tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis(); |
---|
| 339 | glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 340 | glScalef(1,1,-1); |
---|
| 341 | this->getModel(3)->draw(); |
---|
| 342 | glPopMatrix (); |
---|
[6724] | 343 | |
---|
[6801] | 344 | glPushMatrix(); |
---|
| 345 | glTranslatef (this->rotorNodeRight.getAbsCoor ().x, |
---|
| 346 | this->rotorNodeRight.getAbsCoor ().y, |
---|
| 347 | this->rotorNodeRight.getAbsCoor ().z); |
---|
| 348 | tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis(); |
---|
| 349 | glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
| 350 | glScalef(1,1,-1); |
---|
| 351 | this->getModel(4)->draw(); |
---|
[6806] | 352 | glTranslatef(0,-1,0); |
---|
| 353 | glRotatef(this->rotorCycle, 0,1,0); |
---|
| 354 | this->getModel(5)->draw(); |
---|
[6801] | 355 | glPopMatrix (); |
---|
[6443] | 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | * @todo switch statement ?? |
---|
| 360 | */ |
---|
[6799] | 361 | void Hover::process(const Event &event) |
---|
[6443] | 362 | { |
---|
[6804] | 363 | Playable::process(event); |
---|
[6443] | 364 | |
---|
[6637] | 365 | if( event.type == KeyMapper::PEV_LEFT) |
---|
[6805] | 366 | this->bLeft = event.bPressed; |
---|
[6637] | 367 | else if( event.type == KeyMapper::PEV_RIGHT) |
---|
[6805] | 368 | this->bRight = event.bPressed; |
---|
[6443] | 369 | else if( event.type == SDLK_e) |
---|
| 370 | this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
| 371 | else if( event.type == SDLK_c) |
---|
| 372 | this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6637] | 373 | else if( event.type == KeyMapper::PEV_UP) |
---|
[6805] | 374 | this->bForward = event.bPressed; //this->shiftCoor(0,.1,0); |
---|
[6637] | 375 | else if( event.type == KeyMapper::PEV_DOWN) |
---|
[6805] | 376 | this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0); |
---|
[6443] | 377 | else if( event.type == EV_MOUSE_MOTION) |
---|
| 378 | { |
---|
[6807] | 379 | float xMouse, yMouse; |
---|
| 380 | xMouse = event.xRel*mouseSensitivity; |
---|
| 381 | yMouse = event.yRel*mouseSensitivity; |
---|
[6443] | 382 | |
---|
[6805] | 383 | // rotate the Player around the y-axis |
---|
[6807] | 384 | this->direction *= Quaternion(-M_PI/4.0*xMouse, Vector(0,1,0)); |
---|
| 385 | this->rotation += xMouse; |
---|
[6799] | 386 | |
---|
[6807] | 387 | this->cameraLook += yMouse; |
---|
[6805] | 388 | // rotate the Camera around the z-axis |
---|
[6807] | 389 | if (cameraLook > M_PI_4) |
---|
| 390 | cameraLook = M_PI_4; |
---|
| 391 | else if (cameraLook < -M_PI_2) |
---|
| 392 | cameraLook = -M_PI_2; |
---|
| 393 | this->cameraNode.setRelDirSoft(Quaternion(-cameraLook, Vector(0,0,1)),5); |
---|
[6805] | 394 | } |
---|
[6443] | 395 | } |
---|