[7561] | 1 | /* |
---|
[8255] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[7561] | 3 | |
---|
[8255] | 4 | Copyright (C) 2004 orx |
---|
[7561] | 5 | |
---|
[8255] | 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. |
---|
[7561] | 10 | |
---|
| 11 | ### File Specific: |
---|
[8255] | 12 | main-programmer: hdavid, amaechler |
---|
[7561] | 13 | */ |
---|
| 14 | |
---|
| 15 | #include "rain_effect.h" |
---|
| 16 | |
---|
| 17 | #include "util/loading/load_param.h" |
---|
| 18 | #include "util/loading/factory.h" |
---|
[7646] | 19 | #include "util/loading/resource_manager.h" |
---|
[7561] | 20 | |
---|
| 21 | #include "glincl.h" |
---|
[7652] | 22 | #include "p_node.h" |
---|
[7562] | 23 | #include "state.h" |
---|
[7628] | 24 | #include "spark_particles.h" |
---|
| 25 | #include "plane_emitter.h" |
---|
[7696] | 26 | #include "shell_command.h" |
---|
[8255] | 27 | #include "light.h" |
---|
[7562] | 28 | |
---|
| 29 | #include "parser/tinyxml/tinyxml.h" |
---|
| 30 | |
---|
[7696] | 31 | SHELL_COMMAND(activate, RainEffect, activateRain); |
---|
| 32 | SHELL_COMMAND(deactivate, RainEffect, deactivateRain); |
---|
[8495] | 33 | SHELL_COMMAND(startraining, RainEffect, startRaining); |
---|
| 34 | SHELL_COMMAND(stopraining, RainEffect, stopRaining); |
---|
[7577] | 35 | |
---|
[7561] | 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT); |
---|
| 39 | |
---|
[8495] | 40 | /* TODO: |
---|
| 41 | - test multiple rain emitters |
---|
| 42 | - Think about what happens with building poss. to hang movewithcam off |
---|
| 43 | - Possible to activate lightening |
---|
| 44 | - turn off visibility when in a building |
---|
| 45 | - variable emitter size depending on playable |
---|
| 46 | */ |
---|
[8255] | 47 | |
---|
[8495] | 48 | RainEffect::RainEffect(const TiXmlElement* root) { |
---|
| 49 | this->setClassID(CL_RAIN_EFFECT, "RainEffect"); |
---|
[7561] | 50 | |
---|
[8495] | 51 | this->init(); |
---|
[7561] | 52 | |
---|
[8495] | 53 | if (root != NULL) |
---|
| 54 | this->loadParams(root); |
---|
[7646] | 55 | |
---|
[8495] | 56 | //load rain sound |
---|
| 57 | if (this->rainBuffer != NULL) |
---|
| 58 | ResourceManager::getInstance()->unload(this->rainBuffer); |
---|
| 59 | this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV); |
---|
[7646] | 60 | |
---|
[8495] | 61 | //load wind sound |
---|
| 62 | if (this->rainWindForce != 0) { |
---|
| 63 | if (this->windBuffer != NULL) |
---|
| 64 | ResourceManager::getInstance()->unload(this->windBuffer); |
---|
| 65 | this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV); |
---|
| 66 | } |
---|
[7696] | 67 | |
---|
[8495] | 68 | if(rainActivate) { |
---|
| 69 | this->activate(); |
---|
| 70 | RainEffect::rainParticles->precache((int)this->rainLife * 5); // TODO: Figure out the correct value |
---|
| 71 | } |
---|
[7561] | 72 | } |
---|
| 73 | |
---|
[8495] | 74 | RainEffect::~RainEffect() { |
---|
| 75 | this->deactivate(); |
---|
[7696] | 76 | |
---|
[8495] | 77 | if (this->rainBuffer != NULL) |
---|
| 78 | ResourceManager::getInstance()->unload(this->rainBuffer); |
---|
[7696] | 79 | |
---|
[8495] | 80 | if (this->windBuffer != NULL) |
---|
| 81 | ResourceManager::getInstance()->unload(this->windBuffer); |
---|
[7561] | 82 | } |
---|
| 83 | |
---|
[8495] | 84 | void RainEffect::loadParams(const TiXmlElement* root) { |
---|
| 85 | WeatherEffect::loadParams(root); |
---|
[7561] | 86 | |
---|
[8495] | 87 | LoadParam(root, "coord", this, RainEffect, setRainCoord); |
---|
| 88 | LoadParam(root, "size", this, RainEffect, setRainSize); |
---|
| 89 | LoadParam(root, "rate", this, RainEffect, setRainRate); |
---|
| 90 | LoadParam(root, "velocity", this, RainEffect, setRainVelocity); |
---|
| 91 | LoadParam(root, "life", this, RainEffect, setRainLife); |
---|
| 92 | LoadParam(root, "wind", this, RainEffect, setRainWind); |
---|
| 93 | LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn); |
---|
| 94 | LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut); |
---|
[8255] | 95 | |
---|
[8495] | 96 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 97 | { |
---|
| 98 | LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption); |
---|
| 99 | } |
---|
| 100 | LOAD_PARAM_END_CYCLE(element); |
---|
[8255] | 101 | |
---|
[7628] | 102 | } |
---|
[7561] | 103 | |
---|
| 104 | |
---|
[8495] | 105 | void RainEffect::init() { |
---|
| 106 | //Default values |
---|
| 107 | this->rainActivate = false; |
---|
| 108 | this->rainMove = false; |
---|
| 109 | this->rainCoord = Vector(500, 500, 500); |
---|
| 110 | this->rainSize = Vector2D(1000, 1000); |
---|
| 111 | this->rainRate = 4000; |
---|
| 112 | this->rainVelocity = -300; |
---|
| 113 | this->rainLife = 4; |
---|
| 114 | this->rainMaxParticles = this->rainRate * this->rainLife; |
---|
| 115 | this->rainWindForce = 0; |
---|
| 116 | this->rainFadeInDuration = 0; |
---|
| 117 | this->rainFadeOutDuration = 0; |
---|
| 118 | this->localTimer = 0; |
---|
| 119 | this->soundRainVolume = 0.8f; |
---|
[7646] | 120 | |
---|
[8495] | 121 | this->emitter = new PlaneEmitter(this->rainSize); |
---|
[8255] | 122 | |
---|
[8495] | 123 | lightMan = LightManager::getInstance(); |
---|
| 124 | this->rainAmbient = lightMan->getAmbientColor(); |
---|
[7561] | 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[7577] | 128 | SparkParticles* RainEffect::rainParticles = NULL; |
---|
[7561] | 129 | |
---|
[8495] | 130 | void RainEffect::activate() { |
---|
| 131 | PRINTF(0)( "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] | 132 | |
---|
[8495] | 133 | this->rainActivate = true; |
---|
[8255] | 134 | |
---|
[8495] | 135 | if (unlikely(RainEffect::rainParticles == NULL)) { |
---|
| 136 | RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles); |
---|
| 137 | RainEffect::rainParticles->setName("RainParticles"); |
---|
| 138 | RainEffect::rainParticles->setLifeSpan(this->rainLife, 2); |
---|
| 139 | RainEffect::rainParticles->setRadius(0, 0.03); |
---|
| 140 | RainEffect::rainParticles->setRadius(0.2, 0.02); |
---|
| 141 | RainEffect::rainParticles->setRadius(1, 0.01); |
---|
| 142 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1 |
---|
| 143 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2 |
---|
| 144 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey |
---|
| 145 | } |
---|
[7577] | 146 | |
---|
[8495] | 147 | this->emitter->setSystem(RainEffect::rainParticles); |
---|
[7577] | 148 | |
---|
[8495] | 149 | this->emitter->setRelCoor(this->rainCoord); |
---|
[7577] | 150 | |
---|
[8495] | 151 | this->emitter->setEmissionRate(this->rainRate); |
---|
| 152 | this->emitter->setEmissionVelocity(this->rainVelocity); |
---|
[7628] | 153 | |
---|
[8495] | 154 | this->emitter->setSpread(this->rainWindForce / 50, 0.2); |
---|
[8316] | 155 | |
---|
[8495] | 156 | this->soundSource.play(this->rainBuffer, this->soundRainVolume, true); |
---|
[8255] | 157 | |
---|
[8495] | 158 | if (this->rainWindForce != 0) |
---|
| 159 | this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true); |
---|
[8316] | 160 | |
---|
[8495] | 161 | if (this->rainFadeInDuration == 0) |
---|
| 162 | lightMan->setAmbientColor(.1,.1,.1); |
---|
| 163 | |
---|
[7561] | 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
[8495] | 167 | void RainEffect::deactivate() { |
---|
| 168 | PRINTF(0)("Deactivating RainEffect\n"); |
---|
[8316] | 169 | |
---|
[8495] | 170 | this->rainActivate = false; |
---|
| 171 | this->emitter->setSystem(NULL); |
---|
[7577] | 172 | |
---|
[8495] | 173 | // Stop Sound |
---|
| 174 | this->soundSource.stop(); |
---|
[7646] | 175 | |
---|
[8495] | 176 | // Restore Light Ambient |
---|
| 177 | lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient); |
---|
[8316] | 178 | |
---|
[7696] | 179 | } |
---|
| 180 | |
---|
[8495] | 181 | void RainEffect::tick (float dt) { |
---|
| 182 | if (!this->rainActivate) |
---|
| 183 | return; |
---|
[8316] | 184 | |
---|
[8495] | 185 | if (this->rainMove) { |
---|
| 186 | this->rainCoord = State::getCameraNode()->getAbsCoor(); |
---|
| 187 | this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z); |
---|
| 188 | } |
---|
[8255] | 189 | |
---|
[8495] | 190 | if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration) { |
---|
| 191 | this->localTimer += dt; |
---|
| 192 | float progress = this->localTimer / this->rainFadeInDuration; |
---|
[8255] | 193 | |
---|
[8495] | 194 | // Dim Light |
---|
| 195 | lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9); |
---|
[8255] | 196 | |
---|
[8495] | 197 | // use alpha in color to fade in |
---|
| 198 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1 |
---|
| 199 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2 |
---|
| 200 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2 * progress); // light grey |
---|
[8255] | 201 | |
---|
[8495] | 202 | // increase radius for more "heavy" rain |
---|
| 203 | RainEffect::rainParticles->setRadius(0, 0.03 * progress); |
---|
| 204 | RainEffect::rainParticles->setRadius(0.2, 0.02 * progress); |
---|
| 205 | RainEffect::rainParticles->setRadius(1, 0.01 * progress); |
---|
[8255] | 206 | |
---|
[8495] | 207 | // increase sound volume |
---|
| 208 | // this->soundSource.fadein(this->rainBuffer, 10); |
---|
| 209 | } else if ( this->rainFadeOutDuration != 0 ) { |
---|
| 210 | if ( this->localTimer < this->rainFadeOutDuration ) { |
---|
| 211 | this->localTimer += dt; |
---|
| 212 | float progress = 1 - (this->localTimer / this->rainFadeOutDuration); |
---|
[8316] | 213 | |
---|
[8495] | 214 | // Fade In Light |
---|
| 215 | lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9); |
---|
| 216 | |
---|
| 217 | // use alpha in color to fade out |
---|
| 218 | RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1 |
---|
| 219 | RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2 |
---|
| 220 | RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2 * progress); // light grey |
---|
| 221 | |
---|
| 222 | // decrease radius |
---|
| 223 | RainEffect::rainParticles->setRadius(0, 0.03 * progress); |
---|
| 224 | RainEffect::rainParticles->setRadius(0.2, 0.02 * progress); |
---|
| 225 | RainEffect::rainParticles->setRadius(1, 0.01 * progress); |
---|
| 226 | |
---|
| 227 | // decrease sound volume |
---|
| 228 | // this->soundSource.fadeout(this->rainBuffer, 10); |
---|
| 229 | } else |
---|
| 230 | this->deactivate(); |
---|
| 231 | } |
---|
| 232 | |
---|
[7646] | 233 | } |
---|
[8255] | 234 | |
---|
| 235 | void RainEffect::startRaining() { |
---|
| 236 | |
---|
[8495] | 237 | if (this->rainActivate) |
---|
| 238 | this->deactivate(); |
---|
[8255] | 239 | |
---|
[8495] | 240 | if (!this->rainFadeInDuration > 0) |
---|
| 241 | this->rainFadeInDuration = 20; |
---|
[8255] | 242 | |
---|
[8495] | 243 | this->localTimer = 0; |
---|
| 244 | |
---|
| 245 | this->activate(); |
---|
| 246 | |
---|
[8255] | 247 | } |
---|
[8495] | 248 | |
---|
| 249 | void RainEffect::stopRaining() { |
---|
| 250 | |
---|
| 251 | if (!this->rainActivate) |
---|
| 252 | this->activate(); |
---|
| 253 | |
---|
| 254 | if (!this->rainFadeOutDuration > 0) |
---|
| 255 | this->rainFadeOutDuration = 20; |
---|
| 256 | |
---|
| 257 | this->localTimer = 0; |
---|
| 258 | |
---|
| 259 | } |
---|