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