added simple enemy dispenser (can only dispense bouncing snowballs so far and looks...
[supertux.git] / src / badguy / bouncing_snowball.cpp
1 #include <config.h>
2
3 #include "bouncing_snowball.h"
4
5 static const float JUMPSPEED = 450;
6 static const float WALKSPEED = 80;
7
8 BouncingSnowball::BouncingSnowball(LispReader& reader)
9 {
10   reader.read_float("x", start_position.x);
11   reader.read_float("y", start_position.y);
12   bbox.set_size(32, 32);
13   sprite = sprite_manager->create("bouncingsnowball");
14 }
15
16 BouncingSnowball::BouncingSnowball(float pos_x, float pos_y)
17 {
18    start_position.x = pos_x;
19    start_position.y = pos_y;
20    bbox.set_size(32, 32);
21    sprite = sprite_manager->create("bouncingsnowball");
22 }
23
24 void
25 BouncingSnowball::write(LispWriter& writer)
26 {
27   writer.start_list("bouncingsnowball");
28
29   writer.write_float("x", get_pos().x);
30   writer.write_float("y", get_pos().y);
31
32   writer.end_list("bouncingsnowball");
33 }
34
35 void
36 BouncingSnowball::activate()
37 {
38   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
39   sprite->set_action(dir == LEFT ? "left" : "right");
40 }
41
42 bool
43 BouncingSnowball::collision_squished(Player& player)
44 {
45   sprite->set_action("squished");
46   kill_squished(player);
47   return true;
48 }
49
50 HitResponse
51 BouncingSnowball::collision_solid(GameObject& , const CollisionHit& hit)
52 {
53   if(hit.normal.y < -.5) { // hit floor
54     physic.set_velocity_y(JUMPSPEED);
55   } else if(hit.normal.y > .5) { // bumped on roof
56     physic.set_velocity_y(0);
57   } else { // left or right collision
58     dir = dir == LEFT ? RIGHT : LEFT;
59     sprite->set_action(dir == LEFT ? "left" : "right");
60     physic.set_velocity_x(-physic.get_velocity_x());
61   }
62
63   return CONTINUE;
64 }
65