mirror of
https://github.com/TombEngine/TombEngine.git
synced 2025-05-08 11:38:08 +03:00
Begin to split GameLogicScript. First up is Entity.
This commit is contained in:
parent
6cc329f43f
commit
8bcc41ef77
13 changed files with 196 additions and 117 deletions
|
@ -41,6 +41,7 @@
|
|||
<ClInclude Include="include\ReservedScriptNames.h" />
|
||||
<ClInclude Include="include\ScriptAssert.h" />
|
||||
<ClInclude Include="include\Scripting\AudioTracks.h" />
|
||||
<ClInclude Include="include\Scripting\Entity\ScriptInterfaceEntity.h" />
|
||||
<ClInclude Include="include\Scripting\GameFlowScript.h" />
|
||||
<ClInclude Include="include\Scripting\GameLogicScript.h" />
|
||||
<ClInclude Include="include\Scripting\GameScriptAIObject.h" />
|
||||
|
@ -77,6 +78,7 @@
|
|||
<ClInclude Include="include\ScriptInterfaceLevel.h" />
|
||||
<ClInclude Include="include\ScriptUtil.h" />
|
||||
<ClInclude Include="src\AudioTracks.h" />
|
||||
<ClInclude Include="src\Entity\Entity.h" />
|
||||
<ClInclude Include="src\Entity\Static\GameScriptMeshInfo.h" />
|
||||
<ClInclude Include="src\GameFlowScript.h" />
|
||||
<ClInclude Include="src\GameLogicScript.h" />
|
||||
|
@ -113,6 +115,7 @@
|
|||
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">frameworkandsol.h</PrecompiledHeaderFile>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Entity\Entity.cpp" />
|
||||
<ClCompile Include="src\Entity\Static\GameScriptMeshInfo.cpp" />
|
||||
<ClCompile Include="src\GameFlowScript.cpp" />
|
||||
<ClCompile Include="src\ScriptInterfaceState.cpp" />
|
||||
|
|
|
@ -276,6 +276,12 @@
|
|||
<ClInclude Include="src\Entity\Static\GameScriptMeshInfo.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Entity\Entity.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\Scripting\Entity\ScriptInterfaceEntity.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="frameworkandsol.cpp">
|
||||
|
@ -350,6 +356,9 @@
|
|||
<ClCompile Include="src\Entity\Static\GameScriptMeshInfo.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Entity\Entity.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
|
25
Scripting/include/Scripting/Entity/ScriptInterfaceEntity.h
Normal file
25
Scripting/include/Scripting/Entity/ScriptInterfaceEntity.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include "Specific/level.h"
|
||||
|
||||
typedef DWORD D3DCOLOR;
|
||||
using VarMapVal = std::variant< short,
|
||||
std::reference_wrapper<MESH_INFO>,
|
||||
std::reference_wrapper<LEVEL_CAMERA_INFO>,
|
||||
std::reference_wrapper<SINK_INFO>,
|
||||
std::reference_wrapper<SOUND_SOURCE_INFO>,
|
||||
std::reference_wrapper<AI_OBJECT>>;
|
||||
|
||||
using CallbackDrawString = std::function<void(std::string const&, D3DCOLOR, int, int, int)>;
|
||||
|
||||
class ScriptInterfaceEntity {
|
||||
public:
|
||||
virtual ~ScriptInterfaceEntity() = default;
|
||||
|
||||
virtual bool AddName(std::string const& key, VarMapVal val) = 0;
|
||||
virtual void FreeEntities() = 0;
|
||||
virtual void AssignLara() = 0;
|
||||
};
|
||||
|
||||
extern ScriptInterfaceEntity* g_GameScriptEntities;
|
|
@ -29,11 +29,9 @@ public:
|
|||
|
||||
virtual void SetCallbackDrawString(CallbackDrawString) = 0;
|
||||
virtual void FreeLevelScripts() = 0;
|
||||
virtual bool AddName(std::string const& key, VarMapVal val) = 0;
|
||||
virtual void ExecuteScriptFile(std::string const& luaFileName) = 0;
|
||||
virtual void ExecuteFunction(std::string const& luaFileName) = 0;
|
||||
|
||||
virtual void AssignItemsAndLara() = 0;
|
||||
};
|
||||
|
||||
extern ScriptInterfaceGame* g_GameScript;
|
|
@ -1,12 +1,14 @@
|
|||
#pragma once
|
||||
#include "ScriptInterfaceGame.h"
|
||||
#include "ScriptInterfaceFlow.h"
|
||||
#include "Entity/ScriptInterfaceEntity.h"
|
||||
|
||||
class ScriptInterfaceState
|
||||
{
|
||||
public:
|
||||
static ScriptInterfaceGame* CreateGame();
|
||||
static ScriptInterfaceFlow* CreateFlow();
|
||||
static ScriptInterfaceEntity* CreateEntities();
|
||||
static void ScriptInterfaceState::Init();
|
||||
};
|
||||
|
||||
|
|
90
Scripting/src/Entity/Entity.cpp
Normal file
90
Scripting/src/Entity/Entity.cpp
Normal file
|
@ -0,0 +1,90 @@
|
|||
#include "frameworkandsol.h"
|
||||
#include "ReservedScriptNames.h"
|
||||
#include "Lara/lara.h"
|
||||
#include "Entity.h"
|
||||
|
||||
GameEntities::GameEntities(sol::state* lua) : LuaHandler{ lua }
|
||||
{
|
||||
/***
|
||||
Get an ItemInfo by its name.
|
||||
@function GetItemByName
|
||||
@tparam string name the unique name of the item as set in, or generated by, Tomb Editor
|
||||
@treturn ItemInfo a non-owning ItemInfo referencing the item.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetItemByName, &GameEntities::GetByName<GameScriptItemInfo, ScriptReserved_ItemInfo>, this);
|
||||
|
||||
/***
|
||||
Get a MeshInfo by its name.
|
||||
@function GetMeshByName
|
||||
@tparam string name the unique name of the mesh as set in, or generated by, Tomb Editor
|
||||
@treturn MeshInfo a non-owning MeshInfo referencing the mesh.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetMeshByName, &GameEntities::GetByName<GameScriptMeshInfo, ScriptReserved_MeshInfo>, this);
|
||||
|
||||
/***
|
||||
Get a CameraInfo by its name.
|
||||
@function GetCameraByName
|
||||
@tparam string name the unique name of the camera as set in, or generated by, Tomb Editor
|
||||
@treturn CameraInfo a non-owning CameraInfo referencing the camera.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetCameraByName, &GameEntities::GetByName<GameScriptCameraInfo, ScriptReserved_CameraInfo>, this);
|
||||
|
||||
/***
|
||||
Get a SinkInfo by its name.
|
||||
@function GetSinkByName
|
||||
@tparam string name the unique name of the sink as set in, or generated by, Tomb Editor
|
||||
@treturn SinkInfo a non-owning SinkInfo referencing the sink.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetSinkByName, &GameEntities::GetByName<GameScriptSinkInfo, ScriptReserved_SinkInfo>, this);
|
||||
|
||||
/***
|
||||
Get a SoundSourceInfo by its name.
|
||||
@function GetSoundSourceByName
|
||||
@tparam string name the unique name of the sink as set in, or generated by, Tomb Editor
|
||||
@treturn SoundSourceInfo a non-owning SoundSourceInfo referencing the sink.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetSoundSourceByName, &GameEntities::GetByName<GameScriptSoundSourceInfo, ScriptReserved_SoundSourceInfo>, this);
|
||||
|
||||
GameScriptItemInfo::Register(m_lua);
|
||||
GameScriptItemInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptMeshInfo::Register(m_lua);
|
||||
GameScriptMeshInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptCameraInfo::Register(m_lua);
|
||||
GameScriptCameraInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptSinkInfo::Register(m_lua);
|
||||
GameScriptSinkInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptAIObject::Register(m_lua);
|
||||
GameScriptAIObject::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptSoundSourceInfo::Register(m_lua);
|
||||
GameScriptSoundSourceInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
void GameEntities::AssignLara()
|
||||
{
|
||||
m_lua->set("Lara", GameScriptItemInfo(Lara.itemNumber, false));
|
||||
}
|
||||
|
49
Scripting/src/Entity/Entity.h
Normal file
49
Scripting/src/Entity/Entity.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#pragma once
|
||||
#include <unordered_map>
|
||||
#include "LuaHandler.h"
|
||||
#include "Scripting/Entity/ScriptInterfaceEntity.h"
|
||||
#include "GameScriptItemInfo.h"
|
||||
#include "Entity/Static/GameScriptMeshInfo.h"
|
||||
#include "GameScriptSinkInfo.h"
|
||||
#include "GameScriptAIObject.h"
|
||||
#include "GameScriptSoundSourceInfo.h"
|
||||
#include "GameScriptCameraInfo.h"
|
||||
|
||||
class GameEntities : public ScriptInterfaceEntity, public LuaHandler
|
||||
{
|
||||
|
||||
public:
|
||||
GameEntities::GameEntities(sol::state* lua);
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, VarMapVal> m_nameMap{};
|
||||
std::unordered_map<std::string, short> m_itemsMapName{};
|
||||
|
||||
|
||||
void AssignLara() override;
|
||||
|
||||
template <typename R, char const* S>
|
||||
std::unique_ptr<R> GetByName(std::string const& name)
|
||||
{
|
||||
ScriptAssertF(m_nameMap.find(name) != m_nameMap.end(), "{} name not found: {}", S, name);
|
||||
return std::make_unique<R>(std::get<R::IdentifierType>(m_nameMap.at(name)), false);
|
||||
}
|
||||
|
||||
bool AddName(std::string const& key, VarMapVal val) override
|
||||
{
|
||||
auto p = std::pair<std::string const&, VarMapVal>{ key, val };
|
||||
return m_nameMap.insert(p).second;
|
||||
}
|
||||
|
||||
bool RemoveName(std::string const& key)
|
||||
{
|
||||
return m_nameMap.erase(key);
|
||||
}
|
||||
|
||||
void FreeEntities() override
|
||||
{
|
||||
m_nameMap.clear();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -18,6 +18,7 @@ using std::unordered_map;
|
|||
|
||||
ScriptInterfaceFlow* g_GameFlow;
|
||||
ScriptInterfaceGame* g_GameScript;
|
||||
ScriptInterfaceEntity* g_GameScriptEntities;
|
||||
|
||||
GameFlow::GameFlow(sol::state* lua) : LuaHandler{ lua }
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "GameScriptAnimations.h"
|
||||
#include "ScriptInterfaceGame.h"
|
||||
#include "ScriptInterfaceFlow.h"
|
||||
#include "Entity/Entity.h"
|
||||
|
||||
class GameFlow : public LuaHandler, public ScriptInterfaceFlow
|
||||
{
|
||||
|
|
|
@ -46,45 +46,6 @@ static void MakeSpecialTable(sol::state * state, std::string const & name, funcI
|
|||
|
||||
GameScript::GameScript(sol::state* lua) : LuaHandler{ lua }
|
||||
{
|
||||
/***
|
||||
Get an ItemInfo by its name.
|
||||
@function GetItemByName
|
||||
@tparam string name the unique name of the item as set in, or generated by, Tomb Editor
|
||||
@treturn ItemInfo a non-owning ItemInfo referencing the item.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetItemByName, &GameScript::GetByName<GameScriptItemInfo, ScriptReserved_ItemInfo>, this);
|
||||
|
||||
/***
|
||||
Get a MeshInfo by its name.
|
||||
@function GetMeshByName
|
||||
@tparam string name the unique name of the mesh as set in, or generated by, Tomb Editor
|
||||
@treturn MeshInfo a non-owning MeshInfo referencing the mesh.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetMeshByName, &GameScript::GetByName<GameScriptMeshInfo, ScriptReserved_MeshInfo>, this);
|
||||
|
||||
/***
|
||||
Get a CameraInfo by its name.
|
||||
@function GetCameraByName
|
||||
@tparam string name the unique name of the camera as set in, or generated by, Tomb Editor
|
||||
@treturn CameraInfo a non-owning CameraInfo referencing the camera.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetCameraByName, &GameScript::GetByName<GameScriptCameraInfo, ScriptReserved_CameraInfo>, this);
|
||||
|
||||
/***
|
||||
Get a SinkInfo by its name.
|
||||
@function GetSinkByName
|
||||
@tparam string name the unique name of the sink as set in, or generated by, Tomb Editor
|
||||
@treturn SinkInfo a non-owning SinkInfo referencing the sink.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetSinkByName, &GameScript::GetByName<GameScriptSinkInfo, ScriptReserved_SinkInfo>, this);
|
||||
|
||||
/***
|
||||
Get a SoundSourceInfo by its name.
|
||||
@function GetSoundSourceByName
|
||||
@tparam string name the unique name of the sink as set in, or generated by, Tomb Editor
|
||||
@treturn SoundSourceInfo a non-owning SoundSourceInfo referencing the sink.
|
||||
*/
|
||||
m_lua->set_function(ScriptReserved_GetSoundSourceByName, &GameScript::GetByName<GameScriptSoundSourceInfo, ScriptReserved_SoundSourceInfo>, this);
|
||||
|
||||
/***
|
||||
Show some text on-screen.
|
||||
|
@ -113,42 +74,7 @@ with a call to @{ShowString}, or this function will have no effect.
|
|||
|
||||
MakeSpecialTable(m_lua, ScriptReserved_GameVars, &LuaVariables::GetVariable, &LuaVariables::SetVariable, &m_globals);
|
||||
|
||||
GameScriptItemInfo::Register(m_lua);
|
||||
GameScriptItemInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptMeshInfo::Register(m_lua);
|
||||
GameScriptMeshInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptCameraInfo::Register(m_lua);
|
||||
GameScriptCameraInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptSinkInfo::Register(m_lua);
|
||||
GameScriptSinkInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptAIObject::Register(m_lua);
|
||||
GameScriptAIObject::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
GameScriptSoundSourceInfo::Register(m_lua);
|
||||
GameScriptSoundSourceInfo::SetNameCallbacks(
|
||||
[this](auto && ... param) { return AddName(std::forward<decltype(param)>(param)...); },
|
||||
[this](auto && ... param) { return RemoveName(std::forward<decltype(param)>(param)...); }
|
||||
);
|
||||
|
||||
|
||||
GameScriptDisplayString::Register(m_lua);
|
||||
GameScriptDisplayString::SetCallbacks(
|
||||
[this](auto && ... param) {return SetDisplayString(std::forward<decltype(param)>(param)...); },
|
||||
|
@ -230,7 +156,6 @@ void GameScript::SetCallbackDrawString(CallbackDrawString cb)
|
|||
|
||||
void GameScript::FreeLevelScripts()
|
||||
{
|
||||
m_nameMap.clear();
|
||||
m_levelFuncs.clear();
|
||||
m_locals = LuaVariables{};
|
||||
ResetLevelTables();
|
||||
|
@ -343,10 +268,6 @@ std::unique_ptr<R> GetByName(std::string const & type, std::string const & name,
|
|||
return std::make_unique<R>(map.at(name), false);
|
||||
}
|
||||
|
||||
void GameScript::AssignItemsAndLara()
|
||||
{
|
||||
m_lua->set("Lara", GameScriptItemInfo(Lara.itemNumber, false));
|
||||
}
|
||||
|
||||
/*** Special objects
|
||||
@section specialobjects
|
||||
|
|
|
@ -8,12 +8,6 @@
|
|||
#include "GameScriptColor.h"
|
||||
#include "GameScriptPosition.h"
|
||||
#include "GameScriptRotation.h"
|
||||
#include "GameScriptItemInfo.h"
|
||||
#include "Entity/Static/GameScriptMeshInfo.h"
|
||||
#include "GameScriptSinkInfo.h"
|
||||
#include "GameScriptAIObject.h"
|
||||
#include "GameScriptSoundSourceInfo.h"
|
||||
#include "GameScriptCameraInfo.h"
|
||||
#include "GameScriptDisplayString.h"
|
||||
|
||||
struct LuaFunction {
|
||||
|
@ -57,8 +51,6 @@ private:
|
|||
LuaVariables m_globals{};
|
||||
LuaVariables m_locals{};
|
||||
DisplayStringMap m_userDisplayStrings{};
|
||||
std::unordered_map<std::string, VarMapVal> m_nameMap{};
|
||||
std::unordered_map<std::string, short> m_itemsMapName{};
|
||||
std::unordered_map<std::string, sol::protected_function> m_levelFuncs{};
|
||||
sol::protected_function m_onStart{};
|
||||
sol::protected_function m_onLoad{};
|
||||
|
@ -82,30 +74,10 @@ std::optional<std::reference_wrapper<UserDisplayString>> GetDisplayString(Displa
|
|||
bool SetLevelFunc(sol::table tab, std::string const& luaName, sol::object obj);
|
||||
sol::protected_function GetLevelFunc(sol::table tab, std::string const& luaName);
|
||||
|
||||
void AssignItemsAndLara() override;
|
||||
|
||||
|
||||
void ExecuteScriptFile(const std::string& luaFilename) override;
|
||||
void ExecuteFunction(std::string const & name) override;
|
||||
void MakeItemInvisible(short id);
|
||||
|
||||
template <typename R, char const* S>
|
||||
std::unique_ptr<R> GetByName(std::string const& name)
|
||||
{
|
||||
ScriptAssertF(m_nameMap.find(name) != m_nameMap.end(), "{} name not found: {}", S, name);
|
||||
return std::make_unique<R>(std::get<R::IdentifierType>(m_nameMap.at(name)), false);
|
||||
}
|
||||
|
||||
bool AddName(std::string const& key, VarMapVal val) override
|
||||
{
|
||||
auto p = std::pair<std::string const&, VarMapVal>{ key, val };
|
||||
return m_nameMap.insert(p).second;
|
||||
}
|
||||
|
||||
bool RemoveName(std::string const& key)
|
||||
{
|
||||
return m_nameMap.erase(key);
|
||||
}
|
||||
|
||||
// Variables
|
||||
template <typename T>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "ScriptInterfaceState.h"
|
||||
#include "GameLogicScript.h"
|
||||
#include "GameFlowScript.h"
|
||||
#include "Entity/Entity.h"
|
||||
|
||||
sol::state g_solState;
|
||||
|
||||
|
@ -20,8 +21,14 @@ ScriptInterfaceFlow* ScriptInterfaceState::CreateFlow()
|
|||
return new GameFlow(&g_solState);
|
||||
}
|
||||
|
||||
ScriptInterfaceEntity* CreateEntities()
|
||||
{
|
||||
return new GameEntities(&g_solState);
|
||||
}
|
||||
|
||||
void ScriptInterfaceState::Init()
|
||||
{
|
||||
g_solState.open_libraries(sol::lib::base, sol::lib::math);
|
||||
g_solState.set_exception_handler(lua_exception_handler);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue