Had a bit of time today and worked on supertux:
[supertux.git] / src / badguy / mrtree.cpp
1 #include <config.h>
2
3 #include "mrtree.h"
4
5 static const float WALKSPEED = 50;
6 static const float WALKSPEED_SMALL = 30;
7
8 MrTree::MrTree(const lisp::Lisp& reader)
9   : mystate(STATE_BIG)
10 {
11   reader.get("x", start_position.x);
12   reader.get("y", start_position.y);
13   bbox.set_size(84.8, 95.8);
14   sprite = sprite_manager->create("mrtree");
15 }
16
17 void
18 MrTree::write(lisp::Writer& writer)
19 {
20   writer.start_list("mrtree");
21
22   writer.write_float("x", start_position.x);
23   writer.write_float("y", start_position.y);
24
25   writer.end_list("mrtree");
26 }
27
28 void
29 MrTree::activate()
30 {
31   if(mystate == STATE_BIG) {
32     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
33     sprite->set_action(dir == LEFT ? "left" : "right");
34   } else {
35     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
36     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
37   }
38 }
39
40 bool
41 MrTree::collision_squished(Player& player)
42 {
43   if(mystate == STATE_BIG) {
44     mystate = STATE_NORMAL;
45     activate();
46
47     SoundManager::get()->play_sound(IDToSound(SND_SQUISH), get_pos(),
48             player.get_pos());      
49     player.bounce(*this);
50   } else {
51     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
52     kill_squished(player);
53   }
54   
55   return true;
56 }
57
58 HitResponse
59 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
60 {
61   if(fabsf(hit.normal.y) > .5) {
62     physic.set_velocity_y(0);
63   } else {
64     dir = dir == LEFT ? RIGHT : LEFT;
65     activate();
66   }
67
68   return CONTINUE;
69 }
70
71 HitResponse
72 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
73 {
74   if(fabsf(hit.normal.x) > .8) { // left or right hit
75     dir = dir == LEFT ? RIGHT : LEFT;
76     activate();
77   }
78
79   return CONTINUE;
80 }
81
82 IMPLEMENT_FACTORY(MrTree, "mrtree")
83