added badguy Nolok_01, as he may appear as the boss of world 1 (still very basic).
[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("dummyguy");
17   sprite->set_action("stand");
18 }
19
20 void
21 Dispenser::write(LispWriter& writer)
22 {
23   writer.start_list("dispenser");
24
25   writer.write_float("x", get_pos().x);
26   writer.write_float("y", get_pos().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   remove_me();
44   player.bounce(*this);
45   return true;
46 }
47
48 void
49 Dispenser::active_action(float )
50 {
51    if (dispense_timer.check()) {
52       launch_badguy();
53    }
54 }
55
56 HitResponse
57 Dispenser::collision_solid(GameObject& , const CollisionHit& hit)
58 {
59   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
60     physic.set_velocity_y(0);
61   } else { // hit right or left
62     dir = dir == LEFT ? RIGHT : LEFT;
63     sprite->set_action(dir == LEFT ? "left" : "right");
64     physic.set_velocity_x(-physic.get_velocity_x());
65   }
66
67   return CONTINUE;
68 }
69
70 //TODO: Add launching velocity to badguys
71 //      Add more badguys and randomizer
72 //      Clean up stuff I copied without understanding what it does :)
73 //      Stop dispensing when game is paused
74 //      Lots-O-Stuff (tm)
75 void
76 Dispenser::launch_badguy()
77 {
78    //FIXME: Does is_offscreen() work right here?
79    if (!is_offscreen()) {
80     if (badguy == "snowball")
81       Sector::current()->add_object(new SnowBall(get_pos().x-2, get_pos().y));
82     else if (badguy == "bouncingsnowball")
83       Sector::current()->add_object(new BouncingSnowball(get_pos().x-2, get_pos().y, dir));
84     else if (badguy == "random")
85       {}
86    }
87 }