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"
23 #include "game_session.hpp"
24 #include "lisp/lisp.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
30 #include "gettext.hpp"
31 #include "object/tilemap.hpp"
34 const float GRACE_DX = 8; // how far off may the player's bounding-box be x-wise
35 const float GRACE_DY = 8; // how far off may the player's bounding-box be y-wise
36 const float ACTIVATE_TRY_FOR = 1; // how long to try correcting mis-alignment of player and climbable before giving up
37 const float POSITION_FIX_AX = 50; // x-wise acceleration applied to player when trying to align player and Climbable
38 const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when trying to align player and Climbable
41 Climbable::Climbable(const lisp::Lisp& reader)
44 reader.get("x", bbox.p1.x);
45 reader.get("y", bbox.p1.y);
47 reader.get("width", w);
48 reader.get("height", h);
52 Climbable::Climbable(const Rect& area)
58 Climbable::~Climbable()
61 climbed_by->stop_climbing(*this);
67 Climbable::write(lisp::Writer& writer)
69 writer.start_list("climbable");
71 writer.write_float("x", bbox.p1.x);
72 writer.write_float("y", bbox.p1.y);
73 writer.write_float("width", bbox.get_width());
74 writer.write_float("height", bbox.get_height());
76 writer.end_list("climbable");
80 Climbable::update(float /*elapsed_time*/)
82 if (!climbed_by) return;
84 if (!may_climb(*climbed_by)) {
85 climbed_by->stop_climbing(*this);
91 Climbable::draw(DrawingContext& context)
94 context.push_transform();
95 context.set_translation(Vector(0, 0));
96 Vector pos = Vector(0, SCREEN_HEIGHT/2 - normal_font->get_height()/2);
97 context.draw_center_text(normal_font, _("Up we go..."), pos, LAYER_GUI, Climbable::text_color);
98 context.pop_transform();
103 Climbable::event(Player& player, EventType type)
105 if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
106 if(player.get_grabbed_object() == NULL){
107 if(may_climb(player)) {
108 climbed_by = &player;
109 player.start_climbing(*this);
110 activate_try_timer.stop();
112 if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
113 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
114 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
115 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
116 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
120 if(type == EVENT_LOSETOUCH) {
121 player.stop_climbing(*this);
127 Climbable::may_climb(Player& player)
129 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
130 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
131 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
132 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
136 IMPLEMENT_FACTORY(Climbable, "climbable");