| 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 |  | 
|---|
| 28 | #include "parser/tinyxml/tinyxml.h" | 
|---|
| 29 |  | 
|---|
| 30 | SHELL_COMMAND(activate, RainEffect, activateRain); | 
|---|
| 31 | SHELL_COMMAND(deactivate, RainEffect, deactivateRain); | 
|---|
| 32 |  | 
|---|
| 33 | using namespace std; | 
|---|
| 34 |  | 
|---|
| 35 | CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT); | 
|---|
| 36 |  | 
|---|
| 37 | // 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 | 
|---|
| 38 |  | 
|---|
| 39 | RainEffect::RainEffect(const TiXmlElement* root) | 
|---|
| 40 | { | 
|---|
| 41 |         this->setClassID(CL_RAIN_EFFECT, "RainEffect"); | 
|---|
| 42 |  | 
|---|
| 43 |         this->init(); | 
|---|
| 44 |  | 
|---|
| 45 |         if (root != NULL) | 
|---|
| 46 |                 this->loadParams(root); | 
|---|
| 47 |  | 
|---|
| 48 |         //load rain sound | 
|---|
| 49 |         if (this->rainBuffer != NULL) | 
|---|
| 50 |                 ResourceManager::getInstance()->unload(this->rainBuffer); | 
|---|
| 51 |         this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV); | 
|---|
| 52 |  | 
|---|
| 53 |         //load wind sound | 
|---|
| 54 |         if (this->rainWindForce > 0) { | 
|---|
| 55 |                 if (this->windBuffer != NULL) | 
|---|
| 56 |                         ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
| 57 |                 this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV); | 
|---|
| 58 |         } | 
|---|
| 59 |  | 
|---|
| 60 |   if(rainActivate) | 
|---|
| 61 |          this->activate(); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | RainEffect::~RainEffect() | 
|---|
| 65 | { | 
|---|
| 66 |         this->deactivate(); | 
|---|
| 67 |  | 
|---|
| 68 |         if (this->rainBuffer != NULL) | 
|---|
| 69 |                 ResourceManager::getInstance()->unload(this->rainBuffer); | 
|---|
| 70 |  | 
|---|
| 71 |         if (this->windBuffer != NULL) | 
|---|
| 72 |                 ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | void RainEffect::loadParams(const TiXmlElement* root) | 
|---|
| 76 | { | 
|---|
| 77 |         WeatherEffect::loadParams(root); | 
|---|
| 78 |  | 
|---|
| 79 |         LoadParam(root, "coord", this, RainEffect, setRainCoord); | 
|---|
| 80 |         LoadParam(root, "size", this, RainEffect, setRainSize); | 
|---|
| 81 |         LoadParam(root, "rate", this, RainEffect, setRainRate); | 
|---|
| 82 |         LoadParam(root, "velocity", this, RainEffect, setRainVelocity); | 
|---|
| 83 |         LoadParam(root, "life", this, RainEffect, setRainLife); | 
|---|
| 84 |         LoadParam(root, "wind", this, RainEffect, setRainWind); | 
|---|
| 85 |    | 
|---|
| 86 |   LOAD_PARAM_START_CYCLE(root, element); | 
|---|
| 87 |   { | 
|---|
| 88 |     LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption); | 
|---|
| 89 |   } | 
|---|
| 90 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
| 91 |    | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | bool RainEffect::init() | 
|---|
| 96 | { | 
|---|
| 97 |         //Default values | 
|---|
| 98 |   this->rainActivate = false; | 
|---|
| 99 |   this->rainMove = false; | 
|---|
| 100 |         this->rainCoord = Vector(500, 500, 500); | 
|---|
| 101 |         this->rainSize = Vector2D(1000, 1000); | 
|---|
| 102 |         this->rainRate = 3000; | 
|---|
| 103 |         this->rainVelocity = -300; | 
|---|
| 104 |         this->rainLife = 4; | 
|---|
| 105 |         this->rainMaxParticles = this->rainRate * this->rainLife; | 
|---|
| 106 |         this->rainWindForce  = 0; | 
|---|
| 107 |  | 
|---|
| 108 |         this->emitter = new PlaneEmitter(this->rainSize); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | SparkParticles* RainEffect::rainParticles = NULL; | 
|---|
| 113 |  | 
|---|
| 114 | bool RainEffect::activate() | 
|---|
| 115 | { | 
|---|
| 116 |         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" ); | 
|---|
| 117 |  | 
|---|
| 118 |         if (unlikely(RainEffect::rainParticles == NULL)) | 
|---|
| 119 |         { | 
|---|
| 120 |                 RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles); | 
|---|
| 121 |                 RainEffect::rainParticles->setName("RainParticles"); | 
|---|
| 122 |                 RainEffect::rainParticles->precache((int)this->rainLife); | 
|---|
| 123 |                 RainEffect::rainParticles->setLifeSpan(this->rainLife, 2); | 
|---|
| 124 |                 RainEffect::rainParticles->setRadius(0, 0.03); | 
|---|
| 125 |                 RainEffect::rainParticles->setRadius(0.2, 0.02); | 
|---|
| 126 |                 RainEffect::rainParticles->setRadius(1, 0.01); | 
|---|
| 127 |                 RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1 | 
|---|
| 128 |                 RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2 | 
|---|
| 129 |                 RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey | 
|---|
| 130 |         } | 
|---|
| 131 |  | 
|---|
| 132 |         this->emitter->setSystem(RainEffect::rainParticles); | 
|---|
| 133 |  | 
|---|
| 134 |         this->emitter->setRelCoor(this->rainCoord); | 
|---|
| 135 |  | 
|---|
| 136 |         this->emitter->setEmissionRate(this->rainRate); | 
|---|
| 137 |         this->emitter->setEmissionVelocity(this->rainVelocity); | 
|---|
| 138 |  | 
|---|
| 139 |         this->emitter->setSpread(this->rainWindForce / 50, 0.2); | 
|---|
| 140 |          | 
|---|
| 141 |         this->soundSource.loop(this->rainBuffer, 0.8f); | 
|---|
| 142 |         if (this->rainWindForce > 0) | 
|---|
| 143 |                 this->soundSource.loop(this->windBuffer, 0.5f); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | bool RainEffect::deactivate() | 
|---|
| 148 | { | 
|---|
| 149 |         PRINTF(0)("Deactivating RainEffect\n"); | 
|---|
| 150 |         this->emitter->setSystem(NULL); | 
|---|
| 151 |  | 
|---|
| 152 |         this->soundSource.stop(); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | void RainEffect::tick (float dt) | 
|---|
| 156 | { | 
|---|
| 157 |         if (this->rainMove) { | 
|---|
| 158 |                 this->rainCoord = State::getCameraNode()->getAbsCoor(); | 
|---|
| 159 |                 this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z); | 
|---|
| 160 |         } | 
|---|
| 161 | } | 
|---|