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, Nicolas Schlumberger |
---|
13 | co-programmer: |
---|
14 | |
---|
15 | */ |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "swarm_projectile.h" |
---|
19 | |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | #include "particles/dot_emitter.h" |
---|
23 | #include "particles/sprite_particles.h" |
---|
24 | #include "space_ships/space_ship.h" |
---|
25 | #include "effects/trail.h" |
---|
26 | |
---|
27 | #include "debug.h" |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | #include "math/vector.h" |
---|
32 | |
---|
33 | ObjectListDefinition(SwarmProjectile); |
---|
34 | CREATE_FAST_FACTORY_STATIC(SwarmProjectile); |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | */ |
---|
39 | SwarmProjectile::SwarmProjectile () : Projectile() |
---|
40 | { |
---|
41 | |
---|
42 | /* this->loadModel("models/projectiles/orx-rocket.obj", 0.5);*/ |
---|
43 | this->loadModel("models/projectiles/swarm_projectile.obj"); // no double rescale (see draw()) |
---|
44 | this->loadExplosionSound("sounds/explosions/explosion_4.wav"); |
---|
45 | |
---|
46 | |
---|
47 | this->setMinEnergy(1); |
---|
48 | this->setHealthMax(10); |
---|
49 | this->lifeSpan = 4.0; |
---|
50 | this->agility = 3.5; |
---|
51 | |
---|
52 | this->emitter = new DotEmitter(100, 5, M_2_PI); |
---|
53 | this->emitter->setParent(this); |
---|
54 | this->emitter->setSpread(M_PI, M_PI); |
---|
55 | |
---|
56 | this->turningSpeed = 10; |
---|
57 | |
---|
58 | this->physDamage = 200; |
---|
59 | this->elecDamage = 0; |
---|
60 | |
---|
61 | this->trail = new Trail(2.5,4,.2, this); |
---|
62 | //this->trail->setParent( this); |
---|
63 | this->trail->setTexture( "textures/laser.png"); |
---|
64 | |
---|
65 | |
---|
66 | this->origList = this->getOMListNumber(); |
---|
67 | this->toList(OM_ENVIRON); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /** |
---|
72 | * standard deconstructor |
---|
73 | */ |
---|
74 | SwarmProjectile::~SwarmProjectile () |
---|
75 | { |
---|
76 | |
---|
77 | if (SwarmProjectile::explosionParticles != NULL && SwarmProjectile::objectList().size() <= 1) |
---|
78 | { |
---|
79 | if (ParticleSystem::objectList().exists(SwarmProjectile::explosionParticles)) |
---|
80 | delete SwarmProjectile::explosionParticles; |
---|
81 | SwarmProjectile::explosionParticles = NULL; |
---|
82 | } |
---|
83 | // delete this->emitter; |
---|
84 | delete this->trail; |
---|
85 | } |
---|
86 | |
---|
87 | SpriteParticles* SwarmProjectile::explosionParticles = NULL; |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | void SwarmProjectile::activate() |
---|
92 | { |
---|
93 | this->toList(OM_ENVIRON); |
---|
94 | if (unlikely(SwarmProjectile::explosionParticles == NULL)) |
---|
95 | { |
---|
96 | SwarmProjectile::explosionParticles = new SpriteParticles(200); |
---|
97 | SwarmProjectile::explosionParticles->setName("SwarmProjectileExplosionParticles"); |
---|
98 | SwarmProjectile::explosionParticles->setMaterialTexture("textures/radial-trans-noise.png"); |
---|
99 | SwarmProjectile::explosionParticles->setLifeSpan(.5, .3); |
---|
100 | SwarmProjectile::explosionParticles->setRadius(0.0, 10); |
---|
101 | SwarmProjectile::explosionParticles->setRadius(.5, 15.0); |
---|
102 | SwarmProjectile::explosionParticles->setRadius(1.0, 10.0); |
---|
103 | SwarmProjectile::explosionParticles->setColor(0.0, 0,1,0,1); |
---|
104 | SwarmProjectile::explosionParticles->setColor(0.5, .8,.8,0,.8); |
---|
105 | SwarmProjectile::explosionParticles->setColor(0.8, .8,.8,.3,.8); |
---|
106 | SwarmProjectile::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
107 | } |
---|
108 | |
---|
109 | this->emitter->setEmissionRate(50.0); |
---|
110 | this->emitter->setEmissionVelocity(0.0); |
---|
111 | this->emitter->setInheritSpeed(0); |
---|
112 | |
---|
113 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
114 | |
---|
115 | this->maxVelocity = 300; |
---|
116 | |
---|
117 | this->rotationSpeed = 360; |
---|
118 | this->angle = 0; |
---|
119 | |
---|
120 | this->curDir = this->lastDir = this->velocity; |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void SwarmProjectile::deactivate() |
---|
125 | { |
---|
126 | this->emitter->setSystem(NULL); |
---|
127 | this->lifeCycle = 0.0; |
---|
128 | |
---|
129 | this->toList(OM_DEAD); |
---|
130 | this->removeNode(); |
---|
131 | SwarmProjectile::fastFactory->kill(this); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | void SwarmProjectile::hit (WorldEntity* entity, float damage) |
---|
136 | { |
---|
137 | if (this->hitEntity != entity) |
---|
138 | this->destroy( entity ); |
---|
139 | this->hitEntity = entity; |
---|
140 | //dynamic_cast<SpaceShip*>(entity)->damage(this->getPhysDamage(),this->getElecDamage()); |
---|
141 | // this->destroy(this); |
---|
142 | this->deactivate(); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void SwarmProjectile::setTarget(PNode* target) |
---|
147 | { |
---|
148 | this->target = target; |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | /** |
---|
154 | * this function gets called by tick to calculate the new flight direction |
---|
155 | * @param curDirection direction vector |
---|
156 | * @param estTargetDir target vector, pointing to where the target will be on hit |
---|
157 | * @param angle = tick * turningSpeed |
---|
158 | * @return (new) direction vector |
---|
159 | */ |
---|
160 | Vector SwarmProjectile::newDirection(Vector curDirection, Vector estTargetDir, float angle) |
---|
161 | { |
---|
162 | if (unlikely(curDirection.len() == 0)) |
---|
163 | return curDirection; |
---|
164 | //printf("recalculating direction\n"); |
---|
165 | float tmp = angleRad ( curDirection, estTargetDir); |
---|
166 | if ( unlikely(tmp == 0) ) { return curDirection * maxVelocity / curDirection.len(); } |
---|
167 | // printf("turning angle: %f\ntemp: %f\n", angle, tmp); |
---|
168 | |
---|
169 | if( fabsf(angle) > fabsf(tmp) ) |
---|
170 | angle = tmp; |
---|
171 | else |
---|
172 | angle *= tmp/fabsf(tmp); |
---|
173 | |
---|
174 | Vector d = curDirection.cross(estTargetDir).cross(curDirection); |
---|
175 | d.normalize(); |
---|
176 | if( unlikely( fabsf(angle) == 90)) { return d; } //avoid complication |
---|
177 | |
---|
178 | Vector newDir = curDirection + d * curDirection.len() * tan (angle); |
---|
179 | newDir.normalize(); |
---|
180 | newDir *= curDirection.len(); |
---|
181 | return newDir; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | /** |
---|
188 | * signal tick, time dependent things will be handled here |
---|
189 | * @param time since last tick |
---|
190 | */ |
---|
191 | void SwarmProjectile::tick (float time) |
---|
192 | { |
---|
193 | if(unlikely(this->target == NULL)) /** Check whether the target still exists*/ |
---|
194 | this->deactivate(); |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | /** old guiding function*/ |
---|
199 | |
---|
200 | float projectileVelocity = this->getVelocity().len(); |
---|
201 | if (target != NULL){ |
---|
202 | Vector estTargetDir = (this->target->getAbsCoor() - this->getAbsCoor()).getNormalized(); |
---|
203 | this->velocity = this->newDirection(this->velocity, estTargetDir, this->turningSpeed * time ); |
---|
204 | } |
---|
205 | else |
---|
206 | if (likely(projectileVelocity != 0 || projectileVelocity != this->maxVelocity) ) |
---|
207 | this->velocity *= (this->maxVelocity / projectileVelocity); // set speed to max |
---|
208 | /* |
---|
209 | printf("position: %f, %f, %f\n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z); |
---|
210 | printf("target position: %f, %f, %f\n", this->target->getAbsCoor().x, this->target->getAbsCoor().y, this->target->getAbsCoor().z);*/ |
---|
211 | |
---|
212 | this->shiftCoor(this->velocity * time); |
---|
213 | |
---|
214 | if(this->tickLifeCycle(time)) |
---|
215 | this->deactivate(); |
---|
216 | |
---|
217 | this->trail->tick(time); |
---|
218 | |
---|
219 | this->angle += this->rotationSpeed * time; |
---|
220 | while (this->angle > 360) |
---|
221 | this->angle -= 360; |
---|
222 | |
---|
223 | this->lastDir = this->curDir; |
---|
224 | this->curDir = this->velocity; |
---|
225 | |
---|
226 | if( this->target != NULL && (this->getAbsCoor() - this->target->getAbsCoor()).len() < 3) // FIXME Temp fake workaround for collision :) |
---|
227 | { |
---|
228 | dynamic_cast<WorldEntity*>(target)->hit(this->getDamage(), this); |
---|
229 | this->deactivate(); |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * the function gets called, when the projectile is destroyed |
---|
235 | */ |
---|
236 | void SwarmProjectile::destroy (WorldEntity* killer) |
---|
237 | { |
---|
238 | |
---|
239 | // printf("THIS SHOULD WORK!\n"); |
---|
240 | |
---|
241 | Projectile::destroy( killer ); |
---|
242 | PRINTF(5)("DESTROY SwarmProjectile\n"); |
---|
243 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
244 | this->emitter->setSystem(SwarmProjectile::explosionParticles); |
---|
245 | |
---|
246 | this->emitter->setEmissionRate(1000.0); |
---|
247 | this->emitter->setEmissionVelocity(50.0); |
---|
248 | this->deactivate(); |
---|
249 | |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | void SwarmProjectile::draw () const |
---|
254 | { |
---|
255 | glMatrixMode(GL_MODELVIEW); |
---|
256 | glPushMatrix(); |
---|
257 | |
---|
258 | Vector tmpDir = this->curDir; // *.7 + this->lastDir * .3; |
---|
259 | tmpDir.slerpTo(this->lastDir, .4); |
---|
260 | |
---|
261 | float matrix[4][4]; |
---|
262 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
263 | Vector tmpRot = this->getAbsCoor().cross(tmpDir); |
---|
264 | glRotatef (angleRad ( this->getAbsCoor(), tmpDir), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
265 | glRotatef(this->angle, 1.0f, 0.0f, 0.0f); //spinning missile |
---|
266 | this->getAbsDir().matrix (matrix); |
---|
267 | glMultMatrixf((float*)matrix); |
---|
268 | //glScalef(2.0, 2.0, 2.0); // no double rescale |
---|
269 | this->getModel()->draw(); |
---|
270 | glTranslatef(-.9,0,0); |
---|
271 | this->trail->draw(); |
---|
272 | glPopMatrix(); |
---|
273 | } |
---|