Fixed sprite, bounding-boxes and behaviour of Mr. Tree
[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 = 100;
26 static const float WALKSPEED_SMALL = 120;
27 static const float INVINCIBLE_TIME = 1;
28
29 MrTree::MrTree(const lisp::Lisp& reader)
30   : mystate(STATE_BIG)
31 {
32   reader.get("x", start_position.x);
33   reader.get("y", start_position.y);
34   stay_on_platform = false;
35   reader.get("stay-on-platform", stay_on_platform);
36   bbox.set_size(84.8, 84.8);
37   sprite = sprite_manager->create("images/creatures/mr_tree/mr_tree.sprite");
38 }
39
40 void
41 MrTree::write(lisp::Writer& writer)
42 {
43   writer.start_list("mrtree");
44
45   writer.write_float("x", start_position.x);
46   writer.write_float("y", start_position.y);
47
48   writer.end_list("mrtree");
49 }
50
51 void
52 MrTree::activate()
53 {
54   if (mystate == STATE_BIG) {
55     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
56     sprite->set_action(dir == LEFT ? "large-left" : "large-right");
57     return;
58   }
59   if (mystate == STATE_NORMAL) {
60     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
61     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
62     return;
63   }
64 }
65
66 void
67 MrTree::active_update(float elapsed_time)
68 {
69   if (stay_on_platform && may_fall_off_platform())
70   {
71     dir = (dir == LEFT ? RIGHT : LEFT);
72     activate();
73   }
74
75   BadGuy::active_update(elapsed_time);
76 }
77
78 bool
79 MrTree::collision_squished(Player& player)
80 {
81   // if we're big, we shrink
82   if(mystate == STATE_BIG) {
83     mystate = STATE_NORMAL;
84     activate();
85
86     // shrink bounding box and adjust sprite position to where the stump once was
87     bbox.set_size(42, 62);
88     Vector pos = get_pos();
89     pos.x += 20;
90     pos.y += 23;
91     set_pos(pos);
92
93     sound_manager->play("sounds/mr_tree.ogg", get_pos());
94     player.bounce(*this);
95
96     invincible_timer.start(INVINCIBLE_TIME);
97
98     return true;
99   }
100
101   // if we're small, but still invincible, we ignore the hit
102   if (!invincible_timer.check()) {
103     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
104     player.bounce(*this);
105     return true;
106   }
107
108   // if we're small and no longer invincible, we die
109   sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
110   bbox.set_size(42, 42);
111   kill_squished(player);
112   return true;
113 }
114
115 HitResponse
116 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
117 {
118   if(fabsf(hit.normal.y) > .5) {
119     physic.set_velocity_y(0);
120   } else {
121     dir = dir == LEFT ? RIGHT : LEFT;
122     activate();
123   }
124
125   return CONTINUE;
126 }
127
128 HitResponse
129 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
130 {
131   if(fabsf(hit.normal.x) > .8) { // left or right hit
132     dir = dir == LEFT ? RIGHT : LEFT;
133     activate();
134   }
135
136   return CONTINUE;
137 }
138
139 IMPLEMENT_FACTORY(MrTree, "mrtree")
140