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 "ExplosionChunk.h" |
---|
30 | |
---|
31 | #include <OgreParticleSystem.h> |
---|
32 | |
---|
33 | #include "core/GameMode.h" |
---|
34 | #include "core/CoreIncludes.h" |
---|
35 | #include "core/Executor.h" |
---|
36 | #include "objects/Scene.h" |
---|
37 | #include "tools/ParticleInterface.h" |
---|
38 | #include "util/Exception.h" |
---|
39 | |
---|
40 | namespace orxonox |
---|
41 | { |
---|
42 | CreateFactory(ExplosionChunk); |
---|
43 | |
---|
44 | ExplosionChunk::ExplosionChunk(BaseObject* creator) : MovableEntity(creator) |
---|
45 | { |
---|
46 | RegisterObject(ExplosionChunk); |
---|
47 | |
---|
48 | if ( GameMode::showsGraphics() && ( !this->getScene() || !this->getScene()->getSceneManager() ) ) |
---|
49 | ThrowException(AbortLoading, "Can't create ExplosionChunk, no scene or no scene manager given."); |
---|
50 | |
---|
51 | this->bStop_ = false; |
---|
52 | this->LOD_ = LODParticle::normal; |
---|
53 | |
---|
54 | if ( GameMode::showsGraphics() ) |
---|
55 | { |
---|
56 | try |
---|
57 | { |
---|
58 | this->fire_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/fire4", this->LOD_); |
---|
59 | this->attachOgreObject(this->fire_->getParticleSystem()); |
---|
60 | this->smoke_ = new ParticleInterface(this->getScene()->getSceneManager(), "Orxonox/smoke7", this->LOD_); |
---|
61 | this->attachOgreObject(this->smoke_->getParticleSystem()); |
---|
62 | } |
---|
63 | catch (...) |
---|
64 | { |
---|
65 | COUT(1) << "Error: Couln't load particle effect in ExplosionChunk." << std::endl; |
---|
66 | this->fire_ = 0; |
---|
67 | this->smoke_ = 0; |
---|
68 | } |
---|
69 | } |
---|
70 | else |
---|
71 | { |
---|
72 | this->fire_ = 0; |
---|
73 | this->smoke_ = 0; |
---|
74 | } |
---|
75 | |
---|
76 | if (GameMode::isMaster()) |
---|
77 | { |
---|
78 | Vector3 velocity(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)); |
---|
79 | velocity.normalise(); |
---|
80 | velocity *= rnd(60, 80); |
---|
81 | this->setVelocity(velocity); |
---|
82 | |
---|
83 | this->destroyTimer_.setTimer(rnd(1, 2), false, this, createExecutor(createFunctor(&ExplosionChunk::stop))); |
---|
84 | } |
---|
85 | |
---|
86 | this->registerVariables(); |
---|
87 | } |
---|
88 | |
---|
89 | ExplosionChunk::~ExplosionChunk() |
---|
90 | { |
---|
91 | if (this->isInitialized()) |
---|
92 | { |
---|
93 | if (this->fire_) |
---|
94 | { |
---|
95 | this->detachOgreObject(this->fire_->getParticleSystem()); |
---|
96 | delete this->fire_; |
---|
97 | } |
---|
98 | if (this->smoke_) |
---|
99 | { |
---|
100 | this->detachOgreObject(this->smoke_->getParticleSystem()); |
---|
101 | delete this->smoke_; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void ExplosionChunk::registerVariables() |
---|
107 | { |
---|
108 | registerVariable((int&)(this->LOD_), variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::LODchanged)); |
---|
109 | registerVariable(this->bStop_, variableDirection::toclient, new NetworkCallback<ExplosionChunk>(this, &ExplosionChunk::checkStop)); |
---|
110 | } |
---|
111 | |
---|
112 | void ExplosionChunk::LODchanged() |
---|
113 | { |
---|
114 | if (this->fire_) |
---|
115 | this->fire_->setDetailLevel(this->LOD_); |
---|
116 | if (this->smoke_) |
---|
117 | this->smoke_->setDetailLevel(this->LOD_); |
---|
118 | } |
---|
119 | |
---|
120 | void ExplosionChunk::checkStop() |
---|
121 | { |
---|
122 | if (this->bStop_) |
---|
123 | this->stop(); |
---|
124 | } |
---|
125 | |
---|
126 | void ExplosionChunk::stop() |
---|
127 | { |
---|
128 | if (this->fire_) |
---|
129 | this->fire_->setEnabled(false); |
---|
130 | if (this->smoke_) |
---|
131 | this->smoke_->setEnabled(false); |
---|
132 | |
---|
133 | if (GameMode::isMaster()) |
---|
134 | { |
---|
135 | this->bStop_ = true; |
---|
136 | this->destroyTimer_.setTimer(1.0f, false, this, createExecutor(createFunctor(&ExplosionChunk::destroy))); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | void ExplosionChunk::destroy() |
---|
141 | { |
---|
142 | delete this; |
---|
143 | } |
---|
144 | |
---|
145 | void ExplosionChunk::tick(float dt) |
---|
146 | { |
---|
147 | static const unsigned int CHANGES_PER_SECOND = 5; |
---|
148 | |
---|
149 | if (GameMode::isMaster() && rnd() < dt*CHANGES_PER_SECOND) |
---|
150 | { |
---|
151 | float length = this->getVelocity().length(); |
---|
152 | |
---|
153 | Vector3 change(rnd(-1, 1), rnd(-1, 1), rnd(-1, 1)); |
---|
154 | change.normalise(); |
---|
155 | change *= rnd(0.4, 0.8); |
---|
156 | Vector3 velocity = this->getVelocity(); |
---|
157 | velocity.normalise(); |
---|
158 | velocity += change; |
---|
159 | velocity.normalise(); |
---|
160 | velocity *= length * rnd(0.8, 1.0); |
---|
161 | |
---|
162 | this->setVelocity(velocity); |
---|
163 | } |
---|
164 | |
---|
165 | SUPER(ExplosionChunk, tick, dt); |
---|
166 | } |
---|
167 | } |
---|