Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world_entities/weapons/test_gun.cc @ 4974

Last change on this file since 4974 was 4974, checked in by bensch, 19 years ago

orxonox/trunk: segfault prevention, model was not loaded, because the TestGun did not init() itself from the constructor

File size: 6.5 KB
Line 
1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific
14   main-programmer: Patrick Boenzli
15   co-programmer:
16
17
18   @todo: direction in which the projectile flights
19   @todo: a target to set/hit
20*/
21
22
23#include "test_gun.h"
24
25#include "world_entity.h"
26#include "model.h"
27#include "test_bullet.h"
28#include "weapon_manager.h"
29
30#include "state.h"
31#include "vector.h"
32#include "list.h"
33#include "animation3d.h"
34#include "sound_engine.h"
35
36#include "fast_factory.h"
37
38
39using namespace std;
40
41CREATE_FACTORY(TestGun);
42
43/**
44 *  standard constructor
45
46   creates a new weapon
47*/
48TestGun::TestGun (WeaponManager* weaponManager, int leftRight)
49  : Weapon(weaponManager)
50{
51  this->init();
52
53
54  this->leftRight = leftRight;
55
56  this->objectComponent1 = new PNode();
57  Animation3D* animation1 = this->getAnimation(WS_SHOOTING, this->objectComponent1);
58  Animation3D* animation2 = this->getAnimation(WS_ACTIVATING, this);
59  Animation3D* animation3 = this->getAnimation(WS_DEACTIVATING, this);
60  //parent->addChild(this->objectComponent1, PNODE_ALL);
61  this->addChild(this->objectComponent1, PNODE_ALL);
62
63  animation1->setInfinity(ANIM_INF_CONSTANT);
64  animation2->setInfinity(ANIM_INF_CONSTANT);
65  animation3->setInfinity(ANIM_INF_CONSTANT);
66
67  if( this->leftRight == W_LEFT)
68  {
69    this->setEmissionPoint(1.0, 0.0, -0.35);
70
71    animation1->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.1, ANIM_LINEAR, ANIM_CONSTANT);
72    animation1->addKeyFrame(Vector(-0.4, 0, 0), Quaternion(), 0.1, ANIM_LINEAR, ANIM_CONSTANT);
73    animation1->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.0, ANIM_LINEAR, ANIM_CONSTANT);
74
75    animation2->addKeyFrame(Vector(0.0, 0.0, -1.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
76    animation2->addKeyFrame(Vector(0.0, 0.0, 0.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
77
78    animation3->addKeyFrame(Vector(0.0, 0.0, 0.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
79    animation3->addKeyFrame(Vector(0.0, 0.0, -1.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
80  }
81  else if( this->leftRight == W_RIGHT)
82  {
83    this->setEmissionPoint(1.0, 0.0, 0.5);
84
85    this->objectComponent1->setRelCoor(Vector(0,0,0.35));
86    animation1->addKeyFrame(Vector(0, 0, .5), Quaternion(), 0.1, ANIM_LINEAR, ANIM_CONSTANT);
87    animation1->addKeyFrame(Vector(-0.4, 0, .5), Quaternion(), 0.1, ANIM_LINEAR, ANIM_CONSTANT);
88    animation1->addKeyFrame(Vector(0, 0, .5), Quaternion(), 0.0, ANIM_LINEAR, ANIM_CONSTANT);
89
90    animation2->addKeyFrame(Vector(.0, .0, 1.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
91    animation2->addKeyFrame(Vector(.0, .0, .0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
92
93    animation3->addKeyFrame(Vector(.0, .0, 0.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
94    animation3->addKeyFrame(Vector(.0, .0, 1.0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT);
95  }
96}
97
98
99TestGun::TestGun(const TiXmlElement* root)
100{
101  this->init();
102  this->loadParams(root);
103}
104
105/**
106 *  standard deconstructor
107*/
108TestGun::~TestGun ()
109{
110  // model will be deleted from WorldEntity-destructor
111}
112
113
114void TestGun::init()
115{
116  this->setClassID(CL_TEST_GUN, "TestGun");
117
118  this->model = (Model*)ResourceManager::getInstance()->load("models/test_gun.obj", OBJ, RP_CAMPAIGN);
119
120  this->setStateDuration(WS_SHOOTING, .4);
121  this->setStateDuration(WS_RELOADING, 1);
122  this->setStateDuration(WS_ACTIVATING, .4);
123  this->setStateDuration(WS_DEACTIVATING, .4);
124
125  this->setMaximumEnergy(1000, 10);
126  this->increaseEnergy(100);
127  //this->minCharge = 2;
128
129  this->setActionSound(WA_SHOOT, "sound/shot1.wav");
130
131  this->setProjectile(CL_TEST_BULLET);
132  //this->getProjectileFactory()->prepare(100);
133}
134
135
136void TestGun::loadParams(const TiXmlElement* root)
137{
138
139
140}
141
142
143/**
144 *  this activates the weapon
145
146   This is needed, since there can be more than one weapon on a ship. the
147   activation can be connected with an animation. for example the weapon is
148   been armed out.
149*/
150void TestGun::activate()
151{
152}
153
154
155/**
156 *  this deactivates the weapon
157
158   This is needed, since there can be more than one weapon on a ship. the
159   activation can be connected with an animation. for example the weapon is
160   been armed out.
161*/
162void TestGun::deactivate()
163{
164}
165
166
167/**
168 *  fires the weapon
169
170   this is called from the player.cc, when fire-button is been pushed
171   @todo: the ObjectManager deliveres Projectiles not TestBullets! this should be diffrent
172*/
173void TestGun::fire()
174{
175  Projectile* pj =  dynamic_cast<Projectile*>(this->getProjectileFactory()->resurrect());
176
177/*
178  PNode* target = this->getWeaponManager()->getFixedTarget();
179  if (target != NULL)
180  {
181    pj->setVelocity(this->getVelocity()+(target->getAbsCoor() - this->getAbsCoor())*.5);//this->getVelocity());
182  }
183  else*/
184  pj->setVelocity(this->getVelocity() + this->getAbsDir().apply(Vector(1,0,0))*20);
185
186  pj->setAbsCoor(this->getEmissionPoint());
187  pj->setAbsDir(this->getAbsDir());
188  State::getWorldEntityList()->add(pj);
189}
190
191
192/**
193 *  is called, when the weapon gets hit (=collide with something)
194 * @param from which entity it is been hit
195 * @param where it is been hit
196
197   this may not be used, since it would make the game relay complicated when one
198   can destroy the weapons of enemies or vice versa.
199*/
200void TestGun::hit (WorldEntity* entity, Vector* position)
201{}
202
203
204/**
205 *  is called, when the weapon is destroyed
206
207   this is in conjunction with the hit function, so when a weapon is able to get
208   hit, it can also be destoryed.
209*/
210void TestGun::destroy ()
211{}
212
213/**
214 *  this will draw the weapon
215*/
216void TestGun::draw ()
217{
218  this->getWeaponManager()->getFixedTarget()->debugDraw(10);
219
220  /* draw gun body */
221  glMatrixMode(GL_MODELVIEW);
222  glPushMatrix();
223  float matrix[4][4];
224  glTranslatef (this->getAbsCoor ().x,
225                this->getAbsCoor ().y,
226                this->getAbsCoor ().z);
227  this->getAbsDir ().matrix (matrix);
228  glMultMatrixf((float*)matrix);
229  if( this->leftRight == W_RIGHT)
230    glScalef(1.0, 1.0, -1.0);
231  this->model->draw(1);
232  glPopMatrix();
233
234  /* draw objectComponent1: gun coil - animated stuff */
235  glMatrixMode(GL_MODELVIEW);
236  glPushMatrix();
237  glTranslatef (this->objectComponent1->getAbsCoor ().x,
238                this->objectComponent1->getAbsCoor ().y,
239                this->objectComponent1->getAbsCoor ().z);
240  this->objectComponent1->getAbsDir ().matrix (matrix);
241  glMultMatrixf((float*)matrix);
242  this->model->draw(0);
243  glPopMatrix();
244}
245
Note: See TracBrowser for help on using the repository browser.