Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/effects/explosion.cc @ 10758

Last change on this file since 10758 was 10724, checked in by snellen, 18 years ago

explosions now have sound

File size: 4.8 KB
Line 
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
31ObjectListDefinition(Explosion);
32CREATE_FAST_FACTORY_STATIC(Explosion);
33
34#include "script_class.h"
35CREATE_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*/
51Explosion::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*/
72Explosion::~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
82SpriteParticles* Explosion::explosionParticles = NULL;
83
84void 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
93void 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
104void 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, .3);
112    Explosion::explosionParticles->setRadius(0.0, 10);
113    Explosion::explosionParticles->setRadius(.5, 30.0);
114    Explosion::explosionParticles->setRadius(1.0, 10.0);
115    Explosion::explosionParticles->setColor(0.0, 1,0,0,1);
116    Explosion::explosionParticles->setColor(0.5, .8,.8,0,.5);
117    Explosion::explosionParticles->setColor(0.8, .8,.8,.3,.3);
118    Explosion::explosionParticles->setColor(1.0, 1,1,1,.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
131void Explosion::deactivate()
132{
133  this->emitter->setSystem(NULL);
134  this->toList(OM_DEAD);
135  Explosion::fastFactory->kill(this);
136}
137
138void 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*/
148void Explosion::tick (float dt)
149{
150  this->lifeCycle += dt;
151  if(this->lifeTime < this->lifeCycle)
152    this->deactivate();
153}
Note: See TracBrowser for help on using the repository browser.