e3a95d05c4eab52ac463a6cb7a7163b586b4a416
[supertux.git] / src / badguy / dispenser.cpp
1 #include <config.h>
2
3 #include "dispenser.h"
4 #include "badguy/bouncing_snowball.h"
5 #include "badguy/snowball.h"
6 #include "badguy/mrbomb.h"
7 #include "badguy/mriceblock.h"
8 #include "badguy/mrrocket.h"
9
10 Dispenser::Dispenser(const lisp::Lisp& reader)
11 {
12   reader.get("x", start_position.x);
13   reader.get("y", start_position.y);
14   reader.get("cycle", cycle);
15   reader.get("badguy", badguy);
16   bbox.set_size(32, 32);
17   sprite = sprite_manager->create("dispenser");
18   sprite->set_action("working");
19 }
20
21 void
22 Dispenser::write(lisp::Writer& writer)
23 {
24   writer.start_list("dispenser");
25
26   writer.write_float("x", start_position.x);
27   writer.write_float("y", start_position.y);
28   writer.write_float("cycle", cycle);
29   writer.write_string("badguy", badguy);
30
31   writer.end_list("dispenser");
32 }
33
34 void
35 Dispenser::activate()
36 {  
37    dispense_timer.start(cycle, true);
38    launch_badguy();
39 }
40
41 bool
42 Dispenser::collision_squished(Player& player)
43 {
44   //TODO: Should it act like a normal tile when killed?
45   sprite->set_action("broken");
46   dispense_timer.start(0);
47   player.bounce(*this);
48   kill_squished(player);
49   return true;
50 }
51
52 void
53 Dispenser::active_action(float )
54 {
55   if (dispense_timer.check()) {
56     launch_badguy();
57   }
58 }
59
60 //TODO: Add launching velocity to certain badguys
61 //      Add themed randomizer
62 //      Fix initial direction (everyone but MrRocket walks the wrong direction)
63 void
64 Dispenser::launch_badguy()
65 {
66   //FIXME: Does is_offscreen() work right here?
67   if (!is_offscreen()) {
68     if (badguy == "snowball")
69       Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y+32, dir));
70     else if (badguy == "bouncingsnowball")
71       Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y+32, dir));
72     else if (badguy == "mrbomb")
73       Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y+32, dir));
74     else if (badguy == "mriceblock")
75       Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y+32, dir));
76     else if (badguy == "mrrocket")
77       Sector::current()->add_object(new MrRocket(get_pos().x, get_pos().y+32, dir));
78     else if (badguy == "random")
79     {
80       switch (rand()%5)
81       {
82         case 0: Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y+32, dir)); break;
83         case 1: Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y+32, dir)); break;
84         case 2: Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y+32, dir)); break;
85         case 3: Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y+32, dir)); break;
86         case 4: Sector::current()->add_object(new MrRocket(get_pos().x, get_pos().y+32, dir)); break;
87       }
88     }
89   }
90 }
91
92 IMPLEMENT_FACTORY(Dispenser, "dispenser")