Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/snow_effect.cc @ 9346

Last change on this file since 9346 was 9235, checked in by bensch, 18 years ago

merged the presentation back

File size: 6.9 KB
RevLine 
[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"
[9112]29#include "script_class.h"
[9235]30#include "cloud_effect.h"
[7573]31
32#include "parser/tinyxml/tinyxml.h"
33
[7649]34SHELL_COMMAND(activate, SnowEffect, activateSnow);
35SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
[7576]36
[7573]37using namespace std;
38
[9112]39CREATE_SCRIPTABLE_CLASS(SnowEffect, CL_SNOW_EFFECT,
40                            addMethod("activate", ExecutorLua0<SnowEffect>(&SnowEffect::activate))
41                            ->addMethod("deactivate", ExecutorLua0<SnowEffect>(&SnowEffect::deactivate))
42                       );
43
[7573]44CREATE_FACTORY(SnowEffect, CL_SNOW_EFFECT);
45
46SnowEffect::SnowEffect(const TiXmlElement* root)
47{
[7652]48        this->setClassID(CL_SNOW_EFFECT, "SnowEffect");
[7573]49
[7652]50        this->init();
[7651]51
[7652]52        if (root != NULL)
53                this->loadParams(root);
[7573]54
[7696]55        //load wind sound
[9235]56        if (this->snowWindForce >= 1) {
[7696]57                if (this->windBuffer != NULL)
58                        ResourceManager::getInstance()->unload(this->windBuffer);
[9235]59          this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
[7696]60        }
61
[8495]62  if(snowActivate) {
[8255]63                this->activate();
[8495]64    SnowEffect::snowParticles->precache((int) this->snowLife);
[8793]65  }
[7573]66}
67
68
69SnowEffect::~SnowEffect()
70{
[7652]71        this->deactivate();
[7573]72}
73
74SpriteParticles* SnowEffect::snowParticles = NULL;
75
76void SnowEffect::loadParams(const TiXmlElement* root)
77{
[7652]78        WeatherEffect::loadParams(root);
[7573]79
[7652]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);
[8255]87        LoadParam(root, "wind", this, SnowEffect, wind);
[7652]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
[8255]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]101void SnowEffect::init()
[7573]102{
[7652]103        this->emitter = new PlaneEmitter();
[7651]104
[7652]105        // Default values
[8255]106        this->snowActivate = false;
107        this->snowMove = false;
[7696]108        this->particles = 12000;
109        this->texture = "maps/snow_flake_01_32x32.png";
[8495]110        this->snowLife = 8;
[7696]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);
[8255]123        this->snowCoord = Vector(100,450,400);
[7696]124        this->snowWindForce = 1;
[9235]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);
[7573]129}
130
[8495]131void SnowEffect::activate()
[7573]132{
[9006]133        PRINTF(3)("Activating SnowEffect\n");
[7576]134
[8255]135        this->snowActivate = true;
136
[7652]137        SnowEffect::snowParticles = new SpriteParticles(particles);
138        SnowEffect::snowParticles->setName("SnowEffectTrailParticles");
139        SnowEffect::snowParticles->setMaterialTexture(texture);
[8495]140        SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife);
[7652]141        SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius);
[7683]142        SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8);
143        SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5);
[7652]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);
[7696]154        this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce);
[7652]155        this->emitter->setSize(snowSize);
[7696]156
[8495]157  if (this->snowWindForce != 0)
158    this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true);
[9235]159 
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]171void SnowEffect::deactivate()
[7573]172{
[9006]173        PRINTF(3)("Deactivating SnowEffect\n");
[7652]174
[8255]175        this->snowActivate = false;
[7652]176        this->emitter->setSystem(NULL);
[7696]177
178        if (this->windBuffer != NULL)
179                ResourceManager::getInstance()->unload(this->windBuffer);
[9235]180 
181  // Restore the old cloud- and sky color
182  CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime);
183  CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime);
[7573]184}
185
186void SnowEffect::draw() const
187{
[8255]188        if (!this->snowActivate)
189                return;
[7573]190}
191
192void SnowEffect::tick(float dt)
193{
[8255]194        if (!this->snowActivate)
195                return;
[7683]196
[8255]197        /*
198        float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
[8316]199
[8255]200        if(activated)
201        {
202        if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y)
203                        this->deactivate();
204        else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y)
205        this->alpha = 0.15;
206        else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y)
207        this->alpha = 0.25;
208        else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y)
209        this->alpha = 0.4;
[8316]210
[8255]211        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
212        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
213        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
214        }
215        else
216        {
217        if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y )
218        this->activate();
219        if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y )
220        this->alpha = 0.25;
221        else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y )
222        this->alpha = 0.4;
223        else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y )
224        this->alpha = 0.5;
[8316]225
[8255]226        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
227        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
228        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
229        }*/
[7683]230
[8255]231        if (this->snowMove) {
232                this->snowCoord = State::getCameraNode()->getAbsCoor();
233                this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z);
234        }
[7573]235}
Note: See TracBrowser for help on using the repository browser.