Fixed MrTree floating in the air when squished /
[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 #include "random_generator.hpp"
25 #include "object/sprite_particle.hpp"
26
27 static const float WALKSPEED = 100;
28 static const float WALKSPEED_SMALL = 120;
29 static const float INVINCIBLE_TIME = 1;
30
31 static const float POISONIVY_WIDTH = 32;
32 static const float POISONIVY_HEIGHT = 32;
33 static const float POISONIVY_Y_OFFSET = 24;
34
35
36 MrTree::MrTree(const lisp::Lisp& reader)
37   : WalkingBadguy(reader, "images/creatures/mr_tree/mr_tree.sprite","large-left","large-right"), mystate(STATE_BIG)
38 {
39   walk_speed = WALKSPEED;
40   max_drop_height = 0;
41   sound_manager->preload("sounds/mr_tree.ogg");
42   sound_manager->preload("sounds/mr_treehit.ogg");
43 }
44
45 void
46 MrTree::write(lisp::Writer& writer)
47 {
48   writer.start_list("mrtree");
49   WalkingBadguy::write(writer);
50   writer.end_list("mrtree");
51 }
52
53 void
54 MrTree::activate()
55 {
56   switch (mystate) {
57     case STATE_BIG:
58       WalkingBadguy::activate();
59       break;
60     case STATE_NORMAL:
61       walk_speed = WALKSPEED_SMALL;
62       walk_left_action = "small-left";
63       walk_right_action = "small-right";
64       WalkingBadguy::activate();
65       break;
66     case STATE_INVINCIBLE:
67       physic.set_velocity_x(0);
68       sprite->set_action(dir == LEFT ? "small-left" : "small-right");
69       break;
70   }
71 }
72
73 void
74 MrTree::active_update(float elapsed_time)
75 {
76   switch (mystate) {
77     case STATE_BIG:
78     case STATE_NORMAL:
79       WalkingBadguy::active_update(elapsed_time);
80       break;
81     case STATE_INVINCIBLE:
82       if (invincible_timer.check()) {
83         mystate = STATE_NORMAL;
84         activate();
85       }
86       BadGuy::active_update(elapsed_time);
87       break;
88   }
89 }
90
91 bool
92 MrTree::collision_squished(Player& player)
93 {
94   // if we're big, we shrink
95   if(mystate == STATE_BIG) {
96     mystate = STATE_INVINCIBLE;
97     invincible_timer.start(INVINCIBLE_TIME);
98
99     float old_x_offset = sprite->get_current_hitbox_x_offset();
100     float old_y_offset = sprite->get_current_hitbox_y_offset();
101     activate();
102
103     // shrink bounding box and adjust sprite position to where the stump once was
104     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
105     Vector pos = get_pos();
106     pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset;
107     pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset;
108     set_pos(pos);
109
110     sound_manager->play("sounds/mr_tree.ogg", get_pos());
111     player.bounce(*this);
112     // spawn some particles
113     // TODO: provide convenience function in MovingSprite or MovingObject?
114     for (int i = 0; i < 25; i++) {
115       Vector ppos = bbox.get_middle();
116       float angle = systemRandom.randf(-M_PI_2, M_PI_2);
117       float velocity = systemRandom.randf(45, 90);
118       float vx = sin(angle)*velocity;
119       float vy = -cos(angle)*velocity;
120       Vector pspeed = Vector(vx, vy);
121       Vector paccel = Vector(0, 100);
122       Sector::current()->add_object(new SpriteParticle("images/objects/particles/leaf.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
123     }
124     Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET);
125     Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
126     if (Sector::current()->is_free_space(leaf1_bbox)) {
127       PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
128       leaf1 = leaf1;
129       Sector::current()->add_object(leaf1);
130     }
131     Vector leaf2_pos = Vector(pos.x + sprite->get_current_hitbox_width() + 1, pos.y - POISONIVY_Y_OFFSET);
132     Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
133     if (Sector::current()->is_free_space(leaf2_bbox)) {
134       PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
135       leaf2 = leaf2;
136       Sector::current()->add_object(leaf2);
137     }
138
139     return true;
140   }
141
142   // if we're still invincible, we ignore the hit
143   if (mystate == STATE_INVINCIBLE) {
144     sound_manager->play("sounds/mr_treehit.ogg", get_pos());
145     player.bounce(*this);
146     return true;
147   }
148
149   // if we're small, we die
150   if (mystate == STATE_NORMAL) {
151     sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
152     set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
153     kill_squished(player);
154     // spawn some particles
155     // TODO: provide convenience function in MovingSprite or MovingObject?
156     for (int i = 0; i < 25; i++) {
157       Vector ppos = bbox.get_middle();
158       float angle = systemRandom.randf(-M_PI_2, M_PI_2);
159       float velocity = systemRandom.randf(45, 90);
160       float vx = sin(angle)*velocity;
161       float vy = -cos(angle)*velocity;
162       Vector pspeed = Vector(vx, vy);
163       Vector paccel = Vector(0, 100);
164       Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
165     }
166
167     return true;
168
169   }
170
171   //TODO: exception?
172   return true;
173 }
174
175 void
176 MrTree::collision_solid(const CollisionHit& hit)
177 {
178   update_on_ground_flag(hit);
179
180   switch (mystate) {
181     case STATE_BIG:
182     case STATE_NORMAL:
183       WalkingBadguy::collision_solid(hit);
184       break;
185     case STATE_INVINCIBLE:
186       if(hit.top || hit.bottom) {
187         physic.set_velocity_y(0);
188       }
189       if(hit.left || hit.right) {
190         physic.set_velocity_x(0);
191       }
192       break;
193   }
194 }
195
196 HitResponse
197 MrTree::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
198 {
199   switch (mystate) {
200     case STATE_BIG:
201     case STATE_NORMAL:
202       return WalkingBadguy::collision_badguy(badguy, hit);
203       break;
204     case STATE_INVINCIBLE:
205       if(hit.top || hit.bottom) {
206         physic.set_velocity_y(0);
207       }
208       if(hit.left || hit.right) {
209         physic.set_velocity_x(0);
210       }
211       return CONTINUE;
212       break;
213   }
214   return CONTINUE;
215 }
216
217 IMPLEMENT_FACTORY(MrTree, "mrtree")
218