| 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 "rain_effect.h" | 
|---|
| 16 |  | 
|---|
| 17 | #include "util/loading/load_param.h" | 
|---|
| 18 | #include "util/loading/factory.h" | 
|---|
| 19 | #include "util/loading/resource_manager.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "glincl.h" | 
|---|
| 22 | #include "p_node.h" | 
|---|
| 23 | #include "state.h" | 
|---|
| 24 | #include "spark_particles.h" | 
|---|
| 25 | #include "plane_emitter.h" | 
|---|
| 26 | #include "shell_command.h" | 
|---|
| 27 | #include "light.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include "parser/tinyxml/tinyxml.h" | 
|---|
| 30 |  | 
|---|
| 31 | SHELL_COMMAND(activate, RainEffect, activateRain); | 
|---|
| 32 | SHELL_COMMAND(deactivate, RainEffect, deactivateRain); | 
|---|
| 33 |  | 
|---|
| 34 | SHELL_COMMAND(startRaining, RainEffect, startRaining); | 
|---|
| 35 |  | 
|---|
| 36 | using namespace std; | 
|---|
| 37 |  | 
|---|
| 38 | CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT); | 
|---|
| 39 |  | 
|---|
| 40 | // TODO: Dim Light with Rain, Finish preCaching, check out if multiple rain emitters work,  Think about what happens with building poss. to hang movewithcam off, benchmark, possible to activate lightening, turn off visibility when in a building, variable emitter size depending on playable, also rain velocity | 
|---|
| 41 |  | 
|---|
| 42 | RainEffect::RainEffect(const TiXmlElement* root) | 
|---|
| 43 | { | 
|---|
| 44 |         this->setClassID(CL_RAIN_EFFECT, "RainEffect"); | 
|---|
| 45 |  | 
|---|
| 46 |         this->init(); | 
|---|
| 47 |  | 
|---|
| 48 |         if (root != NULL) | 
|---|
| 49 |                 this->loadParams(root); | 
|---|
| 50 |  | 
|---|
| 51 |         //load rain sound | 
|---|
| 52 |         if (this->rainBuffer != NULL) | 
|---|
| 53 |                 ResourceManager::getInstance()->unload(this->rainBuffer); | 
|---|
| 54 |         this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV); | 
|---|
| 55 |  | 
|---|
| 56 |         //load wind sound | 
|---|
| 57 |         if (this->rainWindForce != 0) { | 
|---|
| 58 |                 if (this->windBuffer != NULL) | 
|---|
| 59 |                         ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
| 60 |                 this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV); | 
|---|
| 61 |         } | 
|---|
| 62 |  | 
|---|
| 63 |         if(rainActivate) | 
|---|
| 64 |                 this->activate(); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | RainEffect::~RainEffect() | 
|---|
| 68 | { | 
|---|
| 69 |         this->deactivate(); | 
|---|
| 70 |  | 
|---|
| 71 |         if (this->rainBuffer != NULL) | 
|---|
| 72 |                 ResourceManager::getInstance()->unload(this->rainBuffer); | 
|---|
| 73 |  | 
|---|
| 74 |         if (this->windBuffer != NULL) | 
|---|
| 75 |                 ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void RainEffect::loadParams(const TiXmlElement* root) | 
|---|
| 79 | { | 
|---|
| 80 |         WeatherEffect::loadParams(root); | 
|---|
| 81 |  | 
|---|
| 82 |         LoadParam(root, "coord", this, RainEffect, setRainCoord); | 
|---|
| 83 |         LoadParam(root, "size", this, RainEffect, setRainSize); | 
|---|
| 84 |         LoadParam(root, "rate", this, RainEffect, setRainRate); | 
|---|
| 85 |         LoadParam(root, "velocity", this, RainEffect, setRainVelocity); | 
|---|
| 86 |         LoadParam(root, "life", this, RainEffect, setRainLife); | 
|---|
| 87 |         LoadParam(root, "wind", this, RainEffect, setRainWind); | 
|---|
| 88 |         LoadParam(root, "startraining", this, RainEffect, setRainStart); | 
|---|
| 89 |  | 
|---|
| 90 |         LOAD_PARAM_START_CYCLE(root, element); | 
|---|
| 91 |         { | 
|---|
| 92 |                 LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption); | 
|---|
| 93 |         } | 
|---|
| 94 |         LOAD_PARAM_END_CYCLE(element); | 
|---|
| 95 |  | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | bool RainEffect::init() | 
|---|
| 100 | { | 
|---|
| 101 |         //Default values | 
|---|
| 102 |         this->rainActivate = false; | 
|---|
| 103 |         this->rainMove = false; | 
|---|
| 104 |         this->rainCoord = Vector(500, 500, 500); | 
|---|
| 105 |         this->rainSize = Vector2D(1000, 1000); | 
|---|
| 106 |         this->rainRate = 3000; | 
|---|
| 107 |         this->rainVelocity = -300; | 
|---|
| 108 |         this->rainLife = 4; | 
|---|
| 109 |         this->rainMaxParticles = this->rainRate * this->rainLife; | 
|---|
| 110 |         this->rainWindForce  = 0; | 
|---|
| 111 |         this->rainStartDuration = 0; | 
|---|
| 112 |         this->localTimer = 0; | 
|---|
| 113 |         this->soundRainVolume = 0.8f; | 
|---|
| 114 |  | 
|---|
| 115 |         this->emitter = new PlaneEmitter(this->rainSize); | 
|---|
| 116 |  | 
|---|
| 117 |         lightMan = LightManager::getInstance(); | 
|---|
| 118 |         this->rainAmbient = lightMan->getAmbientColor(); | 
|---|
| 119 |  | 
|---|
| 120 |         return true; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | SparkParticles* RainEffect::rainParticles = NULL; | 
|---|
| 125 |  | 
|---|
| 126 | bool RainEffect::activate() | 
|---|
| 127 | { | 
|---|
| 128 |         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" ); | 
|---|
| 129 |  | 
|---|
| 130 |         this->rainActivate = true; | 
|---|
| 131 |  | 
|---|
| 132 |         if (unlikely(RainEffect::rainParticles == NULL)) | 
|---|
| 133 |         { | 
|---|
| 134 |                 RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles); | 
|---|
| 135 |                 RainEffect::rainParticles->setName("RainParticles"); | 
|---|
| 136 |                 RainEffect::rainParticles->precache((int)this->rainLife); | 
|---|
| 137 |                 RainEffect::rainParticles->setLifeSpan(this->rainLife, 2); | 
|---|
| 138 |                 RainEffect::rainParticles->setRadius(0, 0.03); | 
|---|
| 139 |                 RainEffect::rainParticles->setRadius(0.2, 0.02); | 
|---|
| 140 |                 RainEffect::rainParticles->setRadius(1, 0.01); | 
|---|
| 141 |                 RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2);     // grey blue 1 | 
|---|
| 142 |                 RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2);   // grey blue 2 | 
|---|
| 143 |                 RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2);     // light grey | 
|---|
| 144 |         } | 
|---|
| 145 |  | 
|---|
| 146 |         this->emitter->setSystem(RainEffect::rainParticles); | 
|---|
| 147 |  | 
|---|
| 148 |         this->emitter->setRelCoor(this->rainCoord); | 
|---|
| 149 |  | 
|---|
| 150 |         this->emitter->setEmissionRate(this->rainRate); | 
|---|
| 151 |         this->emitter->setEmissionVelocity(this->rainVelocity); | 
|---|
| 152 |  | 
|---|
| 153 |         this->emitter->setSpread(this->rainWindForce / 50, 0.2); | 
|---|
| 154 |  | 
|---|
| 155 |         this->soundSource.loop(this->rainBuffer, this->soundRainVolume); | 
|---|
| 156 |         if (this->rainWindForce != 0) this->soundSource.loop(this->windBuffer, 0.1f * this->rainWindForce); | 
|---|
| 157 |  | 
|---|
| 158 |         lightMan->setAmbientColor(.1,.1,.1); | 
|---|
| 159 |  | 
|---|
| 160 |         return true; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | bool RainEffect::deactivate() | 
|---|
| 165 | { | 
|---|
| 166 |         PRINTF(0)("Deactivating RainEffect\n"); | 
|---|
| 167 |  | 
|---|
| 168 |         this->rainActivate = false; | 
|---|
| 169 |         this->emitter->setSystem(NULL); | 
|---|
| 170 |  | 
|---|
| 171 |         // Stop Sound | 
|---|
| 172 |         this->soundSource.stop(); | 
|---|
| 173 |  | 
|---|
| 174 |         // Restore Light Ambient | 
|---|
| 175 |         lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient); | 
|---|
| 176 |  | 
|---|
| 177 |         return true; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | void RainEffect::tick (float dt) | 
|---|
| 181 | { | 
|---|
| 182 |         if (!this->rainActivate) | 
|---|
| 183 |                 return; | 
|---|
| 184 |  | 
|---|
| 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 |         } | 
|---|
| 189 |  | 
|---|
| 190 |         if (this->rainStartDuration != 0 && this->localTimer < this->rainStartDuration) { | 
|---|
| 191 |                 this->localTimer += dt; | 
|---|
| 192 |                 float progress = this->localTimer / this->rainStartDuration; | 
|---|
| 193 |  | 
|---|
| 194 |                 // Dim Light | 
|---|
| 195 |                 lightMan->setAmbientColor(1.1 - progress, 1.1 - progress, 1.1 - progress); | 
|---|
| 196 |  | 
|---|
| 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 | 
|---|
| 201 |  | 
|---|
| 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); | 
|---|
| 206 |  | 
|---|
| 207 |                 // increase sound volume | 
|---|
| 208 |                 // this->soundSource.fadein(this->rainBuffer, 10); | 
|---|
| 209 |         } | 
|---|
| 210 |  | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | void RainEffect::startRaining() { | 
|---|
| 214 |  | 
|---|
| 215 |         if (this->rainActivate) | 
|---|
| 216 |                 this->deactivate(); | 
|---|
| 217 |  | 
|---|
| 218 |         this->rainStartDuration = 10; | 
|---|
| 219 |         this->localTimer = 0; | 
|---|
| 220 |         this->activate(); | 
|---|
| 221 |  | 
|---|
| 222 | } | 
|---|