b7428c012db33bdf273a43be7c6feae978b5c118
[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/player.hpp"
24 #include "object/sprite_particle.hpp"
25 #include "sprite/sprite.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "supertux/object_factory.hpp"
28 #include "supertux/sector.hpp"
29
30 #include <math.h>
31
32 Explosion::Explosion(const Vector& pos) :
33   MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
34   hurt(true),
35   push(false),
36   state(STATE_WAITING),
37   light(0.0f,0.0f,0.0f),
38   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
39 {
40   sound_manager->preload("sounds/explosion.wav");
41   sound_manager->preload("sounds/firecracker.ogg");
42   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
43   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
44   lightsprite->set_color(Color(0.6f, 0.6f, 0.6f));
45 }
46
47 Explosion::Explosion(const Reader& reader) :
48   MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
49   hurt(true),
50   push(false),
51   state(STATE_WAITING),
52   light(0.0f,0.0f,0.0f),
53   lightsprite(sprite_manager->create("images/objects/lightmap_light/lightmap_light-large.sprite"))
54 {
55   sound_manager->preload("sounds/explosion.wav");
56   sound_manager->preload("sounds/firecracker.ogg");
57   lightsprite->set_blend(Blend(GL_SRC_ALPHA, GL_ONE));
58   lightsprite->set_color(Color(0.6f, 0.6f, 0.6f));
59 }
60
61 void
62 Explosion::explode()
63 {
64   if (state != STATE_WAITING)
65     return;
66   state = STATE_EXPLODING;
67
68   set_action(hurt ? "default" : "pop", 1);
69   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
70   sprite->set_angle(graphicsRandom.randf(0, 360)); // a random rotation on the sprite to make explosions appear more random
71   sound_manager->play(hurt ? "sounds/explosion.wav" : "sounds/firecracker.ogg", get_pos());   
72
73 #if 0
74   // spawn some particles
75   // TODO: provide convenience function in MovingSprite or MovingObject?
76   for (int i = 0; i < 100; i++) {
77     Vector ppos = bbox.get_middle();
78     float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
79     float velocity = graphicsRandom.randf(450, 900);
80     float vx = sin(angle)*velocity;
81     float vy = -cos(angle)*velocity;
82     Vector pspeed = Vector(vx, vy);
83     Vector paccel = Vector(0, 1000);
84     Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
85   }
86 #endif
87
88   if (push) {
89     Vector center = get_bbox ().get_middle ();
90     std::vector<MovingObject*> near_objects = Sector::current()->get_nearby_objects (center, 10.0 * 32.0);
91
92     for (size_t i = 0; i < near_objects.size (); i++) {
93       MovingObject *obj = near_objects[i];
94       Vector obj_vector = obj->get_bbox ().get_middle ();
95       Vector direction = obj_vector - center;
96       float distance = direction.norm ();
97
98       /* If the distance is very small, for example because "obj" is the badguy
99        * causing the explosion, skip this object. */
100       if (distance <= 1.0)
101         continue;
102
103       /* The force decreases with the distance squared. In the distance of one
104        * tile (32 pixels) you will have a speed increase of 150 pixels/s. */
105       float force = 150.0 * 32.0*32.0 / (distance * distance);
106       if (force > 200.0)
107         force = 200.0;
108
109       Vector add_speed = direction.unit () * force;
110
111       Player *player = dynamic_cast<Player *> (obj);
112       if (player) {
113         player->add_velocity (add_speed);
114       }
115
116       WalkingBadguy *badguy = dynamic_cast<WalkingBadguy *> (obj);
117       if (badguy) {
118         badguy->add_velocity (add_speed);
119       }
120     } /* for (i = 0 ... near_objects) */
121   } /* if (push) */
122 }
123
124 void 
125 Explosion::update(float )
126 {
127   switch(state) {
128     case STATE_WAITING:
129       explode();
130       break;
131     case STATE_EXPLODING:
132       if(sprite->animation_done()) {
133         remove_me();
134       }
135       break;
136   }
137 }
138
139 void
140 Explosion::draw(DrawingContext& context)
141 {
142   //Draw the Sprite.
143   sprite->draw(context, get_pos(), LAYER_OBJECTS+40);
144   //Explosions produce light (if ambient light is not maxed)
145   context.get_light( get_bbox().get_middle(), &light);
146   if (light.red + light.green + light.blue < 3.0){
147     context.push_target();
148     context.set_target(DrawingContext::LIGHTMAP);
149     lightsprite->draw(context, get_bbox().get_middle(), 0);
150     context.pop_target();
151   }
152 }
153
154 HitResponse
155 Explosion::collision(GameObject& other, const CollisionHit& )
156 {
157   if ((state != STATE_EXPLODING) || !hurt)
158     return ABORT_MOVE;
159
160   Player* player = dynamic_cast<Player*>(&other);
161   if(player != 0) {
162     player->kill(false);
163   }
164
165   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
166   if(badguy != 0) {
167     badguy->kill_fall();
168   }
169
170   return ABORT_MOVE;
171 }
172
173 /* EOF */