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