1f1034b7ce25c8ad275ce2b3bb5271ce8c81cca0
[supertux.git] / src / object / level_time.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 "level_time.hpp"
23
24 #include <stdexcept>
25 #include <iostream>
26 #include "main.hpp"
27 #include "resources.hpp"
28 #include "sector.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"
34 #include "log.hpp"
35
36 /** When to alert player they're low on time! */
37 static const float TIME_WARNING = 20;
38
39 LevelTime::LevelTime(const lisp::Lisp& reader)
40 : final_level_time(0.f), final_remaining_time(0.f)
41 {
42     float time = -1;
43     lisp::ListIterator iter(&reader);
44     while(iter.next()) {
45         if(iter.item() == "time") {
46             iter.value()->get(time);
47             break;
48         } else {
49             log_warning << "Unknown token '" << iter.item() << "' in LevelTime object" << std::endl;
50         }
51     }
52     if(time < 0)
53       throw std::runtime_error("Invalid leveltime specified");
54     time_left.start(time);
55 }
56
57 LevelTime::~LevelTime()
58 {}
59
60 void
61 LevelTime::update(float )
62 {
63   if(time_left.check()) {
64     Sector::current()->player->kill(false);
65   }
66 }
67
68 void
69 LevelTime::draw(DrawingContext& context)
70 {
71   context.push_transform();
72   context.set_translation(Vector(0, 0));
73
74   char str[60];
75
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);
86   }
87
88   context.pop_transform();
89 }
90
91 float
92 LevelTime::get_level_time()
93 {
94   if (!time_left.started())
95     return final_level_time;
96   return time_left.get_period();
97 }
98
99 float
100 LevelTime::get_remaining_time()
101 {
102   if (!time_left.started())
103     return final_remaining_time;
104   return time_left.get_timeleft();
105 }
106
107 void
108 LevelTime::stop()
109 {
110   final_level_time = time_left.get_period();
111   final_remaining_time = time_left.get_timeleft();
112   time_left.stop();
113 }
114
115 IMPLEMENT_FACTORY(LevelTime, "leveltime");