| 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 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
|---|
| 16 | |
|---|
| 17 | #include "aiming_turret.h" |
|---|
| 18 | |
|---|
| 19 | #include "weapon_manager.h" |
|---|
| 20 | #include "aim.h" |
|---|
| 21 | #include "projectile.h" |
|---|
| 22 | |
|---|
| 23 | #include "model.h" |
|---|
| 24 | |
|---|
| 25 | #include "animation3d.h" |
|---|
| 26 | |
|---|
| 27 | #include "factory.h" |
|---|
| 28 | |
|---|
| 29 | CREATE_FACTORY(AimingTurret, CL_AIMING_TURRET); |
|---|
| 30 | |
|---|
| 31 | using namespace std; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * standard constructor |
|---|
| 36 | |
|---|
| 37 | creates a new weapon |
|---|
| 38 | */ |
|---|
| 39 | AimingTurret::AimingTurret () |
|---|
| 40 | : Weapon() |
|---|
| 41 | { |
|---|
| 42 | this->init(); |
|---|
| 43 | |
|---|
| 44 | this->loadModel("models/guns/turret2.obj"); |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | this->setActionSound(WA_SHOOT, "sound/shot1.wav"); |
|---|
| 48 | this->setActionSound(WA_ACTIVATE, "sound/voices/rockets.wav"); |
|---|
| 49 | this->setActionSound(WA_RELOAD, "sound/vocals/reload.wav"); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | AimingTurret::AimingTurret(const TiXmlElement* root) |
|---|
| 54 | { |
|---|
| 55 | this->init(); |
|---|
| 56 | this->loadParams(root); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * standard deconstructor |
|---|
| 61 | */ |
|---|
| 62 | AimingTurret::~AimingTurret () |
|---|
| 63 | { |
|---|
| 64 | // model will be deleted from WorldEntity-destructor |
|---|
| 65 | // delete this->target; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void AimingTurret::init() |
|---|
| 69 | { |
|---|
| 70 | this->setClassID(CL_AIMING_TURRET, "AimingTurret"); |
|---|
| 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, .1); |
|---|
| 84 | this->setStateDuration(WS_RELOADING, .1); |
|---|
| 85 | this->setStateDuration(WS_ACTIVATING, .4); |
|---|
| 86 | this->setStateDuration(WS_DEACTIVATING, .4); |
|---|
| 87 | |
|---|
| 88 | this->setMaximumEnergy(10000, 50); |
|---|
| 89 | this->increaseEnergy(100000); |
|---|
| 90 | |
|---|
| 91 | this->setCapability(WTYPE_ALLDIRS | WTYPE_TURRET); |
|---|
| 92 | this->setProjectileType(CL_GUIDED_MISSILE); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | this->setEmissionPoint(1.684, 0.472, 0); |
|---|
| 96 | //this->getProjectileFactory()->prepare(100); |
|---|
| 97 | |
|---|
| 98 | this->target = new Aim(this); |
|---|
| 99 | this->target->setVisibility(false); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void AimingTurret::loadParams(const TiXmlElement* root) |
|---|
| 103 | { |
|---|
| 104 | static_cast<Weapon*>(this)->loadParams(root); |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void AimingTurret::activate() |
|---|
| 109 | { |
|---|
| 110 | this->target->setVisibility(true); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void AimingTurret::deactivate() |
|---|
| 114 | { |
|---|
| 115 | this->target->setVisibility(false); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void AimingTurret::tick(float dt) |
|---|
| 119 | { |
|---|
| 120 | Quaternion quat; |
|---|
| 121 | Vector direction = this->target->getAbsCoor() - this->getAbsCoor(); |
|---|
| 122 | |
|---|
| 123 | direction.normalize(); |
|---|
| 124 | |
|---|
| 125 | if (likely (this->getParent() != NULL)) |
|---|
| 126 | quat = Quaternion(direction, this->getParent()->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; |
|---|
| 127 | else |
|---|
| 128 | quat = Quaternion(direction, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; |
|---|
| 129 | |
|---|
| 130 | this->setAbsDirSoft(quat, 5); |
|---|
| 131 | |
|---|
| 132 | this->target->tick(dt); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void AimingTurret::fire() |
|---|
| 136 | { |
|---|
| 137 | Projectile* pj = this->getProjectile(); |
|---|
| 138 | if (pj == NULL) |
|---|
| 139 | return; |
|---|
| 140 | |
|---|
| 141 | pj->setVelocity(/*this->getVelocity()+*/(this->getAbsDir().apply(Vector(1,0,0))*250.0 + VECTOR_RAND(13) |
|---|
| 142 | /*target->getAbsCoor() - this->getAbsCoor()*/)*.5);//this->getVelocity()); |
|---|
| 143 | |
|---|
| 144 | pj->setTarget(this->target->getParent()); |
|---|
| 145 | pj->setParent(PNode::getNullParent()); |
|---|
| 146 | pj->setAbsCoor(this->getEmissionPoint()); |
|---|
| 147 | pj->setAbsDir(this->getAbsDir()); |
|---|
| 148 | pj->activate(); |
|---|
| 149 | this->target->searchTarget(100); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | void AimingTurret::destroy () |
|---|
| 153 | {} |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * draws the AimingTurret |
|---|
| 157 | */ |
|---|
| 158 | void AimingTurret::draw () const |
|---|
| 159 | { |
|---|
| 160 | /* draw gun body */ |
|---|
| 161 | glMatrixMode(GL_MODELVIEW); |
|---|
| 162 | glPushMatrix(); |
|---|
| 163 | glTranslatef (this->getAbsCoor ().x, |
|---|
| 164 | this->getAbsCoor ().y, |
|---|
| 165 | this->getAbsCoor ().z); |
|---|
| 166 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
|---|
| 167 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
|---|
| 168 | |
|---|
| 169 | this->getModel()->draw(); |
|---|
| 170 | glPopMatrix(); |
|---|
| 171 | } |
|---|
| 172 | |
|---|