00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "trigger/climbable.hpp"
00018
00019 #include "object/player.hpp"
00020 #include "supertux/globals.hpp"
00021 #include "supertux/object_factory.hpp"
00022 #include "util/gettext.hpp"
00023 #include "util/reader.hpp"
00024
00025 namespace {
00026 const float GRACE_DX = 8;
00027 const float GRACE_DY = 8;
00028 const float ACTIVATE_TRY_FOR = 1;
00029 const float POSITION_FIX_AX = 50;
00030 const float POSITION_FIX_AY = 50;
00031 }
00032
00033 Climbable::Climbable(const Reader& reader) :
00034 climbed_by(0),
00035 activate_try_timer()
00036 {
00037 reader.get("x", bbox.p1.x);
00038 reader.get("y", bbox.p1.y);
00039 float w = 32, h = 32;
00040 reader.get("width", w);
00041 reader.get("height", h);
00042 bbox.set_size(w, h);
00043 }
00044
00045 Climbable::Climbable(const Rectf& area) :
00046 climbed_by(0),
00047 activate_try_timer()
00048 {
00049 bbox = area;
00050 }
00051
00052 Climbable::~Climbable()
00053 {
00054 if (climbed_by) {
00055 climbed_by->stop_climbing(*this);
00056 climbed_by = 0;
00057 }
00058 }
00059
00060 void
00061 Climbable::update(float )
00062 {
00063 if (!climbed_by) return;
00064
00065 if (!may_climb(*climbed_by)) {
00066 climbed_by->stop_climbing(*this);
00067 climbed_by = 0;
00068 }
00069 }
00070
00071 void
00072 Climbable::draw(DrawingContext& context)
00073 {
00074 if (climbed_by) {
00075 context.push_transform();
00076 context.set_translation(Vector(0, 0));
00077 Vector pos = Vector(0, SCREEN_HEIGHT/2 - Resources::normal_font->get_height()/2);
00078 context.draw_center_text(Resources::normal_font, _("Up we go..."), pos, LAYER_HUD, Climbable::text_color);
00079 context.pop_transform();
00080 }
00081 }
00082
00083 void
00084 Climbable::event(Player& player, EventType type)
00085 {
00086 if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
00087 if(player.get_grabbed_object() == NULL){
00088 if(may_climb(player)) {
00089 climbed_by = &player;
00090 player.start_climbing(*this);
00091 activate_try_timer.stop();
00092 } else {
00093 if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
00094 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
00095 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
00096 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
00097 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
00098 }
00099 }
00100 }
00101 if(type == EVENT_LOSETOUCH) {
00102 player.stop_climbing(*this);
00103 climbed_by = 0;
00104 }
00105 }
00106
00107 bool
00108 Climbable::may_climb(Player& player)
00109 {
00110 if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
00111 if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
00112 if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
00113 if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
00114 return true;
00115 }
00116
00117