Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the mountain-lake back here

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