Unified Messaging Subsystem
[supertux.git] / src / object / level_time.cpp
1 #include <config.h>
2
3 #include "level_time.hpp"
4
5 #include <stdexcept>
6 #include <iostream>
7 #include "main.hpp"
8 #include "resources.hpp"
9 #include "sector.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"
15 #include "msg.hpp"
16
17 /** When to alert player they're low on time! */
18 static const float TIME_WARNING = 20;
19
20 LevelTime::LevelTime(const lisp::Lisp& reader)
21 : final_level_time(0.f), final_remaining_time(0.f)
22 {
23     float time = -1;
24     lisp::ListIterator iter(&reader);
25     while(iter.next()) {
26         if(iter.item() == "time") {
27             iter.value()->get(time);
28             break;
29         } else {
30             msg_warning("Unknown token '" << iter.item() 
31                       << "' in LevelTime object");
32         }
33     }
34     if(time < 0)
35       throw std::runtime_error("Invalid leveltime specified");
36     time_left.start(time);
37 }
38
39 LevelTime::~LevelTime()
40 {}
41
42 void
43 LevelTime::update(float )
44 {
45   if(time_left.check()) {
46     Sector::current()->player->kill(Player::KILL);
47   }
48 }
49
50 void
51 LevelTime::draw(DrawingContext& context)
52 {
53   context.push_transform();
54   context.set_translation(Vector(0, 0));
55
56   char str[60];
57     
58   if(get_remaining_time() < 0) {
59     context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, BORDER_Y),
60         CENTER_ALLIGN, LAYER_FOREGROUND1);
61   } else if (get_remaining_time() > TIME_WARNING
62       || int(game_time * 2.5) % 2) {
63     snprintf(str, sizeof(str), " %d", int(get_remaining_time()));
64     context.draw_text(white_text, _("TIME"),
65         Vector(SCREEN_WIDTH/2, BORDER_Y), CENTER_ALLIGN, LAYER_FOREGROUND1);
66     context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + 4*16, BORDER_Y),
67                       CENTER_ALLIGN, LAYER_FOREGROUND1);
68   }
69
70   context.pop_transform();
71 }
72
73 float
74 LevelTime::get_level_time()
75 {
76   if (!time_left.started())
77     return final_level_time;
78   return time_left.get_period();
79 }
80
81 float
82 LevelTime::get_remaining_time()
83 {
84   if (!time_left.started())
85     return final_remaining_time;
86   return time_left.get_timeleft();
87 }
88
89 void
90 LevelTime::stop()
91 {
92   final_level_time = time_left.get_period();
93   final_remaining_time = time_left.get_timeleft();
94   time_left.stop();
95 }
96
97 IMPLEMENT_FACTORY(LevelTime, "leveltime");