Improved stay-on-platform code to handle non-32x32 badguys. Implemented s-o-p for...
[supertux.git] / src / badguy / mrtree.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "mrtree.hpp"
24
25 static const float WALKSPEED = 50;
26 static const float WALKSPEED_SMALL = 30;
27
28 MrTree::MrTree(const lisp::Lisp& reader)
29   : mystate(STATE_BIG)
30 {
31   reader.get("x", start_position.x);
32   reader.get("y", start_position.y);
33   stay_on_platform = false;
34   reader.get("stay-on-platform", stay_on_platform);
35   bbox.set_size(84.8, 95.8);
36   sprite = sprite_manager->create("images/creatures/mr_tree/mr_tree.sprite");
37 }
38
39 void
40 MrTree::write(lisp::Writer& writer)
41 {
42   writer.start_list("mrtree");
43
44   writer.write_float("x", start_position.x);
45   writer.write_float("y", start_position.y);
46
47   writer.end_list("mrtree");
48 }
49
50 void
51 MrTree::activate()
52 {
53   if(mystate == STATE_BIG) {
54     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
55     sprite->set_action(dir == LEFT ? "left" : "right");
56   } else {
57     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
58     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
59   }
60 }
61
62 void
63 MrTree::active_update(float elapsed_time)
64 {
65   if (stay_on_platform && may_fall_off_platform())
66   {
67     dir = (dir == LEFT ? RIGHT : LEFT);
68     sprite->set_action(dir == LEFT ? "left" : "right");
69     physic.set_velocity_x(-physic.get_velocity_x());
70   }
71
72   BadGuy::active_update(elapsed_time);
73 }
74
75 bool
76 MrTree::collision_squished(Player& player)
77 {
78   if(mystate == STATE_BIG) {
79     mystate = STATE_NORMAL;
80     activate();
81
82     sound_manager->play("sounds/squish.wav", get_pos());
83     player.bounce(*this);
84   } else {
85     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
86     kill_squished(player);
87   }
88   
89   return true;
90 }
91
92 HitResponse
93 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
94 {
95   if(fabsf(hit.normal.y) > .5) {
96     physic.set_velocity_y(0);
97   } else {
98     dir = dir == LEFT ? RIGHT : LEFT;
99     activate();
100   }
101
102   return CONTINUE;
103 }
104
105 HitResponse
106 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
107 {
108   if(fabsf(hit.normal.x) > .8) { // left or right hit
109     dir = dir == LEFT ? RIGHT : LEFT;
110     activate();
111   }
112
113   return CONTINUE;
114 }
115
116 IMPLEMENT_FACTORY(MrTree, "mrtree")
117