Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/lightning_effect.cc @ 9235

Last change on this file since 9235 was 9235, checked in by bensch, 18 years ago

merged the presentation back

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