Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/lib/graphics/effects/lightning_effect.cc @ 10763

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

merged the mountain-lake back here

File size: 8.3 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    this->thunderTextureA = true;
115    this->setTexture();
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    if (this->thunderBuffer != NULL)
131        ResourceManager::getInstance()->unload(this->thunderBuffer);
132    this->thunderBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/thunder.wav", WAV);
133
134}
135
136void LightningEffect::coord(float x, float y, float z) {
137    int i;
138
139    if (this->lightningMove) {
140        this->cameraCoor = State::getCameraNode()->getAbsCoor();
141        for (i = 0; i < 4; i++)
142            this->thunderBolt[i]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
143    } else
144        for (i = 0; i < 4; i++)
145            this->thunderBolt[i]->setAbsCoor(x, y, z);
146
147    this->mainPosX = x;
148    this->mainPosY = y;
149    this->mainPosZ = z;
150}
151
152
153void LightningEffect::setFlashSize(float width, float height, float seedWidth, float seedHeight) {
154    this->width = width;
155    this->height = height;
156    this->seedWidth = seedWidth;
157    this->seedHeight = seedHeight;
158
159    int i;
160    for (i = 0; i < 4; i++)
161        this->thunderBolt[i]->setSize(this->width, this->height);
162
163}
164
165
166void LightningEffect::activate() {
167    PRINTF(3)( "Activating LightningEffect\n" );
168    this->lightningActivate = true;
169
170    this->time = 0;
171}
172
173
174void LightningEffect::deactivate() {
175    PRINTF(3)("Deactivating LightningEffect\n");
176    this->lightningActivate = false;
177
178    int i;
179    for (i = 0; i < 4; i++)
180        this->thunderBolt[i]->setVisibiliy(false);
181
182}
183
184void LightningEffect::tick (float dt) {
185    if(!lightningActivate)
186        return;
187
188    this->time += dt;
189
190    float x;
191    x = (float)rand()/(float)RAND_MAX;
192
193    if( this->time > this->flashFrequency) {
194        // Reset timer
195        this->time = 0.0f;
196
197        // Move thunderBolt to start position
198        this->flashLight->setAbsCoor(this->thunderBolt[0]->getAbsCoor().x, this->thunderBolt[0]->getAbsCoor().y, this->thunderBolt[0]->getAbsCoor().z);
199
200        // Start a flash & lightning cycle
201        this->thunderBolt[0]->setVisibiliy(true);
202
203        // Lighten up environment
204        this->flashLight->setDiffuseColor(1,1,1);
205        this->flashLight->setSpecularColor(1,1,1);
206
207
208        // Play thunder sound
209        this->soundSource.play(this->thunderBuffer);
210    }
211
212    if( this->thunderBolt[0]->isVisible() && this->time > this->flashRisingTime*1/3 ) {
213        this->thunderBolt[0]->setVisibiliy(false);
214        this->thunderBolt[1]->setVisibiliy(true);
215    } else if( this->thunderBolt[1]->isVisible() && this->time > this->flashRisingTime*2/3 ) {
216        this->thunderBolt[1]->setVisibiliy(false);
217        this->thunderBolt[2]->setVisibiliy(true);
218
219    } else if( this->thunderBolt[2]->isVisible() && this->time > this->flashRisingTime) {
220        this->thunderBolt[2]->setVisibiliy(false);
221        this->thunderBolt[3]->setVisibiliy(true);
222    }
223
224    if( this->thunderBolt[3]->isVisible() && this->time > this->flashHoldTime) {
225        this->thunderBolt[3]->setVisibiliy(false);
226
227        this->time = 0.0f;
228        this->flashLight->setDiffuseColor(0,0,0);
229        this->flashLight->setSpecularColor(0,0,0);
230
231        this->newCoordinates();
232    }
233}
234
235void LightningEffect::newCoordinates() {
236
237    float posX, posZ;
238
239    if(this->lightningMove) {
240        this->cameraCoor = State::getCameraNode()->getAbsCoor();
241        posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX + this->cameraCoor.x;
242        posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX + this->cameraCoor.z;
243    } else {
244        posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX;
245        posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX;
246    }
247
248    this->switchTexture();
249
250    int i;
251    for (i = 0; i < 4; i++)
252        this->thunderBolt[i]->setAbsCoor(posX, this->mainPosY, posZ);
253
254    this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX;
255
256    float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX;
257    float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX;
258
259    for (i = 0; i < 4; i++)
260        this->thunderBolt[i]->setSize(w, h);
261
262}
263
264void LightningEffect::setTexture() {
265
266  if (this->thunderTextureA) {
267    this->thunderBolt[0]->setTexture("maps/thunderbA1.png");
268    this->thunderBolt[1]->setTexture("maps/thunderbA2.png");
269    this->thunderBolt[2]->setTexture("maps/thunderbA3.png");
270    this->thunderBolt[3]->setTexture("maps/thunderbA4.png");
271  }
272  else {
273    this->thunderBolt[0]->setTexture("maps/thunderbB1.png");
274    this->thunderBolt[1]->setTexture("maps/thunderbB2.png");
275    this->thunderBolt[2]->setTexture("maps/thunderbB3.png");
276    this->thunderBolt[3]->setTexture("maps/thunderbB4.png");
277  }
278
279}
280
281void LightningEffect::switchTexture() {
282
283  if (this->thunderTextureA)
284    this->thunderTextureA = false;
285  else
286    this->thunderTextureA = true;
287
288  this->setTexture();
289
290}
Note: See TracBrowser for help on using the repository browser.