292767f3cab936c5bb59f6a2275067e426a3d2ad
[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
26 SSpiky::SSpiky(const lisp::Lisp& reader)
27         : WalkingBadguy(reader, "images/creatures/spiky/sleepingspiky.sprite", "left", "right"), state(SSPIKY_SLEEPING)
28 {
29   walk_speed = WALKSPEED;
30   max_drop_height = -1;
31 }
32
33 void
34 SSpiky::write(lisp::Writer& writer)
35 {
36   writer.start_list("sspiky");
37   WalkingBadguy::write(writer);
38   writer.end_list("sspiky");
39 }
40
41 void
42 SSpiky::activate()
43 {
44   state = SSPIKY_SLEEPING;
45   physic.set_velocity_x(0);
46   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
47 }
48
49 void
50 SSpiky::collision_solid(const CollisionHit& hit)
51 {
52   if(state != SSPIKY_WALKING) {
53     BadGuy::collision_solid(hit);
54     return;
55   }
56   WalkingBadguy::collision_solid(hit);
57 }
58
59 HitResponse
60 SSpiky::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
61 {
62   if(state != SSPIKY_WALKING) {
63     return BadGuy::collision_badguy(badguy, hit);
64   }
65   return WalkingBadguy::collision_badguy(badguy, hit);
66 }
67
68 void 
69 SSpiky::active_update(float elapsed_time) {
70
71   if(state == SSPIKY_WALKING) {
72     WalkingBadguy::active_update(elapsed_time);
73     return;
74   }
75
76   if(state == SSPIKY_SLEEPING) {
77
78     Player* player = this->get_nearest_player();
79     if (player) {
80       Rect mb = this->get_bbox();
81       Rect pb = player->get_bbox();
82
83       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
84       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
85       bool inReach_top = (pb.p2.y >= mb.p1.y);
86       bool inReach_bottom = (pb.p1.y <= mb.p2.y);
87
88       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
89         // wake up
90         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
91         state = SSPIKY_WAKING;
92       }
93     }
94   
95     BadGuy::active_update(elapsed_time);
96   }
97
98   if(state == SSPIKY_WAKING) {
99     if(sprite->animation_done()) {
100       // start walking
101       state = SSPIKY_WALKING;
102       WalkingBadguy::activate();
103     }
104
105     BadGuy::active_update(elapsed_time);
106   }
107 }
108
109 IMPLEMENT_FACTORY(SSpiky, "sspiky")