src/object/level_time.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 /* EOF */

Generated on Mon Jun 9 03:38:19 2014 for SuperTux by  doxygen 1.5.1