Set launchdirection ie. the initial direction of badguys created by the dispenser.
[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   std::string launchdirection = "";
37   launchdir = dir;
38   reader.get("launchdirection", launchdirection);
39   if( launchdirection == "left" || launchdirection == "LEFT"  )
40     launchdir = LEFT;
41   if( launchdirection == "right" || launchdirection == "RIGHT"  ) 
42     launchdir = RIGHT;
43   reader.get("cycle", cycle);
44   reader.get("badguy", badguy);
45   if (badguy == "mrrocket") {
46      sprite->set_action(launchdir == LEFT ? "working-left" : "working-right");
47   }
48   else {sprite->set_action("dropper");}
49   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
50   countMe = false;
51 }
52
53 void
54 Dispenser::write(lisp::Writer& writer)
55 {
56   writer.start_list("dispenser");
57
58   writer.write_float("x", start_position.x);
59   writer.write_float("y", start_position.y);
60   writer.write_float("cycle", cycle);
61   writer.write_string("badguy", badguy);
62
63   writer.end_list("dispenser");
64 }
65
66 void
67 Dispenser::activate()
68 {  
69    dispense_timer.start(cycle, true);
70    launch_badguy();
71 }
72
73 bool
74 Dispenser::collision_squished(Player& player)
75 {
76   //TODO: Should it act like a normal tile when killed?
77   sprite->set_action(launchdir == LEFT ? "broken-left" : "broken-right");
78   dispense_timer.start(0);
79   player.bounce(*this);
80   kill_squished(player);
81   return true;
82 }
83
84 void
85 Dispenser::active_update(float )
86 {
87   if (dispense_timer.check()) {
88     launch_badguy();
89   }
90 }
91
92 //TODO: Add launching velocity to certain badguys
93 //      Add themed randomizer
94 void
95 Dispenser::launch_badguy()
96 {
97   //FIXME: Does is_offscreen() work right here?
98   if (!is_offscreen()) {
99     if (badguy == "snowball")
100       Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), launchdir));
101     else if (badguy == "bouncingsnowball")
102       Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), launchdir));
103     else if (badguy == "mrbomb")
104       Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), launchdir));
105     else if (badguy == "mriceblock")
106       Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), launchdir));
107     else if (badguy == "snail")
108       Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), launchdir));
109     else if (badguy == "mrrocket") {
110       Sector::current()->add_object(new MrRocket(Vector(get_pos().x+(launchdir == LEFT ? -32 : 32), get_pos().y), launchdir));}
111     else if (badguy == "poisonivy")
112       Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), launchdir));
113     else if (badguy == "skullyhop")
114       Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), launchdir));
115     else if (badguy == "random")
116     {
117       switch (systemRandom.rand(7))
118       {
119         case 0: Sector::current()->add_object(new SnowBall(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
120         case 1: Sector::current()->add_object(new BouncingSnowball(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
121         case 2: Sector::current()->add_object(new MrBomb(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
122         case 3: Sector::current()->add_object(new MrIceBlock(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
123         case 4: Sector::current()->add_object(new PoisonIvy(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
124         case 5: Sector::current()->add_object(new Snail(Vector(get_pos().x, get_pos().y+32), launchdir)); break;
125         case 6: Sector::current()->add_object(new SkullyHop(Vector(get_pos().x, get_pos().y+44), launchdir)); break;
126       }
127     }
128   }
129 }
130
131 IMPLEMENT_FACTORY(Dispenser, "dispenser")