7c39d89abcc596090cb3af65241cd91c7498a738
[supertux.git] / src / object / level_time.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/level_time.hpp"
18
19 #include "object/player.hpp"
20 #include "scripting/level_time.hpp"
21 #include "scripting/squirrel_util.hpp"
22 #include "supertux/globals.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/resources.hpp"
25 #include "supertux/sector.hpp"
26 #include "util/reader.hpp"
27 #include "video/drawing_context.hpp"
28
29 #include <math.h>
30
31 /** When to alert player they're low on time! */
32 static const float TIME_WARNING = 20;
33
34 LevelTime::LevelTime(const Reader& reader) :
35   time_surface(),
36   running(true), 
37   time_left(0)
38 {
39   reader.get("name", name);
40   reader.get("time", time_left);
41   if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
42   time_surface.reset(new Surface("images/engine/hud/time-0.png"));
43 }
44
45 void
46 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
47 {
48   if (name.empty()) return;
49   Scripting::LevelTime* interface = new Scripting::LevelTime(this);
50   expose_object(vm, table_idx, interface, name, true);
51 }
52
53 void
54 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
55 {
56   if (name.empty()) return;
57   Scripting::unexpose_object(vm, table_idx, name);
58 }
59
60 void
61 LevelTime::update(float elapsed_time)
62 {
63   if (!running) return;
64
65   int prev_time = (int) floor(time_left*5);
66   time_left -= elapsed_time;
67   if(time_left <= 0) {
68     if(time_left <= -5 || !Sector::current()->player->get_coins())
69     {
70       Sector::current()->player->kill(true);
71       stop();
72     }
73     if(prev_time != (int) floor(time_left*5))
74     {
75       Sector::current()->player->add_coins(-1);
76     }
77   }
78 }
79
80 void
81 LevelTime::draw(DrawingContext& context)
82 {
83   context.push_transform();
84   context.set_translation(Vector(0, 0));
85
86   if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
87     std::stringstream ss;
88     ss << int(time_left);
89     std::string time_text = ss.str();
90
91     Surface* time_surf = time_surface.get();
92     if (time_surf) {
93       float all_width = time_surf->get_width() + Resources::normal_font->get_text_width(time_text);
94       context.draw_surface(time_surf, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
95       context.draw_text(Resources::normal_font, time_text, Vector((SCREEN_WIDTH - all_width)/2 + time_surf->get_width(), BORDER_Y), ALIGN_LEFT, LAYER_FOREGROUND1, LevelTime::text_color);
96     }
97   }
98
99   context.pop_transform();
100 }
101
102 void
103 LevelTime::start()
104 {
105   running = true;
106 }
107
108 void
109 LevelTime::stop()
110 {
111   running = false;
112 }
113
114 float
115 LevelTime::get_time()
116 {
117   return time_left;
118 }
119
120 void
121 LevelTime::set_time(float time_left)
122 {
123   this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
124 }
125
126 IMPLEMENT_FACTORY(LevelTime, "leveltime");
127
128 /* EOF */