1 // SuperTux - Climbable area
2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
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.
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.
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/>.
17 #include "trigger/climbable.hpp"
19 #include "object/player.hpp"
20 #include "supertux/main.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "util/gettext.hpp"
23 #include "util/reader.hpp"
26 const float GRACE_DX = 8; // how far off may the player's bounding-box be x-wise
27 const float GRACE_DY = 8; // how far off may the player's bounding-box be y-wise
28 const float ACTIVATE_TRY_FOR = 1; // how long to try correcting mis-alignment of player and climbable before giving up
29 const float POSITION_FIX_AX = 50; // x-wise acceleration applied to player when trying to align player and Climbable
30 const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when trying to align player and Climbable
33 Climbable::Climbable(const Reader& reader) :
37 reader.get("x", bbox.p1.x);
38 reader.get("y", bbox.p1.y);
40 reader.get("width", w);
41 reader.get("height", h);
45 Climbable::Climbable(const Rect& area) :
52 Climbable::~Climbable()
55 climbed_by->stop_climbing(*this);
61 Climbable::update(float /*elapsed_time*/)
63 if (!climbed_by) return;
65 if (!may_climb(*climbed_by)) {
66 climbed_by->stop_climbing(*this);
72 Climbable::draw(DrawingContext& context)
75 context.push_transform();
76 context.set_translation(Vector(0, 0));
77 Vector pos = Vector(0, SCREEN_HEIGHT/2 - normal_font->get_height()/2);
78 context.draw_center_text(normal_font, _("Up we go..."), pos, LAYER_GUI, Climbable::text_color);
79 context.pop_transform();
84 Climbable::event(Player& player, EventType type)
86 if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
87 if(player.get_grabbed_object() == NULL){
88 if(may_climb(player)) {
90 player.start_climbing(*this);
91 activate_try_timer.stop();
93 if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
94 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
95 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
96 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
97 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
101 if(type == EVENT_LOSETOUCH) {
102 player.stop_climbing(*this);
108 Climbable::may_climb(Player& player)
110 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
111 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
112 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
113 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
117 IMPLEMENT_FACTORY(Climbable, "climbable");