| 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 | #include "turret.h" | 
|---|
| 18 |  | 
|---|
| 19 | #include "weapon_manager.h" | 
|---|
| 20 | #include "test_bullet.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "state.h" | 
|---|
| 23 | #include "vector.h" | 
|---|
| 24 | #include "list.h" | 
|---|
| 25 | #include "animation3d.h" | 
|---|
| 26 | #include "sound_engine.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "fast_factory.h" | 
|---|
| 29 |  | 
|---|
| 30 | CREATE_FACTORY(Turret); | 
|---|
| 31 |  | 
|---|
| 32 | using namespace std; | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 |  *  standard constructor | 
|---|
| 37 |  | 
|---|
| 38 |    creates a new weapon | 
|---|
| 39 | */ | 
|---|
| 40 | Turret::Turret (WeaponManager* weaponManager) | 
|---|
| 41 |   : Weapon(weaponManager) | 
|---|
| 42 | { | 
|---|
| 43 |   this->init(); | 
|---|
| 44 |  | 
|---|
| 45 |   this->loadModel("models/turret1.obj"); | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 |   this->setActionSound(WA_SHOOT, "sound/shot1.wav"); | 
|---|
| 49 |   this->setActionSound(WA_ACTIVATE, "sound/vocals/missiles.wav"); | 
|---|
| 50 |   this->setActionSound(WA_RELOAD, "sound/vocals/reload.wav"); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 | Turret::Turret(const TiXmlElement* root) | 
|---|
| 55 | { | 
|---|
| 56 |   this->init(); | 
|---|
| 57 |   this->loadParams(root); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 61 |  *  standard deconstructor | 
|---|
| 62 | */ | 
|---|
| 63 | Turret::~Turret () | 
|---|
| 64 | { | 
|---|
| 65 |   // model will be deleted from WorldEntity-destructor | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void Turret::init() | 
|---|
| 69 | { | 
|---|
| 70 |   this->setClassID(CL_TURRET, "Turret"); | 
|---|
| 71 |  | 
|---|
| 72 |   Animation3D* animation1 = this->getAnimation(WS_ACTIVATING, this); | 
|---|
| 73 |   Animation3D* animation2 = this->getAnimation(WS_DEACTIVATING, this); | 
|---|
| 74 |  | 
|---|
| 75 |   animation1->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); | 
|---|
| 76 |   animation1->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); | 
|---|
| 77 |   animation2->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); | 
|---|
| 78 |   animation2->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); | 
|---|
| 79 |  | 
|---|
| 80 |   animation1->setInfinity(ANIM_INF_CONSTANT); | 
|---|
| 81 |   animation2->setInfinity(ANIM_INF_CONSTANT); | 
|---|
| 82 |  | 
|---|
| 83 |   this->setStateDuration(WS_SHOOTING, .4); | 
|---|
| 84 |   this->setStateDuration(WS_RELOADING, 2); | 
|---|
| 85 |   this->setStateDuration(WS_ACTIVATING, .4); | 
|---|
| 86 |   this->setStateDuration(WS_DEACTIVATING, .4); | 
|---|
| 87 |  | 
|---|
| 88 |   this->setMaximumEnergy(1000, 10); | 
|---|
| 89 |   this->increaseEnergy(100); | 
|---|
| 90 |   //this->minCharge = 2; | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 |   this->setProjectile(CL_TEST_BULLET); | 
|---|
| 94 |  | 
|---|
| 95 |  | 
|---|
| 96 |   this->setEmissionPoint(1.684, 0.472, 0); | 
|---|
| 97 |   //this->getProjectileFactory()->prepare(100); | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void Turret::loadParams(const TiXmlElement* root) | 
|---|
| 103 | { | 
|---|
| 104 |   static_cast<Weapon*>(this)->loadParams(root); | 
|---|
| 105 |  | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void Turret::activate() | 
|---|
| 109 | { | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void Turret::deactivate() | 
|---|
| 113 | { | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void Turret::tick(float dt) | 
|---|
| 117 | { | 
|---|
| 118 |   Vector direction = this->getWeaponManager()->getFixedTarget()->getAbsCoor() - this->getAbsCoor(); | 
|---|
| 119 |   direction.normalize(); | 
|---|
| 120 |   if (likely (this->getParent() != NULL)) | 
|---|
| 121 |   { | 
|---|
| 122 |     Quaternion quat = Quaternion(direction, this->getParent()->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; | 
|---|
| 123 |     this->setAbsDir(quat); | 
|---|
| 124 |   } | 
|---|
| 125 |   else | 
|---|
| 126 |   { | 
|---|
| 127 |     Quaternion quat = Quaternion(direction, this->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; | 
|---|
| 128 |     this->setAbsDir(quat); | 
|---|
| 129 |   } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void Turret::fire() | 
|---|
| 133 | { | 
|---|
| 134 |   Projectile* pj =  dynamic_cast<Projectile*>(this->getProjectileFactory()->resurrect()); | 
|---|
| 135 |  | 
|---|
| 136 |   PNode* target = this->getWeaponManager()->getFixedTarget(); | 
|---|
| 137 |  | 
|---|
| 138 |   if (target != NULL) | 
|---|
| 139 |   { | 
|---|
| 140 |     pj->setVelocity(this->getVelocity()+(target->getAbsCoor() - this->getAbsCoor())*.5);//this->getVelocity()); | 
|---|
| 141 |     pj->setAbsDir(this->getAbsDir()); | 
|---|
| 142 |   } | 
|---|
| 143 |   else | 
|---|
| 144 |     pj->setVelocity(target->getVelocity()); | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 |   pj->setAbsCoor(this->getEmissionPoint()); | 
|---|
| 148 |   pj->setAbsDir(this->getAbsDir()); | 
|---|
| 149 |   State::getWorldEntityList()->add(pj); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 |  | 
|---|
| 153 | void Turret::destroy () | 
|---|
| 154 | {} | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 |  * draws the Turret | 
|---|
| 158 | */ | 
|---|
| 159 | void Turret::draw () | 
|---|
| 160 | { | 
|---|
| 161 |   this->getWeaponManager()->getFixedTarget()->debugDraw(10); | 
|---|
| 162 |  | 
|---|
| 163 |   /* draw gun body */ | 
|---|
| 164 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 165 |   glPushMatrix(); | 
|---|
| 166 |   float matrix[4][4]; | 
|---|
| 167 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 168 |                 this->getAbsCoor ().y, | 
|---|
| 169 |                 this->getAbsCoor ().z); | 
|---|
| 170 |   this->getAbsDir ().matrix (matrix); | 
|---|
| 171 |   glMultMatrixf((float*)matrix); | 
|---|
| 172 |  | 
|---|
| 173 |   this->model->draw(); | 
|---|
| 174 |   glPopMatrix(); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|