1 // SuperTux -- Explosion object
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "object/explosion.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "badguy/walking_badguy.hpp"
22 #include "math/random_generator.hpp"
23 #include "object/particles.hpp"
24 #include "object/player.hpp"
25 #include "object/sprite_particle.hpp"
26 #include "sprite/sprite.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "supertux/object_factory.hpp"
29 #include "supertux/sector.hpp"
33 Explosion::Explosion(const Vector& pos) :
34 MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
38 light(0.0f,0.0f,0.0f),
39 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
41 SoundManager::current()->preload("sounds/explosion.wav");
42 SoundManager::current()->preload("sounds/firecracker.ogg");
43 set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
44 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
45 lightsprite->set_color(Color(0.6f, 0.6f, 0.6f));
48 Explosion::Explosion(const Reader& reader) :
49 MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
53 light(0.0f,0.0f,0.0f),
54 lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
56 SoundManager::current()->preload("sounds/explosion.wav");
57 SoundManager::current()->preload("sounds/firecracker.ogg");
58 lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
59 lightsprite->set_color(Color(0.6f, 0.6f, 0.6f));
65 if (state != STATE_WAITING)
67 state = STATE_EXPLODING;
69 set_action(hurt ? "default" : "pop", 1);
70 sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
71 sprite->set_angle(graphicsRandom.randf(0, 360)); // a random rotation on the sprite to make explosions appear more random
72 SoundManager::current()->play(hurt ? "sounds/explosion.wav" : "sounds/firecracker.ogg", get_pos());
74 // spawn some particles
75 int pnumber = push ? 8 : 100;
76 Sector::current()->add_object(std::make_shared<Particles>(
77 bbox.get_middle(), -360, 360, 450, 900, Vector(0, 1000), pnumber, Color(.4f, .4f, .4f), 3, .8f, LAYER_OBJECTS-1));
80 Vector center = get_bbox ().get_middle ();
81 std::vector<MovingObject*> near_objects = Sector::current()->get_nearby_objects (center, 10.0 * 32.0);
83 for (size_t i = 0; i < near_objects.size (); i++) {
84 MovingObject *obj = near_objects[i];
85 Vector obj_vector = obj->get_bbox ().get_middle ();
86 Vector direction = obj_vector - center;
87 float distance = direction.norm ();
89 /* If the distance is very small, for example because "obj" is the badguy
90 * causing the explosion, skip this object. */
94 /* The force decreases with the distance squared. In the distance of one
95 * tile (32 pixels) you will have a speed increase of 150 pixels/s. */
96 float force = 150.0 * 32.0*32.0 / (distance * distance);
100 Vector add_speed = direction.unit () * force;
102 Player *player = dynamic_cast<Player *> (obj);
104 player->add_velocity (add_speed);
107 WalkingBadguy *badguy = dynamic_cast<WalkingBadguy *> (obj);
109 badguy->add_velocity (add_speed);
111 } /* for (i = 0 ... near_objects) */
116 Explosion::update(float )
122 case STATE_EXPLODING:
123 if(sprite->animation_done()) {
131 Explosion::draw(DrawingContext& context)
134 sprite->draw(context, get_pos(), LAYER_OBJECTS+40);
135 //Explosions produce light (if ambient light is not maxed)
136 context.get_light( get_bbox().get_middle(), &light);
137 if (light.red + light.green + light.blue < 3.0){
138 context.push_target();
139 context.set_target(DrawingContext::LIGHTMAP);
140 lightsprite->draw(context, get_bbox().get_middle(), 0);
141 context.pop_target();
146 Explosion::collision(GameObject& other, const CollisionHit& )
148 if ((state != STATE_EXPLODING) || !hurt)
151 Player* player = dynamic_cast<Player*>(&other);
156 BadGuy* badguy = dynamic_cast<BadGuy*>(&other);