implemented a new object factory mechanism which is now really independent of the...
[supertux.git] / src / badguy / snowball.cpp
1 #include <config.h>
2
3 #include "snowball.h"
4
5 static const float WALKSPEED = 80;
6
7 SnowBall::SnowBall(const lisp::Lisp& reader)
8 {
9   reader.get("x", start_position.x);
10   reader.get("y", start_position.y);
11   bbox.set_size(31.8, 31.8);
12   sprite = sprite_manager->create("snowball");
13   set_direction = false;
14 }
15
16 SnowBall::SnowBall(float pos_x, float pos_y, Direction d)
17 {
18   start_position.x = pos_x;
19   start_position.y = pos_y;
20   bbox.set_size(31.8, 31.8);
21   sprite = sprite_manager->create("snowball");
22   set_direction = true;
23   initial_direction = d;
24 }
25
26 void
27 SnowBall::write(lisp::Writer& writer)
28 {
29   writer.start_list("snowball");
30
31   writer.write_float("x", start_position.x);
32   writer.write_float("y", start_position.y);
33
34   writer.end_list("snowball");
35 }
36
37 void
38 SnowBall::activate()
39 {
40   if (set_direction) {dir = initial_direction;}
41   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
42   sprite->set_action(dir == LEFT ? "left" : "right");
43 }
44
45 bool
46 SnowBall::collision_squished(Player& player)
47 {
48   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
49   kill_squished(player);
50   return true;
51 }
52
53 HitResponse
54 SnowBall::collision_solid(GameObject& , const CollisionHit& hit)
55 {
56   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
57     physic.set_velocity_y(0);
58   } else { // hit right or left
59     dir = dir == LEFT ? RIGHT : LEFT;
60     sprite->set_action(dir == LEFT ? "left" : "right");
61     physic.set_velocity_x(-physic.get_velocity_x());
62   }
63
64   return CONTINUE;
65 }
66
67 HitResponse
68 SnowBall::collision_badguy(BadGuy& , const CollisionHit& hit)
69 {
70   if(fabsf(hit.normal.x) > .8) { // left or right hit
71     dir = dir == LEFT ? RIGHT : LEFT;
72     sprite->set_action(dir == LEFT ? "left" : "right");
73     physic.set_velocity_x(-physic.get_velocity_x());       
74   }
75
76   return CONTINUE;
77 }
78
79 IMPLEMENT_FACTORY(SnowBall, "snowball")