| [7573] | 1 | /* | 
|---|
| [7652] | 2 |         orxonox - the future of 3D-vertical-scrollers | 
|---|
| [7573] | 3 |  | 
|---|
| [7652] | 4 |         Copyright (C) 2004 orx | 
|---|
| [7573] | 5 |  | 
|---|
| [7652] | 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. | 
|---|
| [7573] | 10 |  | 
|---|
 | 11 | ### File Specific: | 
|---|
| [7652] | 12 |         main-programmer: hdavid, amaechler | 
|---|
| [7573] | 13 | */ | 
|---|
 | 14 |  | 
|---|
 | 15 | #include "snow_effect.h" | 
|---|
 | 16 |  | 
|---|
 | 17 | #include "util/loading/load_param.h" | 
|---|
 | 18 | #include "util/loading/factory.h" | 
|---|
| [7696] | 19 | #include "util/loading/resource_manager.h" | 
|---|
| [7573] | 20 |  | 
|---|
 | 21 | #include "glincl.h" | 
|---|
| [7576] | 22 | #include "debug.h" | 
|---|
| [7573] | 23 |  | 
|---|
| [7650] | 24 | #include "p_node.h" | 
|---|
| [7573] | 25 | #include "state.h" | 
|---|
 | 26 | #include "sprite_particles.h" | 
|---|
 | 27 | #include "plane_emitter.h" | 
|---|
| [7649] | 28 | #include "shell_command.h" | 
|---|
| [7573] | 29 |  | 
|---|
 | 30 | #include "parser/tinyxml/tinyxml.h" | 
|---|
 | 31 |  | 
|---|
| [7649] | 32 | SHELL_COMMAND(activate, SnowEffect, activateSnow); | 
|---|
 | 33 | SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow); | 
|---|
| [7576] | 34 |  | 
|---|
| [7573] | 35 | using namespace std; | 
|---|
 | 36 |  | 
|---|
 | 37 | CREATE_FACTORY(SnowEffect, CL_SNOW_EFFECT); | 
|---|
 | 38 |  | 
|---|
 | 39 | SnowEffect::SnowEffect(const TiXmlElement* root) | 
|---|
 | 40 | { | 
|---|
| [7652] | 41 |         this->setClassID(CL_SNOW_EFFECT, "SnowEffect"); | 
|---|
| [7573] | 42 |  | 
|---|
| [7652] | 43 |         this->init(); | 
|---|
| [7651] | 44 |  | 
|---|
| [7652] | 45 |         if (root != NULL) | 
|---|
 | 46 |                 this->loadParams(root); | 
|---|
| [7573] | 47 |  | 
|---|
| [7696] | 48 |         //load wind sound | 
|---|
 | 49 |         if (this->snowWindForce > 1) { | 
|---|
 | 50 |                 if (this->windBuffer != NULL) | 
|---|
 | 51 |                         ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
| [7810] | 52 |                         this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV); | 
|---|
| [7696] | 53 |         } | 
|---|
 | 54 |  | 
|---|
| [7652] | 55 |         this->activate(); | 
|---|
| [7573] | 56 | } | 
|---|
 | 57 |  | 
|---|
 | 58 |  | 
|---|
 | 59 | SnowEffect::~SnowEffect() | 
|---|
 | 60 | { | 
|---|
| [7652] | 61 |         this->deactivate(); | 
|---|
| [7573] | 62 | } | 
|---|
 | 63 |  | 
|---|
 | 64 | SpriteParticles* SnowEffect::snowParticles = NULL; | 
|---|
 | 65 |  | 
|---|
 | 66 | void SnowEffect::loadParams(const TiXmlElement* root) | 
