Tweak to allow MinGW to compile, issues #26 & 27
[supertux.git] / src / badguy / sspiky.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "badguy/sspiky.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite.hpp"
21 #include "supertux/object_factory.hpp"
22
23 SSpiky::SSpiky(const Reader& reader)
24   : WalkingBadguy(reader, "images/creatures/spiky/sleepingspiky.sprite", "left", "right"), state(SSPIKY_SLEEPING)
25 {
26   walk_speed = 80;
27   max_drop_height = 600;
28 }
29
30 void
31 SSpiky::initialize()
32 {
33   state = SSPIKY_SLEEPING;
34   physic.set_velocity_x(0);
35   sprite->set_action(dir == LEFT ? "sleeping-left" : "sleeping-right");
36 }
37
38 void
39 SSpiky::collision_solid(const CollisionHit& hit)
40 {
41   if(state != SSPIKY_WALKING) {
42     BadGuy::collision_solid(hit);
43     return;
44   }
45   WalkingBadguy::collision_solid(hit);
46 }
47
48 HitResponse
49 SSpiky::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
50 {
51   if(state != SSPIKY_WALKING) {
52     return BadGuy::collision_badguy(badguy, hit);
53   }
54   return WalkingBadguy::collision_badguy(badguy, hit);
55 }
56
57 void
58 SSpiky::active_update(float elapsed_time) {
59
60   if(state == SSPIKY_WALKING) {
61     WalkingBadguy::active_update(elapsed_time);
62     return;
63   }
64
65   if(state == SSPIKY_SLEEPING) {
66
67     Player* player = this->get_nearest_player();
68     if (player) {
69       Rectf mb = this->get_bbox();
70       Rectf pb = player->get_bbox();
71
72       bool inReach_left = (pb.p2.x >= mb.p2.x-((dir == LEFT) ? 256 : 0));
73       bool inReach_right = (pb.p1.x <= mb.p1.x+((dir == RIGHT) ? 256 : 0));
74       bool inReach_top = (pb.p2.y >= mb.p1.y);
75       bool inReach_bottom = (pb.p1.y <= mb.p2.y);
76
77       if (inReach_left && inReach_right && inReach_top && inReach_bottom) {
78         // wake up
79         sprite->set_action(dir == LEFT ? "waking-left" : "waking-right", 1);
80         state = SSPIKY_WAKING;
81       }
82     }
83
84     BadGuy::active_update(elapsed_time);
85   }
86
87   if(state == SSPIKY_WAKING) {
88     if(sprite->animation_done()) {
89       // start walking
90       state = SSPIKY_WALKING;
91       WalkingBadguy::initialize();
92     }
93
94     BadGuy::active_update(elapsed_time);
95   }
96 }
97
98 void
99 SSpiky::freeze()
100 {
101   WalkingBadguy::freeze();
102   sprite->set_action(dir == LEFT ? "iced-left" : "iced-right");
103   state = SSPIKY_WALKING; // if we get hit while sleeping, wake up :)
104 }
105
106 bool
107 SSpiky::is_freezable() const
108 {
109   return true;
110 }
111
112 /* EOF */