object/explosion.[ch]pp: Added the "hurt" and "push" members.
[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 "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27
28 #include <math.h>
29
30 Explosion::Explosion(const Vector& pos) :
31   MovingSprite(pos, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
32   hurt(true),
33   push(false),
34   state(STATE_WAITING)
35 {
36   sound_manager->preload("sounds/explosion.wav");
37   set_pos(get_pos() - (get_bbox().get_middle() - get_pos()));
38 }
39
40 Explosion::Explosion(const Reader& reader) :
41   MovingSprite(reader, "images/objects/explosion/explosion.sprite", LAYER_OBJECTS+40, COLGROUP_MOVING),
42   hurt(true),
43   push(false),
44   state(STATE_WAITING)
45 {
46   sound_manager->preload("sounds/explosion.wav");
47 }
48
49 void
50 Explosion::explode()
51 {
52   if (state != STATE_WAITING)
53     return;
54   state = STATE_EXPLODING;
55
56   set_action("default", 1);
57   sprite->set_animation_loops(1); //TODO: this is necessary because set_action will not set "loops" when "action" is the default action
58   sound_manager->play("sounds/explosion.wav", get_pos());
59
60 #if 0
61   // spawn some particles
62   // TODO: provide convenience function in MovingSprite or MovingObject?
63   for (int i = 0; i < 100; i++) {
64     Vector ppos = bbox.get_middle();
65     float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
66     float velocity = graphicsRandom.randf(450, 900);
67     float vx = sin(angle)*velocity;
68     float vy = -cos(angle)*velocity;
69     Vector pspeed = Vector(vx, vy);
70     Vector paccel = Vector(0, 1000);
71     Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
72   }
73 #endif
74
75   if (push) {
76     Vector center = get_bbox ().get_middle ();
77     std::vector<MovingObject*> near_objects = Sector::current()->get_nearby_objects (center, 10.0 * 32.0);
78
79     for (size_t i = 0; i < near_objects.size (); i++) {
80       MovingObject *obj = near_objects[i];
81       Vector obj_vector = obj->get_bbox ().get_middle ();
82       Vector direction = obj_vector - center;
83       float distance = direction.norm ();
84
85       /* If the distance is very small, for example because "obj" is the badguy
86        * causing the explosion, skip this object. */
87       if (distance <= 1.0)
88         continue;
89
90       /* The force decreases with the distance squared. In the distance of one
91        * tile (32 pixels) you will have a speed increase of 150 pixels/s. */
92       float force = 150.0 * 32.0*32.0 / (distance * distance);
93       if (force > 200.0)
94         force = 200.0;
95
96       Vector add_speed = direction.unit () * force;
97
98       Player *player = dynamic_cast<Player *> (obj);
99       if (player) {
100         player->add_velocity (add_speed);
101       }
102
103       WalkingBadguy *badguy = dynamic_cast<WalkingBadguy *> (obj);
104       if (badguy) {
105         badguy->add_velocity (add_speed);
106       }
107     } /* for (i = 0 ... near_objects) */
108   } /* if (push) */
109 }
110
111 void 
112 Explosion::update(float )
113 {
114   switch(state) {
115     case STATE_WAITING:
116       explode();
117       break;
118     case STATE_EXPLODING:
119       if(sprite->animation_done()) {
120         remove_me();
121       }
122       break;
123   }
124 }
125
126 HitResponse
127 Explosion::collision(GameObject& other, const CollisionHit& )
128 {
129   if ((state != STATE_EXPLODING) || !hurt)
130     return ABORT_MOVE;
131
132   Player* player = dynamic_cast<Player*>(&other);
133   if(player != 0) {
134     player->kill(false);
135   }
136
137   BadGuy* badguy = dynamic_cast<BadGuy*>(&other);
138   if(badguy != 0) {
139     badguy->kill_fall();
140   }
141
142   return ABORT_MOVE;
143 }
144
145 /* EOF */