dispenser can now launch snowballs and bouncingsnowballs
[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   //FIXME: Create dispenser sprite
16   sprite = sprite_manager->create("snowball");
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   remove_me();
43   player.bounce(*this);
44   return true;
45 }
46
47 void
48 Dispenser::active_action(float elapsed_time)
49 {
50    if (dispense_timer.check()) {
51       launch_badguy();
52    }
53 }
54
55 HitResponse
56 Dispenser::collision_solid(GameObject& , const CollisionHit& hit)
57 {
58   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
59     physic.set_velocity_y(0);
60   } else { // hit right or left
61     dir = dir == LEFT ? RIGHT : LEFT;
62     sprite->set_action(dir == LEFT ? "left" : "right");
63     physic.set_velocity_x(-physic.get_velocity_x());
64   }
65
66   return CONTINUE;
67 }
68
69 //TODO: Add launching velocity to badguys
70 //      Add more badguys and randomizer
71 //      Clean up stuff I copied without understanding what it does :)
72 //      Lots-O-Stuff (tm)
73 void
74 Dispenser::launch_badguy()
75 {
76    //FIXME: Does is_offscreen() work right here?
77    if (!is_offscreen()) {
78     if (badguy == "snowball")
79       Sector::current()->add_object(new SnowBall(get_pos().x-2, get_pos().y));
80     else if (badguy == "bouncingsnowball")
81       Sector::current()->add_object(new BouncingSnowball(get_pos().x-2, get_pos().y));
82     else if (badguy == "random")
83       {}
84    }
85 }