00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "object/level_time.hpp"
00018
00019 #include "object/player.hpp"
00020 #include "scripting/level_time.hpp"
00021 #include "scripting/squirrel_util.hpp"
00022 #include "supertux/globals.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/resources.hpp"
00025 #include "supertux/sector.hpp"
00026 #include "util/reader.hpp"
00027 #include "video/drawing_context.hpp"
00028
00029 #include <math.h>
00030
00032 static const float TIME_WARNING = 20;
00033
00034 LevelTime::LevelTime(const Reader& reader) :
00035 time_surface(),
00036 running(true),
00037 time_left(0)
00038 {
00039 reader.get("name", name);
00040 reader.get("time", time_left);
00041 if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
00042 time_surface = Surface::create("images/engine/hud/time-0.png");
00043 }
00044
00045 void
00046 LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
00047 {
00048 if (name.empty()) return;
00049 scripting::LevelTime* _this = new scripting::LevelTime(this);
00050 expose_object(vm, table_idx, _this, name, true);
00051 }
00052
00053 void
00054 LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00055 {
00056 if (name.empty()) return;
00057 scripting::unexpose_object(vm, table_idx, name);
00058 }
00059
00060 void
00061 LevelTime::update(float elapsed_time)
00062 {
00063 if (!running) return;
00064
00065 int prev_time = (int) floor(time_left*5);
00066 time_left -= elapsed_time;
00067 if(time_left <= 0) {
00068 if(time_left <= -5 || !Sector::current()->player->get_coins())
00069 {
00070 Sector::current()->player->kill(true);
00071 stop();
00072 }
00073 if(prev_time != (int) floor(time_left*5))
00074 {
00075 Sector::current()->player->add_coins(-1);
00076 }
00077 }
00078 }
00079
00080 void
00081 LevelTime::draw(DrawingContext& context)
00082 {
00083 context.push_transform();
00084 context.set_translation(Vector(0, 0));
00085
00086 if ((time_left > TIME_WARNING) || (int(game_time * 2.5) % 2)) {
00087 std::stringstream ss;
00088 ss << int(time_left);
00089 std::string time_text = ss.str();
00090
00091 if (time_surface)
00092 {
00093 float all_width = time_surface->get_width() + Resources::normal_font->get_text_width(time_text);
00094 context.draw_surface(time_surface, Vector((SCREEN_WIDTH - all_width)/2, BORDER_Y + 1), LAYER_FOREGROUND1);
00095 context.draw_text(Resources::normal_font, time_text,
00096 Vector((SCREEN_WIDTH - all_width)/2 + time_surface->get_width(), BORDER_Y),
00097 ALIGN_LEFT, LAYER_FOREGROUND1, LevelTime::text_color);
00098 }
00099 }
00100
00101 context.pop_transform();
00102 }
00103
00104 void
00105 LevelTime::start()
00106 {
00107 running = true;
00108 }
00109
00110 void
00111 LevelTime::stop()
00112 {
00113 running = false;
00114 }
00115
00116 float
00117 LevelTime::get_time()
00118 {
00119 return time_left;
00120 }
00121
00122 void
00123 LevelTime::set_time(float time_left)
00124 {
00125 this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
00126 }
00127
00128