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 | CloudEffect::flashSky(0.02); |
---|
202 | |
---|
203 | // Play thunder sound |
---|
204 | this->soundSource.play(this->thunderBuffer); |
---|
205 | } |
---|
206 | |
---|
207 | if( this->thunderBolt[0]->isVisible() && this->time > this->flashRisingTime*1/3 ) { |
---|
208 | this->thunderBolt[0]->setVisibiliy(false); |
---|
209 | this->thunderBolt[1]->setVisibiliy(true); |
---|
210 | } else if( this->thunderBolt[1]->isVisible() && this->time > this->flashRisingTime*2/3 ) { |
---|
211 | this->thunderBolt[1]->setVisibiliy(false); |
---|
212 | this->thunderBolt[2]->setVisibiliy(true); |
---|
213 | |
---|
214 | } else if( this->thunderBolt[2]->isVisible() && this->time > this->flashRisingTime) { |
---|
215 | this->thunderBolt[2]->setVisibiliy(false); |
---|
216 | this->thunderBolt[3]->setVisibiliy(true); |
---|
217 | } |
---|
218 | |
---|
219 | if( this->thunderBolt[3]->isVisible() && this->time > this->flashHoldTime) { |
---|
220 | this->thunderBolt[3]->setVisibiliy(false); |
---|
221 | |
---|
222 | this->time = 0.0f; |
---|
223 | this->flashLight->setDiffuseColor(0,0,0); |
---|
224 | this->flashLight->setSpecularColor(0,0,0); |
---|
225 | CloudEffect::changeSkyColor(Vector(0,0,1), 0.02); |
---|
226 | |
---|
227 | this->newCoordinates(); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | void LightningEffect::newCoordinates() { |
---|
232 | |
---|
233 | float posX, posZ; |
---|
234 | |
---|
235 | if(this->lightningMove) { |
---|
236 | this->cameraCoor = State::getCameraNode()->getAbsCoor(); |
---|
237 | posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX + this->cameraCoor.x; |
---|
238 | posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX + this->cameraCoor.z; |
---|
239 | } else { |
---|
240 | posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX; |
---|
241 | posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX; |
---|
242 | } |
---|
243 | |
---|
244 | this->switchTexture(); |
---|
245 | |
---|
246 | int i; |
---|
247 | for (i = 0; i < 4; i++) |
---|
248 | this->thunderBolt[i]->setAbsCoor(posX, this->mainPosY, posZ); |
---|
249 | |
---|
250 | this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX; |
---|
251 | |
---|
252 | float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX; |
---|
253 | float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX; |
---|
254 | |
---|
255 | for (i = 0; i < 4; i++) |
---|
256 | this->thunderBolt[i]->setSize(w, h); |
---|
257 | |
---|
258 | } |
---|
259 | |
---|
260 | void LightningEffect::setTexture() { |
---|
261 | |
---|
262 | if (this->thunderTextureA) { |
---|
263 | this->thunderBolt[0]->setTexture("maps/thunderbA1.png"); |
---|
264 | this->thunderBolt[1]->setTexture("maps/thunderbA2.png"); |
---|
265 | this->thunderBolt[2]->setTexture("maps/thunderbA3.png"); |
---|
266 | this->thunderBolt[3]->setTexture("maps/thunderbA4.png"); |
---|
267 | } |
---|
268 | else { |
---|
269 | this->thunderBolt[0]->setTexture("maps/thunderbB1.png"); |
---|
270 | this->thunderBolt[1]->setTexture("maps/thunderbB2.png"); |
---|
271 | this->thunderBolt[2]->setTexture("maps/thunderbB3.png"); |
---|
272 | this->thunderBolt[3]->setTexture("maps/thunderbB4.png"); |
---|
273 | } |
---|
274 | |
---|
275 | } |
---|
276 | |
---|
277 | void LightningEffect::switchTexture() { |
---|
278 | |
---|
279 | if (this->thunderTextureA) |
---|
280 | this->thunderTextureA = false; |
---|
281 | else |
---|
282 | this->thunderTextureA = true; |
---|
283 | |
---|
284 | this->setTexture(); |
---|
285 | |
---|
286 | } |
---|