4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
23 #include "poisonivy.hpp"
24 #include "random_generator.hpp"
25 #include "object/sprite_particle.hpp"
27 static const float WALKSPEED = 100;
28 static const float WALKSPEED_SMALL = 120;
29 static const float INVINCIBLE_TIME = 1;
31 static const float POISONIVY_WIDTH = 32;
32 static const float POISONIVY_HEIGHT = 32;
33 static const float POISONIVY_Y_OFFSET = 24;
36 MrTree::MrTree(const lisp::Lisp& reader)
37 : BadGuy(reader, "images/creatures/mr_tree/mr_tree.sprite"), mystate(STATE_BIG)
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;
46 MrTree::write(lisp::Writer& writer)
48 writer.start_list("mrtree");
50 writer.write_float("x", start_position.x);
51 writer.write_float("y", start_position.y);
53 writer.end_list("mrtree");
59 if (mystate == STATE_BIG) {
60 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
61 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
64 if (mystate == STATE_INVINCIBLE) {
65 physic.set_velocity_x(0);
66 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
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");
77 MrTree::active_update(float elapsed_time)
79 recently_changed_direction = false;
80 if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
81 mystate = STATE_NORMAL;
85 if (might_fall() && !recently_changed_direction )
87 recently_changed_direction = true;
88 dir = (dir == LEFT ? RIGHT : LEFT);
92 BadGuy::active_update(elapsed_time);
96 MrTree::collision_squished(Player& player)
98 // if we're big, we shrink
99 if(mystate == STATE_BIG) {
100 mystate = STATE_INVINCIBLE;
101 invincible_timer.start(INVINCIBLE_TIME);
103 float old_x_offset = sprite->get_current_hitbox_x_offset();
104 float old_y_offset = sprite->get_current_hitbox_y_offset();
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;
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));
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);
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);
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);
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));
178 MrTree::collision_solid(const CollisionHit& hit)
180 if(hit.top || hit.bottom) {
181 physic.set_velocity_y(0);
183 if( recently_changed_direction ) return;
184 recently_changed_direction = true;
185 dir = dir == LEFT ? RIGHT : LEFT;
191 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
193 if(hit.left || hit.right) {
194 if( recently_changed_direction ) return CONTINUE;
195 recently_changed_direction = true;
196 dir = dir == LEFT ? RIGHT : LEFT;
202 IMPLEMENT_FACTORY(MrTree, "mrtree")