Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / badguy / plant.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/plant.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22
23 static const float WALKSPEED = 80;
24 static const float WAKE_TIME = .5;
25
26 Plant::Plant(const Reader& reader) :
27   BadGuy(reader, "images/creatures/plant/plant.sprite"),
28   timer(),
29   state()
30 {
31   state = PLANT_SLEEPING;
32 }
33
34 void
35 Plant::initialize()
36 {
37   //FIXME: turns sspiky around for debugging
38   dir = dir == LEFT ? RIGHT : LEFT;
39
40   state = PLANT_SLEEPING;
41   physic.set_velocity_x(0);
42   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
43 }
44
45 void
46 Plant::collision_solid(const CollisionHit& hit)
47 {
48   if(hit.top || hit.bottom) {
49     physic.set_velocity_y(0);
50   } else if(hit.left || hit.right) {
51     dir = dir == LEFT ? RIGHT : LEFT;
52     sprite->set_action(dir == LEFT ? "left" : "right");
53     physic.set_velocity_x(-physic.get_velocity_x());
54   }
55 }
56
57 HitResponse
58 Plant::collision_badguy(BadGuy& , const CollisionHit& hit)
59 {
60   if(state != PLANT_WALKING) return CONTINUE;
61
62   if(hit.left || hit.right) {
63     dir = dir == LEFT ? RIGHT : LEFT;
64     sprite->set_action(dir == LEFT ? "left" : "right");
65     physic.set_velocity_x(-physic.get_velocity_x());
66   }
67
68   return CONTINUE;
69 }
70
71 void
72 Plant::active_update(float elapsed_time) {
73   BadGuy::active_update(elapsed_time);
74
75   if(state == PLANT_SLEEPING) {
76
77     Player* player = this->get_nearest_player();
78     if (player) {
79       Rect mb = this->get_bbox();
80       Rect pb = player->get_bbox();
81
82       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
83       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
84       bool inReach_top = (pb.p2.y >= mb.p2.y);
85       bool inReach_bottom = (pb.p1.y <= mb.p1.y);
86
87       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
88         // wake up
89         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
90         if(!timer.started()) timer.start(WAKE_TIME);
91         state = PLANT_WAKING;
92       }
93     }
94   }
95
96   if(state == PLANT_WAKING) {
97     if(timer.check()) {
98       // start walking
99       sprite->set_action(dir == LEFT ? "left" : "right");
100       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
101       state = PLANT_WALKING;
102     }
103   }
104
105 }
106
107 IMPLEMENT_FACTORY(Plant, "plant");
108
109 /* EOF */