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