3 // SuperTux - Climbable area
4 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "climbable.hpp"
24 #include "game_session.hpp"
25 #include "lisp/lisp.hpp"
26 #include "lisp/writer.hpp"
27 #include "object_factory.hpp"
31 #include "gettext.hpp"
32 #include "object/tilemap.hpp"
35 const float GRACE_DX = 8; // how far off may the player's bounding-box be x-wise
36 const float GRACE_DY = 8; // how far off may the player's bounding-box be y-wise
37 const float ACTIVATE_TRY_FOR = 1; // how long to try correcting mis-alignment of player and climbable before giving up
38 const float POSITION_FIX_AX = 50; // x-wise acceleration applied to player when trying to align player and Climbable
39 const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when trying to align player and Climbable
42 Climbable::Climbable(const lisp::Lisp& reader)
45 reader.get("x", bbox.p1.x);
46 reader.get("y", bbox.p1.y);
48 reader.get("width", w);
49 reader.get("height", h);
53 Climbable::Climbable(const Rect& area)
59 Climbable::~Climbable()
62 climbed_by->stop_climbing(*this);
68 Climbable::write(lisp::Writer& writer)
70 writer.start_list("climbable");
72 writer.write("x", bbox.p1.x);
73 writer.write("y", bbox.p1.y);
74 writer.write("width", bbox.get_width());
75 writer.write("height", bbox.get_height());
77 writer.end_list("climbable");
81 Climbable::update(float /*elapsed_time*/)
83 if (!climbed_by) return;
85 if (!may_climb(*climbed_by)) {
86 climbed_by->stop_climbing(*this);
92 Climbable::draw(DrawingContext& context)
95 context.push_transform();
96 context.set_translation(Vector(0, 0));
97 Vector pos = Vector(0, SCREEN_HEIGHT/2 - normal_font->get_height()/2);
98 context.draw_center_text(normal_font, _("Up we go..."), pos, LAYER_GUI, Climbable::text_color);
99 context.pop_transform();
104 Climbable::event(Player& player, EventType type)
106 if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
107 if(player.get_grabbed_object() == NULL){
108 if(may_climb(player)) {
109 climbed_by = &player;
110 player.start_climbing(*this);
111 activate_try_timer.stop();
113 if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
114 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
115 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
116 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
117 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
121 if(type == EVENT_LOSETOUCH) {
122 player.stop_climbing(*this);
128 Climbable::may_climb(Player& player)
130 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
131 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
132 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
133 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
137 IMPLEMENT_FACTORY(Climbable, "climbable");