implemented a new object factory mechanism which is now really independent of the...
[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(const lisp::Lisp& reader)
9 {
10   reader.get("x", start_position.x);
11   reader.get("y", start_position.y);
12   bbox.set_size(31.8, 31.8);
13   sprite = sprite_manager->create("bouncingsnowball");
14   set_direction = false;
15 }
16
17 BouncingSnowball::BouncingSnowball(float pos_x, float pos_y, Direction d)
18 {
19    start_position.x = pos_x;
20    start_position.y = pos_y;
21    bbox.set_size(31.8, 31.8);
22    sprite = sprite_manager->create("bouncingsnowball");
23    set_direction = true;
24    initial_direction = d;
25 }
26
27 void
28 BouncingSnowball::write(lisp::Writer& writer)
29 {
30   writer.start_list("bouncingsnowball");
31
32   writer.write_float("x", start_position.x);
33   writer.write_float("y", start_position.y);
34
35   writer.end_list("bouncingsnowball");
36 }
37
38 void
39 BouncingSnowball::activate()
40 {
41   if (set_direction) {dir = initial_direction;}
42   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
43   sprite->set_action(dir == LEFT ? "left" : "right");
44 }
45
46 bool
47 BouncingSnowball::collision_squished(Player& player)
48 {
49   sprite->set_action("squished");
50   kill_squished(player);
51   return true;
52 }
53
54 HitResponse
55 BouncingSnowball::collision_solid(GameObject& , const CollisionHit& hit)
56 {
57   if(hit.normal.y < -.5) { // hit floor
58     physic.set_velocity_y(JUMPSPEED);
59   } else if(hit.normal.y > .5) { // bumped on roof
60     physic.set_velocity_y(0);
61   } else { // left or right collision
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 HitResponse
71 BouncingSnowball::collision_badguy(BadGuy& , const CollisionHit& hit)
72 {
73   if(fabsf(hit.normal.x) > .8) { // left/right?
74     dir = dir == LEFT ? RIGHT : LEFT;
75     sprite->set_action(dir == LEFT ? "left" : "right");    
76     physic.set_velocity_x(-physic.get_velocity_x());
77   } else if(hit.normal.y < -.8) { // grounf
78     physic.set_velocity_y(JUMPSPEED);
79   }
80
81   return CONTINUE;
82 }
83
84 IMPLEMENT_FACTORY(BouncingSnowball, "bouncingsnowball")
85