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