fix miniswig using wrong stack numbers in functions with HSQUIRRELVM arguments
[supertux.git] / src / object / level_time.cpp
1 #include <config.h>
2
3 #include "level_time.hpp"
4
5 #include <stdexcept>
6 #include "main.hpp"
7 #include "resources.hpp"
8 #include "sector.hpp"
9 #include "gettext.hpp"
10 #include "object_factory.hpp"
11 #include "object/player.hpp"
12 #include "video/drawing_context.hpp"
13 #include "lisp/list_iterator.hpp"
14
15 /** When to alert player they're low on time! */
16 static const float TIME_WARNING = 20;
17
18 LevelTime::LevelTime(const lisp::Lisp& reader)
19 {
20     float time = -1;
21     lisp::ListIterator iter(&reader);
22     while(iter.next()) {
23         if(iter.item() == "time") {
24             iter.value()->get(time);
25             break;
26         } else {
27             std::cerr << "Unknown token '" << iter.item() 
28                       << "' in LevelTime object.\n";
29         }
30     }
31     if(time < 0)
32       throw std::runtime_error("Invalid leveltime specified");
33     time_left.start(time);
34 }
35
36 LevelTime::~LevelTime()
37 {}
38
39 void
40 LevelTime::update(float )
41 {
42   if(time_left.check()) {
43     Sector::current()->player->kill(Player::KILL);
44   }
45 }
46
47 void
48 LevelTime::draw(DrawingContext& context)
49 {
50   context.push_transform();
51   context.set_translation(Vector(0, 0));
52
53   char str[60];
54     
55   if(time_left.get_timeleft() < 0) {
56     context.draw_text(white_text, _("TIME's UP"), Vector(SCREEN_WIDTH/2, 0),
57         CENTER_ALLIGN, LAYER_FOREGROUND1);
58   } else if (time_left.get_timeleft() > TIME_WARNING
59       || int(game_time * 2.5) % 2) {
60     snprintf(str, sizeof(str), " %d", int(time_left.get_timeleft()));
61     context.draw_text(white_text, _("TIME"),
62         Vector(SCREEN_WIDTH/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
63     context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2 + 4*16, 0),
64                       CENTER_ALLIGN, LAYER_FOREGROUND1);
65   }
66
67   context.pop_transform();
68 }
69
70 IMPLEMENT_FACTORY(LevelTime, "leveltime");