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 "math/quaternion.h" |
---|
33 | |
---|
34 | #include "util/loading/factory.h" |
---|
35 | |
---|
36 | #include "class_id_DEPRECATED.h" |
---|
37 | |
---|
38 | using namespace std; |
---|
39 | |
---|
40 | ObjectListDefinition(SpikeLauncher); |
---|
41 | CREATE_FACTORY(SpikeLauncher); |
---|
42 | |
---|
43 | /** |
---|
44 | * standard constructor |
---|
45 | * |
---|
46 | * creates a new SpikeLauncher |
---|
47 | */ |
---|
48 | SpikeLauncher::SpikeLauncher() |
---|
49 | : Weapon() |
---|
50 | { |
---|
51 | this->init(); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * creates a new SpikeLauncher from a TiXmlElement |
---|
56 | */ |
---|
57 | SpikeLauncher::SpikeLauncher(const TiXmlElement* root) |
---|
58 | { |
---|
59 | this->init(); |
---|
60 | if (root != NULL) |
---|
61 | this->loadParams(root); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * standard deconstructor |
---|
66 | */ |
---|
67 | SpikeLauncher::~SpikeLauncher () |
---|
68 | { |
---|
69 | delete this->launcher; |
---|
70 | |
---|
71 | // model will be deleted from WorldEntity-destructor |
---|
72 | } |
---|
73 | |
---|
74 | void SpikeLauncher::init() |
---|
75 | { |
---|
76 | this->registerObject(this, SpikeLauncher::_objectList); |
---|
77 | |
---|
78 | Animation3D* animation1 = this->getAnimation(WS_ACTIVATING, this); |
---|
79 | Animation3D* animation2 = this->getAnimation(WS_DEACTIVATING, this); |
---|
80 | |
---|
81 | animation1->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); |
---|
82 | animation1->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); |
---|
83 | animation2->addKeyFrame(Vector(0, 0, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); |
---|
84 | animation2->addKeyFrame(Vector(0, -.5, 0), Quaternion(), 0.3, ANIM_LINEAR, ANIM_CONSTANT); |
---|
85 | |
---|
86 | animation1->setInfinity(ANIM_INF_CONSTANT); |
---|
87 | animation2->setInfinity(ANIM_INF_CONSTANT); |
---|
88 | |
---|
89 | this->setStateDuration(WS_SHOOTING, .6); |
---|
90 | this->setStateDuration(WS_RELOADING, 1.0f); |
---|
91 | this->setStateDuration(WS_ACTIVATING, .4); |
---|
92 | this->setStateDuration(WS_DEACTIVATING, .4); |
---|
93 | |
---|
94 | this->setEnergyMax(10); |
---|
95 | this->increaseEnergy(10); |
---|
96 | //this->minCharge = 2; |
---|
97 | |
---|
98 | this->setCapability(WTYPE_ALLDIRS | WTYPE_TURRET | WTYPE_DIRECTIONAL | WTYPE_LIGHT); |
---|
99 | this->setProjectileTypeC("Spike"); |
---|
100 | |
---|
101 | // this->loadModel("models/guns/turret1.obj", 1.0); |
---|
102 | this->size = 2.5; |
---|
103 | |
---|
104 | this->setEmissionPoint(0.0, 0.0, 0.0); |
---|
105 | |
---|
106 | |
---|
107 | this->spikes = 26; |
---|
108 | this->launcher = new Vector [this->spikes]; |
---|
109 | |
---|
110 | this->launcher[0] = Vector(1.0, 0.0, 0.0); |
---|
111 | this->launcher[1] = Vector(0.0, 1.0, 0.0); |
---|
112 | this->launcher[2] = Vector(0.0, 0.0, 1.0); |
---|
113 | |
---|
114 | this->launcher[3] = Vector(1.0, 1.0, 0.0); |
---|
115 | this->launcher[4] = Vector(0.0, 1.0, 1.0); |
---|
116 | this->launcher[5] = Vector(1.0, 0.0, 1.0); |
---|
117 | this->launcher[6] = Vector(1.0, -1.0, 0.0); |
---|
118 | this->launcher[7] = Vector(0.0, 1.0, -1.0); |
---|
119 | this->launcher[8] = Vector(-1.0, 0.0, 1.0); |
---|
120 | |
---|
121 | this->launcher[9] = Vector(-1.0, 1.0, 1.0); |
---|
122 | this->launcher[10] = Vector(1.0, 1.0, 1.0); |
---|
123 | this->launcher[11] = Vector(1.0, -1.0, 1.0); |
---|
124 | this->launcher[12] = Vector(-1.0, -1.0, 1.0); |
---|
125 | |
---|
126 | int tmp = this->spikes / 2; |
---|
127 | for (int i = 0; i < tmp; i++) |
---|
128 | { |
---|
129 | this->launcher[i].normalize(); |
---|
130 | this->launcher[tmp + i] = this->launcher[i] * (-1); |
---|
131 | } |
---|
132 | |
---|
133 | this->getProjectileFactory()->prepare(this->spikes); //we don't need more... |
---|
134 | |
---|
135 | // this->setActionSound(WA_SHOOT, "sound/explosions/explosion_1.wav"); |
---|
136 | // this->setActionSound(WA_ACTIVATE, "sound/vocals/missiles.wav"); |
---|
137 | // this->setActionSound(WA_RELOAD, "sound/vocals/reload.wav"); |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | void SpikeLauncher::loadParams(const TiXmlElement* root) |
---|
142 | { |
---|
143 | Weapon::loadParams(root); |
---|
144 | } |
---|
145 | |
---|
146 | void SpikeLauncher::activate() |
---|
147 | { |
---|
148 | } |
---|
149 | |
---|
150 | void SpikeLauncher::deactivate() |
---|
151 | { |
---|
152 | } |
---|
153 | |
---|
154 | void SpikeLauncher::tick(float dt) |
---|
155 | { |
---|
156 | if (!Weapon::tickW(dt)) |
---|
157 | return; |
---|
158 | |
---|
159 | Quaternion quat; |
---|
160 | Vector direction; |
---|
161 | if (this->getDefaultTarget() == NULL) |
---|
162 | direction = this->getAbsCoor(); |
---|
163 | else |
---|
164 | direction = this->getDefaultTarget()->getAbsCoor() - this->getAbsCoor(); |
---|
165 | |
---|
166 | direction.normalize(); |
---|
167 | |
---|
168 | if (likely (this->getParent() != NULL)) |
---|
169 | quat = Quaternion(direction, this->getParent()->getAbsDir().apply(Vector(0,1,0))) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; |
---|
170 | else |
---|
171 | quat = Quaternion(direction, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0)) ; |
---|
172 | |
---|
173 | this->setAbsDirSoft(quat, 5); |
---|
174 | } |
---|
175 | |
---|
176 | void SpikeLauncher::updateFireDir(Vector v, float angle){ |
---|
177 | |
---|
178 | float** m = new float* [3]; |
---|
179 | for( int i = 0; i < 3 ; i++) |
---|
180 | m[i] = new float; |
---|
181 | |
---|
182 | float nx, ny, nz, ca, sa; |
---|
183 | |
---|
184 | nx = v.x; |
---|
185 | ny = v.y; |
---|
186 | nz = v.z; |
---|
187 | |
---|
188 | ca = cos (angle); |
---|
189 | sa = sin (angle); |
---|
190 | // final version below... easier to to cheat with the one above. |
---|
191 | /* |
---|
192 | nx = this->getParent()->getRotationVector().x; |
---|
193 | ny = this->getParent()->getRotationVector().y; |
---|
194 | nz = this->getParent()->getRotationVector().z; |
---|
195 | |
---|
196 | ca = cos (this->getParent()->getAngle()); |
---|
197 | sa = sin (this->getParent()->getAngle());*/ |
---|
198 | |
---|
199 | m[0][0] = nx * nx * (1 - ca) + ca; |
---|
200 | m[0][1] = nx * ny * (1 - ca) + nz * sa; |
---|
201 | m[0][2] = nx * nz * (1 - ca) - ny * sa; |
---|
202 | m[1][0] = nx * nz * (1 - ca) - nz * sa; |
---|
203 | m[1][1] = ny * ny * (1 - ca) + ca; |
---|
204 | m[1][2] = ny * nz * (1 - ca) + nx * sa; |
---|
205 | m[2][0] = nx * nz * (1 - ca) + ny * sa; |
---|
206 | m[2][1] = ny * nz * (1 - ca) - nx * sa; |
---|
207 | m[2][2] = nz * nz * (1 - ca) + ca; |
---|
208 | |
---|
209 | float x, y, z; |
---|
210 | for (int i = 0; i < this->spikes;i++){ |
---|
211 | x = m[0][0] * this->launcher[i].x + m[0][1] * this->launcher[i].y + m[0][2] * this->launcher[i].z; |
---|
212 | y = m[1][0] * this->launcher[i].x + m[1][1] * this->launcher[i].y + m[1][2] * this->launcher[i].z; |
---|
213 | z = m[2][0] * this->launcher[i].x + m[2][1] * this->launcher[i].y + m[2][2] * this->launcher[i].z; |
---|
214 | |
---|
215 | this->launcher[i] = Vector (x, y, z); |
---|
216 | } |
---|
217 | |
---|
218 | for( int i = 0; i < 3 ; i++) |
---|
219 | delete m[i]; |
---|
220 | delete m; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | void SpikeLauncher::fire() |
---|
225 | { |
---|
226 | // temporary solution to test module |
---|
227 | updateFireDir(VECTOR_RAND(1), 20); |
---|
228 | |
---|
229 | |
---|
230 | Projectile* pj = NULL; |
---|
231 | for (int i = 0; i < this->spikes; i++) |
---|
232 | { |
---|
233 | pj = this->getProjectile(); |
---|
234 | if (pj == NULL) // if true, we do have a problem!! |
---|
235 | return; |
---|
236 | |
---|
237 | // pj->setVelocity(this->getVelocity()+(this->getAbsDir().apply(Vector(1,0,0))*115.0 + VECTOR_RAND(10))); |
---|
238 | pj->setVelocity(this->launcher[i] * 300.0); |
---|
239 | |
---|
240 | pj->setParent(PNode::getNullParent()); |
---|
241 | pj->setAbsCoor(this->getAbsCoor() + this->launcher[i] * this->size); |
---|
242 | pj->setRelDir(0, this->launcher[i].x, this->launcher[i].y, this->launcher[i].z); |
---|
243 | |
---|
244 | pj->toList(this->getOMListNumber()); |
---|
245 | pj->activate(); |
---|
246 | } |
---|
247 | } |
---|