Sorry, was too early for that patch.
[supertux.git] / src / badguy / bomb.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "bomb.hpp"
23 #include "random_generator.hpp"
24 #include "object/sprite_particle.hpp"
25
26 Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
27         : BadGuy( pos, dir, custom_sprite )
28 {
29   state = STATE_TICKING;
30   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
31   countMe = false;
32
33   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
34   ticking->set_position(get_pos());
35   ticking->set_looping(true);
36   ticking->set_gain(2.0);
37   ticking->set_reference_distance(32);
38   ticking->play();
39 }
40
41 Bomb::Bomb(const Bomb& other)
42         : BadGuy(other), state(other.state)
43 {
44   if (state == STATE_TICKING) {
45     ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
46     ticking->set_position(get_pos());
47     ticking->set_looping(true);
48     ticking->set_gain(2.0);
49     ticking->set_reference_distance(32);
50     ticking->play();
51   }
52 }
53
54 void
55 Bomb::write(lisp::Writer& )
56 {
57   // bombs are only temporarily so don't write them out...
58 }
59
60 void
61 Bomb::collision_solid(const CollisionHit& hit)
62 {
63   if(hit.bottom)
64     physic.set_velocity_y(0);
65 }
66
67 HitResponse
68 Bomb::collision_player(Player& player, const CollisionHit& )
69 {
70   if(state == STATE_EXPLODING) {
71     player.kill(false);
72   }
73   return ABORT_MOVE;
74 }
75
76 HitResponse
77 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
78 {
79   if(state == STATE_EXPLODING)
80     badguy.kill_fall();
81   return ABORT_MOVE;
82 }
83
84 void
85 Bomb::active_update(float )
86 {
87   switch(state) {
88     case STATE_TICKING:
89       ticking->set_position(get_pos());
90       if(sprite->animation_done()) {
91         explode();
92       }
93       break;
94     case STATE_EXPLODING:
95       if(sprite->animation_done()) {
96         remove_me();
97       }
98       break;
99   }
100 }
101
102 void
103 Bomb::explode()
104 {
105   ticking->stop();
106   state = STATE_EXPLODING;
107   set_group(COLGROUP_TOUCHABLE);
108   sound_manager->play("sounds/explosion.wav", get_pos());
109   set_action("explosion", 1, ANCHOR_BOTTOM);
110
111   // spawn some particles
112   // TODO: provide convenience function in MovingSprite or MovingObject?
113   for (int i = 0; i < 100; i++) {
114     Vector ppos = bbox.get_middle();
115     float angle = systemRandom.randf(-M_PI_2, M_PI_2);
116     float velocity = systemRandom.randf(450, 900);
117     float vx = sin(angle)*velocity;
118     float vy = -cos(angle)*velocity;
119     Vector pspeed = Vector(vx, vy);
120     Vector paccel = Vector(0, 1000);
121     Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
122   }
123
124 }
125
126 void
127 Bomb::kill_fall()
128 {
129   if (state != STATE_EXPLODING)  // we don't want it exploding again
130     explode();
131 }