Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/lightening_effect.cc @ 8937

Last change on this file since 8937 was 8793, checked in by patrick, 18 years ago

trunk: merged the weather engine branche to the trunk

File size: 8.6 KB
RevLine 
[7679]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 "lightening_effect.h"
16
[7807]17#include "state.h"
[8793]18
[7679]19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
[8793]21#include "util/loading/resource_manager.h"
22
[8255]23#include "effects/billboard.h"
[7679]24
25#include "glincl.h"
26#include "parser/tinyxml/tinyxml.h"
27
[8255]28#include "shell_command.h"
29#include "light.h"
30
31SHELL_COMMAND(activate, LighteningEffect, activateLightening);
32SHELL_COMMAND(deactivate, LighteningEffect, deactivateLightening);
33
[7679]34using namespace std;
35
36CREATE_FACTORY(LighteningEffect, CL_LIGHTENING_EFFECT);
37
38LighteningEffect::LighteningEffect(const TiXmlElement* root)
39{
[8793]40  this->setClassID(CL_LIGHTENING_EFFECT, "LighteningEffect");
[8316]41
[8793]42  this->init();
[7679]43
[8793]44  if (root != NULL)
45    this->loadParams(root);
[7679]46
[8793]47  if(this->lighteningActivate)
48    this->activate();
[7679]49}
50
51LighteningEffect::~LighteningEffect()
52{
[8793]53  this->deactivate();
[7679]54}
55
56void LighteningEffect::loadParams(const TiXmlElement* root)
57{
[8793]58  WeatherEffect::loadParams(root);
[8316]59
[8793]60  LoadParam(root, "coord", this, LighteningEffect, coord);
61  LoadParam(root, "frequency", this, LighteningEffect, setFlashFrequency);
62  LoadParam(root, "const-time", this, LighteningEffect, setFlashConstTime);
63  LoadParam(root, "rising-time", this, LighteningEffect, setFlashRisingTime);
64  LoadParam(root, "size", this, LighteningEffect, setFlashSize);
65  LoadParam(root, "seed", this, LighteningEffect, setFlashSeed);
[8495]66
[8793]67  LOAD_PARAM_START_CYCLE(root, element);
68  {
69    LoadParam_CYCLE(element, "option", this, LighteningEffect, setLighteningOption);
70  }
71  LOAD_PARAM_END_CYCLE(element);
[7679]72}
73
74
[8495]75void LighteningEffect::init()
[7679]76{
[8793]77  //default values
78  this->lighteningActivate = false;
[8316]79
[8793]80  this->flashFrequency = 4.0f;
81  this->flashFrequencyBase = 4.0f;
82  this->flashFrequencySeed = 2.0f;
[8255]83
[8793]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 = 700.0f;
91  this->height = 250.0f;
92  this->seedWidth = 50.0f;
93  this->seedHeight = 50.0f;
94
[8495]95  this->lighteningMove = false;
[8255]96
[8793]97  this->mainPosX = 1200;
[8495]98  this->mainPosY = 900;
99  this->mainPosZ = 0;
[8255]100
[8793]101  this->time = 0.0;
[8255]102
[8793]103  // initialize lightening textures
104  this->billboard[0] = new Billboard(NULL);
105  this->billboard[0]->setTexture("maps/lightning_bolt1.png");
106  this->billboard[0]->setSize(this->width, this->height);
107  this->billboard[0]->setVisibiliy(false);
[8255]108
[8793]109  this->billboard[1] = new Billboard(NULL);
110  this->billboard[1]->setTexture("maps/lightning_bolt2.png");
111  this->billboard[1]->setSize(this->width, this->height);
112  this->billboard[1]->setVisibiliy(false);
[8255]113
[8793]114  this->billboard[2] = new Billboard(NULL);
115  this->billboard[2]->setTexture("maps/lightning_bolt3.png");
116  this->billboard[2]->setSize(this->width, this->height);
117  this->billboard[2]->setVisibiliy(false);
[8255]118
[8793]119  this->billboard[3] = new Billboard(NULL);
120  this->billboard[3]->setTexture("maps/lightning_bolt4.png");
121  this->billboard[3]->setSize(this->width, this->height);
122  this->billboard[3]->setVisibiliy(false);
[8316]123
[8793]124  if (this->lighteningMove)
125  {
126    this->cameraCoor = State::getCameraNode()->getAbsCoor();
127    this->billboard[0]->setAbsCoor(this->cameraCoor.x+1200,900,this->cameraCoor.z+0);
128    this->billboard[1]->setAbsCoor(this->cameraCoor.x+1200,900,this->cameraCoor.z+0);
129    this->billboard[2]->setAbsCoor(this->cameraCoor.x+1200,900,this->cameraCoor.z+0);
130    this->billboard[3]->setAbsCoor(this->cameraCoor.x+1200,900,this->cameraCoor.z+0);
131  }
132  else
133  {
134    this->billboard[0]->setAbsCoor(1200,900,0);
135    this->billboard[1]->setAbsCoor(1200,900,0);
136    this->billboard[2]->setAbsCoor(1200,900,0);
137    this->billboard[3]->setAbsCoor(1200,900,0);
138  }
[8495]139
[8793]140  this->flashLight = new Light();
141  this->flashLight->setDiffuseColor(0,0,0);
142  this->flashLight->setSpecularColor(0,0,0);
[8495]143
[8793]144  //load sound
145  if (this->thunderBuffer != NULL)
146    ResourceManager::getInstance()->unload(this->thunderBuffer);
147  this->thunderBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/thunder.wav", WAV);
[8495]148
[7679]149}
150
[8255]151void LighteningEffect::coord(float x, float y, float z)
152{
[8793]153  if (this->lighteningMove)
154  {
155    this->cameraCoor = State::getCameraNode()->getAbsCoor();
156    this->billboard[0]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
157    this->billboard[1]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
158    this->billboard[2]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
159    this->billboard[3]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
160  }
161  else
162  {
163    this->billboard[0]->setAbsCoor(x, y, z);
164    this->billboard[1]->setAbsCoor(x, y, z);
165    this->billboard[2]->setAbsCoor(x, y, z);
166    this->billboard[3]->setAbsCoor(x, y, z);
167  }
[8495]168
[8793]169  this->mainPosX = x;
170  this->mainPosY = y;
171  this->mainPosZ = z;
[8255]172}
[7679]173
[8255]174
175void LighteningEffect::setFlashSize(float width, float height, float seedWidth, float seedHeight)
176{
[8793]177  this->width = width;
178  this->height = height;
179  this->seedWidth = seedWidth;
180  this->seedHeight = seedHeight;
[8495]181
[8793]182  this->billboard[0]->setSize(this->width, this->height);
183  this->billboard[1]->setSize(this->width, this->height);
184  this->billboard[2]->setSize(this->width, this->height);
185  this->billboard[3]->setSize(this->width, this->height);
[8255]186}
187
188
[8495]189void LighteningEffect::activate()
[7679]190{
[8793]191  PRINTF(0)( "Activating LighteningEffect\n" );
192  this->lighteningActivate = true;
[8316]193
[8793]194  this->time = 0;
[7679]195}
196
197
[8495]198void LighteningEffect::deactivate()
[7679]199{
[8793]200  PRINTF(0)("Deactivating LighteningEffect\n");
201  this->lighteningActivate = false;
[8316]202
[8793]203  this->billboard[0]->setVisibiliy(false);
204  this->billboard[1]->setVisibiliy(false);
205  this->billboard[2]->setVisibiliy(false);
206  this->billboard[3]->setVisibiliy(false);
[7679]207}
208
209void LighteningEffect::tick (float dt)
210{
[8793]211  if(!lighteningActivate)
212    return;
[8316]213
[8793]214  this->time += dt;
[7679]215
[8793]216  float x;
217  x = (float)rand()/(float)RAND_MAX;
[8316]218
[8793]219  // TODO: Make random flashing with short light dingsda:)
[8316]220
[8793]221  if( this->time > this->flashFrequency)
222  {
223    // Reset timer
224    this->time = 0.0f;
[8316]225
[8793]226    // Move billboard to start position
227    this->flashLight->setAbsCoor(this->billboard[0]->getAbsCoor().x, this->billboard[0]->getAbsCoor().y, this->billboard[0]->getAbsCoor().z);
[8316]228
[8793]229    // Start a flash & lightening cycle
230    this->billboard[0]->setVisibiliy(true);
[8316]231
[8793]232    // Lighten up environment
233    this->flashLight->setDiffuseColor(1,1,1);
234    this->flashLight->setSpecularColor(1,1,1);
[8255]235
[8793]236    // Play thunder sound
237    this->soundSource.play(this->thunderBuffer);
238  }
[8255]239
[8793]240  if( this->billboard[0]->isVisible() && this->time > this->flashRisingTime*1/3 )
241  {
242    this->billboard[0]->setVisibiliy(false);
243    this->billboard[1]->setVisibiliy(true);
244  }
245  else if( this->billboard[1]->isVisible() && this->time > this->flashRisingTime*2/3 )
246  {
247    this->billboard[1]->setVisibiliy(false);
248    this->billboard[2]->setVisibiliy(true);
[8495]249
[8793]250  }
251  else if( this->billboard[2]->isVisible() && this->time > this->flashRisingTime)
252  {
253    this->billboard[2]->setVisibiliy(false);
254    this->billboard[3]->setVisibiliy(true);
255  }
[8495]256
[8793]257  if( this->billboard[3]->isVisible() && this->time > this->flashHoldTime)
258  {
259    this->billboard[3]->setVisibiliy(false);
260    this->time = 0.0f;
261    this->flashLight->setDiffuseColor(0,0,0);
262    this->flashLight->setSpecularColor(0,0,0);
[8495]263
[8793]264    this->newCoordinates();
265  }
266}
[8495]267
[8793]268void LighteningEffect::newCoordinates() {
[8495]269
[8793]270    float posX, posZ;
[8495]271
[8793]272    if(this->lighteningMove)
273    {
[8495]274
[8793]275      this->cameraCoor = State::getCameraNode()->getAbsCoor();
276      posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX + this->cameraCoor.x;
277      posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX + this->cameraCoor.z;
[8495]278
[8793]279    }
280    else
281    {
[8495]282
[8793]283      posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX;
284      posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX;
[8495]285
[8793]286    }
[8495]287
[8793]288    this->billboard[0]->setAbsCoor(posX, this->mainPosY, posZ);
289    this->billboard[1]->setAbsCoor(posX, this->mainPosY, posZ);
290    this->billboard[2]->setAbsCoor(posX, this->mainPosY, posZ);
291    this->billboard[3]->setAbsCoor(posX, this->mainPosY, posZ);
[8495]292
[8793]293    this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX;
[8495]294
[8793]295    float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX;
296    float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX;
[8495]297
[8793]298    this->billboard[0]->setSize(w, h);
299    this->billboard[1]->setSize(w, h);
300    this->billboard[2]->setSize(w, h);
301    this->billboard[3]->setSize(w, h);
[7679]302}
Note: See TracBrowser for help on using the repository browser.