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: Silvan Nellen |
---|
13 | co-programmer: |
---|
14 | |
---|
15 | */ |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "guided_missile.h" |
---|
19 | |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | #include "particles/dot_emitter.h" |
---|
23 | #include "particles/sprite_particles.h" |
---|
24 | |
---|
25 | #include "debug.h" |
---|
26 | |
---|
27 | |
---|
28 | ObjectListDefinition(GuidedMissile); |
---|
29 | CREATE_FAST_FACTORY_STATIC(GuidedMissile); |
---|
30 | |
---|
31 | /** |
---|
32 | * standard constructor |
---|
33 | */ |
---|
34 | GuidedMissile::GuidedMissile () : Projectile() |
---|
35 | { |
---|
36 | this->registerObject(this, GuidedMissile::_objectList); |
---|
37 | |
---|
38 | this->loadModel("models/projectiles/orx-rocket.obj", 2.0); |
---|
39 | this->loadExplosionSound("sounds/explosions/explosion_4.wav"); |
---|
40 | |
---|
41 | |
---|
42 | this->setMinEnergy(1); |
---|
43 | this->setHealthMax(10); |
---|
44 | this->lifeSpan = 4.0; |
---|
45 | this->agility = 3.5; |
---|
46 | this->maxVelocity = 100; |
---|
47 | |
---|
48 | this->emitter = new DotEmitter(100, 5, M_2_PI); |
---|
49 | this->emitter->setParent(this); |
---|
50 | this->emitter->setSpread(M_PI, M_PI); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * standard deconstructor |
---|
56 | */ |
---|
57 | GuidedMissile::~GuidedMissile () |
---|
58 | { |
---|
59 | // delete this->emitter; |
---|
60 | |
---|
61 | /* this is normaly done by World.cc by deleting the ParticleEngine */ |
---|
62 | if (GuidedMissile::trailParticles != NULL && GuidedMissile::objectList().size() <= 1) |
---|
63 | { |
---|
64 | if (ParticleSystem::objectList().exists(GuidedMissile::trailParticles)) |
---|
65 | delete GuidedMissile::trailParticles; |
---|
66 | GuidedMissile::trailParticles = NULL; |
---|
67 | } |
---|
68 | if (GuidedMissile::explosionParticles != NULL && GuidedMissile::objectList().size() <= 1) |
---|
69 | { |
---|
70 | if (ParticleSystem::objectList().exists(GuidedMissile::explosionParticles)) |
---|
71 | delete GuidedMissile::explosionParticles; |
---|
72 | GuidedMissile::explosionParticles = NULL; |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | SpriteParticles* GuidedMissile::trailParticles = NULL; |
---|
78 | SpriteParticles* GuidedMissile::explosionParticles = NULL; |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | void GuidedMissile::activate() |
---|
83 | { |
---|
84 | if (unlikely(GuidedMissile::trailParticles == NULL)) |
---|
85 | { |
---|
86 | GuidedMissile::trailParticles = new SpriteParticles(2000); |
---|
87 | GuidedMissile::trailParticles->setName("GuidedMissileTrailParticles"); |
---|
88 | GuidedMissile::trailParticles->setMaterialTexture("textures/radial-trans-noise.png"); |
---|
89 | GuidedMissile::trailParticles->setLifeSpan(1.0, .3); |
---|
90 | GuidedMissile::trailParticles->setRadius(0.0, .5); |
---|
91 | GuidedMissile::trailParticles->setRadius(0.2, 4.0); |
---|
92 | GuidedMissile::trailParticles->setRadius(.5, 1.5); |
---|
93 | GuidedMissile::trailParticles->setRadius(1.0, 1.5); |
---|
94 | GuidedMissile::trailParticles->setColor(0.0, 1,0,0,.7); |
---|
95 | GuidedMissile::trailParticles->setColor(0.2, .8,.8,0,.5); |
---|
96 | GuidedMissile::trailParticles->setColor(0.5, .8,.8,.8,.8); |
---|
97 | GuidedMissile::trailParticles->setColor(1.0, .8,.8,.8,.0); |
---|
98 | } |
---|
99 | if (unlikely(GuidedMissile::explosionParticles == NULL)) |
---|
100 | { |
---|
101 | GuidedMissile::explosionParticles = new SpriteParticles(200); |
---|
102 | GuidedMissile::explosionParticles->setName("GuidedMissileExplosionParticles"); |
---|
103 | GuidedMissile::explosionParticles->setMaterialTexture("textures/radial-trans-noise.png"); |
---|
104 | GuidedMissile::explosionParticles->setLifeSpan(.5, .3); |
---|
105 | GuidedMissile::explosionParticles->setRadius(0.0, 10); |
---|
106 | GuidedMissile::explosionParticles->setRadius(.5, 15.0); |
---|
107 | GuidedMissile::explosionParticles->setRadius(1.0, 10.0); |
---|
108 | GuidedMissile::explosionParticles->setColor(0.0, 0,1,0,1); |
---|
109 | GuidedMissile::explosionParticles->setColor(0.5, .8,.8,0,.8); |
---|
110 | GuidedMissile::explosionParticles->setColor(0.8, .8,.8,.3,.8); |
---|
111 | GuidedMissile::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
112 | } |
---|
113 | |
---|
114 | this->emitter->setSystem(GuidedMissile::trailParticles); |
---|
115 | |
---|
116 | this->updateNode(0); |
---|
117 | this->emitter->setEmissionRate(45.0); |
---|
118 | this->emitter->setEmissionVelocity(0.0); |
---|
119 | |
---|
120 | this->setHealth(10.0* (float)rand()/(float)RAND_MAX); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void GuidedMissile::deactivate() |
---|
125 | { |
---|
126 | this->emitter->setSystem(NULL); |
---|
127 | this->lifeCycle = 0.0; |
---|
128 | |
---|
129 | this->toList(OM_DEAD); |
---|
130 | this->removeNode(); |
---|
131 | GuidedMissile::fastFactory->kill(this); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | void GuidedMissile::collidesWith(WorldEntity* entity, const Vector& location) |
---|
136 | { |
---|
137 | if (this->hitEntity != entity) |
---|
138 | this->destroy( entity ); |
---|
139 | this->hitEntity = entity; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * signal tick, time dependent things will be handled here |
---|
144 | * @param time since last tick |
---|
145 | */ |
---|
146 | void GuidedMissile::tick (float time) |
---|
147 | { |
---|
148 | |
---|
149 | |
---|
150 | if (this->target != NULL && this->target->getParent() != PNode::getNullParent()) |
---|
151 | { |
---|
152 | speed = velocity.len(); |
---|
153 | diffVector = ((target->getAbsCoor() - this->getAbsCoor()).getNormalized()); |
---|
154 | |
---|
155 | if(velocity.dot(diffVector) != 0) |
---|
156 | { |
---|
157 | correctionVector = (( ( diffVector * (speed * speed/( velocity.dot(diffVector ) ) )) - velocity).getNormalized()) * agility; |
---|
158 | |
---|
159 | if( (diffVector * (speed * speed/( velocity.dot(diffVector ) ) ) -velocity).len() < agility ) |
---|
160 | velocity = ((diffVector * (speed * speed/( velocity.dot(diffVector ) ) )).getNormalized())*agility; |
---|
161 | else if(velocity.dot(diffVector) > 0) |
---|
162 | velocity += correctionVector; |
---|
163 | else if (velocity.dot(diffVector) < 0) |
---|
164 | velocity -= correctionVector; |
---|
165 | } |
---|
166 | else |
---|
167 | velocity += diffVector * agility; |
---|
168 | |
---|
169 | this->setAbsDir(Quaternion(velocity, Vector(0,1,0)) * Quaternion ( -M_PI_2, Vector(0,1,0))); |
---|
170 | } |
---|
171 | |
---|
172 | velocity *= maxVelocity/velocity.len(); |
---|
173 | Vector v = this->velocity * (time); |
---|
174 | this->shiftCoor(v); |
---|
175 | |
---|
176 | if(this->tickLifeCycle(time)) |
---|
177 | this->deactivate(); |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * the function gets called, when the projectile is destroyed |
---|
182 | */ |
---|
183 | void GuidedMissile::destroy (WorldEntity* killer) |
---|
184 | { |
---|
185 | |
---|
186 | printf("THIS SHOULD WORLk\n"); |
---|
187 | |
---|
188 | Projectile::destroy( killer ); |
---|
189 | PRINTF(5)("DESTROY GuidedMissile\n"); |
---|
190 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
191 | this->emitter->setSystem(GuidedMissile::explosionParticles); |
---|
192 | |
---|
193 | this->emitter->setEmissionRate(1000.0); |
---|
194 | this->emitter->setEmissionVelocity(50.0); |
---|
195 | // this->deactivate(); |
---|
196 | |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | void GuidedMissile::draw () const |
---|
201 | { |
---|
202 | glMatrixMode(GL_MODELVIEW); |
---|
203 | glPushMatrix(); |
---|
204 | |
---|
205 | float matrix[4][4]; |
---|
206 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
207 | this->getAbsDir().matrix (matrix); |
---|
208 | glMultMatrixf((float*)matrix); |
---|
209 | glScalef(2.0, 2.0, 2.0); |
---|
210 | this->getModel()->draw(); |
---|
211 | |
---|
212 | glPopMatrix(); |
---|
213 | |
---|
214 | } |
---|
215 | |
---|