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