Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/orxonox/tools/ParticleInterface.cc @ 4906

Last change on this file since 4906 was 3370, checked in by rgrieder, 15 years ago

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30* @file
31* @brief class to control praticle effects
32*/
33
34#include "ParticleInterface.h"
35
36#include <cassert>
37#include <string>
38#include <OgreParticleSystem.h>
39#include <OgreParticleEmitter.h>
40#include <OgreSceneManager.h>
41
42#include "util/Convert.h"
43#include "util/Math.h"
44#include "core/CoreIncludes.h"
45#include "core/ConfigValueIncludes.h"
46#include "core/GameMode.h"
47
48namespace orxonox
49{
50    unsigned int ParticleInterface::counter_s = 0;
51    ParticleInterface* ParticleInterface::currentParticleInterface_s = 0;
52
53    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::Value detaillevel)
54    {
55        RegisterObject(ParticleInterface);
56
57        assert(scenemanager);
58
59        this->scenemanager_ = scenemanager;
60        this->particleSystem_ = 0;
61
62        this->bEnabled_ = true;
63        this->bVisible_ = true;
64        this->bAllowedByLOD_ = true;
65        this->speedFactor_ = 1.0f;
66
67        if (GameMode::showsGraphics())
68        {
69            try
70            {
71                this->particleSystem_ = this->scenemanager_->createParticleSystem("particles" + multi_cast<std::string>(ParticleInterface::counter_s++), templateName);
72                this->setSpeedFactor(1.0f);
73            }
74            catch (...)
75            {
76                COUT(1) << "Error: Couln't load particle system \"" << templateName << "\"" << std::endl;
77                this->particleSystem_ = 0;
78            }
79        }
80
81        this->setDetailLevel(static_cast<unsigned int>(detaillevel));
82    }
83
84    ParticleInterface::~ParticleInterface()
85    {
86        if (this->particleSystem_)
87        {
88            this->particleSystem_->removeAllEmitters();
89            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
90        }
91    }
92
93    void ParticleInterface::setConfigValues()
94    {
95        SetConfigValue(globalDetailLevel_, 2)
96            .description("O: off, 1: low, 2: normal, 3: high").callback(this, &ParticleInterface::detailLevelChanged);
97    }
98
99    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
100    {
101        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
102        {
103            Ogre::ParticleEmitter* newemitter = this->particleSystem_->addEmitter(this->particleSystem_->getEmitter(0)->getType());
104            this->particleSystem_->getEmitter(0)->copyParametersTo(newemitter);
105            return newemitter;
106        }
107        else
108            return 0;
109    }
110    Ogre::ParticleEmitter* ParticleInterface::getEmitter(unsigned int emitterNr) const
111    {
112        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
113            return this->particleSystem_->getEmitter(emitterNr);
114        else
115            return 0;
116    }
117    void ParticleInterface::removeEmitter(unsigned int emitterNr)
118    {
119        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
120            this->particleSystem_->removeEmitter(emitterNr);
121    }
122    void ParticleInterface::removeAllEmitters()
123    {
124        if (this->particleSystem_)
125            this->particleSystem_->removeAllEmitters();
126    }
127    unsigned int ParticleInterface::getNumEmitters() const
128    {
129        if (this->particleSystem_)
130            return this->particleSystem_->getNumEmitters();
131        else
132            return 0;
133    }
134
135    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
136    {
137        if (this->particleSystem_)
138            return this->particleSystem_->addAffector(name);
139        else
140            return 0;
141    }
142    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr)
143    {
144        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
145            return this->particleSystem_->getAffector(affectorNr);
146        else
147            return 0;
148    }
149    void ParticleInterface::removeAffector(unsigned int affectorNr)
150    {
151        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
152            this->particleSystem_->removeAffector(affectorNr);
153    }
154    void ParticleInterface::removeAllAffectors()
155    {
156        if (this->particleSystem_)
157            this->particleSystem_->removeAllAffectors();
158    }
159    unsigned int ParticleInterface::getNumAffectors() const
160    {
161        if (this->particleSystem_)
162            return this->particleSystem_->getNumAffectors();
163        else
164            return 0;
165    }
166
167    void ParticleInterface::setEnabled(bool enable)
168    {
169        this->bEnabled_ = enable;
170
171        if (this->particleSystem_)
172            for (unsigned int i = 0; i < this->particleSystem_->getNumEmitters(); i++)
173                this->particleSystem_->getEmitter(i)->setEnabled(this->bEnabled_ && this->bAllowedByLOD_);
174    }
175
176    void ParticleInterface::setVisible(bool visible)
177    {
178        this->bVisible_ = visible;
179
180        if (this->particleSystem_)
181            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
182    }
183
184    void ParticleInterface::setDetailLevel(unsigned int level)
185    {
186        this->detaillevel_ = level;
187        if (GameMode::showsGraphics())
188            this->detailLevelChanged();
189    }
190
191    void ParticleInterface::detailLevelChanged()
192    {
193        if (this->globalDetailLevel_ >= this->detaillevel_)
194            this->bAllowedByLOD_ = true;
195        else
196            this->bAllowedByLOD_ = false;
197
198        this->updateVisibility();
199    }
200
201    void ParticleInterface::updateVisibility()
202    {
203        this->setEnabled(this->isEnabled());
204        this->setVisible(this->isVisible());
205    }
206
207    void ParticleInterface::setSpeedFactor(float factor)
208    {
209        this->speedFactor_ = factor;
210
211        if (this->particleSystem_)
212            this->particleSystem_->setSpeedFactor(factor * this->getTimeFactor());
213    }
214    void ParticleInterface::changedTimeFactor(float factor_new, float factor_old)
215    {
216        this->setSpeedFactor(this->speedFactor_);
217    }
218
219    bool ParticleInterface::getKeepParticlesInLocalSpace() const
220    {
221        if (this->particleSystem_)
222            return this->particleSystem_->getKeepParticlesInLocalSpace();
223        else
224            return false;
225    }
226    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
227    {
228        if (this->particleSystem_)
229            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
230    }
231}
Note: See TracBrowser for help on using the repository browser.