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