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