- Reworked Surface class and drawing stuff:
[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
16 /** When to alert player they're low on time! */
17 static const float TIME_WARNING = 20;
18
19 LevelTime::LevelTime(const lisp::Lisp& reader)
20 {
21     float time = -1;
22     lisp::ListIterator iter(&reader);
23     while(iter.next()) {
24         if(iter.item() == "time") {
25             iter.value()->get(time);
26             break;
27         } else {
28             std::cerr << "Unknown token '" << iter.item() 
29                       << "' in LevelTime object.\n";
30         }
31     }
32     if(time < 0)
33       throw std::runtime_error("Invalid leveltime specified");
34     time_left.start(time);
35 }
36
37 LevelTime::~LevelTime()
38 {}
39
40 void
41 LevelTime::update(float )
42 {
43   if(time_left.check()) {
44     Sector::current()->player->kill(Player::KILL);
45   }
46 }
47
48 void
49 LevelTime::draw(DrawingContext& context)
50 {
51   context.push_transform();
52   context.set_translation(Vector(0, 0));
53
54   char str[60];
55     
56   if(time_left.get_timeleft() < 0) {
57     context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, 0),
58         CENTER_ALLIGN, LAYER_FOREGROUND1);
59   } else if (time_left.get_timeleft() > TIME_WARNING
60       || int(game_time * 2.5) % 2) {
61     snprintf(str, sizeof(str), " %d", int(time_left.get_timeleft()));
62     context.draw_text(white_text, _("TIME"),
63         Vector(SCREEN_WIDTH/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
64     context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + 4*16, 0),
65                       CENTER_ALLIGN, LAYER_FOREGROUND1);
66   }
67
68   context.pop_transform();
69 }
70
71 IMPLEMENT_FACTORY(LevelTime, "leveltime");