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