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