Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/snow_effect.cc @ 8480

Last change on this file since 8480 was 8480, checked in by amaechler, 18 years ago

sound fixed

File size: 5.8 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
30#include "parser/tinyxml/tinyxml.h"
31
32SHELL_COMMAND(activate, SnowEffect, activateSnow);
33SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
34
35using namespace std;
36
37CREATE_FACTORY(SnowEffect, CL_SNOW_EFFECT);
38
39SnowEffect::SnowEffect(const TiXmlElement* root)
40{
41        this->setClassID(CL_SNOW_EFFECT, "SnowEffect");
42
43        this->init();
44
45        if (root != NULL)
46                this->loadParams(root);
47
48        //load wind sound
49        if (this->snowWindForce > 1) {
50                if (this->windBuffer != NULL)
51                        ResourceManager::getInstance()->unload(this->windBuffer);
52                        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
53        }
54
55  if(snowActivate) {
56                this->activate();
57    SnowEffect::snowParticles->precache((int) this->snowLife);
58  } 
59}
60
61
62SnowEffect::~SnowEffect()
63{
64        this->deactivate();
65}
66
67SpriteParticles* SnowEffect::snowParticles = NULL;
68
69void SnowEffect::loadParams(const TiXmlElement* root)
70{
71        WeatherEffect::loadParams(root);
72
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);
80        LoadParam(root, "wind", this, SnowEffect, wind);
81        LoadParam(root, "size", this, SnowEffect, size);
82        LoadParam(root, "coord", this, SnowEffect, coord);
83
84        LOAD_PARAM_START_CYCLE(root, element);
85        {
86                LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption);
87        }
88        LOAD_PARAM_END_CYCLE(element);
89}
90
91void SnowEffect::init()
92{
93        this->emitter = new PlaneEmitter();
94
95        // Default values
96        this->snowActivate = false;
97        this->snowMove = false;
98        this->particles = 12000;
99        this->texture = "maps/snow_flake_01_32x32.png";
100        this->snowLife = 8;
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);
113        this->snowCoord = Vector(100,450,400);
114        this->snowWindForce = 1;
115}
116
117void SnowEffect::activate()
118{
119        PRINTF(0)("Activating SnowEffect\n");
120
121        this->snowActivate = true;
122
123        SnowEffect::snowParticles = new SpriteParticles(particles);
124        SnowEffect::snowParticles->setName("SnowEffectTrailParticles");
125        SnowEffect::snowParticles->setMaterialTexture(texture);
126        SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife);
127        SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius);
128        SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8);
129        SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5);
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);
140        this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce);
141        this->emitter->setSize(snowSize);
142
143  if (this->snowWindForce != 0)
144    this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true);
145 
146}
147
148
149void SnowEffect::deactivate()
150{
151        PRINTF(0)("Deactivating SnowEffect\n");
152
153        this->snowActivate = false;
154        this->emitter->setSystem(NULL);
155
156        if (this->windBuffer != NULL)
157                ResourceManager::getInstance()->unload(this->windBuffer);
158
159}
160
161void SnowEffect::draw() const
162{
163        if (!this->snowActivate)
164                return;
165}
166
167void SnowEffect::tick(float dt)
168{
169        if (!this->snowActivate)
170                return;
171
172        /*
173        float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
174
175        if(activated)
176        {
177        if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y)
178                        this->deactivate();
179        else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y)
180        this->alpha = 0.15;
181        else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y)
182        this->alpha = 0.25;
183        else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y)
184        this->alpha = 0.4;
185
186        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
187        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
188        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
189        }
190        else
191        {
192        if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y )
193        this->activate();
194        if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y )
195        this->alpha = 0.25;
196        else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y )
197        this->alpha = 0.4;
198        else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y )
199        this->alpha = 0.5;
200
201        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
202        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
203        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
204        }*/
205
206        if (this->snowMove) {
207                this->snowCoord = State::getCameraNode()->getAbsCoor();
208                this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z);
209        }
210}
Note: See TracBrowser for help on using the repository browser.