#include "video/drawing_context.hpp"
#include "lisp/list_iterator.hpp"
#include "log.hpp"
+#include "scripting/level_time.hpp"
+#include "scripting/squirrel_util.hpp"
/** When to alert player they're low on time! */
static const float TIME_WARNING = 20;
LevelTime::LevelTime(const lisp::Lisp& reader)
: running(true), time_left(0)
{
+ reader.get("name", name);
reader.get("time", time_left);
if(time_left <= 0) throw std::runtime_error("No or invalid leveltime specified");
time_surface.reset(new Surface("images/engine/hud/time-0.png"));
}
void
+LevelTime::expose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+ if (name.empty()) return;
+ Scripting::LevelTime* interface = new Scripting::LevelTime(this);
+ expose_object(vm, table_idx, interface, name, true);
+}
+
+void
+LevelTime::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
+{
+ if (name.empty()) return;
+ Scripting::unexpose_object(vm, table_idx, name);
+}
+
+void
LevelTime::update(float elapsed_time)
{
if (!running) return;
}
void
+LevelTime::start()
+{
+ running = true;
+}
+
+void
LevelTime::stop()
{
running = false;
}
+
+float
+LevelTime::get_time()
+{
+ return time_left;
+}
+
+void
+LevelTime::set_time(float time_left)
+{
+ this->time_left = std::min(std::max(time_left, 0.0f), 999.0f);
+}
IMPLEMENT_FACTORY(LevelTime, "leveltime");
}
+static SQInteger LevelTime_release_hook(SQUserPointer ptr, SQInteger )
+{
+ Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (ptr);
+ delete _this;
+ return 0;
+}
+
+static SQInteger LevelTime_start_wrapper(HSQUIRRELVM vm)
+{
+ SQUserPointer data;
+ if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+ sq_throwerror(vm, _SC("'start' called without instance"));
+ return SQ_ERROR;
+ }
+ Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (data);
+
+ try {
+ _this->start();
+
+ return 0;
+
+ } catch(std::exception& e) {
+ sq_throwerror(vm, e.what());
+ return SQ_ERROR;
+ } catch(...) {
+ sq_throwerror(vm, _SC("Unexpected exception while executing function 'start'"));
+ return SQ_ERROR;
+ }
+
+}
+
+static SQInteger LevelTime_stop_wrapper(HSQUIRRELVM vm)
+{
+ SQUserPointer data;
+ if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+ sq_throwerror(vm, _SC("'stop' called without instance"));
+ return SQ_ERROR;
+ }
+ Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (data);
+
+ try {
+ _this->stop();
+
+ return 0;
+
+ } catch(std::exception& e) {
+ sq_throwerror(vm, e.what());
+ return SQ_ERROR;
+ } catch(...) {
+ sq_throwerror(vm, _SC("Unexpected exception while executing function 'stop'"));
+ return SQ_ERROR;
+ }
+
+}
+
+static SQInteger LevelTime_get_time_wrapper(HSQUIRRELVM vm)
+{
+ SQUserPointer data;
+ if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+ sq_throwerror(vm, _SC("'get_time' called without instance"));
+ return SQ_ERROR;
+ }
+ Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (data);
+
+ try {
+ float return_value = _this->get_time();
+
+ sq_pushfloat(vm, return_value);
+ return 1;
+
+ } catch(std::exception& e) {
+ sq_throwerror(vm, e.what());
+ return SQ_ERROR;
+ } catch(...) {
+ sq_throwerror(vm, _SC("Unexpected exception while executing function 'get_time'"));
+ return SQ_ERROR;
+ }
+
+}
+
+static SQInteger LevelTime_set_time_wrapper(HSQUIRRELVM vm)
+{
+ SQUserPointer data;
+ if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {
+ sq_throwerror(vm, _SC("'set_time' called without instance"));
+ return SQ_ERROR;
+ }
+ Scripting::LevelTime* _this = reinterpret_cast<Scripting::LevelTime*> (data);
+ SQFloat arg0;
+ if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) {
+ sq_throwerror(vm, _SC("Argument 1 not a float"));
+ return SQ_ERROR;
+ }
+
+ try {
+ _this->set_time(static_cast<float> (arg0));
+
+ return 0;
+
+ } catch(std::exception& e) {
+ sq_throwerror(vm, e.what());
+ return SQ_ERROR;
+ } catch(...) {
+ sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_time'"));
+ return SQ_ERROR;
+ }
+
+}
+
static SQInteger display_wrapper(HSQUIRRELVM vm)
{
return Scripting::display(vm);
sq_remove(v, -2); // remove root table
}
+void create_squirrel_instance(HSQUIRRELVM v, Scripting::LevelTime* object, bool setup_releasehook)
+{
+ using namespace Wrapper;
+
+ sq_pushroottable(v);
+ sq_pushstring(v, "LevelTime", -1);
+ if(SQ_FAILED(sq_get(v, -2))) {
+ std::ostringstream msg;
+ msg << "Couldn't resolved squirrel type 'LevelTime'";
+ throw SquirrelError(v, msg.str());
+ }
+
+ if(SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, object))) {
+ std::ostringstream msg;
+ msg << "Couldn't setup squirrel instance for object of type 'LevelTime'";
+ throw SquirrelError(v, msg.str());
+ }
+ sq_remove(v, -2); // remove object name
+
+ if(setup_releasehook) {
+ sq_setreleasehook(v, -1, LevelTime_release_hook);
+ }
+
+ sq_remove(v, -2); // remove root table
+}
+
void register_supertux_wrapper(HSQUIRRELVM v)
{
using namespace Wrapper;
throw SquirrelError(v, "Couldn't register class 'SSector'");
}
+ // Register class LevelTime
+ sq_pushstring(v, "LevelTime", -1);
+ if(sq_newclass(v, SQFalse) < 0) {
+ std::ostringstream msg;
+ msg << "Couldn't create new class 'LevelTime'";
+ throw SquirrelError(v, msg.str());
+ }
+ sq_pushstring(v, "start", -1);
+ sq_newclosure(v, &LevelTime_start_wrapper, 0);
+ if(SQ_FAILED(sq_createslot(v, -3))) {
+ throw SquirrelError(v, "Couldn't register function 'start'");
+ }
+
+ sq_pushstring(v, "stop", -1);
+ sq_newclosure(v, &LevelTime_stop_wrapper, 0);
+ if(SQ_FAILED(sq_createslot(v, -3))) {
+ throw SquirrelError(v, "Couldn't register function 'stop'");
+ }
+
+ sq_pushstring(v, "get_time", -1);
+ sq_newclosure(v, &LevelTime_get_time_wrapper, 0);
+ if(SQ_FAILED(sq_createslot(v, -3))) {
+ throw SquirrelError(v, "Couldn't register function 'get_time'");
+ }
+
+ sq_pushstring(v, "set_time", -1);
+ sq_newclosure(v, &LevelTime_set_time_wrapper, 0);
+ if(SQ_FAILED(sq_createslot(v, -3))) {
+ throw SquirrelError(v, "Couldn't register function 'set_time'");
+ }
+
+ if(SQ_FAILED(sq_createslot(v, -3))) {
+ throw SquirrelError(v, "Couldn't register class 'LevelTime'");
+ }
+
}
} // end of namespace Scripting