Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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/main.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   running(true), 
36   time_left(0)
37 {
38   reader.get("name", name);
39   reader.get("time", time_left);
40   if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
41   time_surface.reset(new Surface("images/engine/hud/time-0.png"));
42 }
43
44 void
45 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
46 {
47   if (name.empty()) return;
48   Scripting::LevelTime* interface = new Scripting::LevelTime(this);
49   expose_object(vm, table_idx, interface, name, true);
50 }
51
52 void
53 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
54 {
55   if (name.empty()) return;
56   Scripting::unexpose_object(vm, table_idx, name);
57 }
58
59 void
60 LevelTime::update(float elapsed_time)
61 {
62   if (!running) return;
63
64   int prev_time = (int) floor(time_left*5);
65   time_left -= elapsed_time;
66   if(time_left <= 0) {
67     if(time_left <= -5 || !Sector::current()->player->get_coins())
68     {
69       Sector::current()->player->kill(true);
70       stop();
71     }
72     if(prev_time != (int) floor(time_left*5))
73     {
74       Sector::current()->player->add_coins(-1);
75     }
76   }
77 }
78
79 void
80 LevelTime::draw(DrawingContext& context)
81 {
82   context.push_transform();
83   context.set_translation(Vector(0, 0));
84
85   if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
86     std::stringstream ss;
87     ss << int(time_left);
88     std::string time_text = ss.str();
89
90     Surface* time_surf = time_surface.get();
91     if (time_surf) {
92       float all_width = time_surf->get_width() + normal_font->get_text_width(time_text);
93       context.draw_surface(time_surf, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
94       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);
95     }
96   }
97
98   context.pop_transform();
99 }
100
101 void
102 LevelTime::start()
103 {
104   running = true;
105 }
106
107 void
108 LevelTime::stop()
109 {
110   running = false;
111 }
112
113 float
114 LevelTime::get_time()
115 {
116   return time_left;
117 }
118
119 void
120 LevelTime::set_time(float time_left)
121 {
122   this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
123 }
124
125 IMPLEMENT_FACTORY(LevelTime, "leveltime");
126
127 /* EOF */