Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/adm/src/world_entities/npcs/adm_turret.cc @ 10679

Last change on this file since 10679 was 10679, checked in by rennerc, 17 years ago
File size: 6.3 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: Reto Luechinger
13*/
14
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
17#include "adm_turret.h"
18#include "weapons/weapon_manager.h"
19#include "weapons/weapon.h"
20#include "lib/util/loading/factory.h"
21#include "world_entities/projectiles/projectile.h"
22#include "loading/load_param.h"
23#include "debug.h"
24#include "loading/load_param_xml.h"
25
26ObjectListDefinition(AdmTurret);
27CREATE_FACTORY(AdmTurret);
28
29
30/**
31*  Standard constructor
32*/
33AdmTurret::AdmTurret ()
34{
35        this->init();
36}
37
38/**
39* destructs the turret, deletes allocated memory
40*/
41AdmTurret::~AdmTurret ()
42{
43          // will be deleted
44}
45
46/**
47* Constructor with XML Element
48*/
49AdmTurret::AdmTurret (const  TiXmlElement* root)
50{
51        this->init();
52        if (root != NULL)
53        {
54                this->loadParams(root);
55        }
56}
57/**
58* XML Loader
59*/
60void AdmTurret::loadParams(const TiXmlElement* root)
61{
62        if (root != NULL)
63        {
64                WorldEntity::loadParams(root);
65
66                LoadParam(root, "target", this, AdmTurret, setTarget)
67                        .describe("The filename of the World Entity, that is to be shot at")
68                        .defaultValues("");
69
70                LoadParamXML(root, "Cannons", this, AdmTurret, addCannons)
71                        .describe("add cannons to ADM");
72                LoadParamXML(root, "Sensor", this, AdmTurret, addSensor)
73                        .describe("add sensor to ADM");
74
75        }
76       
77        //HACK this is really ugly because if you move the AdmTurret its cannons wont follow
78        if ( this->cannons )
79          this->cannons->setParent( NullParent::getNullParent() );
80}
81
82/**
83* Sets Target onto the World Entity with filename "target", given as String
84*/
85void AdmTurret::setTarget(const std::string& target)
86{
87        WorldEntity* targetEntity = WorldEntity::objectList().getObject(target);
88        if (targetEntity != NULL) 
89        {
90                this->myTarget = targetEntity;
91        }
92        else
93        {
94                PRINTF(1)("ERROR ADMTURRET : Target %s does not exist\n", target.c_str());
95        }
96}
97
98void AdmTurret::init()
99{
100        this->registerObject(this, AdmTurret::_objectList);
101       
102        //this->removeNodeFlags( 7 );
103}
104
105void AdmTurret::tick(float dt)
106{
107  WorldEntity::tick(dt);
108
109  //rotate sensor 2PI/sec
110  this->sensor->setAbsDir( this->sensor->getAbsDir() * Quaternion( PI*2*dt, Vector( 0, 1, 0 ) ) );
111
112
113  Vector playerPos = this->myTarget->getAbsCoor();
114  this->moveTowards( playerPos - ( this->cannons->getAbsCoor() ),  dt);
115}
116
117void AdmTurret::draw() const
118{
119  WorldEntity::draw();
120
121
122  glMatrixMode(GL_MODELVIEW);
123  glPushMatrix();
124
125  glPushAttrib(GL_ENABLE_BIT);
126
127  glDisable(GL_LIGHTING);
128  glDisable(GL_TEXTURE_2D);
129  glDisable(GL_BLEND);
130  glLineWidth(2.0);
131
132
133  Vector mp = this->cannons->getAbsCoor();
134  Vector op = this->cannons->getAbsDir().apply( Vector(-1, 0, 0) );
135  op *= 100;
136  op += mp;
137
138  glColor3f(1.0, 0.0, 0.0 );
139  glBegin(GL_LINE_STRIP);
140    glVertex3f(mp.x, mp.y, mp.z);
141    glVertex3f(op.x, op.y, op.z);
142  glEnd();
143
144  op = this->myTarget->getAbsCoor() - ( this->cannons->getAbsCoor() );
145  op += mp;
146
147  glColor3f(0.0, 1.0, 0.0 );
148  glBegin(GL_LINE_STRIP);
149    glVertex3f(mp.x, mp.y, mp.z);
150    glVertex3f(op.x, op.y, op.z);
151  glEnd();
152 
153  glPopAttrib();
154  glPopMatrix();
155 
156}
157
158void AdmTurret::collidesWith(WorldEntity* entity, const Vector& location)
159{
160}
161
162void AdmTurret::activate()
163{
164}
165
166void AdmTurret::deactivate()
167{
168}
169
170bool AdmTurret::isVisible(const WorldEntity myTarget)
171{
172return true;
173}
174
175float AdmTurret::aim(const WorldEntity Target)
176{
177return 0;
178}
179
180void AdmTurret::fire()
181{
182        // Projectile* pj =  this->getProjectile();
183        // if (pj == NULL) return;
184
185        //pj->setOwner(this->getOwner());
186        //pj->setParent(PNode::getNullParent());
187        //pj->setVelocity(this->getAbsDir().apply(Vector( , , )) );
188        //pj->setAbsCoor(this->getEmissionPoint());
189        //pj->setAbsDir(this->getAbsDir());
190        //pj->activate();
191}
192
193void AdmTurret::addCannons( const TiXmlElement * root )
194{
195        this->cannons = new WorldEntity();
196        this->cannons->setParent(this);
197        this->cannons->loadParams( root );
198
199        //this->cannons->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
200        //this->cannons->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
201       
202        this->cannons->toList( getOMListNumber() );
203}
204
205void AdmTurret::addSensor( const TiXmlElement * root )
206{
207        this->sensor = new WorldEntity();
208        this->sensor->setParent(this);
209        this->sensor->loadParams( root );
210
211        //this->sensor->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
212        //this->sensor->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
213        this->sensor->toList( getOMListNumber() );
214}
215
216void AdmTurret::moveTowards( Vector targetDir, float dt )
217{
218#if 0
219  static Quaternion mrot = this->getAbsDir();
220  Quaternion ry( dt, Vector(0, 1, 0) );
221  mrot *= ry;
222  this->setAbsDir(mrot);
223
224  static Quaternion rot = this->cannons->getAbsDir();
225  Quaternion rx( dt, Vector( 0, 0, 1 ) );
226  rot *= rx;
227  this->cannons->setAbsDir(rot*mrot);
228  return;
229#endif
230
231
232
233
234  Quaternion cur = this->getAbsDir();
235 
236  Quaternion ry( dt, cur.inverse().apply( Vector( 0, 1, 0 ) ) );
237 
238  Quaternion tmp1 = cur * ry;
239  Quaternion tmp2 = cur * ry.inverse();
240 
241  bool usetmp1 = false;
242  Quaternion r1;
243  if ( tmp1.apply( Vector(1, 0, 0) ).dot(targetDir) < tmp2.apply( Vector(1, 0, 0)).dot(targetDir) )
244  {
245    usetmp1 = true;
246    cur = tmp1;
247    r1 = ry;
248  }
249  else
250  {
251    cur = tmp2;
252    r1 = ry.inverse();
253  }
254 
255  this->setAbsDir( cur );
256
257 
258  Quaternion cur2 = this->cannons->getAbsDir();
259  Quaternion rx( dt, cur.inverse().apply( Vector( 0, 0, 1 ) ) );
260  Quaternion rr( dt, cur2.inverse().apply( Vector( 0, 1, 0 ) ) );
261  if ( !usetmp1 )
262    rr = rr.inverse();
263 
264 
265  tmp1 = cur2 * rx;
266  tmp2 = cur2 * rx.inverse();
267
268  Quaternion dec;
269  Quaternion r2;
270  if ( tmp1.apply(  Vector(-1, 0, 0) ).dot(targetDir) > tmp2.apply( Vector(-1, 0, 0) ).dot(targetDir) )
271  {
272    r2 = rx;
273    dec = tmp1;
274  }
275  else
276  {
277    r2 = rx.inverse();
278    dec = tmp2;
279  }
280 
281  float dp = dec.apply( Vector(-1, 0, 0) ).dot(Vector(0, 1, 0));
282  if ( dp > -0.9 && dp < 0.9 )
283  {
284    cur2 = dec;
285  }
286
287  this->cannons->setAbsDir( cur2  );
288
289
290
291}
Note: See TracBrowser for help on using the repository browser.