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