3a498d7d36bc54dc1928ea4e37afeb84b9c60f1e
[supertux.git] / src / trigger / climbable.cpp
1 //  $Id$
2 //
3 //  SuperTux - Climbable area
4 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "climbable.hpp"
23 #include "game_session.hpp"
24 #include "lisp/lisp.hpp"
25 #include "lisp/writer.hpp"
26 #include "object_factory.hpp"
27 #include "main.hpp"
28 #include "sector.hpp"
29 #include "level.hpp"
30 #include "gettext.hpp"
31 #include "object/tilemap.hpp"
32
33 namespace {
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
39 }
40
41 Climbable::Climbable(const lisp::Lisp& reader)
42   : climbed_by(0)
43 {
44   reader.get("x", bbox.p1.x);
45   reader.get("y", bbox.p1.y);
46   float w = 32, h = 32;
47   reader.get("width", w);
48   reader.get("height", h);
49   bbox.set_size(w, h);
50 }
51
52 Climbable::Climbable(const Rect& area)
53   : climbed_by(0)
54 {
55   bbox = area;
56 }
57
58 Climbable::~Climbable()
59 {
60   if (climbed_by) {
61     climbed_by->stop_climbing(*this);
62     climbed_by = 0;
63   }
64 }
65
66 void
67 Climbable::write(lisp::Writer& writer)
68 {
69   writer.start_list("climbable");
70
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());
75
76   writer.end_list("climbable");
77 }
78
79 void 
80 Climbable::update(float /*elapsed_time*/)
81 {
82   if (!climbed_by) return;
83
84   if (!may_climb(*climbed_by)) {
85     climbed_by->stop_climbing(*this);
86     climbed_by = 0;
87   }
88 }
89
90 void
91 Climbable::draw(DrawingContext& context)
92 {
93   if (climbed_by) {
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();
99   }
100 }
101
102 void
103 Climbable::event(Player& player, EventType type)
104 {
105   if ((type == EVENT_ACTIVATE) || (activate_try_timer.started())) {
106     if (may_climb(player)) {
107       climbed_by = &player;
108       player.start_climbing(*this);
109       activate_try_timer.stop();
110     } else {
111       if (type == EVENT_ACTIVATE) activate_try_timer.start(ACTIVATE_TRY_FOR);
112       if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) player.add_velocity(Vector(POSITION_FIX_AX,0));
113       if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) player.add_velocity(Vector(-POSITION_FIX_AX,0));
114       if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) player.add_velocity(Vector(0,POSITION_FIX_AY));
115       if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) player.add_velocity(Vector(0,-POSITION_FIX_AY));
116     }
117   }
118   if(type == EVENT_LOSETOUCH) {
119     player.stop_climbing(*this);
120     climbed_by = 0;
121   }
122 }
123
124 bool
125 Climbable::may_climb(Player& player) 
126 {
127   if (player.get_bbox().p1.x < get_bbox().p1.x - GRACE_DX) return false;
128   if (player.get_bbox().p2.x > get_bbox().p2.x + GRACE_DX) return false;
129   if (player.get_bbox().p1.y < get_bbox().p1.y - GRACE_DY) return false;
130   if (player.get_bbox().p2.y > get_bbox().p2.y + GRACE_DY) return false;
131   return true;
132 }
133
134 IMPLEMENT_FACTORY(Climbable, "climbable");
135