f3445b3f7c921bd9626f27ab6622b94fdf9e1a6d
[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
9 Dispenser::Dispenser(const lisp::Lisp& reader)
10 {
11   reader.get("x", start_position.x);
12   reader.get("y", start_position.y);
13   reader.get("cycle", cycle);
14   reader.get("badguy", badguy);
15   bbox.set_size(32, 32);
16   sprite = sprite_manager->create("dispenser");
17   sprite->set_action("working");
18 }
19
20 void
21 Dispenser::write(lisp::Writer& writer)
22 {
23   writer.start_list("dispenser");
24
25   writer.write_float("x", start_position.x);
26   writer.write_float("y", start_position.y);
27   writer.write_float("cycle", cycle);
28   writer.write_string("badguy", badguy);
29
30   writer.end_list("dispenser");
31 }
32
33 void
34 Dispenser::activate()
35 {  
36    dispense_timer.start(cycle, true);
37    launch_badguy();
38 }
39
40 bool
41 Dispenser::collision_squished(Player& player)
42 {
43   //FIXME: Should act like a normal tile when killed
44   sprite->set_action("broken");
45   dispense_timer.start(0);
46   player.bounce(*this);
47   return true;
48 }
49
50 void
51 Dispenser::active_action(float )
52 {
53   if (dispense_timer.check()) {
54     launch_badguy();
55   }
56 }
57
58 HitResponse
59 Dispenser::collision_solid(GameObject& , const CollisionHit& hit)
60 {
61   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
62     physic.set_velocity_y(0);
63   } else { // hit right or left
64     dir = dir == LEFT ? RIGHT : LEFT;
65     sprite->set_action(dir == LEFT ? "left" : "right");
66     physic.set_velocity_x(-physic.get_velocity_x());
67   }
68
69   return CONTINUE;
70 }
71
72 //TODO: Add launching velocity to badguys
73 //      Add randomizer
74 //      Clean up stuff I copied without understanding what it does :)
75 //      Stop dispensing when game is paused (timer related problem)
76 //      Lots-O-Stuff (tm)
77 void
78 Dispenser::launch_badguy()
79 {
80   //FIXME: Does is_offscreen() work right here?
81   if (!is_offscreen()) {
82     if (badguy == "snowball")
83       Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y, dir));
84     else if (badguy == "bouncingsnowball")
85       Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y, dir));
86     else if (badguy == "mrbomb")
87       Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y, dir));
88     else if (badguy == "mriceblock")
89       Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y, dir));
90     else if (badguy == "random")
91     {}
92   }
93 }
94