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