Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/world_entities/weapons/spike_launcher.cc @ 10576

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

removed unused includes

File size: 6.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004-2006 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: Marc Schaerrer
13   co-programmer:
14*/
15//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
16
17#include "spike_launcher.h"
18
19#include "weapon_manager.h"
20#include "world_entities/projectiles/projectile.h"
21#include "world_entities/projectiles/spike_ball.h"
22
23#include "model.h"
24
25#include "state.h"
26#include "animation3d.h"
27
28#include <list>
29#include <iterator>
30#include "util/state.h"
31
32#include "util/loading/factory.h"
33
34
35
36using namespace std;
37
38ObjectListDefinition(SpikeLauncher);
39CREATE_FACTORY(SpikeLauncher);
40
41/**
42 *  standard constructor
43 *
44 * creates a new SpikeLauncher
45 */
46SpikeLauncher::SpikeLauncher()
47  : Weapon()
48{
49  this->init();
50}
51
52/**
53 * creates a new SpikeLauncher from a TiXmlElement
54 */
55SpikeLauncher::SpikeLauncher(const TiXmlElement* root)
56{
57  this->init();
58  if (root != NULL)
59    this->loadParams(root);
60}
61
62/**
63 *  standard deconstructor
64*/
65SpikeLauncher::~SpikeLauncher ()
66{
67  delete this->launcher;
68
69  // model will be deleted from WorldEntity-destructor
70}
71
72void SpikeLauncher::init()
73{
74  this->registerObject(this, SpikeLauncher::_objectList);
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, .6);
88  this->setStateDuration(WS_RELOADING, 1.0f);
89  this->setStateDuration(WS_ACTIVATING, .4);
90  this->setStateDuration(WS_DEACTIVATING, .4);
91
92  this->setEnergyMax(10);
93  this->increaseEnergy(10);
94  //this->minCharge = 2;
95
96  this->setCapability(WTYPE_ALLDIRS | WTYPE_TURRET | WTYPE_DIRECTIONAL | WTYPE_LIGHT);
97  this->setProjectileTypeC("Spike");
98
99//   this->loadModel("models/guns/turret1.obj", 1.0);
100  this->size = 2.5;
101
102  this->setEmissionPoint(0.0, 0.0, 0.0);
103
104
105  this->spikes = 26;
106  this->launcher = new Vector [this->spikes];
107
108  this->getProjectileFactory()->prepare(this->spikes);  //we don't need more...
109
110//   this->setActionSound(WA_SHOOT, "sounds/explosions/explosion_1.wav");
111//   this->setActionSound(WA_ACTIVATE, "sounds/voices/rockets.wav");
112//   this->setActionSound(WA_RELOAD, "sounds/voices/reload.wav");
113
114}
115
116void SpikeLauncher::loadParams(const TiXmlElement* root)
117{
118  Weapon::loadParams(root);
119}
120
121void SpikeLauncher::activate()
122{
123  this->launcher[0] = Vector(1.0, 0.0, 0.0);
124  this->launcher[1] = Vector(0.0, 1.0, 0.0);
125  this->launcher[2] = Vector(0.0, 0.0, 1.0);
126
127  this->launcher[3] = Vector(1.0, 1.0, 0.0);
128  this->launcher[4] = Vector(0.0, 1.0, 1.0);
129  this->launcher[5] = Vector(1.0, 0.0, 1.0);
130  this->launcher[6] = Vector(1.0, -1.0, 0.0);
131  this->launcher[7] = Vector(0.0, 1.0, -1.0);
132  this->launcher[8] = Vector(-1.0, 0.0, 1.0);
133
134  this->launcher[9] = Vector(-1.0, 1.0, 1.0);
135  this->launcher[10] = Vector(1.0, 1.0, 1.0);
136  this->launcher[11] = Vector(1.0, -1.0, 1.0);
137  this->launcher[12] = Vector(-1.0, -1.0, 1.0);
138
139  int tmp = this->spikes / 2;
140  for (int i = 0; i < tmp; i++)
141  {
142    this->launcher[i].normalize();
143    this->launcher[tmp + i] =  this->launcher[i] * (-1);
144  }
145}
146
147void SpikeLauncher::deactivate()
148{
149}
150
151void SpikeLauncher::tick(float dt)
152{
153  if (!Weapon::tickW(dt))
154    return;
155
156  Quaternion quat;
157  Vector direction;
158  if (this->getDefaultTarget() == NULL)
159    direction = this->getAbsCoor();
160  else
161    direction = this->getDefaultTarget()->getAbsCoor() - this->getAbsCoor();
162
163  direction.normalize();
164
165  if (likely (this->getParent() != NULL))
166    quat = Quaternion(direction, this->getParent()->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ;
167  else
168    quat = Quaternion(direction, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)) ;
169
170  this->setAbsDirSoft(quat, 5);
171}
172
173void SpikeLauncher::updateFireDir(Vector v, float angle){
174
175  float** m = new float* [3];
176  for( int i = 0; i < 3 ; i++)
177    m[i] = new float;
178
179  float nx, ny, nz, ca, sa;
180
181  nx = v.x;
182  ny = v.y;
183  nz = v.z;
184
185  ca = cos (angle);
186  sa = sin (angle);
187// final version below... easier to to cheat with the one above.
188/*
189  nx = this->getParent()->getRotationVector().x;
190  ny = this->getParent()->getRotationVector().y;
191  nz = this->getParent()->getRotationVector().z;
192
193  ca = cos (this->getParent()->getAngle());
194  sa = sin (this->getParent()->getAngle());*/
195/*
196  float * m = new float [9];
197  m[0] = 1.0f+(1.0f-ca)*(nx*nx-1.0f);
198  m[1] = -nz*sa+(1.0f-ca)*nx*ny;
199  m[2] = ny*sa+(1.0f-ca)*nx*nz;
200  m[3] = nz*sa+(1.0f-ca)*nx*ny;
201  m[4] = 1.0f+(1.0f-ca)*(ny*ny-1.0f);
202  m[5] = -nx*sa+(1.0f-ca)*ny*nz;
203  m[6] = -ny*sa+(1.0f-ca)*nx*nz;
204  m[7] = nx*sa+(1.0f-ca)*ny*nz;
205  m[8] = 1.0f+(1.0f-ca)*(nz*nz-1.0f);*/
206
207  m[0][0] = nx * nx * (1 - ca) + ca;
208  m[0][1] = nx * ny * (1 - ca) + nz * sa;
209  m[0][2] = nx * nz * (1 - ca) - ny * sa;
210  m[1][0] = nx * nz * (1 - ca) - nz * sa;
211  m[1][1] = ny * ny * (1 - ca) + ca;
212  m[1][2] = ny * nz * (1 - ca) + nx * sa;
213  m[2][0] = nx * nz * (1 - ca) + ny * sa;
214  m[2][1] = ny * nz * (1 - ca) - nx * sa;
215  m[2][2] = nz * nz * (1 - ca) + ca;
216
217  float x, y, z;
218  for (int i = 0; i < this->spikes;i++){
219    printf("%i ", i);
220    x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z;
221    y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z;
222    z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z;
223/*
224    x = this->launcher[i].x * m[0] + this->launcher[i].y * m[1] + this->launcher[i].z * m[2];
225    y = this->launcher[i].x * m[3] + this->launcher[i].y * m[4] + this->launcher[i].z * m[5];
226    z = this->launcher[i].x * m[6] + this->launcher[i].y * m[7] + this->launcher[i].z * m[8];*/
227    this->launcher[i] = Vector (x, y, z);
228  }
229  printf("\n");
230
231  for( int i = 0; i < 3 ; i++)
232    delete m[i];
233  delete m;
234}
235
236
237void SpikeLauncher::fire()
238{
239  updateFireDir(VECTOR_RAND(1), 20);
240
241  Projectile* pj = NULL;
242  for (int i = 0; i < this->spikes; i++)
243  {
244    pj  = this->getProjectile();
245    if (pj == NULL) // if true, we do have a problem!!
246      return;
247
248    pj->setVelocity(this->launcher[i].getNormalized() * 300.0);
249
250    pj->setParent(PNode::getNullParent());
251    pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size);
252    Quaternion q;
253    pj->setRelDir(q.lookAt(Vector(), this->launcher[i], VECTOR_RAND(1)));
254
255    pj->activate();
256  }
257}
Note: See TracBrowser for help on using the repository browser.