Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/effects/lightning_effect.cc @ 9686

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

new_class_id: many more classes done

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