5ac1ba2cd40fe0caf8c29bf848eeb8f10d1f9a53
[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 <sstream>
27 #include "main.hpp"
28 #include "resources.hpp"
29 #include "sector.hpp"
30 #include "gettext.hpp"
31 #include "object_factory.hpp"
32 #include "object/player.hpp"
33 #include "video/drawing_context.hpp"
34 #include "lisp/list_iterator.hpp"
35 #include "log.hpp"
36 #include "scripting/level_time.hpp"
37 #include "scripting/squirrel_util.hpp"
38
39 /** When to alert player they're low on time! */
40 static const float TIME_WARNING = 20;
41
42 LevelTime::LevelTime(const lisp::Lisp& reader)
43 : running(true), time_left(0)
44 {
45   reader.get("name", name);
46   reader.get("time", time_left);
47   if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
48   time_surface.reset(new Surface("images/engine/hud/time-0.png"));
49 }
50
51 void
52 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
53 {
54   if (name.empty()) return;
55   Scripting::LevelTime* interface = new Scripting::LevelTime(this);
56   expose_object(vm, table_idx, interface, name, true);
57 }
58
59 void
60 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
61 {
62   if (name.empty()) return;
63   Scripting::unexpose_object(vm, table_idx, name);
64 }
65
66 void
67 LevelTime::update(float elapsed_time)
68 {
69   if (!running) return;
70
71   int prev_time = (int) floor(time_left*5);
72   time_left -= elapsed_time;
73   if(time_left <= 0) {
74     if(time_left <= -5 || !Sector::current()->player->get_coins())
75     {
76       Sector::current()->player->kill(true);
77       stop();
78     }
79     if(prev_time != (int) floor(time_left*5))
80     {
81       Sector::current()->player->add_coins(-1);
82     }
83   }
84 }
85
86 void
87 LevelTime::draw(DrawingContext& context)
88 {
89   context.push_transform();
90   context.set_translation(Vector(0, 0));
91
92   if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
93     std::stringstream ss;
94     ss << int(time_left);
95     std::string time_text = ss.str();
96
97     Surface* time_surf = time_surface.get();
98     if (time_surf) {
99       float all_width = time_surf->get_width() + white_text->get_text_width(time_text);
100       context.draw_surface(time_surf, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
101       context.draw_text(gold_text, time_text, Vector((SCREEN_WIDTH - all_width)/2 + time_surf->get_width(), BORDER_Y), LEFT_ALLIGN, LAYER_FOREGROUND1);
102     }
103   }
104
105   context.pop_transform();
106 }
107
108 void
109 LevelTime::start()
110 {
111   running = true;
112 }
113
114 void
115 LevelTime::stop()
116 {
117   running = false;
118 }
119
120 float
121 LevelTime::get_time()
122 {
123   return time_left;
124 }
125
126 void
127 LevelTime::set_time(float time_left)
128 {
129   this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
130 }
131
132 IMPLEMENT_FACTORY(LevelTime, "leveltime");