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