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 MrTree::MrTree(const lisp::Lisp& reader)
32 reader.get("x", start_position.x);
33 reader.get("y", start_position.y);
34 stay_on_platform = false;
35 reader.get("stay-on-platform", stay_on_platform);
36 bbox.set_size(84.8, 84.8);
37 sprite = sprite_manager->create("images/creatures/mr_tree/mr_tree.sprite");
41 MrTree::write(lisp::Writer& writer)
43 writer.start_list("mrtree");
45 writer.write_float("x", start_position.x);
46 writer.write_float("y", start_position.y);
48 writer.end_list("mrtree");
54 if (mystate == STATE_BIG) {
55 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
56 sprite->set_action(dir == LEFT ? "large-left" : "large-right");
59 if (mystate == STATE_INVINCIBLE) {
60 physic.set_velocity_x(0);
61 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
64 if (mystate == STATE_NORMAL) {
65 physic.set_velocity_x(dir == LEFT ? -WALKSPEED_SMALL : WALKSPEED_SMALL);
66 sprite->set_action(dir == LEFT ? "small-left" : "small-right");
72 MrTree::active_update(float elapsed_time)
74 if ((mystate == STATE_INVINCIBLE) && (invincible_timer.check())) {
75 mystate = STATE_NORMAL;
79 if (stay_on_platform && may_fall_off_platform())
81 dir = (dir == LEFT ? RIGHT : LEFT);
85 BadGuy::active_update(elapsed_time);
89 MrTree::collision_squished(Player& player)
91 // if we're big, we shrink
92 if(mystate == STATE_BIG) {
93 mystate = STATE_INVINCIBLE;
94 invincible_timer.start(INVINCIBLE_TIME);
97 // shrink bounding box and adjust sprite position to where the stump once was
98 bbox.set_size(42, 62);
99 Vector pos = get_pos();
104 sound_manager->play("sounds/mr_tree.ogg", get_pos());
105 player.bounce(*this);
107 Rect leaf1_bbox = Rect(pos.x-32-1, pos.y-23+1, pos.x-32-1+32, pos.y-23+1+32);
108 if (Sector::current()->is_free_space(leaf1_bbox)) {
109 PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1.x, leaf1_bbox.p1.y, LEFT);
110 Sector::current()->add_object(leaf1);
112 Rect leaf2_bbox = Rect(pos.x+42+1, pos.y-23+1, pos.x+32+1+32, pos.y-23+1+32);
113 if (Sector::current()->is_free_space(leaf2_bbox)) {
114 PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1.x, leaf2_bbox.p1.y, RIGHT);
115 Sector::current()->add_object(leaf2);
121 // if we're still invincible, we ignore the hit
122 if (mystate == STATE_INVINCIBLE) {
123 sound_manager->play("sounds/mr_treehit.ogg", get_pos());
124 player.bounce(*this);
128 // if we're small, we die
129 if (mystate == STATE_NORMAL) {
130 sprite->set_action(dir == LEFT ? "squished-left" : "squished-right");
131 bbox.set_size(42, 42);
132 kill_squished(player);
141 MrTree::collision_solid(GameObject& , const CollisionHit& hit)
143 if(fabsf(hit.normal.y) > .5) {
144 physic.set_velocity_y(0);
146 dir = dir == LEFT ? RIGHT : LEFT;
154 MrTree::collision_badguy(BadGuy& , const CollisionHit& hit)
156 if(fabsf(hit.normal.x) > .8) { // left or right hit
157 dir = dir == LEFT ? RIGHT : LEFT;
164 IMPLEMENT_FACTORY(MrTree, "mrtree")