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: Patrick Boenzli |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | |
---|
15 | */ |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "test_bullet.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 | using namespace std; |
---|
33 | |
---|
34 | CREATE_FAST_FACTORY_STATIC(TestBullet, CL_TEST_BULLET); |
---|
35 | |
---|
36 | /** |
---|
37 | * standard constructor |
---|
38 | */ |
---|
39 | TestBullet::TestBullet () : Projectile() |
---|
40 | { |
---|
41 | this->setClassID(CL_TEST_BULLET, "TestBullet"); |
---|
42 | |
---|
43 | float modelSize = .3; |
---|
44 | this->loadModel("models/projectiles/orx-rocket.obj", .3); |
---|
45 | |
---|
46 | this->energyMin = 1; |
---|
47 | this->energyMax = 10; |
---|
48 | this->lifeSpan = 2; |
---|
49 | |
---|
50 | this->emitter = new ParticleEmitter(Vector(0,1,0), M_2_PI, 100, 5); |
---|
51 | this->emitter->setParent(this); |
---|
52 | this->emitter->setSpread(M_PI, M_PI); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * standard deconstructor |
---|
58 | */ |
---|
59 | TestBullet::~TestBullet () |
---|
60 | { |
---|
61 | // delete this->emitter; |
---|
62 | |
---|
63 | /* this is normaly done by World.cc by deleting the ParticleEngine */ |
---|
64 | if (TestBullet::trailParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1) |
---|
65 | { |
---|
66 | if (ClassList::exists(TestBullet::trailParticles, CL_PARTICLE_SYSTEM)) |
---|
67 | delete TestBullet::trailParticles; |
---|
68 | TestBullet::trailParticles = NULL; |
---|
69 | } |
---|
70 | if (TestBullet::explosionParticles != NULL && ClassList::getList(CL_TEST_BULLET)->size() <= 1) |
---|
71 | { |
---|
72 | if (ClassList::exists(TestBullet::explosionParticles, CL_PARTICLE_SYSTEM)) |
---|
73 | delete TestBullet::explosionParticles; |
---|
74 | TestBullet::explosionParticles = NULL; |
---|
75 | } |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | ParticleSystem* TestBullet::trailParticles = NULL; |
---|
80 | ParticleSystem* TestBullet::explosionParticles = NULL; |
---|
81 | |
---|
82 | void TestBullet::activate() |
---|
83 | { |
---|
84 | State::getWorldEntityList()->add(this); |
---|
85 | if (unlikely(TestBullet::trailParticles == NULL)) |
---|
86 | { |
---|
87 | TestBullet::trailParticles = new ParticleSystem(1000, PARTICLE_SPRITE); |
---|
88 | TestBullet::trailParticles->setName("TestBulletTrailParticles"); |
---|
89 | TestBullet::trailParticles->setLifeSpan(.5, .3); |
---|
90 | TestBullet::trailParticles->setRadius(0.0, .5); |
---|
91 | TestBullet::trailParticles->setRadius(0.5, 2.0); |
---|
92 | TestBullet::trailParticles->setRadius(1.0, 5.0); |
---|
93 | TestBullet::trailParticles->setColor(0.0, 1,0,0,.7); |
---|
94 | TestBullet::trailParticles->setColor(0.5, .8,.8,0,.5); |
---|
95 | TestBullet::trailParticles->setColor(1.0, .7,.7,.7,.0); |
---|
96 | } |
---|
97 | if (unlikely(TestBullet::explosionParticles == NULL)) |
---|
98 | { |
---|
99 | TestBullet::explosionParticles = new ParticleSystem(1000, PARTICLE_SPRITE); |
---|
100 | TestBullet::explosionParticles->setName("TestBulletExplosionParticles"); |
---|
101 | TestBullet::explosionParticles->setLifeSpan(.5, .3); |
---|
102 | TestBullet::explosionParticles->setRadius(0.0, 10); |
---|
103 | TestBullet::explosionParticles->setRadius(.5, 20.0); |
---|
104 | TestBullet::explosionParticles->setRadius(1.0, 3.0); |
---|
105 | TestBullet::explosionParticles->setColor(0.0, 0,1,0,.9); |
---|
106 | TestBullet::explosionParticles->setColor(0.5, .8,.8,0,.5); |
---|
107 | TestBullet::explosionParticles->setColor(1.0, 1,1,1,.0); |
---|
108 | } |
---|
109 | |
---|
110 | ParticleEngine::getInstance()->addConnection(this->emitter, TestBullet::trailParticles); |
---|
111 | |
---|
112 | this->emitter->setEmissionRate(20.0); |
---|
113 | this->emitter->setEmissionVelocity(3.0); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | void TestBullet::deactivate() |
---|
118 | { |
---|
119 | ParticleEngine::getInstance()->breakConnections(this->emitter); |
---|
120 | this->lifeCycle = 0.0; |
---|
121 | |
---|
122 | // GarbageCollector::getInstance()->collect(this); |
---|
123 | State::getWorldEntityList()->remove(this); |
---|
124 | TestBullet::fastFactory->kill(this); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void TestBullet::collidesWith(WorldEntity* entity, const Vector& location) |
---|
129 | { |
---|
130 | if (this->hitEntity != entity && entity->isA(CL_NPC)) |
---|
131 | this->destroy(); |
---|
132 | this->hitEntity = entity; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * signal tick, time dependent things will be handled here |
---|
137 | * @param time since last tick |
---|
138 | */ |
---|
139 | void TestBullet::tick (float time) |
---|
140 | { |
---|
141 | //Vector v = *this->flightDirection * ( this->speed * time * 1000 + 0.1); |
---|
142 | Vector v = this->velocity * (time); |
---|
143 | this->shiftCoor(v); |
---|
144 | |
---|
145 | this->lifeCycle += time/this->lifeSpan; |
---|
146 | if( this->lifeCycle >= 1.0) |
---|
147 | { |
---|
148 | PRINTF(5)("FINALIZE==========================\n"); |
---|
149 | PRINTF(5)("current life cycle is: %f\n", this->lifeCycle); |
---|
150 | PRINTF(5)("FINALIZE===========================\n"); |
---|
151 | |
---|
152 | this->deactivate(); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * the function gets called, when the projectile is destroyed |
---|
158 | */ |
---|
159 | void TestBullet::destroy () |
---|
160 | { |
---|
161 | PRINTF(5)("DESTROY TestBullet\n"); |
---|
162 | this->lifeCycle = .95; //!< @todo calculate this usefully. |
---|
163 | ParticleEngine::getInstance()->breakConnection(this->emitter, TestBullet::trailParticles); |
---|
164 | ParticleEngine::getInstance()->addConnection(this->emitter, TestBullet::explosionParticles); |
---|
165 | |
---|
166 | this->emitter->setEmissionRate(30.0); |
---|
167 | this->emitter->setEmissionVelocity(50.0); |
---|
168 | // this->deactivate(); |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | void TestBullet::draw () const |
---|
174 | { |
---|
175 | glMatrixMode(GL_MODELVIEW); |
---|
176 | glPushMatrix(); |
---|
177 | |
---|
178 | float matrix[4][4]; |
---|
179 | glTranslatef (this->getAbsCoor ().x, this->getAbsCoor ().y, this->getAbsCoor ().z); |
---|
180 | this->getAbsDir().matrix (matrix); |
---|
181 | glMultMatrixf((float*)matrix); |
---|
182 | glScalef(2.0, 2.0, 2.0); |
---|
183 | this->getModel()->draw(); |
---|
184 | |
---|
185 | glPopMatrix(); |
---|
186 | } |
---|
187 | |
---|