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