Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more renamings

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