Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/effects/snow_effect.cc @ 9758

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

orxonox/new_class_id: moved the Particle-engine

File size: 7.8 KB
RevLine 
[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"
[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"
[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
[9716]32#include "class_id_DEPRECATED.h"
[7573]33
[9715]34ObjectListDefinitionID(SnowEffect, CL_SNOW_EFFECT);
[7649]35SHELL_COMMAND(activate, SnowEffect, activateSnow);
36SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
[7576]37
[7573]38
[9757]39CREATE_SCRIPTABLE_CLASS(SnowEffect,
[9746]40                            addMethod("activate", Executor0<SnowEffect, lua_State*>(&SnowEffect::activate))
41                            ->addMethod("deactivate", Executor0<SnowEffect, lua_State*>(&SnowEffect::deactivate))
[9112]42                       );
43
[9709]44CREATE_FACTORY(SnowEffect);
[7573]45
46SnowEffect::SnowEffect(const TiXmlElement* root)
47{
[9686]48  this->registerObject(this, SnowEffect::_objectList);
[7573]49
[9406]50        this->init();
[7651]51
[9406]52        if (root != NULL)
53                this->loadParams(root);
[7573]54
[9406]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        }
[7696]60
[8495]61  if(snowActivate) {
[9406]62                this->activate();
[8495]63    SnowEffect::snowParticles->precache((int) this->snowLife);
[8793]64  }
[7573]65}
66
67
68SnowEffect::~SnowEffect()
69{
[9406]70        this->deactivate();
[7573]71}
72
73SpriteParticles* SnowEffect::snowParticles = NULL;
74
75void SnowEffect::loadParams(const TiXmlElement* root)
76{
[9406]77        WeatherEffect::loadParams(root);
[7573]78
[9406]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);
[9235]89  LoadParam(root, "cloudcolor", this, SnowEffect, setCloudColor);
90  LoadParam(root, "skycolor", this, SnowEffect, setSkyColor);
91  LoadParam(root, "fadetime", this, SnowEffect, setFadeTime);
[8316]92
[9406]93        LOAD_PARAM_START_CYCLE(root, element);
94        {
95                LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption);
96        }
97        LOAD_PARAM_END_CYCLE(element);
[7573]98}
99
[8495]100void SnowEffect::init()
[7573]101{
[9406]102        this->emitter = new PlaneEmitter();
[7651]103
[9406]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
[9235]125  this->fadeTime = 10;
126  this->cloudColor = Vector(0.2f, 0.2f, 0.2f);
127  this->skyColor = Vector(0.0f, 0.0f, 0.0f);
[7573]128}
129
[8495]130void SnowEffect::activate()
[7573]131{
[9406]132        PRINTF(3)("Activating SnowEffect\n");
[7576]133
[9406]134        this->snowActivate = true;
[8255]135
[9406]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);
[7652]147
[9406]148        this->emitter->setSystem(SnowEffect::snowParticles);
[7652]149
[9406]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);
[7696]155
[8495]156  if (this->snowWindForce != 0)
157    this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true);
[9406]158
[9235]159  // Store cloud- and sky color before the snow
160  this->oldCloudColor = CloudEffect::cloudColor;
161  this->oldSkyColor   = CloudEffect::skyColor;
[8793]162
[9235]163  // Change the colors
164  CloudEffect::changeCloudColor(this->cloudColor, this->fadeTime);
165  CloudEffect::changeSkyColor(this->skyColor, this->fadeTime);
166
[7573]167}
168
169
[8495]170void SnowEffect::deactivate()
[7573]171{
[9406]172        PRINTF(3)("Deactivating SnowEffect\n");
[7652]173
[9406]174        this->snowActivate = false;
175        this->emitter->setSystem(NULL);
[7696]176
[9406]177        if (this->windBuffer != NULL)
178                ResourceManager::getInstance()->unload(this->windBuffer);
179
[9235]180  // Restore the old cloud- and sky color
181  CloudEffect::changeCloudColor(this->oldCloudColor, this->fadeTime);
182  CloudEffect::changeSkyColor(this->oldSkyColor, this->fadeTime);
[7573]183}
184
185void SnowEffect::draw() const
186{
[9406]187        if (!this->snowActivate)
188                return;
[7573]189}
190
191void SnowEffect::tick(float dt)
192{
[9406]193        if (!this->snowActivate)
194                return;
[7683]195
[9406]196        /*
197        float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
[8316]198
[9406]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;
[8316]209
[9406]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;
[8316]224
[9406]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        }*/
[7683]229
[9406]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        }
[7573]234}
Note: See TracBrowser for help on using the repository browser.