Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: tmuch better lookAt implementation, so that the Turrets are really aiming at the designated target.

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