[7561] | 1 | /* |
---|
[8793] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[9006] | 3 | |
---|
[8793] | 4 | Copyright (C) 2004 orx |
---|
[9006] | 5 | |
---|
[8793] | 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. |
---|
[9006] | 10 | |
---|
[7561] | 11 | ### File Specific: |
---|
[8793] | 12 | main-programmer: hdavid, amaechler |
---|
[7561] | 13 | */ |
---|
| 14 | |
---|
[9006] | 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
| 16 | |
---|
[7561] | 17 | #include "rain_effect.h" |
---|
| 18 | |
---|
| 19 | #include "util/loading/load_param.h" |
---|
| 20 | #include "util/loading/factory.h" |
---|
[7646] | 21 | #include "util/loading/resource_manager.h" |
---|
[7561] | 22 | |
---|
| 23 | #include "glincl.h" |
---|
[7652] | 24 | #include "p_node.h" |
---|
[7562] | 25 | #include "state.h" |
---|
[7628] | 26 | #include "spark_particles.h" |
---|
| 27 | #include "plane_emitter.h" |
---|
[7696] | 28 | #include "shell_command.h" |
---|
[8255] | 29 | #include "light.h" |
---|
[8793] | 30 | #include "cloud_effect.h" |
---|
[9112] | 31 | #include "script_class.h" |
---|
[7562] | 32 | |
---|
| 33 | #include "parser/tinyxml/tinyxml.h" |
---|
| 34 | |
---|
[8793] | 35 | // Define shell commands |
---|
[7696] | 36 | SHELL_COMMAND(activate, RainEffect, activateRain); |
---|
| 37 | SHELL_COMMAND(deactivate, RainEffect, deactivateRain); |
---|
[8495] | 38 | SHELL_COMMAND(startraining, RainEffect, startRaining); |
---|
| 39 | SHELL_COMMAND(stopraining, RainEffect, stopRaining); |
---|
[7577] | 40 | |
---|
[7561] | 41 | using namespace std; |
---|
| 42 | |
---|
[9112] | 43 | CREATE_SCRIPTABLE_CLASS(RainEffect, CL_RAIN_EFFECT, |
---|
| 44 | addMethod("startRaining", ExecutorLua0<RainEffect>(&RainEffect::startRaining)) |
---|
| 45 | ->addMethod("stopRaining", ExecutorLua0<RainEffect>(&RainEffect::stopRaining)) |
---|
| 46 | ->addMethod("activate", ExecutorLua0<RainEffect>(&RainEffect::activate)) |
---|
| 47 | ->addMethod("deactivate", ExecutorLua0<RainEffect>(&RainEffect::deactivate)) |
---|
| 48 | ); |
---|
| 49 | |
---|
[7561] | 50 | CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT); |
---|
| 51 | |
---|
[8793] | 52 | /** |
---|
| 53 | * @brief standard constructor |
---|
| 54 | */ |
---|
[9006] | 55 | RainEffect::RainEffect(const TiXmlElement* root) { |
---|
| 56 | this->setClassID(CL_RAIN_EFFECT, "RainEffect"); |
---|
[8255] | 57 | |
---|
[9006] | 58 | this->init(); |
---|
[7561] | 59 | |
---|
[9006] | 60 | if (root != NULL) |
---|
| 61 | this->loadParams(root); |
---|
[7561] | 62 | |
---|
[9006] | 63 | //load rain sound |
---|
| 64 | if (this->rainBuffer != NULL) |
---|
| 65 | ResourceManager::getInstance()->unload(this->rainBuffer); |
---|
| 66 | this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV); |
---|
[7646] | 67 | |
---|
[9006] | 68 | //load wind sound |
---|
| 69 | if (this->rainWindForce != 0) { |
---|
| 70 | if (this->windBuffer != NULL) |
---|
| 71 | ResourceManager::getInstance()->unload(this->windBuffer); |
---|
| 72 | this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV); |
---|
| 73 | } |
---|
[7646] | 74 | |
---|
[9006] | 75 | if(rainActivate) { |
---|
| 76 | this->activate(); |
---|
| 77 | RainEffect::rainParticles->precache((int)this->rainLife * 2); |
---|
| 78 | } |
---|
[7561] | 79 | } |
---|
| 80 | |
---|
[8793] | 81 | /** |
---|
| 82 | * @brief standard deconstructor |
---|
| 83 | */ |
---|
[9006] | 84 | RainEffect::~RainEffect() { |
---|
| 85 | this->deactivate(); |
---|
[7696] | 86 | |
---|
[9006] | 87 | if (this->rainBuffer != NULL) |
---|
| 88 | ResourceManager::getInstance()->unload(this->rainBuffer); |
---|
[7696] | 89 | |
---|
[9006] | 90 | if (this->windBuffer != NULL) |
---|
| 91 | ResourceManager::getInstance()->unload(this->windBuffer); |
---|
[7561] | 92 | } |
---|
| 93 | |
---|
[8793] | 94 | /** |
---|
| 95 | * @brief initalizes the rain effect with default values |
---|
| 96 | */ |
---|
[9006] | 97 | void RainEffect::init() { |
---|
| 98 | //Default values |
---|
| 99 | this->rainActivate = false; |
---|
| 100 | this->rainFadeInActivate = false; |
---|
| 101 | this->rainFadeOutActivate = false; |
---|
[7561] | 102 | |
---|
[9006] | 103 | this->rainMove = false; |
---|
| 104 | this->rainCoord = Vector(500, 500, 500); |
---|
| 105 | this->rainSize = Vector2D(1000, 1000); |
---|
| 106 | this->rainRate = 4000; |
---|
| 107 | this->rainVelocity = -300; |
---|
| 108 | this->rainLife = 4; |
---|
| 109 | this->rainWindForce = 0; |
---|
| 110 | this->rainFadeInDuration = 10; |
---|
| 111 | this->rainFadeOutDuration = 10; |
---|
[8255] | 112 | |
---|
[9006] | 113 | this->cloudColor = Vector(0.6f, 0.6f, 0.6f); |
---|
| 114 | this->skyColor = Vector(0.0f, 0.0f, 0.0f); |
---|
| 115 | |
---|
| 116 | this->rainMaxParticles = this->rainRate * this->rainLife; |
---|
| 117 | this->localTimer = 0; |
---|
| 118 | this->soundRainVolume = 0.3f; |
---|
| 119 | this->emitter = new PlaneEmitter(this->rainSize); |
---|
| 120 | |
---|
[7628] | 121 | } |
---|
[7561] | 122 | |
---|
[8793] | 123 | /** |
---|
| 124 | * @brief loads the rain effect parameters. |
---|
| 125 | * @param root: the XML-Element to load the data from |
---|
| 126 | */ |
---|
[9006] | 127 | void RainEffect::loadParams(const TiXmlElement* root) { |
---|
| 128 | WeatherEffect::loadParams(root); |
---|
[7561] | 129 | |
---|
[9006] | 130 | LoadParam(root, "coord", this, RainEffect, setRainCoord); |
---|
| 131 | LoadParam(root, "size", this, RainEffect, setRainSize); |
---|
| 132 | LoadParam(root, "rate", this, RainEffect, setRainRate); |
---|
| 133 | LoadParam(root, "velocity", this, RainEffect, setRainVelocity); |
---|
| 134 | LoadParam(root, "life", this, RainEffect, setRainLife); |
---|
| 135 | LoadParam(root, "wind", this, RainEffect, setRainWind); |
---|
| 136 | LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn); |
---|
| 137 | LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut); |
---|
| 138 | LoadParam(root, "cloudcolor", this, RainEffect, setCloudColor); |
---|
| 139 | LoadParam(root, "skycolor", this, RainEffect, setSkyColor); |
---|
[7646] | 140 | |
---|
[9006] | 141 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 142 | { |
---|
| 143 | LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption); |
---|
| 144 | } |
---|
| 145 | LOAD_PARAM_END_CYCLE(element); |
---|
[7561] | 146 | } |
---|
| 147 | |
---|
[7577] | 148 | SparkParticles* RainEffect::rainParticles = NULL; |
---|
[7561] | 149 | |
---|
[8793] | 150 | /** |
---|
| 151 | * @brief activates the rain effect |
---|
| 152 | */ |
---|
[9006] | 153 | void RainEffect::activate() { |
---|
| 154 | PRINTF(3)( "Activating RainEffect, coord: %f, %f, %f, size: %f, %f, rate: %f, velocity: %f, moveRain: %s\n", this->rainCoord.x, this->rainCoord.y, this->rainCoord.z, this->rainSize.x, this-> rainSize.y, this->rainRate, this->rainVelocity, this->rainMove ? "true" : "false" ); |
---|
[7577] | 155 | |
---|
[9006] | 156 | this->rainActivate = true; |
---|
[8255] | 157 | |
---|
[9006] | 158 | if (unlikely(RainEffect::rainParticles == NULL)) { |
---|
| 159 | RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles); |
---|
| 160 | RainEffect::rainParticles->setName("RainParticles"); |
---|
| 161 | RainEffect::rainParticles->setLifeSpan(this->rainLife, 2); |
---|
| 162 | RainEffect::rainParticles->setRadius(0, 0.03); |
---|
| 163 | RainEffect::rainParticles->setRadius(0.2, 0.02); |
---|
| 164 | RainEffect::rainParticles->setRadius(1, 0.01); |
---|
| 165 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1 |
---|
| 166 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2 |
---|
| 167 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey |
---|
| 168 | } |
---|
[7577] | 169 | |
---|
[9006] | 170 | this->emitter->setSystem(RainEffect::rainParticles); |
---|
| 171 | this->emitter->setRelCoor(this->rainCoord); |
---|
| 172 | this->emitter->setEmissionRate(this->rainRate); |
---|
| 173 | this->emitter->setEmissionVelocity(this->rainVelocity); |
---|
| 174 | this->emitter->setSpread(this->rainWindForce / 50, 0.2); |
---|
[7577] | 175 | |
---|
[9006] | 176 | // play rain sound and loop it |
---|
| 177 | this->soundSource.play(this->rainBuffer, this->soundRainVolume, true); |
---|
[7577] | 178 | |
---|
[9006] | 179 | // if there's wind, play wind sound |
---|
| 180 | if (this->rainWindForce > 0) |
---|
| 181 | this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true); |
---|
[7628] | 182 | |
---|
[9006] | 183 | // Store cloud- and sky color before the rain; |
---|
| 184 | this->oldCloudColor = CloudEffect::cloudColor; |
---|
| 185 | this->oldSkyColor = CloudEffect::skyColor; |
---|
[8316] | 186 | |
---|
[9006] | 187 | // If we're not fading, change color immediately |
---|
| 188 | if (!this->rainFadeInActivate || !this->rainFadeOutActivate) { |
---|
| 189 | CloudEffect::changeCloudColor(this->cloudColor, 0); |
---|
| 190 | CloudEffect::changeSkyColor(this->skyColor, 0); |
---|
| 191 | } |
---|
[7561] | 192 | } |
---|
| 193 | |
---|
[8793] | 194 | /** |
---|
| 195 | * @brief deactivates the rain effect |
---|
| 196 | */ |
---|
[9006] | 197 | void RainEffect::deactivate() { |
---|
| 198 | PRINTF(3)("Deactivating RainEffect\n"); |
---|
[7561] | 199 | |
---|
[9006] | 200 | this->rainActivate = false; |
---|
| 201 | this->rainFadeInActivate = false; |
---|
| 202 | this->rainFadeOutActivate = false; |
---|
[8316] | 203 | |
---|
[9006] | 204 | this->emitter->setSystem(NULL); |
---|
[7577] | 205 | |
---|
[9006] | 206 | // Stop Sound |
---|
| 207 | this->soundSource.stop(); |
---|
| 208 | |
---|
| 209 | // Restore the old cloud- and sky color |
---|
| 210 | CloudEffect::changeCloudColor(this->oldCloudColor, 0); |
---|
| 211 | CloudEffect::changeSkyColor(this->oldSkyColor, 0); |
---|
[8793] | 212 | } |
---|
[7646] | 213 | |
---|
[8793] | 214 | /** |
---|
| 215 | * @brief ticks the rain effect |
---|
| 216 | * @param dt: tick float |
---|
| 217 | */ |
---|
[9006] | 218 | void RainEffect::tick (float dt) { |
---|
| 219 | if (!this->rainActivate) |
---|
| 220 | return; |
---|
[8316] | 221 | |
---|
[9006] | 222 | if (this->rainMove) { |
---|
| 223 | this->rainCoord = State::getCameraNode()->getAbsCoor(); |
---|
| 224 | this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z); |
---|
| 225 | } |
---|
[7696] | 226 | |
---|
[9006] | 227 | if (this->rainFadeInActivate) { |
---|
| 228 | PRINTF(4)("tick fading IN RainEffect\n"); |
---|
[8316] | 229 | |
---|
[9006] | 230 | this->localTimer += dt; |
---|
| 231 | float progress = this->localTimer / this->rainFadeInDuration; |
---|
[8255] | 232 | |
---|
[9006] | 233 | // use alpha in color to fade in rain |
---|
| 234 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1 |
---|
| 235 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2 |
---|
| 236 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2 * progress); // light grey |
---|
[8255] | 237 | |
---|
[9006] | 238 | // increase radius for more "heavy" rain |
---|
| 239 | RainEffect::rainParticles->setRadius(0, 0.03 * progress); |
---|
| 240 | RainEffect::rainParticles->setRadius(0.2, 0.02 * progress); |
---|
| 241 | RainEffect::rainParticles->setRadius(1, 0.01 * progress); |
---|
[8255] | 242 | |
---|
[9006] | 243 | // increase sound volume |
---|
| 244 | if (!this->soundSource.isPlaying()) |
---|
| 245 | this->soundSource.play(this->rainBuffer, this->soundRainVolume, true); |
---|
| 246 | this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress); |
---|
[8255] | 247 | |
---|
[9006] | 248 | if (progress >= 1) |
---|
| 249 | this->rainFadeInActivate = false; |
---|
| 250 | } |
---|
[8255] | 251 | |
---|
[9006] | 252 | if (this->rainFadeOutActivate) { |
---|
| 253 | PRINTF(4)("tick fading OUT RainEffect\n"); |
---|
[8316] | 254 | |
---|
[9006] | 255 | this->localTimer += dt; |
---|
| 256 | float progress = 1 - (this->localTimer / this->rainFadeOutDuration); |
---|
[8495] | 257 | |
---|
[9006] | 258 | // use alpha in color to fade out |
---|
| 259 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1 |
---|
| 260 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2 |
---|
| 261 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2 * progress); // light grey |
---|
[8495] | 262 | |
---|
[9006] | 263 | // decrease radius |
---|
| 264 | RainEffect::rainParticles->setRadius(0, 0.03 * progress); |
---|
| 265 | RainEffect::rainParticles->setRadius(0.2, 0.02 * progress); |
---|
| 266 | RainEffect::rainParticles->setRadius(1, 0.01 * progress); |
---|
[8495] | 267 | |
---|
[9006] | 268 | // decrease sound volume |
---|
| 269 | if (!this->soundSource.isPlaying()) |
---|
| 270 | this->soundSource.play(this->rainBuffer, this->soundRainVolume, true); |
---|
| 271 | this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress); |
---|
| 272 | |
---|
| 273 | if (progress <= 0) { |
---|
| 274 | PRINTF(4)("tick fading OUT RainEffect COMPLETED! Deactivating...\n"); |
---|
| 275 | this->rainFadeOutActivate = false; |
---|
| 276 | this->deactivate(); |
---|
| 277 | } |
---|
[8495] | 278 | } |
---|
[7646] | 279 | } |
---|
[8255] | 280 | |
---|
[8793] | 281 | /** |
---|
| 282 | * @brief starts raining slowly |
---|
| 283 | */ |
---|
[9006] | 284 | void RainEffect::startRaining() { |
---|
| 285 | PRINTF(4)("startRaining function;\n"); |
---|
[8255] | 286 | |
---|
[9006] | 287 | // If it is already raining, do nothing |
---|
| 288 | if (this->rainActivate) |
---|
| 289 | return; |
---|
[8255] | 290 | |
---|
[9006] | 291 | this->localTimer = 0; |
---|
| 292 | this->rainFadeInActivate = true; |
---|
[8255] | 293 | |
---|
[9006] | 294 | PRINTF(4)("startRaining function complete; fadedur: %f\n", this->rainFadeInDuration); |
---|
| 295 | this->activate(); |
---|
[8495] | 296 | |
---|
[9006] | 297 | CloudEffect::changeCloudColor(this->cloudColor, this->rainFadeInDuration); |
---|
| 298 | CloudEffect::changeSkyColor(this->skyColor, this->rainFadeInDuration); |
---|
[8255] | 299 | } |
---|
[8495] | 300 | |
---|
[8793] | 301 | /** |
---|
| 302 | * @brief stops raining slowly |
---|
| 303 | */ |
---|
[9006] | 304 | void RainEffect::stopRaining() { |
---|
| 305 | PRINTF(4)("stopRaining function;\n"); |
---|
[8495] | 306 | |
---|
[9006] | 307 | // If it is not raining, do nothing |
---|
| 308 | if (!this->rainActivate) |
---|
| 309 | return; |
---|
[8495] | 310 | |
---|
[9006] | 311 | this->localTimer = 0; |
---|
| 312 | this->rainFadeOutActivate = true; |
---|
[8495] | 313 | |
---|
[9006] | 314 | PRINTF(4)("stopRaining function completed; fadedur: %f\n", this->rainFadeOutDuration); |
---|
| 315 | |
---|
| 316 | CloudEffect::changeCloudColor(this->oldCloudColor, this->rainFadeOutDuration); |
---|
| 317 | CloudEffect::changeSkyColor(this->oldSkyColor, this->rainFadeOutDuration); |
---|
[8793] | 318 | } |
---|
[8495] | 319 | |
---|
[8793] | 320 | /** |
---|
| 321 | * @brief hides the rain |
---|
| 322 | */ |
---|
[9006] | 323 | void RainEffect::hideRain() { |
---|
| 324 | RainEffect::rainParticles->setColor(0, 0,0,0, 0); |
---|
| 325 | RainEffect::rainParticles->setColor(0, 0,0,0, 0); |
---|
[8495] | 326 | } |
---|