implemented a new object factory mechanism which is now really independent of the...
[supertux.git] / src / badguy / spiky.cpp
1 #include <config.h>
2
3 #include "spiky.h"
4
5 static const float WALKSPEED = 80;
6
7 Spiky::Spiky(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("spiky");
13 }
14
15 void
16 Spiky::write(lisp::Writer& writer)
17 {
18   writer.start_list("spiky");
19
20   writer.write_float("x", start_position.x);
21   writer.write_float("y", start_position.y);
22
23   writer.end_list("spiky");
24 }
25
26 void
27 Spiky::activate()
28 {
29   physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
30   sprite->set_action(dir == LEFT ? "left" : "right");
31 }
32
33 HitResponse
34 Spiky::collision_solid(GameObject& , const CollisionHit& hit)
35 {
36   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
37     physic.set_velocity_y(0);
38   } else { // hit right or left
39     dir = dir == LEFT ? RIGHT : LEFT;
40     sprite->set_action(dir == LEFT ? "left" : "right");
41     physic.set_velocity_x(-physic.get_velocity_x());
42   }
43
44   return CONTINUE;
45 }
46
47 HitResponse
48 Spiky::collision_badguy(BadGuy& , const CollisionHit& hit)
49 {
50   if(fabsf(hit.normal.x) > .8) { // left or right
51     dir = dir == LEFT ? RIGHT : LEFT;
52     sprite->set_action(dir == LEFT ? "left" : "right");
53     physic.set_velocity_x(-physic.get_velocity_x());
54   }
55
56   return CONTINUE;
57 }
58
59 IMPLEMENT_FACTORY(Spiky, "spiky")