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