e22f81998bdd8f6afe35dfc723cceb6130f2f441
[supertux.git] / src / badguy / dispenser.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
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/snowsnail.hpp"
30 #include "badguy/skullyhop.hpp"
31
32 Dispenser::Dispenser(const lisp::Lisp& reader)
33 {
34   reader.get("x", start_position.x);
35   reader.get("y", start_position.y);
36   reader.get("cycle", cycle);
37   reader.get("badguy", badguy);
38   bbox.set_size(32, 32);
39   sprite = sprite_manager->create("images/creatures/dispenser/dispenser.sprite");
40   if (badguy == "mrrocket") {
41      sprite->set_action(dir == LEFT ? "working-left" : "working-right");
42   }
43   else {sprite->set_action("dropper");}
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    dispense_timer.start(cycle, true);
64    launch_badguy();
65 }
66
67 bool
68 Dispenser::collision_squished(Player& player)
69 {
70   //TODO: Should it act like a normal tile when killed?
71   sprite->set_action(dir == LEFT ? "broken-left" : "broken-right");
72   dispense_timer.start(0);
73   player.bounce(*this);
74   kill_squished(player);
75   return true;
76 }
77
78 void
79 Dispenser::active_update(float )
80 {
81   if (dispense_timer.check()) {
82     launch_badguy();
83   }
84 }
85
86 //TODO: Add launching velocity to certain badguys
87 //      Add themed randomizer
88 //      Fix initial direction (everyone but MrRocket walks the wrong direction)
89 void
90 Dispenser::launch_badguy()
91 {
92   //FIXME: Does is_offscreen() work right here?
93   if (!is_offscreen()) {
94     if (badguy == "snowball")
95       Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y+32, dir));
96     else if (badguy == "bouncingsnowball")
97       Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y+32, dir));
98     else if (badguy == "mrbomb")
99       Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y+32, dir, false));
100     else if (badguy == "mriceblock")
101       Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y+32, dir, false));
102     else if (badguy == "snowsnail")
103       Sector::current()->add_object(new SnowSnail(get_pos().x, get_pos().y+32, dir));
104     else if (badguy == "mrrocket") {
105       Sector::current()->add_object(new MrRocket(get_pos().x+(dir == LEFT ? -32 : 32), get_pos().y, dir));}
106     else if (badguy == "poisonivy")
107       Sector::current()->add_object(new PoisonIvy(get_pos().x, get_pos().y+32, dir));
108     else if (badguy == "skullyhop")
109       Sector::current()->add_object(new SkullyHop(get_pos().x, get_pos().y+44, dir));
110     else if (badguy == "random")
111     {
112       switch (rand()%7)
113       {
114         case 0: Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y+32, dir)); break;
115         case 1: Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y+32, dir)); break;
116         case 2: Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y+32, dir, false)); break;
117         case 3: Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y+32, dir, false)); break;
118         case 4: Sector::current()->add_object(new PoisonIvy(get_pos().x, get_pos().y+32, dir)); break;
119         case 5: Sector::current()->add_object(new SnowSnail(get_pos().x, get_pos().y+32, dir)); break;
120         case 6: Sector::current()->add_object(new SkullyHop(get_pos().x, get_pos().y+44, dir)); break;
121       }
122     }
123   }
124 }
125
126 IMPLEMENT_FACTORY(Dispenser, "dispenser")