added Christoph's sleepy spiky, created a plant badguy using Grumbel's plant.xcf...
[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
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(99.8, 99.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     bbox.set_size(31.8, 68.8);
59     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
60   }
61 }
62
63 void
64 MrTree::active_update(float elapsed_time)
65 {
66   if (stay_on_platform && may_fall_off_platform())
67   {
68     dir = (dir == LEFT ? RIGHT : LEFT);
69     sprite->set_action(dir == LEFT ? "left" : "right");
70     physic.set_velocity_x(-physic.get_velocity_x());
71   }
72
73   BadGuy::active_update(elapsed_time);
74 }
75
76 bool
77 MrTree::collision_squished(Player& player)
78 {
79   if(mystate == STATE_BIG) {
80     mystate = STATE_NORMAL;
81     activate();
82
83   
84     sound_manager->play("sounds/mr_tree.ogg", get_pos());
85     player.bounce(*this);
86   } else {
87 bbox.set_size(67.8, 99.8);
88 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
89 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
90 player.bounce(*this);
91    
92   }
93   
94   return true;
95 }
96
97 HitResponse
98 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
99 {
100   if(fabsf(hit.normal.y) > .5) {
101     physic.set_velocity_y(0);
102   } else {
103     dir = dir == LEFT ? RIGHT : LEFT;
104     activate();
105   }
106
107   return CONTINUE;
108 }
109
110 HitResponse
111 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
112 {
113   if(fabsf(hit.normal.x) > .8) { // left or right hit
114     dir = dir == LEFT ? RIGHT : LEFT;
115     activate();
116   }
117
118   return CONTINUE;
119 }
120
121 IMPLEMENT_FACTORY(MrTree, "mrtree")
122