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 : WalkingBadguy(reader, "images/creatures/mr_tree/mr_tree.sprite","large-left","large-right"), mystate(STATE_BIG)
39 walk_speed = WALKSPEED;
41 sound_manager->preload("sounds/mr_tree.ogg");
42 sound_manager->preload("sounds/mr_treehit.ogg");
46 MrTree::write(lisp::Writer& writer)
48 writer.start_list("mrtree");
49 WalkingBadguy::write(writer);
50 writer.end_list("mrtree");
58 WalkingBadguy::activate();
61 walk_speed = WALKSPEED_SMALL;
62 walk_left_action = "small-left";
63 walk_right_action = "small-right";
64 WalkingBadguy::activate();
66 case STATE_INVINCIBLE:
67 physic.set_velocity_x(0);
68 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
74 MrTree::active_update(float elapsed_time)
79 WalkingBadguy::active_update(elapsed_time);
81 case STATE_INVINCIBLE:
82 if (invincible_timer.check()) {
83 mystate = STATE_NORMAL;
86 BadGuy::active_update(elapsed_time);
92 MrTree::collision_squished(Player& player)
94 // if we're big, we shrink
95 if(mystate == STATE_BIG) {
96 mystate = STATE_INVINCIBLE;
97 invincible_timer.start(INVINCIBLE_TIME);
99 float old_x_offset = sprite->get_current_hitbox_x_offset();
100 float old_y_offset = sprite->get_current_hitbox_y_offset();
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;
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));
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);
129 Sector::current()->add_object(leaf1);
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);
136 Sector::current()->add_object(leaf2);
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);
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));
176 MrTree::collision_solid(const CollisionHit& hit)
178 update_on_ground_flag(hit);
183 WalkingBadguy::collision_solid(hit);
185 case STATE_INVINCIBLE:
186 if(hit.top || hit.bottom) {
187 physic.set_velocity_y(0);
189 if(hit.left || hit.right) {
190 physic.set_velocity_x(0);
197 MrTree::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
202 return WalkingBadguy::collision_badguy(badguy, hit);
204 case STATE_INVINCIBLE:
205 if(hit.top || hit.bottom) {
206 physic.set_velocity_y(0);
208 if(hit.left || hit.right) {
209 physic.set_velocity_x(0);
217 IMPLEMENT_FACTORY(MrTree, "mrtree")