d7e4caa26d6719dfbb242c0ce31f5d5f6618a5ff
[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, "images/creatures/mr_cherry/cherry.sprite")
28 {
29   state = STATE_TICKING;
30   this->dir = dir;
31   set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
32   countMe = false;
33
34   ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
35   ticking->set_position(get_pos());
36   ticking->set_looping(true);
37   ticking->set_gain(2.0);
38   ticking->set_reference_distance(32);
39   ticking->play();
40 }
41
42 Bomb::Bomb(const Bomb& other)
43         : BadGuy(other), state(other.state)
44 {
45   if (state == STATE_TICKING) {
46     ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
47     ticking->set_position(get_pos());
48     ticking->set_looping(true);
49     ticking->set_gain(2.0);
50     ticking->set_reference_distance(32);
51     ticking->play();
52   }
53 }
54
55 void
56 Bomb::write(lisp::Writer& )
57 {
58   // bombs are only temporarily so don't write them out...
59 }
60
61 HitResponse
62 Bomb::collision_solid(GameObject& , const CollisionHit& hit)
63 {
64   if(fabsf(hit.normal.y) > .5)
65     physic.set_velocity_y(0);
66
67   return CONTINUE;
68 }
69
70 HitResponse
71 Bomb::collision_player(Player& player, const CollisionHit& )
72 {
73   if(state == STATE_EXPLODING) {
74     player.kill(false);
75   }
76   return ABORT_MOVE;
77 }
78
79 HitResponse
80 Bomb::collision_badguy(BadGuy& badguy, const CollisionHit& )
81 {
82   if(state == STATE_EXPLODING)
83     badguy.kill_fall();
84   return ABORT_MOVE;
85 }
86
87 void
88 Bomb::active_update(float )
89 {
90   switch(state) {
91     case STATE_TICKING:
92       ticking->set_position(get_pos());
93       if(sprite->animation_done()) {
94         explode();
95       }
96       break;
97     case STATE_EXPLODING:
98       if(sprite->animation_done()) {
99         remove_me();
100       }
101       break;
102   } 
103 }
104
105 void
106 Bomb::explode()
107 {
108   ticking->stop();
109   state = STATE_EXPLODING;
110   set_group(COLGROUP_TOUCHABLE);
111   sound_manager->play("sounds/explosion.wav", get_pos());
112   set_action_centered("explosion", 1);
113
114   // spawn some particles
115   // TODO: provide convenience function in MovingSprite or MovingObject?
116   for (int i = 0; i < 100; i++) {
117     Vector ppos = bbox.get_middle();
118     float angle = systemRandom.randf(-M_PI_2, M_PI_2);
119     float velocity = systemRandom.randf(450, 900);
120     float vx = sin(angle)*velocity;
121     float vy = -cos(angle)*velocity;
122     Vector pspeed = Vector(vx, vy);
123     Vector paccel = Vector(0, 1000);
124     Sector::current()->add_object(new SpriteParticle("images/objects/particles/kracker.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
125   }
126
127 }
128
129 void
130 Bomb::kill_fall()
131 {
132   if (state != STATE_EXPLODING)  // we don't want it exploding again
133     explode();
134 }
135