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