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