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