2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/level_time.hpp"
19 #include "object/player.hpp"
20 #include "scripting/level_time.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/resources.hpp"
25 #include "supertux/sector.hpp"
26 #include "util/reader.hpp"
27 #include "video/drawing_context.hpp"
31 /** When to alert player they're low on time! */
32 static const float TIME_WARNING = 20;
34 LevelTime::LevelTime(const Reader& reader) :
39 reader.get("name", name);
40 reader.get("time", time_left);
41 if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
42 time_surface = Surface::create("images/engine/hud/time-0.png");
46 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
48 if (name.empty()) return;
49 scripting::LevelTime* _this = new scripting::LevelTime(this);
50 expose_object(vm, table_idx, _this, name, true);
54 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
56 if (name.empty()) return;
57 scripting::unexpose_object(vm, table_idx, name);
61 LevelTime::update(float elapsed_time)
65 int prev_time = (int) floor(time_left*5);
66 time_left -= elapsed_time;
68 if(time_left <= -5 || !Sector::current()->player->get_coins())
70 Sector::current()->player->kill(true);
73 if(prev_time != (int) floor(time_left*5))
75 Sector::current()->player->add_coins(-1);
81 LevelTime::draw(DrawingContext& context)
83 context.push_transform();
84 context.set_translation(Vector(0, 0));
86 if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
89 std::string time_text = ss.str();
93 float all_width = time_surface->get_width() + Resources::normal_font->get_text_width(time_text);
94 context.draw_surface(time_surface, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
95 context.draw_text(Resources::normal_font, time_text,
96 Vector((SCREEN_WIDTH - all_width)/2 + time_surface->get_width(), BORDER_Y),
97 ALIGN_LEFT, LAYER_FOREGROUND1, LevelTime::text_color);
101 context.pop_transform();
117 LevelTime::get_time()
123 LevelTime::set_time(float time_left)
125 this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);