Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added spawn- and destroy-effects to Pawn

  • Property svn:eol-style set to native
File size: 4.0 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/CoreIncludes.h"
33#include "core/Executor.h"
34#include "objects/Scene.h"
35#include "tools/ParticleInterface.h"
36#include "util/Exception.h"
37
38namespace orxonox
39{
40    CreateFactory(ExplosionChunk);
41
42    ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator)
43    {
44        RegisterObject(ExplosionChunk);
45
46        if (!this->getScene() || !this->getScene()->getSceneManager())
47            ThrowException(AbortLoading, "Can't create ExplosionChunk, no scene or no scene manager given.");
48
49        this->LOD_ = LODParticle::normal;
50
51        try
52        {
53            this->fire_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_);
54            this->fire_->addToSceneNode(this->getNode());
55            this->smoke_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_);
56            this->smoke_->addToSceneNode(this->getNode());
57        }
58        catch (...)
59        {
60            COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl;
61            this->fire_ = 0;
62            this->smoke_ = 0;
63        }
64
65        Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
66        velocity.normalise();
67        velocity *= rnd(40, 60);
68        this->setVelocity(velocity);
69
70        this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop)));
71
72        this->registerVariables();
73    }
74
75    ExplosionChunk::~ExplosionChunk()
76    {
77        if (this->isInitialized())
78        {
79            if (this->fire_)
80                delete this->fire_;
81            if (this->smoke_)
82                delete this->smoke_;
83        }
84    }
85
86    void ExplosionChunk::registerVariables()
87    {
88        REGISTERDATA(this->LOD_, direction::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::LODchanged));
89    }
90
91    void ExplosionChunk::LODchanged()
92    {
93        if (this->fire_)
94            this->fire_->setDetailLevel(this->LOD_);
95        if (this->smoke_)
96            this->smoke_->setDetailLevel(this->LOD_);
97    }
98
99    void ExplosionChunk::stop()
100    {
101        if (this->fire_)
102            this->fire_->setEnabled(false);
103        if (this->smoke_)
104            this->smoke_->setEnabled(false);
105
106        this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy)));
107    }
108
109    void ExplosionChunk::destroy()
110    {
111        delete this;
112    }
113
114    void ExplosionChunk::tick(float dt)
115    {
116        static const unsigned int CHANGES_PER_SECOND = 5;
117
118        if (rnd() < dt*CHANGES_PER_SECOND)
119        {
120            float length = this->getVelocity().length();
121
122            Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1));
123            change.normalise();
124            change *= rnd(0.4, 0.8);
125            Vector3 velocity = this->getVelocity();
126            velocity.normalise();
127            velocity += change;
128            velocity.normalise();
129            velocity *= length * rnd(0.8, 1.0);
130
131            this->setVelocity(velocity);
132        }
133
134        SUPER(ExplosionChunk, tick, dt);
135    }
136}
Note: See TracBrowser for help on using the repository browser.