Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/effects/rain_effect.cc @ 7914

Last change on this file since 7914 was 7810, checked in by bensch, 19 years ago

orxonox/trunk: merged the Weather effects back here

File size: 4.4 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 "rain_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 "p_node.h"
23#include "state.h"
24#include "spark_particles.h"
25#include "plane_emitter.h"
26#include "shell_command.h"
27
28#include "parser/tinyxml/tinyxml.h"
29
30SHELL_COMMAND(activate, RainEffect, activateRain);
31SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
32
33using namespace std;
34
35CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
36
37RainEffect::RainEffect(const TiXmlElement* root)
38{
39        this->setClassID(CL_RAIN_EFFECT, "RainEffect");
40
41        this->init();
42
43        if (root != NULL)
44                this->loadParams(root);
45
46        //load rain sound
47        if (this->rainBuffer != NULL)
48                ResourceManager::getInstance()->unload(this->rainBuffer);
49        this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
50
51        //load wind sound
52        if (this->rainWindForce > 0) {
53                if (this->windBuffer != NULL)
54                        ResourceManager::getInstance()->unload(this->windBuffer);
55                this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV);
56        }
57
58        this->activate();
59}
60
61RainEffect::~RainEffect()
62{
63        this->deactivate();
64
65        if (this->rainBuffer != NULL)
66                ResourceManager::getInstance()->unload(this->rainBuffer);
67
68        if (this->windBuffer != NULL)
69                ResourceManager::getInstance()->unload(this->windBuffer);
70}
71
72void RainEffect::loadParams(const TiXmlElement* root)
73{
74        WeatherEffect::loadParams(root);
75
76        LoadParam(root, "coord", this, RainEffect, setRainCoord);
77        LoadParam(root, "size", this, RainEffect, setRainSize);
78        LoadParam(root, "rate", this, RainEffect, setRainRate);
79        LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
80        LoadParam(root, "life", this, RainEffect, setRainLife);
81        LoadParam(root, "wind", this, RainEffect, setRainWind);
82        LoadParam(root, "option", this, RainEffect, setRainOption);
83}
84
85
86bool RainEffect::init()
87{
88        //Default values
89        this->rainCoord = Vector(500, 500, 500);
90        this->rainSize = Vector2D(1000, 1000);
91        this->rainRate = 3000;
92        this->rainVelocity = -300;
93        this->rainLife = 4;
94        this->rainMaxParticles = this->rainRate * this->rainLife;
95        this->rainWindForce  = 0;
96
97        this->emitter = new PlaneEmitter(this->rainSize);
98}
99
100
101SparkParticles* RainEffect::rainParticles = NULL;
102
103bool RainEffect::activate()
104{
105        PRINTF(0)( "Activating RainEffect, coord: %f, %f, %f, size: %f, %f, rate: %f, velocity: %f, moveRain: %s\n", this->rainCoord.x, this->rainCoord.y, this->rainCoord.z, this->rainSize.x, this-> rainSize.y, this->rainRate, this->rainVelocity, this->rainMove ? "true" : "false" );
106
107        if (unlikely(RainEffect::rainParticles == NULL))
108        {
109                RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
110                RainEffect::rainParticles->setName("RainParticles");
111                RainEffect::rainParticles->precache((int)this->rainLife);
112                RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
113                RainEffect::rainParticles->setRadius(0, 0.03);
114                RainEffect::rainParticles->setRadius(0.2, 0.02);
115                RainEffect::rainParticles->setRadius(1, 0.01);
116                RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
117                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
118                RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
119        }
120
121        this->emitter->setSystem(RainEffect::rainParticles);
122
123        this->emitter->setRelCoor(this->rainCoord);
124
125        this->emitter->setEmissionRate(this->rainRate);
126        this->emitter->setEmissionVelocity(this->rainVelocity);
127
128        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
129       
130        this->soundSource.loop(this->rainBuffer);
131        if (this->rainWindForce > 0)
132                this->soundSource.loop(this->windBuffer);
133}
134
135
136bool RainEffect::deactivate()
137{
138        PRINTF(0)("Deactivating RainEffect\n");
139        this->emitter->setSystem(NULL);
140
141        this->soundSource.stop();
142}
143
144void RainEffect::activateRain()
145{
146        this->activate();
147}
148
149void RainEffect::deactivateRain()
150{
151        this->deactivate();
152}
153
154
155void RainEffect::tick (float dt)
156{
157        if (this->rainMove) {
158                this->rainCoord = State::getCameraNode()->getAbsCoor();
159                this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
160        }
161}
Note: See TracBrowser for help on using the repository browser.