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