a555a427ad616491525a60d3b812696d7e6ea133
[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 static const float POISONIVY_WIDTH = 32;
30 static const float POISONIVY_HEIGHT = 32;
31 static const float POISONIVY_Y_OFFSET = 24;
32
33
34 MrTree::MrTree(const lisp::Lisp& reader)
35   : BadGuy(reader, "images/creatures/mr_tree/mr_tree.sprite"), mystate(STATE_BIG)
36 {
37   reader.get("direction", direction);
38   set_direction = false;
39   if( direction != "auto" && direction != ""){
40     set_direction = true;
41     initial_direction = str2dir( direction );
42     dir = str2dir( direction );
43   }
44   sprite->set_action(dir == LEFT ? "large-left" : "large-right");
45   sound_manager->preload("sounds/mr_tree.ogg");
46   sound_manager->preload("sounds/mr_treehit.ogg");
47 }
48
49 void
50 MrTree::write(lisp::Writer& writer)
51 {
52   writer.start_list("mrtree");
53
54   writer.write_string("direction", direction);
55   writer.write_float("x", start_position.x);
56   writer.write_float("y", start_position.y);
57
58   writer.end_list("mrtree");
59 }
60
61 void
62 MrTree::activate()
63 {
64   if( set_direction ){
65       dir = initial_direction;
66   }
67   if (mystate == STATE_BIG) {
68     physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
69     sprite->set_action(dir == LEFT ? "large-left" : "large-right");
70     return;
71   }
72   if (mystate == STATE_INVINCIBLE) {
73     physic.set_velocity_x(0);
74     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
75     return;
76   }
77   if (mystate == STATE_NORMAL) {
78     physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
79     sprite->set_action(dir == LEFT ? "small-left" : "small-right");
80     return;
81   }
82 }
83
84 void
85 MrTree::active_update(float elapsed_time)
86 {
87   if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
88     mystate = STATE_NORMAL;
89     activate();
90   }
91
92   if (might_fall())
93   {
94     dir = (dir == LEFT ? RIGHT : LEFT);
95     activate();
96   }
97
98   BadGuy::active_update(elapsed_time);
99 }
100
101 bool
102 MrTree::collision_squished(Player& player)
103 {
104   // if we're big, we shrink
105   if(mystate == STATE_BIG) {
106     mystate = STATE_INVINCIBLE;
107     invincible_timer.start(INVINCIBLE_TIME);
108
109     float old_x_offset = sprite->get_current_hitbox_x_offset();
110     float old_y_offset = sprite->get_current_hitbox_y_offset();
111     activate();
112
113     // shrink bounding box and adjust sprite position to where the stump once was
114     bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
115     Vector pos = get_pos();
116     pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset;
117     pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset;
118     set_pos(pos);
119
120     sound_manager->play("sounds/mr_tree.ogg", get_pos());
121     player.bounce(*this);
122
123     Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET);
124     Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
125     if (Sector::current()->is_free_space(leaf1_bbox)) {
126       PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
127       Sector::current()->add_object(leaf1);
128     }
129     Vector leaf2_pos = Vector(pos.x + sprite->get_current_hitbox_width() + 1, pos.y - POISONIVY_Y_OFFSET);
130     Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
131     if (Sector::current()->is_free_space(leaf2_bbox)) {
132       PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
133       Sector::current()->add_object(leaf2);
134     }
135
136     return true;
137   }
138
139   // if we're still invincible, we ignore the hit
140   if (mystate == STATE_INVINCIBLE) {
141     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
142     player.bounce(*this);
143     return true;
144   }
145
146   // if we're small, we die
147   if (mystate == STATE_NORMAL) {
148     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
149     bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
150     kill_squished(player);
151     return true;
152   }
153
154   //TODO: exception?
155   return true;
156 }
157
158 HitResponse
159 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
160 {
161   if(fabsf(hit.normal.y) > .5) {
162     physic.set_velocity_y(0);
163   } else {
164     dir = dir == LEFT ? RIGHT : LEFT;
165     set_direction = false;
166     activate();
167   }
168
169   return CONTINUE;
170 }
171
172 HitResponse
173 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
174 {
175   if(fabsf(hit.normal.x) > .8) { // left or right hit
176     dir = dir == LEFT ? RIGHT : LEFT;
177     set_direction = false;
178     activate();
179   }
180
181   return CONTINUE;
182 }
183
184 IMPLEMENT_FACTORY(MrTree, "mrtree")
185