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