3 #include "level_time.hpp"
8 #include "resources.hpp"
10 #include "gettext.hpp"
11 #include "object_factory.hpp"
12 #include "object/player.hpp"
13 #include "video/drawing_context.hpp"
14 #include "lisp/list_iterator.hpp"
17 /** When to alert player they're low on time! */
18 static const float TIME_WARNING = 20;
20 LevelTime::LevelTime(const lisp::Lisp& reader)
21 : final_level_time(0.f), final_remaining_time(0.f)
24 lisp::ListIterator iter(&reader);
26 if(iter.item() == "time") {
27 iter.value()->get(time);
30 log_warning << "Unknown token '" << iter.item() << "' in LevelTime object" << std::endl;
34 throw std::runtime_error("Invalid leveltime specified");
35 time_left.start(time);
38 LevelTime::~LevelTime()
42 LevelTime::update(float )
44 if(time_left.check()) {
45 Sector::current()->player->kill(Player::KILL);
50 LevelTime::draw(DrawingContext& context)
52 context.push_transform();
53 context.set_translation(Vector(0, 0));
57 if(get_remaining_time() < 0) {
58 context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, BORDER_Y),
59 CENTER_ALLIGN, LAYER_FOREGROUND1);
60 } else if (get_remaining_time() > TIME_WARNING
61 || int(game_time * 2.5) % 2) {
62 snprintf(str, sizeof(str), " %d", int(get_remaining_time()));
63 context.draw_text(white_text, _("TIME"),
64 Vector(SCREEN_WIDTH/2, BORDER_Y), CENTER_ALLIGN, LAYER_FOREGROUND1);
65 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + 4*16, BORDER_Y),
66 CENTER_ALLIGN, LAYER_FOREGROUND1);
69 context.pop_transform();
73 LevelTime::get_level_time()
75 if (!time_left.started())
76 return final_level_time;
77 return time_left.get_period();
81 LevelTime::get_remaining_time()
83 if (!time_left.started())
84 return final_remaining_time;
85 return time_left.get_timeleft();
91 final_level_time = time_left.get_period();
92 final_remaining_time = time_left.get_timeleft();
96 IMPLEMENT_FACTORY(LevelTime, "leveltime");