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");
45 MrTree::write(lisp::Writer& writer)
47 writer.start_list("mrtree");
49 writer.write_float("x", start_position.x);
50 writer.write_float("y", start_position.y);
52 writer.end_list("mrtree");
58 if (mystate == STATE_BIG) {
59 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
60 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
63 if (mystate == STATE_INVINCIBLE) {
64 physic.set_velocity_x(0);
65 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
68 if (mystate == STATE_NORMAL) {
69 physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
70 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
76 MrTree::active_update(float elapsed_time)
78 if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
79 mystate = STATE_NORMAL;
85 dir = (dir == LEFT ? RIGHT : LEFT);
89 BadGuy::active_update(elapsed_time);
93 MrTree::collision_squished(Player& player)
95 // if we're big, we shrink
96 if(mystate == STATE_BIG) {
97 mystate = STATE_INVINCIBLE;
98 invincible_timer.start(INVINCIBLE_TIME);
100 float old_x_offset = sprite->get_current_hitbox_x_offset();
101 float old_y_offset = sprite->get_current_hitbox_y_offset();
104 // shrink bounding box and adjust sprite position to where the stump once was
105 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
106 Vector pos = get_pos();
107 pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset;
108 pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset;
111 sound_manager->play("sounds/mr_tree.ogg", get_pos());
112 player.bounce(*this);
113 // spawn some particles
114 // TODO: provide convenience function in MovingSprite or MovingObject?
115 for (int i = 0; i < 25; i++) {
116 Vector ppos = bbox.get_middle();
117 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
118 float velocity = systemRandom.randf(45, 90);
119 float vx = sin(angle)*velocity;
120 float vy = -cos(angle)*velocity;
121 Vector pspeed = Vector(vx, vy);
122 Vector paccel = Vector(0, 100);
123 Sector::current()->add_object(new SpriteParticle("images/objects/particles/leaf.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
125 Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET);
126 Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
127 if (Sector::current()->is_free_space(leaf1_bbox)) {
128 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);
135 Sector::current()->add_object(leaf2);
141 // if we're still invincible, we ignore the hit
142 if (mystate == STATE_INVINCIBLE) {
143 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
144 player.bounce(*this);
148 // if we're small, we die
149 if (mystate == STATE_NORMAL) {
150 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
151 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
152 kill_squished(player);
153 // spawn some particles
154 // TODO: provide convenience function in MovingSprite or MovingObject?
155 for (int i = 0; i < 25; i++) {
156 Vector ppos = bbox.get_middle();
157 float angle = systemRandom.randf(-M_PI_2, M_PI_2);
158 float velocity = systemRandom.randf(45, 90);
159 float vx = sin(angle)*velocity;
160 float vy = -cos(angle)*velocity;
161 Vector pspeed = Vector(vx, vy);
162 Vector paccel = Vector(0, 100);
163 Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
175 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
177 if(fabsf(hit.normal.y) > .5) {
178 physic.set_velocity_y(0);
180 dir = dir == LEFT ? RIGHT : LEFT;
188 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
190 if(fabsf(hit.normal.x) > .8) { // left or right hit
191 dir = dir == LEFT ? RIGHT : LEFT;
198 IMPLEMENT_FACTORY(MrTree, "mrtree")