Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factorize CAnimations::LoadUnitAnim/CAnimations::LoadWaitUnitAnim. #617

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions src/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ static const CAnimation *Advance(const CAnimation *anim, int n)
return anim;
}


/* static */ void CAnimations::LoadUnitAnim(lua_State *l, CUnit &unit, int luaIndex)
static void LoadUnitUnitAnim(lua_State *l, int luaIndex, CUnit::_unit_anim_ &anim)
{
if (!lua_istable(l, luaIndex)) {
LuaError(l, "incorrect argument");
Expand All @@ -487,48 +486,31 @@ static const CAnimation *Advance(const CAnimation *anim, int n)
++j;

if (value == "anim-wait") {
unit.Anim.Wait = LuaToNumber(l, luaIndex, j + 1);
anim.Wait = LuaToNumber(l, luaIndex, j + 1);
} else if (value == "curr-anim") {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.Anim.CurrAnim = AnimationsArray[animIndex];
anim.CurrAnim = AnimationsArray[animIndex];
} else if (value == "anim") {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.Anim.Anim = Advance(unit.Anim.CurrAnim, animIndex);
anim.Anim = Advance(anim.CurrAnim, animIndex);
} else if (value == "unbreakable") {
unit.Anim.Unbreakable = true;
anim.Unbreakable = true;
--j;
} else {
LuaError(l, "Unit anim-data: Unsupported tag: %s", value.data());
}
}
}

/* static */ void CAnimations::LoadWaitUnitAnim(lua_State *l, CUnit &unit, int luaIndex)
{
if (!lua_istable(l, luaIndex)) {
LuaError(l, "incorrect argument");
}
const int nargs = lua_rawlen(l, luaIndex);

for (int j = 0; j != nargs; ++j) {
const std::string_view value = LuaToString(l, luaIndex, j + 1);
++j;
/* static */ void CAnimations::LoadUnitAnim(lua_State *l, CUnit &unit, int luaIndex)
{
LoadUnitUnitAnim(l, luaIndex, unit.Anim);
}

if (value == "anim-wait") {
unit.WaitBackup.Wait = LuaToNumber(l, luaIndex, j + 1);
} else if (value == "curr-anim") {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.WaitBackup.CurrAnim = AnimationsArray[animIndex];
} else if (value == "anim") {
const int animIndex = LuaToNumber(l, luaIndex, j + 1);
unit.WaitBackup.Anim = Advance(unit.WaitBackup.CurrAnim, animIndex);
} else if (value == "unbreakable") {
unit.WaitBackup.Unbreakable = true;
--j;
} else {
LuaError(l, "Unit anim-data: Unsupported tag: %s", value.data());
}
}
/* static */ void CAnimations::LoadWaitUnitAnim(lua_State *l, CUnit &unit, int luaIndex)
{
LoadUnitUnitAnim(l, luaIndex, unit.WaitBackup);
}

/**
Expand Down
Loading