Move patch to another directory
[supertux.git] / src / object / explosion.cpp
1 //  SuperTux -- Explosion object
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "object/explosion.hpp"
18
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"
30
31 #include <math.h>
32
33 Explosion::Explosion(const Vector& pos) :
34   MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
35   hurt(true),
36   push(false),
37   state(STATE_WAITING),
38   light(0.0f,0.0f,0.0f),
39   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
40 {
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));
46 }
47
48 Explosion::Explosion(const Reader& reader) :
49   MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
50   hurt(true),
51   push(false),
52   state(STATE_WAITING),
53   light(0.0f,0.0f,0.0f),
54   lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
55 {
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));
60 }
61
62 void
63 Explosion::explode()
64 {
65   if (state != STATE_WAITING)
66     return;
67   state = STATE_EXPLODING;
68
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());
73
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));
78
79   if (push) {
80     Vector center = get_bbox ().get_middle ();
81     std::vector<MovingObject*> near_objects = Sector::current()->get_nearby_objects (center, 10.0 * 32.0);
82
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 ();
88
89       /* If the distance is very small, for example because "obj" is the badguy
90        * causing the explosion, skip this object. */
91       if (distance <= 1.0)
92         continue;
93
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);
97       if (force > 200.0)
98         force = 200.0;
99
100       Vector add_speed = direction.unit () * force;
101
102       Player *player = dynamic_cast<Player *> (obj);
103       if (player) {
104         player->add_velocity (add_speed);
105       }
106
107       WalkingBadguy *badguy = dynamic_cast<WalkingBadguy *> (obj);
108       if (badguy) {
109         badguy->add_velocity (add_speed);
110       }
111     } /* for (i = 0 ... near_objects) */
112   } /* if (push) */
113 }
114
115 void
116 Explosion::update(float )
117 {
118   switch(state) {
119     case STATE_WAITING:
120       explode();
121       break;
122     case STATE_EXPLODING:
123       if(sprite->animation_done()) {
124         remove_me();
125       }
126       break;
127   }
128 }
129
130 void
131 Explosion::draw(DrawingContext& context)
132 {
133   //Draw the Sprite.
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();
142   }
143 }
144
145 HitResponse
146 Explosion::collision(GameObject& other, const CollisionHit& )
147 {
148   if ((state != STATE_EXPLODING) || !hurt)
149     return ABORT_MOVE;
150
151   Player* player = dynamic_cast<Player*>(&other);
152   if(player != 0) {
153     player->kill(false);
154   }
155
156   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
157   if(badguy != 0) {
158     badguy->kill_fall();
159   }
160
161   return ABORT_MOVE;
162 }
163
164 /* EOF */