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"
25 static const float WALKSPEED = 100;
26 static const float WALKSPEED_SMALL = 120;
27 static const float INVINCIBLE_TIME = 1;
29 static const float POISONIVY_WIDTH = 32;
30 static const float POISONIVY_HEIGHT = 32;
31 static const float POISONIVY_Y_OFFSET = 24;
34 MrTree::MrTree(const lisp::Lisp& reader)
35 : BadGuy(reader, "images/creatures/mr_tree/mr_tree.sprite"), mystate(STATE_BIG)
37 reader.get("direction", direction);
38 set_direction = false;
39 if( direction != "auto" && direction != ""){
41 initial_direction = str2dir( direction );
42 dir = str2dir( direction );
44 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
45 sound_manager->preload("sounds/mr_tree.ogg");
46 sound_manager->preload("sounds/mr_treehit.ogg");
50 MrTree::write(lisp::Writer& writer)
52 writer.start_list("mrtree");
54 writer.write_string("direction", direction);
55 writer.write_float("x", start_position.x);
56 writer.write_float("y", start_position.y);
58 writer.end_list("mrtree");
65 dir = initial_direction;
67 if (mystate == STATE_BIG) {
68 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
69 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
72 if (mystate == STATE_INVINCIBLE) {
73 physic.set_velocity_x(0);
74 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
77 if (mystate == STATE_NORMAL) {
78 physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
79 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
85 MrTree::active_update(float elapsed_time)
87 if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
88 mystate = STATE_NORMAL;
94 dir = (dir == LEFT ? RIGHT : LEFT);
98 BadGuy::active_update(elapsed_time);
102 MrTree::collision_squished(Player& player)
104 // if we're big, we shrink
105 if(mystate == STATE_BIG) {
106 mystate = STATE_INVINCIBLE;
107 invincible_timer.start(INVINCIBLE_TIME);
109 float old_x_offset = sprite->get_current_hitbox_x_offset();
110 float old_y_offset = sprite->get_current_hitbox_y_offset();
113 // shrink bounding box and adjust sprite position to where the stump once was
114 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
115 Vector pos = get_pos();
116 pos.x += sprite->get_current_hitbox_x_offset() - old_x_offset;
117 pos.y += sprite->get_current_hitbox_y_offset() - old_y_offset;
120 sound_manager->play("sounds/mr_tree.ogg", get_pos());
121 player.bounce(*this);
123 Vector leaf1_pos = Vector(pos.x - POISONIVY_WIDTH - 1, pos.y - POISONIVY_Y_OFFSET);
124 Rect leaf1_bbox = Rect(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
125 if (Sector::current()->is_free_space(leaf1_bbox)) {
126 PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
127 Sector::current()->add_object(leaf1);
129 Vector leaf2_pos = Vector(pos.x + sprite->get_current_hitbox_width() + 1, pos.y - POISONIVY_Y_OFFSET);
130 Rect leaf2_bbox = Rect(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
131 if (Sector::current()->is_free_space(leaf2_bbox)) {
132 PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
133 Sector::current()->add_object(leaf2);
139 // if we're still invincible, we ignore the hit
140 if (mystate == STATE_INVINCIBLE) {
141 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
142 player.bounce(*this);
146 // if we're small, we die
147 if (mystate == STATE_NORMAL) {
148 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
149 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
150 kill_squished(player);
159 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
161 if(fabsf(hit.normal.y) > .5) {
162 physic.set_velocity_y(0);
164 dir = dir == LEFT ? RIGHT : LEFT;
165 set_direction = false;
173 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
175 if(fabsf(hit.normal.x) > .8) { // left or right hit
176 dir = dir == LEFT ? RIGHT : LEFT;
177 set_direction = false;
184 IMPLEMENT_FACTORY(MrTree, "mrtree")