LevelTime Class Reference

#include <level_time.hpp>

Inherits GameObject, and ScriptInterface.

List of all members.

Public Member Functions

 LevelTime (const Reader &reader)
virtual void expose (HSQUIRRELVM vm, SQInteger table_idx)
virtual void unexpose (HSQUIRRELVM vm, SQInteger table_idx)
void update (float elapsed_time)
 This function is called once per frame and allows the object to update it's state.
void draw (DrawingContext &context)
 The GameObject should draw itself onto the provided DrawingContext if this function is called.
Scriptable Methods
void start ()
 Resumes the countdown.
void stop ()
 Pauses the countdown.
float get_time ()
 Returns the number of seconds left on the clock.
void set_time (float time_left)
 Changes the number of seconds left on the clock.

Private Attributes

SurfacePtr time_surface
bool running
float time_left

Static Private Attributes

static Color text_color


Detailed Description

Definition at line 28 of file level_time.hpp.


Constructor & Destructor Documentation

LevelTime::LevelTime ( const Reader reader  ) 

Definition at line 34 of file level_time.cpp.

References Surface::create(), lisp::Lisp::get(), GameObject::name, time_left, and time_surface.

00034                                          :
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 }


Member Function Documentation

void LevelTime::expose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 46 of file level_time.cpp.

References scripting::expose_object(), and GameObject::name.

00047 {
00048   if (name.empty()) return;
00049   scripting::LevelTime* _this = new scripting::LevelTime(this);
00050   expose_object(vm, table_idx, _this, name, true);
00051 }

void LevelTime::unexpose ( HSQUIRRELVM  vm,
SQInteger  table_idx 
) [virtual]

Implements ScriptInterface.

Definition at line 54 of file level_time.cpp.

References GameObject::name, and scripting::unexpose_object().

00055 {
00056   if (name.empty()) return;
00057   scripting::unexpose_object(vm, table_idx, name);
00058 }

void LevelTime::update ( float  elapsed_time  )  [virtual]

This function is called once per frame and allows the object to update it's state.

The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)

Implements GameObject.

Definition at line 61 of file level_time.cpp.

References Player::add_coins(), Sector::current(), Player::kill(), Sector::player, running, stop(), and time_left.

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 }

void LevelTime::draw ( DrawingContext context  )  [virtual]

The GameObject should draw itself onto the provided DrawingContext if this function is called.

Implements GameObject.

Definition at line 81 of file level_time.cpp.

References ALIGN_LEFT, BORDER_Y, DrawingContext::draw_surface(), DrawingContext::draw_text(), game_time, LAYER_FOREGROUND1, Resources::normal_font, DrawingContext::pop_transform(), DrawingContext::push_transform(), SCREEN_WIDTH, DrawingContext::set_translation(), text_color, time_left, time_surface, and TIME_WARNING.

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 }

void LevelTime::start (  ) 

Resumes the countdown.

Definition at line 105 of file level_time.cpp.

References running.

Referenced by scripting::LevelTime::start().

00106 {
00107   running = true;
00108 }

void LevelTime::stop (  ) 

Pauses the countdown.

Definition at line 111 of file level_time.cpp.

References running.

Referenced by GameSession::start_sequence(), scripting::LevelTime::stop(), and update().

00112 {
00113   running = false;
00114 }

float LevelTime::get_time (  ) 

Returns the number of seconds left on the clock.

Definition at line 117 of file level_time.cpp.

References time_left.

Referenced by scripting::LevelTime::get_time().

00118 {
00119   return time_left;
00120 }

void LevelTime::set_time ( float  time_left  ) 

Changes the number of seconds left on the clock.

Definition at line 123 of file level_time.cpp.

Referenced by scripting::LevelTime::set_time().

00124 {
00125   this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
00126 }


Member Data Documentation

Color LevelTime::text_color [static, private]

Definition at line 31 of file level_time.hpp.

Referenced by draw().

SurfacePtr LevelTime::time_surface [private]

Definition at line 71 of file level_time.hpp.

Referenced by draw(), and LevelTime().

bool LevelTime::running [private]

Definition at line 72 of file level_time.hpp.

Referenced by start(), stop(), and update().

float LevelTime::time_left [private]

Definition at line 73 of file level_time.hpp.

Referenced by draw(), get_time(), LevelTime(), and update().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:32 2014 for SuperTux by  doxygen 1.5.1