| 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 "snow_effect.h" | 
|---|
| 16 |  | 
|---|
| 17 | #include "util/loading/load_param.h" | 
|---|
| 18 | #include "util/loading/factory.h" | 
|---|
| 19 | #include "sound/resource_sound_buffer.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "glincl.h" | 
|---|
| 22 | #include "debug.h" | 
|---|
| 23 |  | 
|---|
| 24 | #include "p_node.h" | 
|---|
| 25 | #include "state.h" | 
|---|
| 26 | #include "particles/sprite_particles.h" | 
|---|
| 27 | #include "particles/plane_emitter.h" | 
|---|
| 28 | #include "shell_command.h" | 
|---|
| 29 | #include "script_class.h" | 
|---|
| 30 | #include "cloud_effect.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "class_id_DEPRECATED.h" | 
|---|
| 33 |  | 
|---|
| 34 | ObjectListDefinitionID(SnowEffect, CL_SNOW_EFFECT); | 
|---|
| 35 | SHELL_COMMAND(activate, SnowEffect, activateSnow); | 
|---|
| 36 | SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow); | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | CREATE_SCRIPTABLE_CLASS(SnowEffect, | 
|---|
| 40 |                         addMethod("activate", Executor0<SnowEffect, lua_State*>(&SnowEffect::activate)) | 
|---|
| 41 |                         ->addMethod("deactivate", Executor0<SnowEffect, lua_State*>(&SnowEffect::deactivate)) | 
|---|
| 42 |                        ); | 
|---|
| 43 |  | 
|---|
| 44 | CREATE_FACTORY(SnowEffect); | 
|---|
| 45 |  | 
|---|
| 46 | SnowEffect::SnowEffect(const TiXmlElement* root) | 
|---|
| 47 | { | 
|---|
| 48 |   this->registerObject(this, SnowEffect::_objectList); | 
|---|
| 49 |  | 
|---|
| 50 |   this->init(); | 
|---|
| 51 |  | 
|---|
| 52 |   if (root != NULL) | 
|---|
| 53 |     this->loadParams(root); | 
|---|
| 54 |  | 
|---|
| 55 |   //load wind sound | 
|---|
| 56 |   if (this->snowWindForce >= 1) | 
|---|
| 57 |   { | 
|---|
| 58 |     this->windBuffer = OrxSound::ResourceSoundBuffer("sound/atmosphere/wind.wav"); | 
|---|
| 59 |   } | 
|---|
| 60 |  | 
|---|
| 61 |   if(snowActivate) | 
|---|
| 62 |   { | 
|---|
| 63 |     this->activate(); | 
|---|
| 64 |     SnowEffect::snowParticles->precache((int) this->snowLife); | 
|---|
| 65 |   } | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | SnowEffect::~SnowEffect() | 
|---|
| 70 | { | 
|---|
| 71 |   this->deactivate(); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | SpriteParticles* SnowEffect::snowParticles = NULL; | 
|---|
| 75 |  | 
|---|
| 76 | void SnowEffect::loadParams(const TiXmlElement* root) | 
|---|
| 77 | { | 
|---|
| 78 |   WeatherEffect::loadParams(root); | 
|---|
| 79 |  | 
|---|
| 80 |   LoadParam(root, "numParticles", this, SnowEffect, numParticles); | 
|---|
| 81 |   LoadParam(root, "materialTexture", this, SnowEffect, materialTexture); | 
|---|
| 82 |   LoadParam(root, "lifeSpans", this, SnowEffect, lifeSpan); | 
|---|
| 83 |   LoadParam(root, "radius", this, SnowEffect, radius); | 
|---|
| 84 |   LoadParam(root, "mass", this, SnowEffect, mass); | 
|---|
| 85 |   LoadParam(root, "emissionRate", this, SnowEffect, emissionRate); | 
|---|
| 86 |   LoadParam(root, "emissionVelocity", this, SnowEffect, emissionVelocity); | 
|---|
| 87 |   LoadParam(root, "wind", this, SnowEffect, wind); | 
|---|
| 88 |   LoadParam(root, "size", this, SnowEffect, size); | 
|---|
| 89 |   LoadParam(root, "coord", this, SnowEffect, coord); | 
|---|
| 90 |   LoadParam(root, "cloudcolor", this, SnowEffect, setCloudColor); | 
|---|
| 91 |   LoadParam(root, "skycolor", this, SnowEffect, setSkyColor); | 
|---|
| 92 |   LoadParam(root, "fadetime", this, SnowEffect, setFadeTime); | 
|---|
| 93 |  | 
|---|
| 94 |   LOAD_PARAM_START_CYCLE(root, element); | 
|---|
| 95 |   { | 
|---|
| 96 |     LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption); | 
|---|
| 97 |   } | 
|---|
| 98 |   LOAD_PARAM_END_CYCLE(element); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void SnowEffect::init() | 
|---|
| 102 | { | 
|---|
| 103 |   this->emitter = new PlaneEmitter(); | 
|---|
| 104 |  | 
|---|
| 105 |   // Default values | 
|---|
| 106 |   this->snowActivate = false; | 
|---|
| 107 |   this->snowMove = false; | 
|---|
| 108 |   this->particles = 12000; | 
|---|
| 109 |   this->texture = "maps/snow_flake_01_32x32.png"; | 
|---|
| 110 |   this->snowLife = 8; | 
|---|
| 111 |   this->randomLife = 2; | 
|---|
| 112 |   this->snowRadius = 3.5; | 
|---|
| 113 |   this->randomRadius = 1; | 
|---|
| 114 |   this->snowMass = 1.0; | 
|---|
| 115 |   this->randomMass = 0.3; | 
|---|
| 116 |   this->rate = 900; | 
|---|
| 117 |   this->velocity = -100; | 
|---|
| 118 |   this->randomVelocity = 5; | 
|---|
| 119 |   this->angle = 0.5; | 
|---|
| 120 |   this->randomAngle = 0.2; | 
|---|
| 121 |   this->alpha = 0.5; | 
|---|
| 122 |   this->snowSize = Vector2D(2500, 2500); | 
|---|
| 123 |   this->snowCoord = Vector(100,450,400); | 
|---|
| 124 |   this->snowWindForce = 1; | 
|---|
| 125 |  | 
|---|
| 126 |   this->fadeTime = 10; | 
|---|
| 127 |   this->cloudColor = Vector(0.2f, 0.2f, 0.2f); | 
|---|
| 128 |   this->skyColor = Vector(0.0f, 0.0f, 0.0f); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void SnowEffect::activate() | 
|---|
| 132 | { | 
|---|
| 133 |   PRINTF(3)("Activating SnowEffect\n"); | 
|---|
| 134 |  | 
|---|
| 135 |   this->snowActivate = true; | 
|---|
| 136 |  | 
|---|
| 137 |   SnowEffect::snowParticles = new SpriteParticles(particles); | 
|---|
| 138 |   SnowEffect::snowParticles->setName("SnowEffectTrailParticles"); | 
|---|
| 139 |   SnowEffect::snowParticles->setMaterialTexture(texture); | 
|---|
| 140 |   SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife); | 
|---|
| 141 |   SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius); | 
|---|
| 142 |   SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8); | 
|---|
| 143 |   SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5); | 
|---|
| 144 |   SnowEffect::snowParticles->setMass(0, snowMass, randomMass); | 
|---|
| 145 |   SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
| 146 |   SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
| 147 |   SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
| 148 |  | 
|---|
| 149 |   this->emitter->setSystem(SnowEffect::snowParticles); | 
|---|
| 150 |  | 
|---|
| 151 |   this->emitter->setRelCoor(snowCoord); | 
|---|
| 152 |   this->emitter->setEmissionRate(rate); | 
|---|
| 153 |   this->emitter->setEmissionVelocity(velocity, randomVelocity); | 
|---|
| 154 |   this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce); | 
|---|
| 155 |   this->emitter->setSize(snowSize); | 
|---|
| 156 |  | 
|---|
| 157 |   if (this->snowWindForce != 0) | 
|---|
| 158 |     this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true); | 
|---|
| 159 |  | 
|---|
| 160 |   // Store cloud- and sky color before the snow | 
|---|
| 161 |   this->oldCloudColor = CloudEffect::cloudColor; | 
|---|
| 162 |   this->oldSkyColor   = CloudEffect::skyColor; | 
|---|
| 163 |  | 
|---|
| 164 |   // Change the colors | 
|---|
| 165 |   CloudEffect::changeCloudColor(this->cloudColor, this->fadeTime); | 
|---|
| 166 |   CloudEffect::changeSkyColor(this->skyColor, this->fadeTime); | 
|---|
| 167 |  | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 |  | 
|---|
| 171 | void SnowEffect::deactivate() | 
|---|
| 172 | { | 
|---|
| 173 |   PRINTF(3)("Deactivating SnowEffect\n"); | 
|---|
| 174 |  | 
|---|
| 175 |   this->snowActivate = false; | 
|---|
| 176 |   this->emitter->setSystem(NULL); | 
|---|
| 177 |  | 
|---|
| 178 |   // Restore the old cloud- and sky color | 
|---|
| 179 |   CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime); | 
|---|
| 180 |   CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime); | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | void SnowEffect::draw() const | 
|---|
| 184 | { | 
|---|
| 185 |   if (!this->snowActivate) | 
|---|
| 186 |     return; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | void SnowEffect::tick(float dt) | 
|---|
| 190 | { | 
|---|
| 191 |   if (!this->snowActivate) | 
|---|
| 192 |     return; | 
|---|
| 193 |  | 
|---|
| 194 |   /* | 
|---|
| 195 |   float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len(); | 
|---|
| 196 |  | 
|---|
| 197 |   if(activated) | 
|---|
| 198 |   { | 
|---|
| 199 |   if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y) | 
|---|
| 200 |                   this->deactivate(); | 
|---|
| 201 |   else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y) | 
|---|
| 202 |   this->alpha = 0.15; | 
|---|
| 203 |   else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y) | 
|---|
| 204 |   this->alpha = 0.25; | 
|---|
| 205 |   else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y) | 
|---|
| 206 |   this->alpha = 0.4; | 
|---|
| 207 |  | 
|---|
| 208 |   SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
| 209 |   SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
| 210 |   SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
| 211 |   } | 
|---|
| 212 |   else | 
|---|
| 213 |   { | 
|---|
| 214 |   if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y ) | 
|---|
| 215 |   this->activate(); | 
|---|
| 216 |   if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y ) | 
|---|
| 217 |   this->alpha = 0.25; | 
|---|
| 218 |   else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y ) | 
|---|
| 219 |   this->alpha = 0.4; | 
|---|
| 220 |   else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y ) | 
|---|
| 221 |   this->alpha = 0.5; | 
|---|
| 222 |  | 
|---|
| 223 |   SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
| 224 |   SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
| 225 |   SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
| 226 |   }*/ | 
|---|
| 227 |  | 
|---|
| 228 |   if (this->snowMove) | 
|---|
| 229 |   { | 
|---|
| 230 |     this->snowCoord = State::getCameraNode()->getAbsCoor(); | 
|---|
| 231 |     this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z); | 
|---|
| 232 |   } | 
|---|
| 233 | } | 
|---|