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: hdavid, amaechler |
---|
13 | */ |
---|
14 | |
---|
15 | #include "lightning_effect.h" |
---|
16 | |
---|
17 | #include "state.h" |
---|
18 | |
---|
19 | #include "util/loading/load_param.h" |
---|
20 | #include "util/loading/factory.h" |
---|
21 | #include "util/loading/resource_manager.h" |
---|
22 | |
---|
23 | #include "effects/billboard.h" |
---|
24 | |
---|
25 | #include "glincl.h" |
---|
26 | #include "parser/tinyxml/tinyxml.h" |
---|
27 | |
---|
28 | #include "shell_command.h" |
---|
29 | #include "light.h" |
---|
30 | #include "cloud_effect.h" |
---|
31 | |
---|
32 | SHELL_COMMAND(activate, LightningEffect, activateLightning); |
---|
33 | SHELL_COMMAND(deactivate, LightningEffect, deactivateLightning); |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | CREATE_FACTORY(LightningEffect, CL_LIGHTNING_EFFECT); |
---|
38 | |
---|
39 | LightningEffect::LightningEffect(const TiXmlElement* root) { |
---|
40 | this->setClassID(CL_LIGHTNING_EFFECT, "LightningEffect"); |
---|
41 | |
---|
42 | this->init(); |
---|
43 | |
---|
44 | if (root != NULL) |
---|
45 | this->loadParams(root); |
---|
46 | |
---|
47 | if(this->lightningActivate) |
---|
48 | this->activate(); |
---|
49 | } |
---|
50 | |
---|
51 | LightningEffect::~LightningEffect() { |
---|
52 | this->deactivate(); |
---|
53 | } |
---|
54 | |
---|
55 | void LightningEffect::loadParams(const TiXmlElement* root) { |
---|
56 | WeatherEffect::loadParams(root); |
---|
57 | |
---|
58 | LoadParam(root, "coord", this, LightningEffect, coord); |
---|
59 | LoadParam(root, "frequency", this, LightningEffect, setFlashFrequency); |
---|
60 | LoadParam(root, "const-time", this, LightningEffect, setFlashConstTime); |
---|
61 | LoadParam(root, "rising-time", this, LightningEffect, setFlashRisingTime); |
---|
62 | LoadParam(root, "size", this, LightningEffect, setFlashSize); |
---|
63 | LoadParam(root, "seed", this, LightningEffect, setFlashSeed); |
---|
64 | |
---|
65 | LOAD_PARAM_START_CYCLE(root, element); |
---|
66 | { |
---|
67 | LoadParam_CYCLE(element, "option", this, LightningEffect, setLightningOption); |
---|
68 | } |
---|
69 | LOAD_PARAM_END_CYCLE(element); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | void LightningEffect::init() { |
---|
74 | //default values |
---|
75 | this->lightningActivate = false; |
---|
76 | |
---|
77 | this->flashFrequency = 4.0f; |
---|
78 | this->flashFrequencyBase = 4.0f; |
---|
79 | this->flashFrequencySeed = 2.0f; |
---|
80 | |
---|
81 | this->flashHoldTime = 0.1f; |
---|
82 | this->flashRisingTime = 0.03f; |
---|
83 | |
---|
84 | this->seedX = 500.f; |
---|
85 | this->seedZ = 1000.0f; |
---|
86 | |
---|
87 | this->width = 640.0f; |
---|
88 | this->height = 400.0f; |
---|
89 | this->seedWidth = 100.0f; |
---|
90 | this->seedHeight = 100.0f; |
---|
91 | |
---|
92 | this->lightningMove = false; |
---|
93 | |
---|
94 | this->mainPosX = 0; |
---|
95 | this->mainPosY = 100; |
---|
96 | this->mainPosZ = 5; |
---|
97 | |
---|
98 | this->time = 0.0; |
---|
99 | |
---|
100 | // initialize thunder billboard |
---|
101 | int i; |
---|
102 | for (i = 0; i < 4; i++) { |
---|
103 | this->thunderBolt[i] = new Billboard(NULL); |
---|
104 | this->thunderBolt[i]->setSize(this->width, this->height); |
---|
105 | this->thunderBolt[i]->setVisibiliy(false); |
---|
106 | } |
---|
107 | |
---|
108 | this->thunderTextureA = true; |
---|
109 | this->setTexture(); |
---|
110 | |
---|
111 | if (this->lightningMove) { |
---|
112 | this->cameraCoor = State::getCameraNode()->getAbsCoor(); |
---|
113 | for (i = 0; i < 4; i++) |
---|
114 | this->thunderBolt[i]->setAbsCoor(this->cameraCoor.x + this->mainPosX, this->mainPosY, this->cameraCoor.z + this->mainPosZ); |
---|
115 | } else |
---|
116 | for (i = 0; i < 4; i++) |
---|
117 | this->thunderBolt[i]->setAbsCoor(this->mainPosX, this->mainPosY, this->mainPosZ); |
---|
118 | |
---|
119 | this->flashLight = new Light(); |
---|
120 | this->flashLight->setDiffuseColor(0,0,0); |
---|
121 | this->flashLight->setSpecularColor(0,0,0); |
---|
122 | |
---|
123 | //load sound |
---|
124 | if (this->thunderBuffer != NULL) |
---|
125 | ResourceManager::getInstance()->unload(this->thunderBuffer); |
---|
126 | this->thunderBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/thunder.wav", WAV); |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | void LightningEffect::coord(float x, float y, float z) { |
---|
131 | int i; |
---|
132 | |
---|
133 | if (this->lightningMove) { |
---|
134 | this->cameraCoor = State::getCameraNode()->getAbsCoor(); |
---|
135 | for (i = 0; i < 4; i++) |
---|
136 | this->thunderBolt[i]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z); |
---|
137 | } else |
---|
138 | for (i = 0; i < 4; i++) |
---|
139 | this->thunderBolt[i]->setAbsCoor(x, y, z); |
---|
140 | |
---|
141 | this->mainPosX = x; |
---|
142 | this->mainPosY = y; |
---|
143 | this->mainPosZ = z; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | void LightningEffect::setFlashSize(float width, float height, float seedWidth, float seedHeight) { |
---|
148 | this->width = width; |
---|
149 | this->height = height; |
---|
150 | this->seedWidth = seedWidth; |
---|
151 | this->seedHeight = seedHeight; |
---|
152 | |
---|
153 | int i; |
---|
154 | for (i = 0; i < 4; i++) |
---|
155 | this->thunderBolt[i]->setSize(this->width, this->height); |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | void LightningEffect::activate() { |
---|
161 | PRINTF(3)( "Activating LightningEffect\n" ); |
---|
162 | this->lightningActivate = true; |
---|
163 | |
---|
164 | this->time = 0; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void LightningEffect::deactivate() { |
---|
169 | PRINTF(3)("Deactivating LightningEffect\n"); |
---|
170 | this->lightningActivate = false; |
---|
171 | |
---|
172 | int i; |
---|
173 | for (i = 0; i < 4; i++) |
---|
174 | this->thunderBolt[i]->setVisibiliy(false); |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | void LightningEffect::tick (float dt) { |
---|
179 | if(!lightningActivate) |
---|
180 | return; |
---|
181 | |
---|
182 | this->time += dt; |
---|
183 | |
---|
184 | float x; |
---|
185 | x = (float)rand()/(float)RAND_MAX; |
---|
186 | |
---|
187 | if( this->time > this->flashFrequency) { |
---|
188 | // Reset timer |
---|
189 | this->time = 0.0f; |
---|
190 | |
---|
191 | // Move thunderBolt to start position |
---|
192 | this->flashLight->setAbsCoor(this->thunderBolt[0]->getAbsCoor().x, this->thunderBolt[0]->getAbsCoor().y, this->thunderBolt[0]->getAbsCoor().z); |
---|
193 | |
---|
194 | // Start a flash & lightning cycle |
---|
195 | this->thunderBolt[0]->setVisibiliy(true); |
---|
196 | |
---|
197 | // Lighten up environment |
---|
198 | this->flashLight->setDiffuseColor(1,1,1); |
---|
199 | this->flashLight->setSpecularColor(1,1,1); |
---|
200 | |
---|
201 | |
---|
202 | // Play thunder sound |
---|
203 | this->soundSource.play(this->thunderBuffer); |
---|
204 | } |
---|
205 | |
---|
206 | if( this->thunderBolt[0]->isVisible() && this->time > this->flashRisingTime*1/3 ) { |
---|
207 | this->thunderBolt[0]->setVisibiliy(false); |
---|
208 | this->thunderBolt[1]->setVisibiliy(true); |
---|
209 | } else if( this->thunderBolt[1]->isVisible() && this->time > this->flashRisingTime*2/3 ) { |
---|
210 | this->thunderBolt[1]->setVisibiliy(false); |
---|
211 | this->thunderBolt[2]->setVisibiliy(true); |
---|
212 | |
---|
213 | } else if( this->thunderBolt[2]->isVisible() && this->time > this->flashRisingTime) { |
---|
214 | this->thunderBolt[2]->setVisibiliy(false); |
---|
215 | this->thunderBolt[3]->setVisibiliy(true); |
---|
216 | } |
---|
217 | |
---|
218 | if( this->thunderBolt[3]->isVisible() && this->time > this->flashHoldTime) { |
---|
219 | this->thunderBolt[3]->setVisibiliy(false); |
---|
220 | |
---|
221 | this->time = 0.0f; |
---|
222 | this->flashLight->setDiffuseColor(0,0,0); |
---|
223 | this->flashLight->setSpecularColor(0,0,0); |
---|
224 | |
---|
225 | this->newCoordinates(); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | void LightningEffect::newCoordinates() { |
---|
230 | |
---|
231 | float posX, posZ; |
---|
232 | |
---|
233 | if(this->lightningMove) { |
---|
234 | this->cameraCoor = State::getCameraNode()->getAbsCoor(); |
---|
235 | posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX + this->cameraCoor.x; |
---|
236 | posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX + this->cameraCoor.z; |
---|
237 | } else { |
---|
238 | posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX; |
---|
239 | posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX; |
---|
240 | } |
---|
241 | |
---|
242 | this->switchTexture(); |
---|
243 | |
---|
244 | int i; |
---|
245 | for (i = 0; i < 4; i++) |
---|
246 | this->thunderBolt[i]->setAbsCoor(posX, this->mainPosY, posZ); |
---|
247 | |
---|
248 | this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX; |
---|
249 | |
---|
250 | float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX; |
---|
251 | float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX; |
---|
252 | |
---|
253 | for (i = 0; i < 4; i++) |
---|
254 | this->thunderBolt[i]->setSize(w, h); |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | void LightningEffect::setTexture() { |
---|
259 | |
---|
260 | if (this->thunderTextureA) { |
---|
261 | this->thunderBolt[0]->setTexture("maps/thunderbA1.png"); |
---|
262 | this->thunderBolt[1]->setTexture("maps/thunderbA2.png"); |
---|
263 | this->thunderBolt[2]->setTexture("maps/thunderbA3.png"); |
---|
264 | this->thunderBolt[3]->setTexture("maps/thunderbA4.png"); |
---|
265 | } |
---|
266 | else { |
---|
267 | this->thunderBolt[0]->setTexture("maps/thunderbB1.png"); |
---|
268 | this->thunderBolt[1]->setTexture("maps/thunderbB2.png"); |
---|
269 | this->thunderBolt[2]->setTexture("maps/thunderbB3.png"); |
---|
270 | this->thunderBolt[3]->setTexture("maps/thunderbB4.png"); |
---|
271 | } |
---|
272 | |
---|
273 | } |
---|
274 | |
---|
275 | void LightningEffect::switchTexture() { |
---|
276 | |
---|
277 | if (this->thunderTextureA) |
---|
278 | this->thunderTextureA = false; |
---|
279 | else |
---|
280 | this->thunderTextureA = true; |
---|
281 | |
---|
282 | this->setTexture(); |
---|
283 | |
---|
284 | } |
---|