Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/weather_effects/snow_effect.cc @ 9760

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

moved around the weather effecs

File size: 7.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 "particles/sprite_particles.h"
27#include "particles/plane_emitter.h"
28#include "shell_command.h"
29#include "script_class.h"
30#include "cloud_effect.h"
31
32#include "class_id_DEPRECATED.h"
33
34ObjectListDefinitionID(SnowEffect, CL_SNOW_EFFECT);
35SHELL_COMMAND(activate, SnowEffect, activateSnow);
36SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
37
38
39CREATE_SCRIPTABLE_CLASS(SnowEffect,
40                            addMethod("activate", Executor0<SnowEffect, lua_State*>(&SnowEffect::activate))
41                            ->addMethod("deactivate", Executor0<SnowEffect, lua_State*>(&SnowEffect::deactivate))
42                       );
43
44CREATE_FACTORY(SnowEffect);
45
46SnowEffect::SnowEffect(const TiXmlElement* root)
47{
48  this->registerObject(this, SnowEffect::_objectList);
49
50        this->init();
51
52        if (root != NULL)
53                this->loadParams(root);
54
55        this->windBuffer = NULL;
56        //load wind sound
57        if (this->snowWindForce >= 1) {
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  LoadParam(root, "cloudcolor", this, SnowEffect, setCloudColor);
90  LoadParam(root, "skycolor", this, SnowEffect, setSkyColor);
91  LoadParam(root, "fadetime", this, SnowEffect, setFadeTime);
92
93        LOAD_PARAM_START_CYCLE(root, element);
94        {
95                LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption);
96        }
97        LOAD_PARAM_END_CYCLE(element);
98}
99
100void SnowEffect::init()
101{
102        this->emitter = new PlaneEmitter();
103
104        // Default values
105        this->snowActivate = false;
106        this->snowMove = false;
107        this->particles = 12000;
108        this->texture = "maps/snow_flake_01_32x32.png";
109        this->snowLife = 8;
110        this->randomLife = 2;
111        this->snowRadius = 3.5;
112        this->randomRadius = 1;
113        this->snowMass = 1.0;
114        this->randomMass = 0.3;
115        this->rate = 900;
116        this->velocity = -100;
117        this->randomVelocity = 5;
118        this->angle = 0.5;
119        this->randomAngle = 0.2;
120        this->alpha = 0.5;
121        this->snowSize = Vector2D(2500, 2500);
122        this->snowCoord = Vector(100,450,400);
123        this->snowWindForce = 1;
124
125  this->fadeTime = 10;
126  this->cloudColor = Vector(0.2f, 0.2f, 0.2f);
127  this->skyColor = Vector(0.0f, 0.0f, 0.0f);
128}
129
130void SnowEffect::activate()
131{
132        PRINTF(3)("Activating SnowEffect\n");
133
134        this->snowActivate = true;
135
136        SnowEffect::snowParticles = new SpriteParticles(particles);
137        SnowEffect::snowParticles->setName("SnowEffectTrailParticles");
138        SnowEffect::snowParticles->setMaterialTexture(texture);
139        SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife);
140        SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius);
141        SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8);
142        SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5);
143        SnowEffect::snowParticles->setMass(0, snowMass, randomMass);
144        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
145        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
146        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
147
148        this->emitter->setSystem(SnowEffect::snowParticles);
149
150        this->emitter->setRelCoor(snowCoord);
151        this->emitter->setEmissionRate(rate);
152        this->emitter->setEmissionVelocity(velocity, randomVelocity);
153        this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce);
154        this->emitter->setSize(snowSize);
155
156  if (this->snowWindForce != 0)
157    this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true);
158
159  // Store cloud- and sky color before the snow
160  this->oldCloudColor = CloudEffect::cloudColor;
161  this->oldSkyColor   = CloudEffect::skyColor;
162
163  // Change the colors
164  CloudEffect::changeCloudColor(this->cloudColor, this->fadeTime);
165  CloudEffect::changeSkyColor(this->skyColor, this->fadeTime);
166
167}
168
169
170void SnowEffect::deactivate()
171{
172        PRINTF(3)("Deactivating SnowEffect\n");
173
174        this->snowActivate = false;
175        this->emitter->setSystem(NULL);
176
177        if (this->windBuffer != NULL)
178                ResourceManager::getInstance()->unload(this->windBuffer);
179
180  // Restore the old cloud- and sky color
181  CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime);
182  CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime);
183}
184
185void SnowEffect::draw() const
186{
187        if (!this->snowActivate)
188                return;
189}
190
191void SnowEffect::tick(float dt)
192{
193        if (!this->snowActivate)
194                return;
195
196        /*
197        float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
198
199        if(activated)
200        {
201        if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y)
202                        this->deactivate();
203        else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y)
204        this->alpha = 0.15;
205        else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y)
206        this->alpha = 0.25;
207        else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y)
208        this->alpha = 0.4;
209
210        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
211        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
212        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
213        }
214        else
215        {
216        if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y )
217        this->activate();
218        if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y )
219        this->alpha = 0.25;
220        else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y )
221        this->alpha = 0.4;
222        else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y )
223        this->alpha = 0.5;
224
225        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
226        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
227        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
228        }*/
229
230        if (this->snowMove) {
231                this->snowCoord = State::getCameraNode()->getAbsCoor();
232                this->emitter->setRelCoor(this->snowCoord.x , this->snowCoord.y+300, this->snowCoord.z);
233        }
234}
Note: See TracBrowser for help on using the repository browser.