1 | // SuperTux |
2 | // Copyright (C) 2008 Wolfgang Becker <uafr@gmx.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/captainsnowball.hpp" |
18 | |
19 | #include "sprite/sprite.hpp" |
20 | #include "supertux/sector.hpp" |
21 | |
22 | namespace{ |
23 | static const float CAPTAIN_WALK_SPEED = 100; |
24 | static const float BOARDING_SPEED = 200; |
25 | } |
26 | |
27 | CaptainSnowball::CaptainSnowball(const ReaderMapping& reader) |
28 | : WalkingBadguy(reader, "images/creatures/snowball/cpt-snowball.sprite" , "left" , "right" ) |
29 | { |
30 | walk_speed = BOARDING_SPEED; |
31 | max_drop_height = -1; |
32 | m_physic.set_velocity_y(-400); |
33 | } |
34 | |
35 | bool |
36 | CaptainSnowball::might_climb(int width, int height) const |
37 | { |
38 | // make sure we check for at least a 1-pixel climb |
39 | assert(height > 0); |
40 | |
41 | float x1; |
42 | float x2; |
43 | float y1a = m_col.m_bbox.get_top() + 1; |
44 | float y2a = m_col.m_bbox.get_bottom() - 1; |
45 | float y1b = m_col.m_bbox.get_top() + 1 - static_cast<float>(height); |
46 | float y2b = m_col.m_bbox.get_bottom() - 1 - static_cast<float>(height); |
47 | if (m_dir == Direction::LEFT) { |
48 | x1 = m_col.m_bbox.get_left() - static_cast<float>(width); |
49 | x2 = m_col.m_bbox.get_left() - 1; |
50 | } else { |
51 | x1 = m_col.m_bbox.get_right() + 1; |
52 | x2 = m_col.m_bbox.get_right() + static_cast<float>(width); |
53 | } |
54 | return ((!Sector::get().is_free_of_statics(Rectf(x1, y1a, x2, y2a))) && |
55 | (Sector::get().is_free_of_statics(Rectf(x1, y1b, x2, y2b)))); |
56 | } |
57 | |
58 | void |
59 | CaptainSnowball::active_update(float dt_sec) |
60 | { |
61 | if (on_ground() && might_climb(8, 64)) { |
62 | m_physic.set_velocity_y(-400); |
63 | } else if (on_ground() && might_fall(16)) { |
64 | m_physic.set_velocity_y(-400); |
65 | walk_speed = BOARDING_SPEED; |
66 | m_physic.set_velocity_x(m_dir == Direction::LEFT ? -walk_speed : walk_speed); |
67 | } |
68 | WalkingBadguy::active_update(dt_sec); |
69 | } |
70 | |
71 | void |
72 | CaptainSnowball::collision_solid(const CollisionHit& hit) |
73 | { |
74 | if (is_active() && (walk_speed == BOARDING_SPEED)) { |
75 | walk_speed = CAPTAIN_WALK_SPEED; |
76 | m_physic.set_velocity_x(m_dir == Direction::LEFT ? -walk_speed : walk_speed); |
77 | } |
78 | WalkingBadguy::collision_solid(hit); |
79 | } |
80 | |
81 | bool |
82 | CaptainSnowball::collision_squished(GameObject& object) |
83 | { |
84 | m_sprite->set_action(m_dir == Direction::LEFT ? "squished-left" : "squished-right" ); |
85 | kill_squished(object); |
86 | return true; |
87 | } |
88 | |
89 | /* EOF */ |
90 | |