Badguys squish badguys.
[supertux.git] / src / badguy / dispenser.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 "dispenser.hpp"
23 #include "badguy/bouncing_snowball.hpp"
24 #include "badguy/snowball.hpp"
25 #include "badguy/mrbomb.hpp"
26 #include "badguy/mriceblock.hpp"
27 #include "badguy/mrrocket.hpp"
28 #include "badguy/poisonivy.hpp"
29 #include "badguy/snail.hpp"
30 #include "badguy/skullyhop.hpp"
31 #include "random_generator.hpp"
32
33 Dispenser::Dispenser(const lisp::Lisp& reader)
34         : BadGuy(reader, "images/creatures/dispenser/dispenser.sprite")
35 {
36   reader.get("cycle", cycle);
37   reader.get("badguy", badguy);
38   if (badguy == "mrrocket") {
39      if (start_dir == AUTO) log_warning << "Setting a Dispenser's direction to AUTO is no good idea" << std::endl;
40      sprite->set_action(dir == LEFT ? "working-left" : "working-right");
41   }
42   else {sprite->set_action("dropper");}
43   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
44   countMe = false;
45 }
46
47 void
48 Dispenser::write(lisp::Writer& writer)
49 {
50   writer.start_list("dispenser");
51
52   writer.write_float("x", start_position.x);
53   writer.write_float("y", start_position.y);
54   writer.write_float("cycle", cycle);
55   writer.write_string("badguy", badguy);
56
57   writer.end_list("dispenser");
58 }
59
60 void
61 Dispenser::activate()
62 {
63    if(frozen)
64      return;
65    dispense_timer.start(cycle, true);
66    launch_badguy();
67 }
68
69 void
70 Dispenser::deactivate()
71 {
72    dispense_timer.stop();
73 }
74
75 //TODO: Add launching velocity to certain badguys
76 bool
77 Dispenser::collision_squished(GameObject& object)
78 {
79   //TODO: Should it act like a normal tile when killed?
80   sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
81   dispense_timer.start(0);
82   Player* player = dynamic_cast<Player*>(&object);
83   if (player) player->bounce(*this);
84   kill_squished(object);
85   return true;
86 }
87
88 void
89 Dispenser::active_update(float )
90 {
91   if (dispense_timer.check()) {
92     launch_badguy();
93   }
94 }
95
96 //      Add themed randomizer
97 void
98 Dispenser::launch_badguy()
99 {
100   //FIXME: Does is_offscreen() work right here?
101   if (!is_offscreen()) {
102     if (badguy == "snowball")
103       Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir));
104     else if (badguy == "bouncingsnowball")
105       Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir));
106     else if (badguy == "mrbomb")
107       Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir));
108     else if (badguy == "mriceblock")
109       Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir));
110     else if (badguy == "snail")
111       Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir));
112     else if (badguy == "mrrocket") {
113       Sector::current()->add_object(new MrRocket(Vector(get_pos().x+(dir == LEFT ? -32 : 32), get_pos().y), dir));}
114     else if (badguy == "poisonivy")
115       Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir));
116     else if (badguy == "skullyhop")
117       Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir));
118     else if (badguy == "random")
119     {
120       switch (systemRandom.rand(7))
121       {
122         case 0: Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), dir)); break;
123         case 1: Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), dir)); break;
124         case 2: Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), dir)); break;
125         case 3: Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), dir)); break;
126         case 4: Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), dir)); break;
127         case 5: Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), dir)); break;
128         case 6: Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), dir)); break;
129       }
130     }
131   }
132 }
133
134 void
135 Dispenser::freeze()
136 {
137   BadGuy::freeze();
138   dispense_timer.stop();
139 }
140
141 void
142 Dispenser::unfreeze()
143 {
144   BadGuy::unfreeze();
145   activate();
146 }
147
148 bool
149 Dispenser::is_freezable() const
150 {
151   return true;
152 }
153 IMPLEMENT_FACTORY(Dispenser, "dispenser")