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/main.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) :
38 reader.get("name", name);
39 reader.get("time", time_left);
40 if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
41 time_surface.reset(new Surface("images/engine/hud/time-0.png"));
45 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
47 if (name.empty()) return;
48 Scripting::LevelTime* interface = new Scripting::LevelTime(this);
49 expose_object(vm, table_idx, interface, name, true);
53 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
55 if (name.empty()) return;
56 Scripting::unexpose_object(vm, table_idx, name);
60 LevelTime::update(float elapsed_time)
64 int prev_time = (int) floor(time_left*5);
65 time_left -= elapsed_time;
67 if(time_left <= -5 || !Sector::current()->player->get_coins())
69 Sector::current()->player->kill(true);
72 if(prev_time != (int) floor(time_left*5))
74 Sector::current()->player->add_coins(-1);
80 LevelTime::draw(DrawingContext& context)
82 context.push_transform();
83 context.set_translation(Vector(0, 0));
85 if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
88 std::string time_text = ss.str();
90 Surface* time_surf = time_surface.get();
92 float all_width = time_surf->get_width() + normal_font->get_text_width(time_text);
93 context.draw_surface(time_surf, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
94 context.draw_text(normal_font, time_text, Vector((SCREEN_WIDTH - all_width)/2 + time_surf->get_width(), BORDER_Y), ALIGN_LEFT, LAYER_FOREGROUND1, LevelTime::text_color);
98 context.pop_transform();
114 LevelTime::get_time()
120 LevelTime::set_time(float time_left)
122 this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
125 IMPLEMENT_FACTORY(LevelTime, "leveltime");