Badguys now inherit from MovingSprite
[supertux.git] / src / badguy / plant.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "plant.hpp"
23
24 static const float WALKSPEED = 80;
25 static const float WAKE_TIME = .5;
26
27 Plant::Plant(const lisp::Lisp& reader)
28         : BadGuy(reader, "images/creatures/plant/plant.sprite")
29 {
30   state = PLANT_SLEEPING;
31 }
32
33 void
34 Plant::write(lisp::Writer& writer)
35 {
36   writer.start_list("plant");
37
38   writer.write_float("x", start_position.x);
39   writer.write_float("y", start_position.y);
40
41   writer.end_list("plant");
42 }
43
44 void
45 Plant::activate()
46 {
47   //FIXME: turns sspiky around for debugging
48   dir = dir == LEFT ? RIGHT : LEFT;
49
50   state = PLANT_SLEEPING;
51   physic.set_velocity_x(0);
52   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
53 }
54
55 HitResponse
56 Plant::collision_solid(GameObject& , const CollisionHit& hit)
57 {
58   if(fabsf(hit.normal.y) > .5) { // hit floor or roof?
59     physic.set_velocity_y(0);
60   } else { // hit right or left
61     dir = dir == LEFT ? RIGHT : LEFT;
62     sprite->set_action(dir == LEFT ? "left" : "right");
63     physic.set_velocity_x(-physic.get_velocity_x());
64   }
65
66   return CONTINUE;
67 }
68
69 HitResponse
70 Plant::collision_badguy(BadGuy& , const CollisionHit& hit)
71 {
72   if(state != PLANT_WALKING) return CONTINUE;
73
74   if(fabsf(hit.normal.x) > .8) { // left or right
75     dir = dir == LEFT ? RIGHT : LEFT;
76     sprite->set_action(dir == LEFT ? "left" : "right");
77     physic.set_velocity_x(-physic.get_velocity_x());
78   }
79
80   return CONTINUE;
81 }
82
83 void 
84 Plant::active_update(float elapsed_time) {
85   BadGuy::active_update(elapsed_time);
86
87   if(state == PLANT_SLEEPING) {
88
89     Player* player = this->get_nearest_player();
90     if (player) {
91       Rect mb = this->get_bbox();
92       Rect pb = player->get_bbox();
93
94       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
95       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
96       bool inReach_top = (pb.p2.y >= mb.p2.y);
97       bool inReach_bottom = (pb.p1.y <= mb.p1.y);
98
99       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
100         // wake up
101         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
102         if(!timer.started()) timer.start(WAKE_TIME);
103         state = PLANT_WAKING;
104       }
105     }
106   }
107
108   if(state == PLANT_WAKING) {
109     if(timer.check()) {
110       // start walking
111       sprite->set_action(dir == LEFT ? "left" : "right");
112       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
113       state = PLANT_WALKING;
114     }
115   }
116
117 }
118
119 IMPLEMENT_FACTORY(Plant, "plant")