| 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 |  | 
|---|
| 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 |  | 
|---|
| 31 | SHELL_COMMAND(activate, LighteningEffect, activateLightening); | 
|---|
| 32 | SHELL_COMMAND(deactivate, LighteningEffect, deactivateLightening); | 
|---|
| 33 |  | 
|---|
| 34 | using namespace std; | 
|---|
| 35 |  | 
|---|
| 36 | CREATE_FACTORY(LighteningEffect, CL_LIGHTENING_EFFECT); | 
|---|
| 37 |  | 
|---|
| 38 | LighteningEffect::LighteningEffect(const TiXmlElement* root) | 
|---|
| 39 | { | 
|---|
| 40 |   this->setClassID(CL_LIGHTENING_EFFECT, "LighteningEffect"); | 
|---|
| 41 |  | 
|---|
| 42 |   this->init(); | 
|---|
| 43 |  | 
|---|
| 44 |   if (root != NULL) | 
|---|
| 45 |     this->loadParams(root); | 
|---|
| 46 |  | 
|---|
| 47 |   if(this->lighteningActivate) | 
|---|
| 48 |     this->activate(); | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | LighteningEffect::~LighteningEffect() | 
|---|
| 52 | { | 
|---|
| 53 |   this->deactivate(); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void LighteningEffect::loadParams(const TiXmlElement* root) | 
|---|
| 57 | { | 
|---|
| 58 |   WeatherEffect::loadParams(root); | 
|---|
| 59 |  | 
|---|
| 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); | 
|---|
| 66 |  | 
|---|
| 67 |   LOAD_PARAM_START_CYCLE(root, element); | 
|---|
| 68 |   { | 
|---|
| 69 |     LoadParam_CYCLE(element, "option", this, LighteningEffect, setLighteningOption); | 
|---|
| 70 |   } | 
|---|
| 71 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | void LighteningEffect::init() | 
|---|
| 76 | { | 
|---|
| 77 |   //default values | 
|---|
| 78 |   this->lighteningActivate = 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 = 700.0f; | 
|---|
| 91 |   this->height = 250.0f; | 
|---|
| 92 |   this->seedWidth = 50.0f; | 
|---|
| 93 |   this->seedHeight = 50.0f; | 
|---|
| 94 |  | 
|---|
| 95 |   this->lighteningMove = false; | 
|---|
| 96 |  | 
|---|
| 97 |   this->mainPosX = 1200; | 
|---|
| 98 |   this->mainPosY = 900; | 
|---|
| 99 |   this->mainPosZ = 0; | 
|---|
| 100 |  | 
|---|
| 101 |   this->time = 0.0; | 
|---|
| 102 |  | 
|---|
| 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); | 
|---|
| 108 |  | 
|---|
| 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); | 
|---|
| 113 |  | 
|---|
| 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); | 
|---|
| 118 |  | 
|---|
| 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); | 
|---|
| 123 |  | 
|---|
| 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 |   } | 
|---|
| 139 |  | 
|---|
| 140 |   this->flashLight = new Light(); | 
|---|
| 141 |   this->flashLight->setDiffuseColor(0,0,0); | 
|---|
| 142 |   this->flashLight->setSpecularColor(0,0,0); | 
|---|
| 143 |  | 
|---|
| 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); | 
|---|
| 148 |  | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void LighteningEffect::coord(float x, float y, float z) | 
|---|
| 152 | { | 
|---|
| 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 |   } | 
|---|
| 168 |  | 
|---|
| 169 |   this->mainPosX = x; | 
|---|
| 170 |   this->mainPosY = y; | 
|---|
| 171 |   this->mainPosZ = z; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | void LighteningEffect::setFlashSize(float width, float height, float seedWidth, float seedHeight) | 
|---|
| 176 | { | 
|---|
| 177 |   this->width = width; | 
|---|
| 178 |   this->height = height; | 
|---|
| 179 |   this->seedWidth = seedWidth; | 
|---|
| 180 |   this->seedHeight = seedHeight; | 
|---|
| 181 |  | 
|---|
| 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); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 | void LighteningEffect::activate() | 
|---|
| 190 | { | 
|---|
| 191 |   PRINTF(0)( "Activating LighteningEffect\n" ); | 
|---|
| 192 |   this->lighteningActivate = true; | 
|---|
| 193 |  | 
|---|
| 194 |   this->time = 0; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 |  | 
|---|
| 198 | void LighteningEffect::deactivate() | 
|---|
| 199 | { | 
|---|
| 200 |   PRINTF(0)("Deactivating LighteningEffect\n"); | 
|---|
| 201 |   this->lighteningActivate = false; | 
|---|
| 202 |  | 
|---|
| 203 |   this->billboard[0]->setVisibiliy(false); | 
|---|
| 204 |   this->billboard[1]->setVisibiliy(false); | 
|---|
| 205 |   this->billboard[2]->setVisibiliy(false); | 
|---|
| 206 |   this->billboard[3]->setVisibiliy(false); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | void LighteningEffect::tick (float dt) | 
|---|
| 210 | { | 
|---|
| 211 |   if(!lighteningActivate) | 
|---|
| 212 |     return; | 
|---|
| 213 |  | 
|---|
| 214 |   this->time += dt; | 
|---|
| 215 |  | 
|---|
| 216 |   float x; | 
|---|
| 217 |   x = (float)rand()/(float)RAND_MAX; | 
|---|
| 218 |  | 
|---|
| 219 |   // TODO: Make random flashing with short light dingsda:) | 
|---|
| 220 |  | 
|---|
| 221 |   if( this->time > this->flashFrequency) | 
|---|
| 222 |   { | 
|---|
| 223 |     // Reset timer | 
|---|
| 224 |     this->time = 0.0f; | 
|---|
| 225 |  | 
|---|
| 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); | 
|---|
| 228 |  | 
|---|
| 229 |     // Start a flash & lightening cycle | 
|---|
| 230 |     this->billboard[0]->setVisibiliy(true); | 
|---|
| 231 |  | 
|---|
| 232 |     // Lighten up environment | 
|---|
| 233 |     this->flashLight->setDiffuseColor(1,1,1); | 
|---|
| 234 |     this->flashLight->setSpecularColor(1,1,1); | 
|---|
| 235 |  | 
|---|
| 236 |     // Play thunder sound | 
|---|
| 237 |     this->soundSource.play(this->thunderBuffer); | 
|---|
| 238 |   } | 
|---|
| 239 |  | 
|---|
| 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); | 
|---|
| 249 |  | 
|---|
| 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 |   } | 
|---|
| 256 |  | 
|---|
| 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); | 
|---|
| 263 |  | 
|---|
| 264 |     this->newCoordinates(); | 
|---|
| 265 |   } | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | void LighteningEffect::newCoordinates() { | 
|---|
| 269 |  | 
|---|
| 270 |     float posX, posZ; | 
|---|
| 271 |  | 
|---|
| 272 |     if(this->lighteningMove) | 
|---|
| 273 |     { | 
|---|
| 274 |  | 
|---|
| 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; | 
|---|
| 278 |  | 
|---|
| 279 |     } | 
|---|
| 280 |     else | 
|---|
| 281 |     { | 
|---|
| 282 |  | 
|---|
| 283 |       posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX; | 
|---|
| 284 |       posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX; | 
|---|
| 285 |  | 
|---|
| 286 |     } | 
|---|
| 287 |  | 
|---|
| 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); | 
|---|
| 292 |  | 
|---|
| 293 |     this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX; | 
|---|
| 294 |  | 
|---|
| 295 |     float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX; | 
|---|
| 296 |     float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX; | 
|---|
| 297 |  | 
|---|
| 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); | 
|---|
| 302 | } | 
|---|