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 reader.get("direction", direction);
40 set_direction = false;
41 if( direction != "auto" && direction != ""){
43 initial_direction = str2dir( direction );
44 dir = str2dir( direction );
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");
52 MrTree::write(lisp::Writer& writer)
54 writer.start_list("mrtree");
56 writer.write_string("direction", direction);
57 writer.write_float("x", start_position.x);
58 writer.write_float("y", start_position.y);
60 writer.end_list("mrtree");
67 dir = initial_direction;
69 if (mystate == STATE_BIG) {
70 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
71 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
74 if (mystate == STATE_INVINCIBLE) {
75 physic.set_velocity_x(0);
76 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
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");
87 MrTree::active_update(float elapsed_time)
89 if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
90 mystate = STATE_NORMAL;
96 dir = (dir == LEFT ? RIGHT : LEFT);
100 BadGuy::active_update(elapsed_time);
104 MrTree::collision_squished(Player& player)
106 // if we're big, we shrink
107 if(mystate == STATE_BIG) {
108 mystate = STATE_INVINCIBLE;
109 invincible_timer.start(INVINCIBLE_TIME);
111 float old_x_offset = sprite->get_current_hitbox_x_offset();
112 float old_y_offset = sprite->get_current_hitbox_y_offset();
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;
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));
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);
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);
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);
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));
186 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
188 if(fabsf(hit.normal.y) > .5) {
189 physic.set_velocity_y(0);
191 dir = dir == LEFT ? RIGHT : LEFT;
192 set_direction = false;
200 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
202 if(fabsf(hit.normal.x) > .8) { // left or right hit
203 dir = dir == LEFT ? RIGHT : LEFT;
204 set_direction = false;
211 IMPLEMENT_FACTORY(MrTree, "mrtree")