|---|
 | 67 | { | 
|---|
| [7652] | 68 |         WeatherEffect::loadParams(root); | 
|---|
| [7573] | 69 |  | 
|---|
| [7652] | 70 |         LoadParam(root, "numParticles", this, SnowEffect, numParticles); | 
|---|
 | 71 |         LoadParam(root, "materialTexture", this, SnowEffect, materialTexture); | 
|---|
 | 72 |         LoadParam(root, "lifeSpans", this, SnowEffect, lifeSpan); | 
|---|
 | 73 |         LoadParam(root, "radius", this, SnowEffect, radius); | 
|---|
 | 74 |         LoadParam(root, "mass", this, SnowEffect, mass); | 
|---|
 | 75 |         LoadParam(root, "emissionRate", this, SnowEffect, emissionRate); | 
|---|
 | 76 |         LoadParam(root, "emissionVelocity", this, SnowEffect, emissionVelocity); | 
|---|
| [7683] | 77 |   LoadParam(root, "wind", this, SnowEffect, wind); | 
|---|
| [7652] | 78 |         LoadParam(root, "size", this, SnowEffect, size); | 
|---|
 | 79 |         LoadParam(root, "coord", this, SnowEffect, coord); | 
|---|
| [7573] | 80 | } | 
|---|
 | 81 |  | 
|---|
 | 82 | bool SnowEffect::init() | 
|---|
 | 83 | { | 
|---|
| [7652] | 84 |         this->emitter = new PlaneEmitter(); | 
|---|
| [7651] | 85 |  | 
|---|
| [7652] | 86 |         // Default values | 
|---|
| [7696] | 87 |         this->particles = 12000; | 
|---|
 | 88 |         this->texture = "maps/snow_flake_01_32x32.png"; | 
|---|
 | 89 |         this->life = 8; | 
|---|
 | 90 |         this->randomLife = 2; | 
|---|
 | 91 |         this->snowRadius = 3.5; | 
|---|
 | 92 |         this->randomRadius = 1; | 
|---|
 | 93 |         this->snowMass = 1.0; | 
|---|
 | 94 |         this->randomMass = 0.3; | 
|---|
 | 95 |         this->rate = 900; | 
|---|
 | 96 |         this->velocity = -100; | 
|---|
 | 97 |         this->randomVelocity = 5; | 
|---|
 | 98 |         this->angle = 0.5; | 
|---|
 | 99 |         this->randomAngle = 0.2; | 
|---|
 | 100 |         this->alpha = 0.5; | 
|---|
 | 101 |         this->snowSize = Vector2D(2500, 2500); | 
|---|
 | 102 |   this->snowCoord = Vector(100,450,400); | 
|---|
 | 103 |         this->snowWindForce = 1; | 
|---|
| [7651] | 104 |  | 
|---|
| [7696] | 105 |         this->activated = false; | 
|---|
| [7573] | 106 | } | 
|---|
 | 107 |  | 
|---|
 | 108 | bool SnowEffect::activate() | 
|---|
 | 109 | { | 
|---|
| [7652] | 110 |         PRINTF(0)("Activating SnowEffect\n"); | 
|---|
 | 111 |         activated = true; | 
|---|
| [7576] | 112 |  | 
|---|
| [7652] | 113 |         SnowEffect::snowParticles = new SpriteParticles(particles); | 
|---|
 | 114 |         SnowEffect::snowParticles->setName("SnowEffectTrailParticles"); | 
|---|
 | 115 |         SnowEffect::snowParticles->setMaterialTexture(texture); | 
|---|
 | 116 |         SnowEffect::snowParticles->setLifeSpan(life, randomLife); | 
|---|
 | 117 |         SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius); | 
|---|
| [7683] | 118 |         SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8); | 
|---|
 | 119 |         SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5); | 
|---|
| [7652] | 120 |         SnowEffect::snowParticles->setMass(0, snowMass, randomMass); | 
|---|
 | 121 |         SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
 | 122 |         SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
 | 123 |         SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
 | 124 |  | 
|---|
 | 125 |         this->emitter->setSystem(SnowEffect::snowParticles); | 
|---|
 | 126 |  | 
