Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the mountain-lake back here

File size: 6.1 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"
[7573]30
31#include "parser/tinyxml/tinyxml.h"
32
[7649]33SHELL_COMMAND(activate, SnowEffect, activateSnow);
34SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
[7576]35
[7573]36using namespace std;
37
[9112]38CREATE_SCRIPTABLE_CLASS(SnowEffect, CL_SNOW_EFFECT,
39                            addMethod("activate", ExecutorLua0<SnowEffect>(&SnowEffect::activate))
40                            ->addMethod("deactivate", ExecutorLua0<SnowEffect>(&SnowEffect::deactivate))
41                       );
42
[7573]43CREATE_FACTORY(SnowEffect, CL_SNOW_EFFECT);
44
45SnowEffect::SnowEffect(const TiXmlElement* root)
46{
[7652]47        this->setClassID(CL_SNOW_EFFECT, "SnowEffect");
[7573]48
[7652]49        this->init();
[7651]50
[7652]51        if (root != NULL)
52                this->loadParams(root);
[7573]53
[7696]54        //load wind sound
55        if (this->snowWindForce > 1) {
56                if (this->windBuffer != NULL)
57                        ResourceManager::getInstance()->unload(this->windBuffer);
[8495]58                        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
[7696]59        }
60
[8495]61  if(snowActivate) {
[8255]62                this->activate();
[8495]63    SnowEffect::snowParticles->precache((int) this->snowLife);
[8793]64  }
[7573]65}
66
67
68SnowEffect::~SnowEffect()
69{
[7652]70        this->deactivate();
[7573]71}
72
73SpriteParticles* SnowEffect::snowParticles = NULL;
74
75void SnowEffect::loadParams(const TiXmlElement* root)
76{
[7652]77        WeatherEffect::loadParams(root);
[7573]78
[7652]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);
[8255]86        LoadParam(root, "wind", this, SnowEffect, wind);
[7652]87        LoadParam(root, "size", this, SnowEffect, size);
88        LoadParam(root, "coord", this, SnowEffect, coord);
[8316]89
[8255]90        LOAD_PARAM_START_CYCLE(root, element);
91        {
92                LoadParam_CYCLE(element, "option", this, SnowEffect, setSnowOption);
93        }
94        LOAD_PARAM_END_CYCLE(element);
[7573]95}
96
[8495]97void SnowEffect::init()
[7573]98{
[7652]99        this->emitter = new PlaneEmitter();
[7651]100
[7652]101        // Default values
[8255]102        this->snowActivate = false;
103        this->snowMove = false;
[7696]104        this->particles = 12000;
105        this->texture = "maps/snow_flake_01_32x32.png";
[8495]106        this->snowLife = 8;
[7696]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);
[8255]119        this->snowCoord = Vector(100,450,400);
[7696]120        this->snowWindForce = 1;
[7573]121}
122
[8495]123void SnowEffect::activate()
[7573]124{
[9006]125        PRINTF(3)("Activating SnowEffect\n");
[7576]126
[8255]127        this->snowActivate = true;
128
[7652]129        SnowEffect::snowParticles = new SpriteParticles(particles);
130        SnowEffect::snowParticles->setName("SnowEffectTrailParticles");
131        SnowEffect::snowParticles->setMaterialTexture(texture);
[8495]132        SnowEffect::snowParticles->setLifeSpan(snowLife, randomLife);
[7652]133        SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius);
[7683]134        SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8);
135        SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5);
[7652]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);
[7696]146        this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce);
[7652]147        this->emitter->setSize(snowSize);
[7696]148
[8495]149  if (this->snowWindForce != 0)
150    this->soundSource.play(this->windBuffer, 0.1f * this->snowWindForce, true);
[8793]151
[7573]152}
153
154
[8495]155void SnowEffect::deactivate()
[7573]156{
[9006]157        PRINTF(3)("Deactivating SnowEffect\n");
[7652]158
[8255]159        this->snowActivate = false;
[7652]160        this->emitter->setSystem(NULL);
[7696]161
162        if (this->windBuffer != NULL)
163                ResourceManager::getInstance()->unload(this->windBuffer);
[7573]164}
165
166void SnowEffect::draw() const
167{
[8255]168        if (!this->snowActivate)
169                return;
[7573]170}
171
172void SnowEffect::tick(float dt)
173{
[8255]174        if (!this->snowActivate)
175                return;
[7683]176
[8255]177        /*
178        float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
[8316]179
[8255]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;
[8316]190
[8255]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;
[8316]205
[8255]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        }*/
[7683]210
[8255]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        }
[7573]215}
Note: See TracBrowser for help on using the repository browser.