1 // $Id: plant.cpp 2979 2006-01-10 00:00:04Z matzebraun $
4 // Copyright (C) 2005 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
25 static const float WALKSPEED = 80;
26 static const float WAKE_TIME = .5;
28 Plant::Plant(const lisp::Lisp& reader)
30 reader.get("x", start_position.x);
31 reader.get("y", start_position.y);
32 bbox.set_size(31.8, 31.8);
33 sprite = sprite_manager->create("images/creatures/plant/plant.sprite");
34 state = PLANT_SLEEPING;
38 Plant::write(lisp::Writer& writer)
40 writer.start_list("plant");
42 writer.write_float("x", start_position.x);
43 writer.write_float("y", start_position.y);
45 writer.end_list("plant");
51 //FIXME: turns sspiky around for debugging
52 dir = dir == LEFT ? RIGHT : LEFT;
54 state = PLANT_SLEEPING;
55 physic.set_velocity_x(0);
56 sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
60 Plant::collision_solid(GameObject& , const CollisionHit& hit)
62 if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
63 physic.set_velocity_y(0);
64 } else { // hit right or left
65 dir = dir == LEFT ? RIGHT : LEFT;
66 sprite->set_action(dir == LEFT ? "left" : "right");
67 physic.set_velocity_x(-physic.get_velocity_x());
74 Plant::collision_badguy(BadGuy& , const CollisionHit& hit)
76 if(state != PLANT_WALKING) return CONTINUE;
78 if(fabsf(hit.normal.x) > .8) { // left or right
79 dir = dir == LEFT ? RIGHT : LEFT;
80 sprite->set_action(dir == LEFT ? "left" : "right");
81 physic.set_velocity_x(-physic.get_velocity_x());
88 Plant::active_update(float elapsed_time) {
89 BadGuy::active_update(elapsed_time);
91 if(state == PLANT_SLEEPING) {
92 const Vector playerPos = Sector::current()->player->get_pos();
93 const Vector myPos = this->get_pos();
94 float dx = (playerPos.x - myPos.x);
95 float dy = (playerPos.y - myPos.y);
97 // if we "see" the player
98 if ((((dir == LEFT) && (dx > -128) && (dx < 0)) || ((dir == RIGHT) && (dx > 0) && (dx < 128))) && (dy > -32) && (dy < 32)) {
100 sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
101 if(!timer.started()) timer.start(WAKE_TIME);
102 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")