|---|
 | 127 |         this->emitter->setRelCoor(snowCoord); | 
|---|
 | 128 |         this->emitter->setEmissionRate(rate); | 
|---|
 | 129 |         this->emitter->setEmissionVelocity(velocity, randomVelocity); | 
|---|
| [7696] | 130 |         this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce); | 
|---|
| [7652] | 131 |         this->emitter->setSize(snowSize); | 
|---|
| [7696] | 132 |  | 
|---|
| [7683] | 133 |   //SnowEffect::snowParticles->precache(8); | 
|---|
| [7696] | 134 |  | 
|---|
 | 135 |         if (this->snowWindForce > 1) | 
|---|
 | 136 |                 this->soundSource.loop(this->windBuffer); | 
|---|
| [7573] | 137 | } | 
|---|
 | 138 |  | 
|---|
 | 139 |  | 
|---|
 | 140 | bool SnowEffect::deactivate() | 
|---|
 | 141 | { | 
|---|
| [7652] | 142 |         PRINTF(0)("Deactivating SnowEffect\n"); | 
|---|
 | 143 |         activated = false; | 
|---|
 | 144 |  | 
|---|
 | 145 |         this->emitter->setSystem(NULL); | 
|---|
| [7696] | 146 |  | 
|---|
 | 147 |         if (this->windBuffer != NULL) | 
|---|
 | 148 |                 ResourceManager::getInstance()->unload(this->windBuffer); | 
|---|
 | 149 |  | 
|---|
| [7573] | 150 | } | 
|---|
 | 151 |  | 
|---|
| [7649] | 152 | void SnowEffect::activateSnow() | 
|---|
 | 153 | { | 
|---|
| [7652] | 154 |         this->activate(); | 
|---|
| [7649] | 155 | } | 
|---|
 | 156 |  | 
|---|
 | 157 | void SnowEffect::deactivateSnow() | 
|---|
 | 158 | { | 
|---|
| [7652] | 159 |         this->deactivate(); | 
|---|
| [7649] | 160 | } | 
|---|
 | 161 |  | 
|---|
| [7573] | 162 | void SnowEffect::draw() const | 
|---|
 | 163 | { | 
|---|
 | 164 | } | 
|---|
 | 165 |  | 
|---|
 | 166 | void SnowEffect::tick(float dt) | 
|---|
 | 167 | { | 
|---|
| [7683] | 168 |   float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len(); | 
|---|
 | 169 |    | 
|---|
 | 170 |   if(activated) | 
|---|
 | 171 |   { | 
|---|
 | 172 |     if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y) | 
|---|
 | 173 |                   this->deactivate(); | 
|---|
 | 174 |     else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y)  | 
|---|
 | 175 |       this->alpha = 0.15; | 
|---|
 | 176 |     else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y)  | 
|---|
 | 177 |       this->alpha = 0.25; | 
|---|
 | 178 |     else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y)  | 
|---|
 | 179 |       this->alpha = 0.4; | 
|---|
 | 180 |  | 
|---|
 | 181 |     SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
 | 182 |     SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
 | 183 |     SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
 | 184 |   } | 
|---|
 | 185 |   else | 
|---|
 | 186 |   { | 
|---|
 | 187 |   if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y ) | 
|---|
 | 188 |     this->activate(); | 
|---|
 | 189 |   if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y ) | 
|---|
 | 190 |     this->alpha = 0.25; | 
|---|
 | 191 |   else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y ) | 
|---|
 | 192 |     this->alpha = 0.4; | 
|---|
 | 193 |   else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y ) | 
|---|
 | 194 |     this->alpha = 0.5; | 
|---|
 | 195 |  | 
|---|
 | 196 |     SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); | 
|---|
 | 197 |     SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); | 
|---|
 | 198 |     SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); | 
|---|
 | 199 |   } | 
|---|
| [7573] | 200 | } | 
|---|
| [7649] | 201 |  | 
|---|