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/fish.hpp"
18
19#include "sprite/sprite.hpp"
20#include "supertux/tile.hpp"
21
22static const float FISH_JUMP_POWER = -600;
23static const float FISH_WAIT_TIME = 1;
24
25Fish::Fish(const ReaderMapping& reader) :
26 BadGuy(reader, "images/creatures/fish/fish.sprite", LAYER_TILES-1),
27 waiting(),
28 stop_y(0)
29{
30 m_physic.enable_gravity(true);
31}
32
33void
34Fish::collision_solid(const CollisionHit& chit)
35{
36 hit(chit);
37}
38
39HitResponse
40Fish::collision_badguy(BadGuy& , const CollisionHit& chit)
41{
42 return hit(chit);
43}
44
45void
46Fish::draw(DrawingContext& context)
47{
48 if (waiting.started())
49 return;
50
51 BadGuy::draw(context);
52}
53
54HitResponse
55Fish::hit(const CollisionHit& hit_)
56{
57 if (hit_.top) {
58 m_physic.set_velocity_y(0);
59 }
60
61 return CONTINUE;
62}
63
64void
65Fish::collision_tile(uint32_t tile_attributes)
66{
67 if ((tile_attributes & Tile::WATER) && (m_physic.get_velocity_y() >= 0)) {
68
69 // initialize stop position if uninitialized
70 if (stop_y == 0) stop_y = get_pos().y + m_col.m_bbox.get_height();
71
72 // stop when we have reached the stop position
73 if (get_pos().y >= stop_y) {
74 if (!m_frozen)
75 start_waiting();
76 m_col.m_movement = Vector(0, 0);
77 }
78
79 }
80 if ((!(tile_attributes & Tile::WATER) || m_frozen) && (tile_attributes & Tile::HURTS)) {
81 kill_fall();
82 }
83}
84
85void
86Fish::active_update(float dt_sec)
87{
88 BadGuy::active_update(dt_sec);
89
90 // waited long enough?
91 if (waiting.check()) {
92 jump();
93 }
94
95 // set sprite
96 if (!m_frozen)
97 m_sprite->set_action(m_physic.get_velocity_y() < 0 ? "normal" : "down");
98
99 // we can't afford flying out of the tilemap, 'cause the engine would remove us.
100 if ((get_pos().y - 31.8f) < 0) // too high, let us fall
101 {
102 m_physic.set_velocity_y(0);
103 m_physic.enable_gravity(true);
104 }
105
106 if (m_ignited)
107 remove_me();
108}
109
110void
111Fish::start_waiting()
112{
113 waiting.start(FISH_WAIT_TIME);
114 set_colgroup_active(COLGROUP_DISABLED);
115 m_physic.enable_gravity(false);
116 m_physic.set_velocity_y(0);
117}
118
119void
120Fish::jump()
121{
122 m_physic.set_velocity_y(FISH_JUMP_POWER);
123 m_physic.enable_gravity(true);
124 set_colgroup_active(COLGROUP_MOVING);
125}
126
127void
128Fish::freeze()
129{
130 BadGuy::freeze();
131 m_sprite->set_action(m_physic.get_velocity_y() < 0 ? "iced" : "iced-down");
132 m_sprite->set_color(Color(1.0f, 1.0f, 1.0f));
133 waiting.stop();
134}
135
136void
137Fish::unfreeze()
138{ // does this happen at all? (or do fishes die when they fall frozen?)
139 BadGuy::unfreeze();
140 start_waiting();
141}
142
143void
144Fish::kill_fall()
145{
146 m_sprite->set_action("normal");
147 BadGuy::kill_fall();
148}
149
150bool
151Fish::is_freezable() const
152{
153 return true;
154}
155
156/* EOF */
157