[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); |
---|
[8495] | 52 | this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV); |
---|
[7696] | 53 | } |
---|
| 54 | |
---|
[8495] | 55 | if(snowActivate) { |
---|
[8255] | 56 | this->activate(); |
---|
[8495] | 57 | SnowEffect::snowParticles->precache((int) this->snowLife); |
---|
[8793] | 58 | } |
---|
[7573] | 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | SnowEffect::~SnowEffect() |
---|
| 63 | { |
---|
[7652] | 64 | this->deactivate(); |
---|
[7573] | 65 | } |
---|
| 66 | |
---|
| 67 | SpriteParticles* SnowEffect::snowParticles = NULL; |
---|
| 68 | |
---|
| 69 | void SnowEffect::loadParams(const TiXmlElement* root) |
---|
| 70 | { |
---|
[7652] | 71 | WeatherEffect::loadParams(root); |
---|
[7573] | 72 | |
---|
[7652] | 73 | LoadParam(root, "numParticles", this, SnowEffect, numParticles); |
---|
| 74 | LoadParam(root, "materialTexture", this, SnowEffect, materialTexture); |
---|
| 75 | LoadParam(root, "lifeSpans", this, SnowEffect, lifeSpan); |
---|
| 76 | LoadParam(root, "radius", this, SnowEffect, radius); |
---|
| 77 | LoadParam(root, "mass", this, SnowEffect, mass); |
---|
| 78 | LoadParam(root, "emissionRate", this, SnowEffect, emissionRate); |
---|
| 79 | LoadParam(root, "emissionVelocity", this, SnowEffect, emissionVelocity); |
---|
[8255] | 80 | LoadParam(root, "wind", this, SnowEffect, wind); |
---|
[7652] | 81 | LoadParam(root, "size", this, SnowEffect, size); |
---|
| 82 | LoadParam(root, "coord", this, SnowEffect, coord); |
---|
[8316] | 83 | |
---|
[8255] | 84 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 85 | { |
---|
| 86 | LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption); |
---|
| 87 | } |
---|
| 88 | LOAD_PARAM_END_CYCLE(element); |
---|
[7573] | 89 | } |
---|
| 90 | |
---|
[8495] | 91 | void SnowEffect::init() |
---|
[7573] | 92 | { |
---|
[7652] | 93 | this->emitter = new PlaneEmitter(); |
---|
[7651] | 94 | |
---|
[7652] | 95 | // Default values |
---|
[8255] | 96 | this->snowActivate = false; |
---|
| 97 | this->snowMove = false; |
---|
[7696] | 98 | this->particles = 12000; |
---|
| 99 | this->texture = "maps/snow_flake_01_32x32.png"; |
---|
[8495] | 100 | this->snowLife = 8; |
---|
[7696] | 101 | this->randomLife = 2; |
---|
| 102 | this->snowRadius = 3.5; |
---|
| 103 | this->randomRadius = 1; |
---|
| 104 | this->snowMass = 1.0; |
---|
| 105 | this->randomMass = 0.3; |
---|
| 106 | this->rate = 900; |
---|
| 107 | this->velocity = -100; |
---|
| 108 | this->randomVelocity = 5; |
---|
| 109 | this->angle = 0.5; |
---|
| 110 | this->randomAngle = 0.2; |
---|
| 111 | this->alpha = 0.5; |
---|
| 112 | this->snowSize = Vector2D(2500, 2500); |
---|
[8255] | 113 | this->snowCoord = Vector(100,450,400); |
---|
[7696] | 114 | this->snowWindForce = 1; |
---|
[7573] | 115 | } |
---|
| 116 | |
---|
[8495] | 117 | void SnowEffect::activate() |
---|
[7573] | 118 | { |
---|
[9006] | 119 | PRINTF(3)("Activating SnowEffect\n"); |
---|
[7576] | 120 | |
---|
[8255] | 121 | this->snowActivate = true; |
---|
| 122 | |
---|
[7652] | 123 | SnowEffect::snowParticles = new SpriteParticles(particles); |
---|
| 124 | SnowEffect::snowParticles->setName("SnowEffectTrailParticles"); |
---|
| 125 | SnowEffect::snowParticles->setMaterialTexture(texture); |
---|
[8495] | 126 | SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife); |
---|
[7652] | 127 | SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius); |
---|
[7683] | 128 | SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8); |
---|
| 129 | SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5); |
---|
[7652] | 130 | SnowEffect::snowParticles->setMass(0, snowMass, randomMass); |
---|
| 131 | SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); |
---|
| 132 | SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); |
---|
| 133 | SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); |
---|
| 134 | |
---|
| 135 | this->emitter->setSystem(SnowEffect::snowParticles); |
---|
| 136 | |
---|
| 137 | this->emitter->setRelCoor(snowCoord); |
---|
| 138 | this->emitter->setEmissionRate(rate); |
---|
| 139 | this->emitter->setEmissionVelocity(velocity, randomVelocity); |
---|
[7696] | 140 | this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce); |
---|
[7652] | 141 | this->emitter->setSize(snowSize); |
---|
[7696] | 142 | |
---|
[8495] | 143 | if (this->snowWindForce != 0) |
---|
| 144 | this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true); |
---|
[8793] | 145 | |
---|
[7573] | 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
[8495] | 149 | void SnowEffect::deactivate() |
---|
[7573] | 150 | { |
---|
[9006] | 151 | PRINTF(3)("Deactivating SnowEffect\n"); |
---|
[7652] | 152 | |
---|
[8255] | 153 | this->snowActivate = false; |
---|
[7652] | 154 | this->emitter->setSystem(NULL); |
---|
[7696] | 155 | |
---|
| 156 | if (this->windBuffer != NULL) |
---|
| 157 | ResourceManager::getInstance()->unload(this->windBuffer); |
---|
[7573] | 158 | } |
---|
| 159 | |
---|
| 160 | void SnowEffect::draw() const |
---|
| 161 | { |
---|
[8255] | 162 | if (!this->snowActivate) |
---|
| 163 | return; |
---|
[7573] | 164 | } |
---|
| 165 | |
---|
| 166 | void SnowEffect::tick(float dt) |
---|
| 167 | { |
---|
[8255] | 168 | if (!this->snowActivate) |
---|
| 169 | return; |
---|
[7683] | 170 | |
---|
[8255] | 171 | /* |
---|
| 172 | float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len(); |
---|
[8316] | 173 | |
---|
[8255] | 174 | if(activated) |
---|
| 175 | { |
---|
| 176 | if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y) |
---|
| 177 | this->deactivate(); |
---|
| 178 | else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y) |
---|
| 179 | this->alpha = 0.15; |
---|
| 180 | else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y) |
---|
| 181 | this->alpha = 0.25; |
---|
| 182 | else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y) |
---|
| 183 | this->alpha = 0.4; |
---|
[8316] | 184 | |
---|
[8255] | 185 | SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); |
---|
| 186 | SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); |
---|
| 187 | SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
| 191 | if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y ) |
---|
| 192 | this->activate(); |
---|
| 193 | if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y ) |
---|
| 194 | this->alpha = 0.25; |
---|
| 195 | else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y ) |
---|
| 196 | this->alpha = 0.4; |
---|
| 197 | else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y ) |
---|
| 198 | this->alpha = 0.5; |
---|
[8316] | 199 | |
---|
[8255] | 200 | SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha); |
---|
| 201 | SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2); |
---|
| 202 | SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0); |
---|
| 203 | }*/ |
---|
[7683] | 204 | |
---|
[8255] | 205 | if (this->snowMove) { |
---|
| 206 | this->snowCoord = State::getCameraNode()->getAbsCoor(); |
---|
| 207 | this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z); |
---|
| 208 | } |
---|
[7573] | 209 | } |
---|