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