Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/tools/ParticleInterface.cc @ 2662

Last change on this file since 2662 was 2414, checked in by landauf, 16 years ago

added spawn- and destroy-effects to Pawn

  • Property svn:eol-style set to native
File size: 7.7 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 "OrxonoxStableHeaders.h"
35#include "ParticleInterface.h"
36
37#include <OgreParticleSystem.h>
38#include <OgreParticleEmitter.h>
39#include <OgreSceneManager.h>
40#include <cassert>
41
42#include "GraphicsEngine.h"
43#include "core/Core.h"
44#include "core/CoreIncludes.h"
45#include "util/Convert.h"
46
47namespace orxonox
48{
49    unsigned int ParticleInterface::counter_s = 0;
50    ParticleInterface* ParticleInterface::currentParticleInterface_s = 0;
51
52    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::LOD detaillevel)
53    {
54        RegisterObject(ParticleInterface);
55
56        assert(scenemanager);
57
58        this->scenemanager_ = scenemanager;
59        this->sceneNode_ = 0;
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 (Core::showsGraphics())
68        {
69            try
70            {
71                this->particleSystem_ = this->scenemanager_->createParticleSystem("particles" + getConvertedValue<unsigned int, 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((unsigned int)detaillevel);
82    }
83
84    ParticleInterface::~ParticleInterface()
85    {
86        if (this->particleSystem_)
87        {
88            this->particleSystem_->removeAllEmitters();
89            this->detachFromSceneNode();
90            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
91        }
92    }
93
94    void ParticleInterface::addToSceneNode(Ogre::SceneNode* sceneNode)
95    {
96        if (this->sceneNode_)
97            this->detachFromSceneNode();
98
99        if (this->particleSystem_)
100        {
101            this->sceneNode_ = sceneNode;
102            this->sceneNode_->attachObject(this->particleSystem_);
103        }
104    }
105
106    void ParticleInterface::detachFromSceneNode()
107    {
108        if (this->sceneNode_)
109        {
110            if (this->particleSystem_)
111                this->sceneNode_->detachObject(this->particleSystem_);
112            this->sceneNode_ = 0;
113        }
114    }
115
116    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
117    {
118        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
119        {
120            Ogre::ParticleEmitter* newemitter = this->particleSystem_->addEmitter(this->particleSystem_->getEmitter(0)->getType());
121            this->particleSystem_->getEmitter(0)->copyParametersTo(newemitter);
122            return newemitter;
123        }
124        else
125            return 0;
126    }
127    Ogre::ParticleEmitter* ParticleInterface::getEmitter(unsigned int emitterNr) const
128    {
129        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
130            return this->particleSystem_->getEmitter(emitterNr);
131        else
132            return 0;
133    }
134    void ParticleInterface::removeEmitter(unsigned int emitterNr)
135    {
136        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
137            this->particleSystem_->removeEmitter(emitterNr);
138    }
139    void ParticleInterface::removeAllEmitters()
140    {
141        if (this->particleSystem_)
142            this->particleSystem_->removeAllEmitters();
143    }
144    unsigned int ParticleInterface::getNumEmitters() const
145    {
146        if (this->particleSystem_)
147            return this->particleSystem_->getNumEmitters();
148        else
149            return 0;
150    }
151
152    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
153    {
154        if (this->particleSystem_)
155            return this->particleSystem_->addAffector(name);
156        else
157            return 0;
158    }
159    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr) const
160    {
161        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
162            return this->particleSystem_->getAffector(affectorNr);
163        else
164            return 0;
165    }
166    void ParticleInterface::removeAffector(unsigned int affectorNr)
167    {
168        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
169            this->particleSystem_->removeAffector(affectorNr);
170    }
171    void ParticleInterface::removeAllAffectors()
172    {
173        if (this->particleSystem_)
174            this->particleSystem_->removeAllAffectors();
175    }
176    unsigned int ParticleInterface::getNumAffectors() const
177    {
178        if (this->particleSystem_)
179            return this->particleSystem_->getNumAffectors();
180        else
181            return 0;
182    }
183
184    void ParticleInterface::setEnabled(bool enable)
185    {
186        this->bEnabled_ = enable;
187
188        if (this->particleSystem_)
189            for (unsigned int i = 0; i < this->particleSystem_->getNumEmitters(); i++)
190                this->particleSystem_->getEmitter(i)->setEnabled(this->bEnabled_ && this->bAllowedByLOD_);
191    }
192
193    void ParticleInterface::setVisible(bool visible)
194    {
195        this->bVisible_ = visible;
196
197        if (this->particleSystem_)
198            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
199    }
200
201    void ParticleInterface::setDetailLevel(unsigned int level)
202    {
203        this->detaillevel_ = level;
204        if (GraphicsEngine::getInstancePtr())
205            this->detailLevelChanged(GraphicsEngine::getInstance().getDetailLevelParticle());
206    }
207
208    void ParticleInterface::detailLevelChanged(unsigned int newlevel)
209    {
210        if (newlevel >= (unsigned int)this->detaillevel_)
211            this->bAllowedByLOD_ = true;
212        else
213            this->bAllowedByLOD_ = false;
214
215        this->updateVisibility();
216    }
217
218    void ParticleInterface::updateVisibility()
219    {
220        this->setEnabled(this->isEnabled());
221        this->setVisible(this->isVisible());
222    }
223
224    void ParticleInterface::setSpeedFactor(float factor)
225    {
226        this->speedFactor_ = factor;
227
228        if (this->particleSystem_)
229            this->particleSystem_->setSpeedFactor(factor * this->getTimeFactor());
230    }
231    void ParticleInterface::changedTimeFactor(float factor_new, float factor_old)
232    {
233        this->setSpeedFactor(this->speedFactor_);
234    }
235
236    bool ParticleInterface::getKeepParticlesInLocalSpace() const
237    {
238        if (this->particleSystem_)
239            return this->particleSystem_->getKeepParticlesInLocalSpace();
240        else
241            return false;
242    }
243    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
244    {
245        if (this->particleSystem_)
246            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
247    }
248}
Note: See TracBrowser for help on using the repository browser.