[7573] | 1 | /* |
---|
[9406] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[7573] | 3 | |
---|
[9406] | 4 | Copyright (C) 2004 orx |
---|
[7573] | 5 | |
---|
[9406] | 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: |
---|
[9406] | 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" |
---|
[9805] | 19 | #include "sound/resource_sound_buffer.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" |
---|
[9758] | 26 | #include "particles/sprite_particles.h" |
---|
| 27 | #include "particles/plane_emitter.h" |
---|
[7649] | 28 | #include "shell_command.h" |
---|
[9112] | 29 | #include "script_class.h" |
---|
[9235] | 30 | #include "cloud_effect.h" |
---|
[7573] | 31 | |
---|
| 32 | |
---|
[10114] | 33 | |
---|
| 34 | ObjectListDefinition(SnowEffect); |
---|
[7649] | 35 | SHELL_COMMAND(activate, SnowEffect, activateSnow); |
---|
| 36 | SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow); |
---|
[7576] | 37 | |
---|
[7573] | 38 | |
---|
[9757] | 39 | CREATE_SCRIPTABLE_CLASS(SnowEffect, |
---|
[9805] | 40 | addMethod("activate", Executor0<SnowEffect, lua_State*>(&SnowEffect::activate)) |
---|
| 41 | ->addMethod("deactivate", Executor0<SnowEffect, lua_State*>(&SnowEffect::deactivate)) |
---|
[9112] | 42 | ); |
---|
| 43 | |
---|
[9709] | 44 | CREATE_FACTORY(SnowEffect); |
---|
[7573] | 45 | |
---|
| 46 | SnowEffect::SnowEffect(const TiXmlElement* root) |
---|
| 47 | { |
---|
[9686] | 48 | this->registerObject(this, SnowEffect::_objectList); |
---|
[7573] | 49 | |
---|
[9805] | 50 | this->init(); |
---|
[7651] | 51 | |
---|
[9805] | 52 | if (root != NULL) |
---|
| 53 | this->loadParams(root); |
---|
[7573] | 54 | |
---|
[9805] | 55 | //load wind sound |
---|
| 56 | if (this->snowWindForce >= 1) |
---|
| 57 | { |
---|
[10415] | 58 | this->windBuffer = OrxSound::ResourceSoundBuffer("sounds/atmosphere/wind.wav"); |
---|
[9805] | 59 | } |
---|
[7696] | 60 | |
---|
[9805] | 61 | if(snowActivate) |
---|
| 62 | { |
---|
| 63 | this->activate(); |
---|
[8495] | 64 | SnowEffect::snowParticles->precache((int) this->snowLife); |
---|
[8793] | 65 | } |
---|
[7573] | 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | SnowEffect::~SnowEffect() |
---|
| 70 | { |
---|
[9805] | 71 | this->deactivate(); |
---|
[7573] | 72 | } |
---|
| 73 | |
---|
| 74 | SpriteParticles* SnowEffect::snowParticles = NULL; |
---|
| 75 | |
---|
| 76 | void SnowEffect::loadParams(const TiXmlElement* root) |
---|
| 77 | { |
---|
[9805] | 78 | WeatherEffect::loadParams(root); |
---|
[7573] | 79 | |
---|
[9805] | 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); |
---|
[9235] | 90 | LoadParam(root, "cloudcolor", this, SnowEffect, setCloudColor); |
---|
| 91 | LoadParam(root, "skycolor", this, SnowEffect, setSkyColor); |
---|
| 92 | LoadParam(root, "fadetime", this, SnowEffect, setFadeTime); |
---|
[8316] | 93 | |
---|
[9805] | 94 | LOAD_PARAM_START_CYCLE(root, element); |
---|
| 95 | { |
---|
| 96 | LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption); |
---|
| 97 | } |
---|
| 98 | LOAD_PARAM_END_CYCLE(element); |
---|
[7573] | 99 | } |
---|
| 100 | |
---|
[8495] | 101 | void SnowEffect::init() |
---|
[7573] | 102 | { |
---|
[9805] | 103 | this->emitter = new PlaneEmitter(); |
---|
[7651] | 104 | |
---|
[9805] | 105 | // Default values |
---|
| 106 | this->snowActivate = false; |
---|
| 107 | this->snowMove = false; |
---|
| 108 | this->particles = 12000; |
---|
[10317] | 109 | this->texture = "textures/snow_flake_01_32x32.png"; |
---|
[9805] | 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; |
---|
[9406] | 125 | |
---|
[9235] | 126 | this->fadeTime = 10; |
---|
| 127 | this->cloudColor = Vector(0.2f, 0.2f, 0.2f); |
---|
| 128 | this->skyColor = Vector(0.0f, 0.0f, 0.0f); |
---|
[7573] | 129 | } |
---|
| 130 | |
---|
[8495] | 131 | void SnowEffect::activate() |
---|
[7573] | 132 | { |
---|
[9805] | 133 | PRINTF(3)("Activating SnowEffect\n"); |
---|
[7576] | 134 | |
---|
[9805] | 135 | this->snowActivate = true; |
---|
[8255] | 136 | |
---|
[9805] | 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); |
---|
[7652] | 148 | |
---|
[9805] | 149 | this->emitter->setSystem(SnowEffect::snowParticles); |
---|
[7652] | 150 | |
---|
[9805] | 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); |
---|
[7696] | 156 | |
---|
[8495] | 157 | if (this->snowWindForce != 0) |
---|
| 158 | this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true); |
---|
[9406] | 159 | |
---|
[9235] | 160 | // Store cloud- and sky color before the snow |
---|
| 161 | this->oldCloudColor = CloudEffect::cloudColor; |
---|
| 162 | this->oldSkyColor = CloudEffect::skyColor; |
---|
[8793] | 163 | |
---|
[9235] | 164 | // Change the colors |
---|
| 165 | CloudEffect::changeCloudColor(this->cloudColor, this->fadeTime); |
---|
| 166 | CloudEffect::changeSkyColor(this->skyColor, this->fadeTime); |
---|
| 167 | |
---|
[7573] | 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
[8495] | 171 | void SnowEffect::deactivate() |
---|
[7573] | 172 | { |
---|
[9805] | 173 | PRINTF(3)("Deactivating SnowEffect\n"); |
---|
[7652] | 174 | |
---|
[9805] | 175 | this->snowActivate = false; |
---|
| 176 | this->emitter->setSystem(NULL); |
---|
[7696] | 177 | |
---|
[9235] | 178 | // Restore the old cloud- and sky color |
---|
| 179 | CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime); |
---|
| 180 | CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime); |
---|
[7573] | 181 | } |
---|
| 182 | |
---|
| 183 | void SnowEffect::draw() const |
---|
| 184 | { |
---|
[9805] | 185 | if (!this->snowActivate) |
---|
| 186 | return; |
---|
[7573] | 187 | } |
---|
| 188 | |
---|
| 189 | void SnowEffect::tick(float dt) |
---|
| 190 | { |
---|
[9805] | 191 | if (!this->snowActivate) |
---|
| 192 | return; |
---|
[7683] | 193 | |
---|
[9805] | 194 | /* |
---|
| 195 | float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len(); |
---|
[8316] | 196 | |
---|
[9805] | 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; |
---|
[8316] | 207 | |
---|
[9805] | 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; |
---|
[8316] | 222 | |
---|
[9805] | 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 | }*/ |
---|
[7683] | 227 | |
---|
[9805] | 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 | } |
---|
[7573] | 233 | } |
---|