Badguys rely on new (dummy-)function get_nearest_player. /
[supertux.git] / src / badguy / sspiky.cpp
1 //  $Id: sspiky.cpp 2979 2006-01-10 00:00:04Z matzebraun $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20
21 #include <config.h>
22
23 #include "sspiky.hpp"
24
25 static const float WALKSPEED = 80;
26 static const float WAKE_TIME = .5;
27
28 SSpiky::SSpiky(const lisp::Lisp& reader)
29 {
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/sspiky/sspiky.sprite");
34   state = SSPIKY_SLEEPING;
35 }
36
37 void
38 SSpiky::write(lisp::Writer& writer)
39 {
40   writer.start_list("sspiky");
41
42   writer.write_float("x", start_position.x);
43   writer.write_float("y", start_position.y);
44
45   writer.end_list("sspiky");
46 }
47
48 void
49 SSpiky::activate()
50 {
51   //FIXME: turns sspiky around for debugging
52   dir = dir == LEFT ? RIGHT : LEFT;
53
54   state = SSPIKY_SLEEPING;
55   physic.set_velocity_x(0);
56   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
57 }
58
59 HitResponse
60 SSpiky::collision_solid(GameObject& , const CollisionHit& hit)
61 {
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());
68   }
69
70   return CONTINUE;
71 }
72
73 HitResponse
74 SSpiky::collision_badguy(BadGuy& , const CollisionHit& hit)
75 {
76   if(state != SSPIKY_WALKING) return CONTINUE;
77
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());
82   }
83
84   return CONTINUE;
85 }
86
87 void 
88 SSpiky::active_update(float elapsed_time) {
89   BadGuy::active_update(elapsed_time);
90
91   if(state == SSPIKY_SLEEPING) {
92
93     Player* player = this->get_nearest_player();
94     if (player) {
95       Rect mb = this->get_bbox();
96       Rect pb = player->get_bbox();
97
98       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
99       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
100       bool inReach_top = (pb.p2.y >= mb.p2.y);
101       bool inReach_bottom = (pb.p1.y <= mb.p1.y);
102
103       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
104         // wake up
105         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right");
106         if(!timer.started()) timer.start(WAKE_TIME);
107         state = SSPIKY_WAKING;
108       }
109     }
110   }
111
112   if(state == SSPIKY_WAKING) {
113     if(timer.check()) {
114       // start walking
115       sprite->set_action(dir == LEFT ? "left" : "right");
116       physic.set_velocity_x(dir == LEFT ? -WALKSPEED : WALKSPEED);
117       state = SSPIKY_WALKING;
118     }
119   }
120
121 }
122
123 IMPLEMENT_FACTORY(SSpiky, "sspiky")