4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
22 #include "level_time.hpp"
27 #include "resources.hpp"
29 #include "gettext.hpp"
30 #include "object_factory.hpp"
31 #include "object/player.hpp"
32 #include "video/drawing_context.hpp"
33 #include "lisp/list_iterator.hpp"
36 /** When to alert player they're low on time! */
37 static const float TIME_WARNING = 20;
39 LevelTime::LevelTime(const lisp::Lisp& reader)
40 : final_level_time(0.f), final_remaining_time(0.f)
43 lisp::ListIterator iter(&reader);
45 if(iter.item() == "time") {
46 iter.value()->get(time);
49 log_warning << "Unknown token '" << iter.item() << "' in LevelTime object" << std::endl;
53 throw std::runtime_error("Invalid leveltime specified");
54 time_left.start(time);
57 LevelTime::~LevelTime()
61 LevelTime::update(float )
63 if(time_left.check()) {
64 Sector::current()->player->kill(false);
69 LevelTime::draw(DrawingContext& context)
71 context.push_transform();
72 context.set_translation(Vector(0, 0));
76 if(get_remaining_time() < 0) {
77 context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, BORDER_Y),
78 CENTER_ALLIGN, LAYER_FOREGROUND1);
79 } else if (get_remaining_time() > TIME_WARNING
80 || int(game_time * 2.5) % 2) {
81 snprintf(str, sizeof(str), " %d", int(get_remaining_time()));
82 context.draw_text(white_text, _("TIME"),
83 Vector(SCREEN_WIDTH/2, BORDER_Y), CENTER_ALLIGN, LAYER_FOREGROUND1);
84 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + 4*16, BORDER_Y),
85 CENTER_ALLIGN, LAYER_FOREGROUND1);
88 context.pop_transform();
92 LevelTime::get_level_time()
94 if (!time_left.started())
95 return final_level_time;
96 return time_left.get_period();
100 LevelTime::get_remaining_time()
102 if (!time_left.started())
103 return final_remaining_time;
104 return time_left.get_timeleft();
110 final_level_time = time_left.get_period();
111 final_remaining_time = time_left.get_timeleft();
115 IMPLEMENT_FACTORY(LevelTime, "leveltime");