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.
24 static const float WALKSPEED = 80;
25 static const float WAKE_TIME = .5;
27 Plant::Plant(const lisp::Lisp& reader)
28 : BadGuy(reader, "images/creatures/plant/plant.sprite")
30 state = PLANT_SLEEPING;
34 Plant::write(lisp::Writer& writer)
36 writer.start_list("plant");
38 writer.write_float("x", start_position.x);
39 writer.write_float("y", start_position.y);
41 writer.end_list("plant");
47 //FIXME: turns sspiky around for debugging
48 dir = dir == LEFT ? RIGHT : LEFT;
50 state = PLANT_SLEEPING;
51 physic.set_velocity_x(0);
52 sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
56 Plant::collision_solid(const CollisionHit& hit)
58 if(hit.top || hit.bottom) {
59 physic.set_velocity_y(0);
60 } else if(hit.left || hit.right) {
61 dir = dir == LEFT ? RIGHT : LEFT;
62 sprite->set_action(dir == LEFT ? "left" : "right");
63 physic.set_velocity_x(-physic.get_velocity_x());
68 Plant::collision_badguy(BadGuy& , const CollisionHit& hit)
70 if(state != PLANT_WALKING) return CONTINUE;
72 if(hit.left || hit.right) {
73 dir = dir == LEFT ? RIGHT : LEFT;
74 sprite->set_action(dir == LEFT ? "left" : "right");
75 physic.set_velocity_x(-physic.get_velocity_x());
82 Plant::active_update(float elapsed_time) {
83 BadGuy::active_update(elapsed_time);
85 if(state == PLANT_SLEEPING) {
87 Player* player = this->get_nearest_player();
89 Rect mb = this->get_bbox();
90 Rect pb = player->get_bbox();
92 bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
93 bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
94 bool inReach_top = (pb.p2.y >= mb.p2.y);
95 bool inReach_bottom = (pb.p1.y <= mb.p1.y);
97 if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
99 sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
100 if(!timer.started()) timer.start(WAKE_TIME);
101 state = PLANT_WAKING;
106 if(state == PLANT_WAKING) {
109 sprite->set_action(dir == LEFT ? "left" : "right");
110 physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
111 state = PLANT_WALKING;
117 IMPLEMENT_FACTORY(Plant, "plant")