1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | |
---|
15 | */ |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON |
---|
17 | |
---|
18 | #include "explosion.h" |
---|
19 | |
---|
20 | #include "loading/fast_factory.h" |
---|
21 | |
---|
22 | #include "state.h" |
---|
23 | |
---|
24 | #include "particles/box_emitter.h" |
---|
25 | #include "particles/sprite_particles.h" |
---|
26 | |
---|
27 | #include "sound/resource_sound_buffer.h" |
---|
28 | #include "sound_engine.h" |
---|
29 | |
---|
30 | |
---|
31 | ObjectListDefinition(Explosion); |
---|
32 | CREATE_FAST_FACTORY_STATIC(Explosion); |
---|
33 | |
---|
34 | #include "script_class.h" |
---|
35 | CREATE_SCRIPTABLE_CLASS(Explosion, |
---|
36 | // Coordinates |
---|
37 | addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) |
---|
38 | ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) |
---|
39 | ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) |
---|
40 | ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) |
---|
41 | ->addMethod("setAbsDir", Executor4<PNode, lua_State*,float,float,float,float>(&PNode::setAbsDir)) |
---|
42 | //Explode ! |
---|
43 | ->addMethod("explode", Executor3<Explosion, lua_State*, float, float, float>(&Explosion::explode)) |
---|
44 | ->addMethod("setExplosionSound", Executor1<Explosion, lua_State*, const std::string&>(&Explosion::setExplosionSound)) |
---|
45 | ->addMethod("playSound", Executor0<Explosion, lua_State*>(&Explosion::playSound)) |
---|
46 | ); |
---|
47 | |
---|
48 | /** |
---|
49 | * standard constructor |
---|
50 | */ |
---|
51 | Explosion::Explosion () |
---|
52 | { |
---|
53 | this->registerObject(this, Explosion::_objectList); |
---|
54 | this->toList(OM_DEAD_TICK); |
---|
55 | |
---|
56 | this->emitter = new BoxEmitter(Vector(10,10,10), 200, 45, M_2_PI); |
---|
57 | this->emitter->addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); |
---|
58 | this->emitter->setParent(this); |
---|
59 | this->emitter->setSpread(M_PI, M_PI); |
---|
60 | |
---|
61 | this->lifeCycle = 0.0f; |
---|
62 | this->lifeTime = .5f; |
---|
63 | |
---|
64 | this->explosionSound.setSourceNode(this); |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * standard deconstructor |
---|
71 | */ |
---|
72 | Explosion::~Explosion () |
---|
73 | { |
---|
74 | delete this->emitter; |
---|
75 | if (this->explosionSound.isPlaying()) |
---|
76 | this->explosionSound.stop(); |
---|
77 | /* this is normaly done by World.cc by deleting the ParticleEngine */ |
---|
78 | if (Explosion::explosionParticles != NULL && Explosion::objectList().size() <= 1) |
---|
79 | Explosion::explosionParticles = NULL; |
---|
80 | } |
---|
81 | |
---|
82 | SpriteParticles* Explosion::explosionParticles = NULL; |
---|
83 | |
---|
84 | void Explosion::explode(PNode* position, const Vector& size) |
---|
85 | { |
---|
86 | Explosion* explosion = dynamic_cast<Explosion*>(Explosion::fastFactory->resurrect()); |
---|
87 | explosion->setAbsCoor(position->getAbsCoor()); |
---|
88 | explosion->emitter->setSize(size); |
---|
89 | explosion->activate(); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | void Explosion::explode(float x, float y, float z) |
---|
94 | { |
---|
95 | Explosion* explosion = dynamic_cast<Explosion*>(Explosion::fastFactory->resurrect()); |
---|
96 | explosion->setAbsCoor(this->getAbsCoor()); |
---|
97 | explosion->emitter->setSize(Vector(x,y,z)); |
---|
98 | explosion->activate(); |
---|
99 | |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void Explosion::activate() |
---|
105 | { |
---|
106 | if (unlikely(Explosion::explosionParticles == NULL)) |
---|
107 | { |
---|
108 | Explosion::explosionParticles = new SpriteParticles(5000); |
---|
109 | Explosion::explosionParticles->setName("ExplosionExplosionParticles"); |
---|
110 | Explosion::explosionParticles->setMaterialTexture("textures/radial-trans-noise.png"); |
---|
111 | Explosion::explosionParticles->setLifeSpan(1.5, 0.3); |
---|
112 | Explosion::explosionParticles->setRadius(0.0, 30.0); |
---|
113 | Explosion::explosionParticles->setRadius(0.5, 50.0); |
---|
114 | Explosion::explosionParticles->setRadius(1.0, 30.0); |
---|
115 | Explosion::explosionParticles->setColor(0.0, 1.0, 0.5, 0.0, 1.0); |
---|
116 | Explosion::explosionParticles->setColor(0.4, 0.8, 0.3, 0.0, 1.0); |
---|
117 | Explosion::explosionParticles->setColor(0.8, 0.6, 0.0, 0.0, 0.7); |
---|
118 | Explosion::explosionParticles->setColor(1.0, 0.0, 0.0, 0.0, 0.0); |
---|
119 | } |
---|
120 | |
---|
121 | this->emitter->setSystem(Explosion::explosionParticles); |
---|
122 | this->emitter->updateNode(.01); |
---|
123 | this->emitter->updateNode(.01); |
---|
124 | this->toList(OM_DEAD_TICK); |
---|
125 | this->lifeCycle = 0.0; |
---|
126 | this->explosionSound.play(this->explosionSoundBuffer, OrxSound::SoundEngine::getInstance()->getEffectsVolume(), 0.01, false); |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | void Explosion::deactivate() |
---|
132 | { |
---|
133 | this->emitter->setSystem(NULL); |
---|
134 | this->toList(OM_DEAD); |
---|
135 | Explosion::fastFactory->kill(this); |
---|
136 | } |
---|
137 | |
---|
138 | void Explosion::playSound() |
---|
139 | { |
---|
140 | if (!this->explosionSound.isPlaying()) |
---|
141 | this->explosionSound.play(this->explosionSoundBuffer, OrxSound::SoundEngine::getInstance()->getEffectsVolume(), 0.01, false); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * signal tick, time dependent things will be handled here |
---|
146 | * @param time since last tick |
---|
147 | */ |
---|
148 | void Explosion::tick (float dt) |
---|
149 | { |
---|
150 | this->lifeCycle += dt; |
---|
151 | if(this->lifeTime < this->lifeCycle) |
---|
152 | this->deactivate(); |
---|
153 | } |
---|