src/trigger/climbable.cpp

Go to the documentation of this file.
00001 //  SuperTux - Climbable area
00002 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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; // how far off may the player's bounding-box be x-wise
00027 const float GRACE_DY = 8; // how far off may the player's bounding-box be y-wise
00028 const float ACTIVATE_TRY_FOR = 1; // how long to try correcting mis-alignment of player and climbable before giving up
00029 const float POSITION_FIX_AX = 50; // x-wise acceleration applied to player when trying to align player and Climbable
00030 const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when trying to align player and Climbable
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 /*elapsed_time*/)
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 /* EOF */

Generated on Mon Jun 9 03:38:23 2014 for SuperTux by  doxygen 1.5.1