82ec9153d8d25797211bf0f1d7762d01791bb943
[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   bbox.set_size(84.8, 95.8);
34   sprite = sprite_manager->create("mrtree");
35 }
36
37 void
38 MrTree::write(lisp::Writer& writer)
39 {
40   writer.start_list("mrtree");
41
42   writer.write_float("x", start_position.x);
43   writer.write_float("y", start_position.y);
44
45   writer.end_list("mrtree");
46 }
47
48 void
49 MrTree::activate()
50 {
51   if(mystate == STATE_BIG) {
52     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
53     sprite->set_action(dir == LEFT ? "left" : "right");
54   } else {
55     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
56     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
57   }
58 }
59
60 bool
61 MrTree::collision_squished(Player& player)
62 {
63   if(mystate == STATE_BIG) {
64     mystate = STATE_NORMAL;
65     activate();
66
67     sound_manager->play("sounds/squish.ogg", get_pos());
68     player.bounce(*this);
69   } else {
70     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
71     kill_squished(player);
72   }
73   
74   return true;
75 }
76
77 HitResponse
78 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
79 {
80   if(fabsf(hit.normal.y) > .5) {
81     physic.set_velocity_y(0);
82   } else {
83     dir = dir == LEFT ? RIGHT : LEFT;
84     activate();
85   }
86
87   return CONTINUE;
88 }
89
90 HitResponse
91 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
92 {
93   if(fabsf(hit.normal.x) > .8) { // left or right hit
94     dir = dir == LEFT ? RIGHT : LEFT;
95     activate();
96   }
97
98   return CONTINUE;
99 }
100
101 IMPLEMENT_FACTORY(MrTree, "mrtree")
102