Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mountain_lake/src/lib/graphics/effects/lightning_effect.cc @ 8835

Last change on this file since 8835 was 8835, checked in by amaechler, 18 years ago

branches/mountain_lake: lightening polished and extended

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