Hardcoded stay-on-platform behaviour as follows: Mr. Bomb, Mr. Tree and the Totem...
[supertux.git] / src / badguy / mrtree.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "mrtree.hpp"
23 #include "poisonivy.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   bbox.set_size(84.8, 84.8);
35   sprite = sprite_manager->create("images/creatures/mr_tree/mr_tree.sprite");
36 }
37
38 void
39 MrTree::write(lisp::Writer& writer)
40 {
41   writer.start_list("mrtree");
42
43   writer.write_float("x", start_position.x);
44   writer.write_float("y", start_position.y);
45
46   writer.end_list("mrtree");
47 }
48
49 void
50 MrTree::activate()
51 {
52   if (mystate == STATE_BIG) {
53     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
54     sprite->set_action(dir == LEFT ? "large-left" : "large-right");
55     return;
56   }
57   if (mystate == STATE_INVINCIBLE) {
58     physic.set_velocity_x(0);
59     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
60     return;
61   }
62   if (mystate == STATE_NORMAL) {
63     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
64     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
65     return;
66   }
67 }
68
69 void
70 MrTree::active_update(float elapsed_time)
71 {
72   if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
73     mystate = STATE_NORMAL;
74     activate();
75   }
76
77   if (may_fall_off_platform())
78   {
79     dir = (dir == LEFT ? RIGHT : LEFT);
80     activate();
81   }
82
83   BadGuy::active_update(elapsed_time);
84 }
85
86 bool
87 MrTree::collision_squished(Player& player)
88 {
89   // if we're big, we shrink
90   if(mystate == STATE_BIG) {
91     mystate = STATE_INVINCIBLE;
92     invincible_timer.start(INVINCIBLE_TIME);
93     activate();
94
95     // shrink bounding box and adjust sprite position to where the stump once was
96     bbox.set_size(42, 62);
97     Vector pos = get_pos();
98     pos.x += 20;
99     pos.y += 23;
100     set_pos(pos);
101
102     sound_manager->play("sounds/mr_tree.ogg", get_pos());
103     player.bounce(*this);
104
105     Rect leaf1_bbox = Rect(pos.x-32-1, pos.y-23+1, pos.x-32-1+32, pos.y-23+1+32);
106     if (Sector::current()->is_free_space(leaf1_bbox)) {
107       PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1.x, leaf1_bbox.p1.y, LEFT);
108       Sector::current()->add_object(leaf1);
109     }
110     Rect leaf2_bbox = Rect(pos.x+42+1, pos.y-23+1, pos.x+32+1+32, pos.y-23+1+32);
111     if (Sector::current()->is_free_space(leaf2_bbox)) {
112       PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1.x, leaf2_bbox.p1.y, RIGHT);
113       Sector::current()->add_object(leaf2);
114     }
115
116     return true;
117   }
118
119   // if we're still invincible, we ignore the hit
120   if (mystate == STATE_INVINCIBLE) {
121     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
122     player.bounce(*this);
123     return true;
124   }
125
126   // if we're small, we die
127   if (mystate == STATE_NORMAL) {
128     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
129     bbox.set_size(42, 42);
130     kill_squished(player);
131     return true;
132   }
133
134   //TODO: exception?
135   return true;
136 }
137
138 HitResponse
139 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
140 {
141   if(fabsf(hit.normal.y) > .5) {
142     physic.set_velocity_y(0);
143   } else {
144     dir = dir == LEFT ? RIGHT : LEFT;
145     activate();
146   }
147
148   return CONTINUE;
149 }
150
151 HitResponse
152 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
153 {
154   if(fabsf(hit.normal.x) > .8) { // left or right hit
155     dir = dir == LEFT ? RIGHT : LEFT;
156     activate();
157   }
158
159   return CONTINUE;
160 }
161
162 IMPLEMENT_FACTORY(MrTree, "mrtree")
163