Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/ExplosionChunk.cc @ 2710

Last change on this file since 2710 was 2422, checked in by landauf, 16 years ago
  • deatheffect (explosion) of Pawns works on client and server (creator of the effects can't be the Pawn itself because it will be destroyed on the client before synchronizing the effects)
  • fixed a small initialization error in Shader
  • fixed a bug in the resynchronization of already deleted MovableEntities
  • ExplosionChunk only recalculates it's velocity on the Master
  • Property svn:eol-style set to native
File size: 4.5 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#include "OrxonoxStableHeaders.h"
30#include "ExplosionChunk.h"
31
32#include "core/Core.h"
33#include "core/CoreIncludes.h"
34#include "core/Executor.h"
35#include "objects/Scene.h"
36#include "tools/ParticleInterface.h"
37#include "util/Exception.h"
38
39namespace orxonox
40{
41    CreateFactory(ExplosionChunk);
42
43    ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator)
44    {
45        RegisterObject(ExplosionChunk);
46
47        if (!this->getScene() || !this->getScene()->getSceneManager())
48            ThrowException(AbortLoading, "Can't create ExplosionChunk, no scene or no scene manager given.");
49
50        this->bStop_ = false;
51        this->LOD_ = LODParticle::normal;
52
53        try
54        {
55            this->fire_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
56            this->fire_->addToSceneNode(this->getNode());
57            this->smoke_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
58            this->smoke_->addToSceneNode(this->getNode());
59        }
60        catch (...)
61        {
62            COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl;
63            this->fire_ = 0;
64            this->smoke_ = 0;
65        }
66
67        if (Core::isMaster())
68        {
69            Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
70            velocity.normalise();
71            velocity *= rnd(60, 80);
72            this->setVelocity(velocity);
73
74            this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop)));
75        }
76
77        this->registerVariables();
78    }
79
80    ExplosionChunk::~ExplosionChunk()
81    {
82        if (this->isInitialized())
83        {
84            if (this->fire_)
85                delete this->fire_;
86            if (this->smoke_)
87                delete this->smoke_;
88        }
89    }
90
91    void ExplosionChunk::registerVariables()
92    {
93        REGISTERDATA(this->LOD_,   direction::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::LODchanged));
94        REGISTERDATA(this->bStop_, direction::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::checkStop));
95    }
96
97    void ExplosionChunk::LODchanged()
98    {
99        if (this->fire_)
100            this->fire_->setDetailLevel(this->LOD_);
101        if (this->smoke_)
102            this->smoke_->setDetailLevel(this->LOD_);
103    }
104
105    void ExplosionChunk::checkStop()
106    {
107        if (this->bStop_)
108            this->stop();
109    }
110
111    void ExplosionChunk::stop()
112    {
113        if (this->fire_)
114            this->fire_->setEnabled(false);
115        if (this->smoke_)
116            this->smoke_->setEnabled(false);
117
118        if (Core::isMaster())
119        {
120            this->bStop_ = true;
121            this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy)));
122        }
123    }
124
125    void ExplosionChunk::destroy()
126    {
127        delete this;
128    }
129
130    void ExplosionChunk::tick(float dt)
131    {
132        static const unsigned int CHANGES_PER_SECOND = 5;
133
134        if (Core::isMaster() && rnd() < dt*CHANGES_PER_SECOND)
135        {
136            float length = this->getVelocity().length();
137
138            Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
139            change.normalise();
140            change *= rnd(0.4, 0.8);
141            Vector3 velocity = this->getVelocity();
142            velocity.normalise();
143            velocity += change;
144            velocity.normalise();
145            velocity *= length * rnd(0.8, 1.0);
146
147            this->setVelocity(velocity);
148        }
149
150        SUPER(ExplosionChunk, tick, dt);
151    }
152}
Note: See TracBrowser for help on using the repository browser.