Skip to content

Commit

Permalink
Add Actor::SetInterval, Actor::SetTimeout and ActorFrame::SetUpdateFu…
Browse files Browse the repository at this point in the history
…nctionInterval
  • Loading branch information
nico-abram committed Nov 26, 2018
1 parent 869469a commit 6dece16
Show file tree
Hide file tree
Showing 8 changed files with 3,174 additions and 37 deletions.
94 changes: 94 additions & 0 deletions src/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
#include "ThemeManager.h"
#include "XmlFile.h"
#include <typeinfo>
#include <list>
#include <tuple>
#include "FilterManager.h"
#include "LuaReference.h"

static Preference<bool> g_bShowMasks("ShowMasks", false);
static const float default_effect_period = 1.0f;
Expand Down Expand Up @@ -946,6 +949,29 @@ Actor::UpdateInternal(float delta_time)
delta_time = m_fEffectDelta;
}
this->UpdateTweening(delta_time);

for (auto it = delayedFunctions.begin(); it != delayedFunctions.end();
++it) {
auto& delayedF = *it;
delayedF.second -= delta_time;
if (delayedF.second <= 0) {
delayedF.first();
}
}
// Doing this in place did weird things
std::remove_if(delayedFunctions.begin(),
delayedFunctions.end(),
[](auto& x) { return x.second <= 0; });
for (auto it = this->delayedPeriodicFunctions.begin();
it != this->delayedPeriodicFunctions.end();
++it) {
auto& delayedF = *it;
std::get<1>(delayedF) -= delta_time;
if (std::get<1>(delayedF) <= 0) {
std::get<0>(delayedF)();
std::get<1>(delayedF) = std::get<2>(delayedF);
}
}
}

RString
Expand Down Expand Up @@ -1662,6 +1688,20 @@ Actor::TweenInfo::operator=(const TweenInfo& rhs)
return *this;
}

void
Actor::SetTimeout(function<void()> f, float ms)
{
delayedFunctions.emplace_back(make_pair(f, ms));
return;
}

void
Actor::SetInterval(function<void()> f, float ms, int id)
{
delayedPeriodicFunctions.emplace_back(make_tuple(f, ms, ms, id));
return;
}

// lua start
#include "LuaBinding.h"

Expand All @@ -1674,6 +1714,57 @@ class LunaActor : public Luna<Actor>
p->SetName(SArg(1));
COMMON_RETURN_SELF;
}
static int setTimeout(T* p, lua_State* L)
{
auto f = GetFuncArg(1, L);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
f.PushSelf(L);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetTimeout(execF, FArg(2));
COMMON_RETURN_SELF;
}
static int setInterval(T* p, lua_State* L)
{
lua_pushvalue(L, 1);
auto f = luaL_ref(L, LUA_REGISTRYINDEX);
std::function<void()> execF = [f]() {
Lua* L = LUA->Get();
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if (!lua_isnil(L, -1)) {
RString Error =
"Error running RequestChartLeaderBoard Finish Function: ";
LuaHelpers::RunScriptOnStack(
L, Error, 0, 0, true); // 1 args, 0 results
}
LUA->Release(L);
};
p->SetInterval(execF, FArg(2), f);
lua_pushnumber(L, f);
return 1;
}
static int clearInterval(T* p, lua_State* L)
{
int r = IArg(1);
auto& l = p->delayedPeriodicFunctions;
auto it = find_if(
l.begin(), l.end(), [r](auto& x) { return std::get<3>() == r; });
if (it != l.end()) {
luaL_unref(L, LUA_REGISTRYINDEX, r);
l.erase(it);
} else {
LuaHelpers::ReportScriptError(
"Interval function not found (When triying to clearInterval() )");
}
return 0;
}
static int sleep(T* p, lua_State* L)
{
float fTime = FArg(1);
Expand Down Expand Up @@ -2686,6 +2777,9 @@ class LunaActor : public Luna<Actor>
DEFINE_METHOD(IsVisible, IsVisible());
LunaActor()
{
ADD_METHOD(name);
ADD_METHOD(setInterval);
ADD_METHOD(setTimeout);
ADD_METHOD(name);
ADD_METHOD(sleep);
ADD_METHOD(linear);
Expand Down
Loading

0 comments on commit 6dece16

Please sign in to comment.