Fixed problems with Rockets and Cannons sometimes reversing direction on
[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)
27         : BadGuy(pos, dir, "images/creatures/mr_cherry/cherry.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 HitResponse
61 Bomb::collision_solid(GameObject& , const CollisionHit& hit)
62 {
63   if(fabsf(hit.normal.y) > .5)
64     physic.set_velocity_y(0);
65
66   return CONTINUE;
67 }
68
69 HitResponse
70 Bomb::collision_player(Player& player, const CollisionHit& )
71 {
72   if(state == STATE_EXPLODING) {
73     player.kill(false);
74   }
75   return ABORT_MOVE;
76 }
77
78 HitResponse
79 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
80 {
81   if(state == STATE_EXPLODING)
82     badguy.kill_fall();
83   return ABORT_MOVE;
84 }
85
86 void
87 Bomb::active_update(float )
88 {
89   switch(state) {
90     case STATE_TICKING:
91       ticking->set_position(get_pos());
92       if(sprite->animation_done()) {
93         explode();
94       }
95       break;
96     case STATE_EXPLODING:
97       if(sprite->animation_done()) {
98         remove_me();
99       }
100       break;
101   } 
102 }
103
104 void
105 Bomb::explode()
106 {
107   ticking->stop();
108   state = STATE_EXPLODING;
109   set_group(COLGROUP_TOUCHABLE);
110   sound_manager->play("sounds/explosion.wav", get_pos());
111   set_action_centered("explosion", 1);
112
113   // spawn some particles
114   // TODO: provide convenience function in MovingSprite or MovingObject?
115   for (int i = 0; i < 100; i++) {
116     Vector ppos = bbox.get_middle();
117     float angle = systemRandom.randf(-M_PI_2, M_PI_2);
118     float velocity = systemRandom.randf(450, 900);
119     float vx = sin(angle)*velocity;
120     float vy = -cos(angle)*velocity;
121     Vector pspeed = Vector(vx, vy);
122     Vector paccel = Vector(0, 1000);
123     Sector::current()->add_object(new SpriteParticle("images/objects/particles/kracker.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
124   }
125
126 }
127
128 void
129 Bomb::kill_fall()
130 {
131   if (state != STATE_EXPLODING)  // we don't want it exploding again
132     explode();
133 }
134