updated dispenser -- can summon mriceblock, snowball, bouncing_snowball and mrbomb 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
9
10 Dispenser::Dispenser(LispReader& reader)
11 {
12   reader.read_float("x", start_position.x);
13   reader.read_float("y", start_position.y);
14   reader.read_float("cycle", cycle);
15   reader.read_string("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(LispWriter& writer)
23 {
24   writer.start_list("dispenser");
25
26   writer.write_float("x", get_pos().x);
27   writer.write_float("y", get_pos().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   //FIXME: Should act like a normal tile when killed
45   sprite->set_action("broken");
46   dispense_timer.start(0);
47   player.bounce(*this);
48   return true;
49 }
50
51 void
52 Dispenser::active_action(float )
53 {
54    if (dispense_timer.check()) {
55       launch_badguy();
56    }
57 }
58
59 HitResponse
60 Dispenser::collision_solid(GameObject& , const CollisionHit& hit)
61 {
62   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
63     physic.set_velocity_y(0);
64   } else { // hit right or left
65     dir = dir == LEFT ? RIGHT : LEFT;
66     sprite->set_action(dir == LEFT ? "left" : "right");
67     physic.set_velocity_x(-physic.get_velocity_x());
68   }
69
70   return CONTINUE;
71 }
72
73 //TODO: Add launching velocity to badguys
74 //      Add randomizer
75 //      Clean up stuff I copied without understanding what it does :)
76 //      Stop dispensing when game is paused (timer related problem)
77 //      Lots-O-Stuff (tm)
78 void
79 Dispenser::launch_badguy()
80 {
81    //FIXME: Does is_offscreen() work right here?
82    if (!is_offscreen()) {
83     if (badguy == "snowball")
84       Sector::current()->add_object(new SnowBall(get_pos().x, get_pos().y, dir));
85     else if (badguy == "bouncingsnowball")
86       Sector::current()->add_object(new BouncingSnowball(get_pos().x, get_pos().y, dir));
87     else if (badguy == "mrbomb")
88       Sector::current()->add_object(new MrBomb(get_pos().x, get_pos().y, dir));
89     else if (badguy == "mriceblock")
90       Sector::current()->add_object(new MrIceBlock(get_pos().x, get_pos().y, dir));
91     else if (badguy == "random")
92       {}
93    }
94 }