Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/world_entities/src/world_entities/weapons/aiming_turret.cc @ 5567

Last change on this file since 5567 was 5567, checked in by bensch, 18 years ago

orxonox/branches/world_entities: smoother aiming, and also displaying Distance (this is not too nice, but quite good to explain a few Things …

File size: 4.4 KB
Line 
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 "null_parent.h"
26#include "state.h"
27#include "list.h"
28#include "animation3d.h"
29#include "sound_engine.h"
30
31#include "factory.h"
32
33CREATE_FACTORY(AimingTurret);
34
35using namespace std;
36
37
38/**
39 *  standard constructor
40
41   creates a new weapon
42*/
43AimingTurret::AimingTurret (WeaponManager* weaponManager)
44  : Weapon(weaponManager)
45{
46  this->init();
47
48  this->loadModel("models/guns/turret2.obj");
49
50
51  this->setActionSound(WA_SHOOT, "sound/shot1.wav");
52  this->setActionSound(WA_ACTIVATE, "sound/vocals/missiles.wav");
53  this->setActionSound(WA_RELOAD, "sound/vocals/reload.wav");
54}
55
56
57AimingTurret::AimingTurret(const TiXmlElement* root)
58{
59  this->init();
60  this->loadParams(root);
61}
62
63/**
64 *  standard deconstructor
65*/
66AimingTurret::~AimingTurret ()
67{
68  // model will be deleted from WorldEntity-destructor
69  delete this->target;
70}
71
72void AimingTurret::init()
73{
74  this->setClassID(CL_AIMING_TURRET, "AimingTurret");
75
76  Animation3D* animation1 = this->getAnimation(WS_ACTIVATING, this);
77  Animation3D* animation2 = this->getAnimation(WS_DEACTIVATING, this);
78
79  animation1->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
80  animation1->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
81  animation2->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
82  animation2->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
83
84  animation1->setInfinity(ANIM_INF_CONSTANT);
85  animation2->setInfinity(ANIM_INF_CONSTANT);
86
87  this->setStateDuration(WS_SHOOTING, .1);
88  this->setStateDuration(WS_RELOADING, .1);
89  this->setStateDuration(WS_ACTIVATING, .4);
90  this->setStateDuration(WS_DEACTIVATING, .4);
91
92  this->setMaximumEnergy(10000, 50);
93  this->increaseEnergy(100000);
94  //this->minCharge = 2;
95
96  this->setCapability(WTYPE_ALLDIRS | WTYPE_TURRET);
97  this->setProjectileType(CL_ROCKET);
98
99
100  this->setEmissionPoint(1.684, 0.472, 0);
101  //this->getProjectileFactory()->prepare(100);
102
103  this->target = new Aim(this);
104  this->target->setVisibility(false);
105}
106
107void AimingTurret::loadParams(const TiXmlElement* root)
108{
109  static_cast<Weapon*>(this)->loadParams(root);
110
111}
112
113void AimingTurret::activate()
114{
115  this->target->setVisibility(true);
116}
117
118void AimingTurret::deactivate()
119{
120  this->target->setVisibility(false);
121}
122
123void AimingTurret::tick(float dt)
124{
125  Quaternion quat;
126  Vector direction = this->target->getAbsCoor() - this->getAbsCoor();
127
128  direction.normalize();
129
130  if (likely (this->getParent() != NULL))
131    quat = Quaternion(direction, this->getParent()->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ;
132  else
133    quat = Quaternion(direction, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)) ;
134
135  this->setAbsDirSoft(quat, 5);
136
137  this->target->tick(dt);
138}
139
140void AimingTurret::fire()
141{
142  Projectile* pj = this->getProjectile();
143  if (pj == NULL)
144    return;
145
146  PNode* target = this->getWeaponManager()->getFixedTarget();
147
148  if (target != NULL)
149  {
150    pj->setVelocity(/*this->getVelocity()+*/(this->getAbsDir().apply(Vector(1,0,0))*250.0 + VECTOR_RAND(13)
151            /*target->getAbsCoor() - this->getAbsCoor()*/)*.5);//this->getVelocity());
152  }
153  else
154    pj->setVelocity(target->getVelocity());
155
156  pj->setParent(NullParent::getInstance());
157  pj->setAbsCoor(this->getEmissionPoint());
158  pj->setAbsDir(this->getAbsDir());
159  pj->activate();
160  this->target->searchTarget(this);
161}
162
163void AimingTurret::destroy ()
164{}
165
166/**
167 * draws the AimingTurret
168*/
169void AimingTurret::draw () const
170{
171  this->getWeaponManager()->getFixedTarget()->debugDraw(10);
172
173  /* draw gun body */
174  glMatrixMode(GL_MODELVIEW);
175  glPushMatrix();
176  glTranslatef (this->getAbsCoor ().x,
177                this->getAbsCoor ().y,
178                this->getAbsCoor ().z);
179  Vector tmpRot = this->getAbsDir().getSpacialAxis();
180  glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
181
182  this->model->draw();
183  glPopMatrix();
184}
185
Note: See TracBrowser for help on using the repository browser.