From d57ac17b696b43f735455d057d525f85cdf49126 Mon Sep 17 00:00:00 2001 From: ugozapad Date: Thu, 5 Mar 2026 05:05:04 +0300 Subject: [PATCH] LuaPlus extenssion --- data/scripts/actors/actor_base.lua | 16 +++ data/scripts/actors/actor_player.lua | 20 +--- data/scripts/game_init.lua | 6 + data/scripts/weapons/weapon_base.lua | 109 ++++++++++++++++++ data/scripts/weapons/weapon_key.lua | 1 + src/game/game.cpp | 5 + src/game/game_lua_help.cpp | 10 ++ src/game/game_object.cpp | 2 +- thirdparty/luaplus/Src/LuaPlus/LuaFunction.h | 60 +++++----- .../luaplus/Src/LuaPlus/LuaPlusInternal.h | 4 + 10 files changed, 183 insertions(+), 50 deletions(-) create mode 100644 data/scripts/actors/actor_base.lua create mode 100644 data/scripts/weapons/weapon_base.lua create mode 100644 data/scripts/weapons/weapon_key.lua diff --git a/data/scripts/actors/actor_base.lua b/data/scripts/actors/actor_base.lua new file mode 100644 index 0000000..1e946f6 --- /dev/null +++ b/data/scripts/actors/actor_base.lua @@ -0,0 +1,16 @@ +-- базовый класс актора +actor_base = inherit_table(game_object) + +-- инициализация FSM + +function actor_base:on_init() + game_object.on_init(self) +end + +function actor_base:on_shutdown() + game_object.on_shutdown(self) +end + +function actor_base:on_update(dt) + game_object.on_update(self, dt) +end diff --git a/data/scripts/actors/actor_player.lua b/data/scripts/actors/actor_player.lua index 9ea59a2..94bfb9b 100644 --- a/data/scripts/actors/actor_player.lua +++ b/data/scripts/actors/actor_player.lua @@ -1,21 +1,3 @@ --- базовый класс актора -actor_base = inherit_table(game_object) - --- инициализация FSM - - -function actor_base:on_init() - game_object.on_init(self) -end - -function actor_base:on_shutdown() - game_object.on_shutdown(self) -end - -function actor_base:on_update(dt) - game_object.on_update(self, dt) -end - -- игрок actor_player = inherit_table(actor_base) @@ -40,5 +22,5 @@ function actor_player:on_update(dt) end function actor_player:on_collide(other) - --console.print(string.format("actor_player:on_collide: %s", other:get_classname())) + console.print(string.format("actor_player:on_collide: %s", other:get_classname())) end \ No newline at end of file diff --git a/data/scripts/game_init.lua b/data/scripts/game_init.lua index abb819c..a78a7b7 100644 --- a/data/scripts/game_init.lua +++ b/data/scripts/game_init.lua @@ -1,9 +1,15 @@ -- Game initialization script +console.print("--- Game initialization ---") + -- загружаем скрипты load_script("game_utils.lua") load_script("game_object.lua") load_script("test_object.lua") + +load_script("weapons/weapon_base.lua") +load_script("actors/actor_base.lua") + load_script("actors/actor_player.lua") -- глобальная таблица сущностей diff --git a/data/scripts/weapons/weapon_base.lua b/data/scripts/weapons/weapon_base.lua new file mode 100644 index 0000000..be38909 --- /dev/null +++ b/data/scripts/weapons/weapon_base.lua @@ -0,0 +1,109 @@ +----------------------------------------------------------- +-- weapon_base.lua, Базоавый скрипт оружия +-- Автор: Кирилл +-- Изменяли: +-- Дата: 05.03.2026 +----------------------------------------------------------- + +-- индификаторы состояний FSM +WEAPON_FSM_STATE_IDLE = 1 +WEAPON_FSM_STATE_ATTACK = 2 +WEAPON_FSM_STATE_ATTACK2 = 3 +WEAPON_FSM_STATE_RELOAD = 4 + +-- базовый класс оружия +weapon_base = inherit_table(game_object) + +-- инициализация FSM +weapon_base.m_fsm = {} + +-- покой +weapon_base.m_fsm[WEAPON_FSM_STATE_IDLE] = { + anim = "idle", -- имя анимации + anim_playback = ANIM_PLAYBACK_REPEAT, -- бесконечно играть + anim_speed = 1.0 -- обычная скорость анимации +} + +-- атака +weapon_base.m_fsm[WEAPON_FSM_STATE_ATTACK] = { + anim = "attack", -- имя анимации + anim_playback = ANIM_PLAYBACK_NONE, + anim_speed = 1.0 -- обычная скорость анимации +} + +-- перезарядка +weapon_base.m_fsm[WEAPON_FSM_STATE_RELOAD] = { + anim = "reload", -- имя анимации + anim_playback = ANIM_PLAYBACK_NONE, + anim_speed = 1.0 -- обычная скорость анимации +} + +function weapon_base:on_init() + game_object.on_init(self) + + -- начальное состояние FSM + self.m_state = WEAPON_FSM_STATE_IDLE + self.m_next_state = WEAPON_FSM_STATE_IDLE + self.m_state_time = 0.0 + self.m_end_state_time = 0.0 + self.m_damage = 25 +end + +function weapon_base:on_shutdown() + game_object.on_shutdown(self) +end + +function weapon_base:on_update(dt) + game_object.on_update(self, dt) + + self:on_fsm_state_update(dt) +end + +function weapon_base:on_fsm_state_update(dt) + -- обновления времени + self.m_state_time = self.m_state_time + dt + + -- проверка на стрельбу + if (self.m_state == WEAPON_STATE_ATTACK or + self.m_state == WEAPON_STATE_ATTACK2) and + self.m_state_time >= self.m_end_state_time then + + -- переходим в ожидание + self:set_state(WEAPON_STATE_IDLE) + end + + -- костыль, нету анимаций бесконечных + if self.m_state == WEAPON_STATE_IDLE and self.m_state_time >= self.m_end_state_time then + -- переходим в ожидание + self:set_state(WEAPON_STATE_IDLE) + end + + self:fsm_update(dt) +end + +function weapon_base:set_state(next_state) + self.m_next_state = next_state +end + +function weapon_base:fsm_update(dt) + -- надо ли менять состояние + if self.m_next_state ~= self.m_state then + + if self.m_next_state == WEAPON_STATE_ATTACK then + self:set_anim("attack1") + elseif self.m_next_state == WEAPON_STATE_ATTACK2 then + self:set_anim("attack1") + end + + -- сброс времени и установка его на таймер анимации + self.m_state_time = 0.0 + self.m_end_state_time = render.get_anim_time(self:get_anim()) + + -- запускаем атаку + if self.m_next_state == WEAPON_STATE_ATTACK or self.m_next_state == WEAPON_STATE_ATTACK2 then + self:attack() + end + + self.m_state = self.m_next_state + end +end \ No newline at end of file diff --git a/data/scripts/weapons/weapon_key.lua b/data/scripts/weapons/weapon_key.lua new file mode 100644 index 0000000..73ee916 --- /dev/null +++ b/data/scripts/weapons/weapon_key.lua @@ -0,0 +1 @@ +weapon_key = inherit_table(weapon_base) diff --git a/src/game/game.cpp b/src/game/game.cpp index 58db76a..6d0cb52 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -58,6 +58,11 @@ void registerEngine() LuaObject consoleTable = GetLuaState().GetGlobals().CreateTable("console"); consoleTable.RegisterDirect("print", &consoleMsg); + + // animations globals + GetLuaState().DoString("ANIM_PLAYBACK_NONE = 0"); + GetLuaState().DoString("ANIM_PLAYBACK_REPEAT = 1"); + } void registerClasses() diff --git a/src/game/game_lua_help.cpp b/src/game/game_lua_help.cpp index 6d2f135..252a094 100644 --- a/src/game/game_lua_help.cpp +++ b/src/game/game_lua_help.cpp @@ -89,3 +89,13 @@ glm::mat4 getMatrixFromLua(LuaPlus::LuaObject& matrix) glm::mat4 return_matrix = glm::identity(); return return_matrix; } + +void LuaPlusErrorFunction(const char* errorMsg) +{ + Core::Error(errorMsg); +} + +namespace LuaPlus +{ + void (*g_errorFunction)(const char*) = LuaPlusErrorFunction; +} \ No newline at end of file diff --git a/src/game/game_object.cpp b/src/game/game_object.cpp index 6fe00af..7e4b013 100644 --- a/src/game/game_object.cpp +++ b/src/game/game_object.cpp @@ -131,7 +131,7 @@ void Entity::OnCollide(IEntityBase* other) LuaPlus::LuaObject otherTable = lookup.GetByIndex(other->GetID()); if (!otherTable.IsNil()) { - LuaPlus:: LuaFunctionVoid function = m_onCollideFunction; + LuaPlus::LuaFunctionVoid function = m_onCollideFunction; function(m_luaObject, otherTable); } } diff --git a/thirdparty/luaplus/Src/LuaPlus/LuaFunction.h b/thirdparty/luaplus/Src/LuaPlus/LuaFunction.h index 96a1fa4..e2f1220 100644 --- a/thirdparty/luaplus/Src/LuaPlus/LuaFunction.h +++ b/thirdparty/luaplus/Src/LuaPlus/LuaFunction.h @@ -35,8 +35,8 @@ public: functionObj.Push(L); if (lua_pcall(L, 0, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -50,8 +50,8 @@ public: LPCD::Type::Push(L, p1); if (lua_pcall(L, 1, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -66,8 +66,8 @@ public: LPCD::Type::Push(L, p2); if (lua_pcall(L, 2, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -83,8 +83,8 @@ public: LPCD::Type::Push(L, p3); if (lua_pcall(L, 3, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -101,8 +101,8 @@ public: LPCD::Type::Push(L, p4); if (lua_pcall(L, 4, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -120,8 +120,8 @@ public: LPCD::Type::Push(L, p5); if (lua_pcall(L, 5, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -140,8 +140,8 @@ public: LPCD::Type::Push(L, p6); if (lua_pcall(L, 6, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -161,8 +161,8 @@ public: LPCD::Type::Push(L, p7); if (lua_pcall(L, 7, 1, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } return LPCD::Type::Get(L, -1); } @@ -191,8 +191,8 @@ public: functionObj.Push(L); if (lua_pcall(L, 0, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -205,8 +205,8 @@ public: LPCD::Type::Push(L, p1); if (lua_pcall(L, 1, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -220,8 +220,8 @@ public: LPCD::Type::Push(L, p2); if (lua_pcall(L, 2, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -236,8 +236,8 @@ public: LPCD::Type::Push(L, p3); if (lua_pcall(L, 3, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -253,8 +253,8 @@ public: LPCD::Type::Push(L, p4); if (lua_pcall(L, 4, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -271,8 +271,8 @@ public: LPCD::Type::Push(L, p5); if (lua_pcall(L, 5, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } @@ -290,8 +290,8 @@ public: LPCD::Type::Push(L, p6); if (lua_pcall(L, 6, 0, 0)) { - const char* errorString = lua_tostring(L, -1); (void)errorString; - luaplus_assert(0); + const char* errorString = lua_tostring(L, -1); + g_errorFunction(errorString); } } diff --git a/thirdparty/luaplus/Src/LuaPlus/LuaPlusInternal.h b/thirdparty/luaplus/Src/LuaPlus/LuaPlusInternal.h index 3ecf6a7..16fdecf 100644 --- a/thirdparty/luaplus/Src/LuaPlus/LuaPlusInternal.h +++ b/thirdparty/luaplus/Src/LuaPlus/LuaPlusInternal.h @@ -94,4 +94,8 @@ struct LuaArgNil {}; #define LuaState_to_lua_State(state) ((lua_State*)(state)) #define lua_State_to_LuaState(L) ((LuaPlus::LuaState*)L) +namespace LuaPlus { +extern void (*g_errorFunction)(const char* errorMsg); +} + #endif // LUAPLUS__LUAPLUSINTERNAL_H