Updated addon repository URL and improved debug output on download
[supertux.git] / src / trigger / climbable.cpp
1 //  SuperTux - Climbable area
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "trigger/climbable.hpp"
18
19 #include "object/player.hpp"
20 #include "supertux/globals.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "util/gettext.hpp"
23 #include "util/reader.hpp"
24
25 namespace {
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 = 30; // 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
31 }
32
33 Climbable::Climbable(const Reader& reader) :
34   climbed_by(0),
35   activate_try_timer()
36 {
37   reader.get("x", bbox.p1.x);
38   reader.get("y", bbox.p1.y);
39   float w = 32, h = 32;
40   reader.get("width", w);
41   reader.get("height", h);
42   bbox.set_size(w, h);
43 }
44
45 Climbable::Climbable(const Rectf& area) :
46   climbed_by(0),
47   activate_try_timer()
48 {
49   bbox = area;
50 }
51
52 Climbable::~Climbable()
53 {
54   if (climbed_by) {
55     climbed_by->stop_climbing(*this);
56     climbed_by = 0;
57   }
58 }
59
60 void
61 Climbable::update(float /*elapsed_time*/)
62 {
63   if (!climbed_by) return;
64
65   if (!may_climb(*climbed_by)) {
66     climbed_by->stop_climbing(*this);
67     climbed_by = 0;
68   }
69 }
70
71 void
72 Climbable::draw(DrawingContext& context)
73 {
74   if (climbed_by) {
75     context.push_transform();
76     context.set_translation(Vector(0, 0));
77     Vector pos = Vector(0, SCREEN_HEIGHT/2 - Resources::normal_font->get_height()/2);
78     context.draw_center_text(Resources::normal_font, _("Up we go..."), pos, LAYER_HUD, Climbable::text_color);
79     context.pop_transform();
80   }
81 }
82
83 void
84 Climbable::event(Player& player, EventType type)
85 {
86   if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
87     if(player.get_grabbed_object() == NULL){
88       if(may_climb(player)) {
89         climbed_by = &player;
90         player.start_climbing(*this);
91         activate_try_timer.stop();
92       } else {
93         if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
94         // the "-13" to y velocity prevents Tux from walking in place on the ground for horizonal adjustments
95         if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,-13));
96         if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,-13));
97         if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
98         if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
99       }
100     }
101   }
102   if(type == EVENT_LOSETOUCH) {
103     player.stop_climbing(*this);
104     climbed_by = 0;
105   }
106 }
107
108 bool
109 Climbable::may_climb(Player& player)
110 {
111   if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
112   if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
113   if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
114   if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
115   return true;
116 }
117
118 /* EOF */