summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cwd/assets/altcraft/scripts/init.lua17
-rw-r--r--src/AssetManager.cpp7
-rw-r--r--src/Game.cpp13
-rw-r--r--src/Plugin.cpp75
-rw-r--r--src/Plugin.hpp4
5 files changed, 99 insertions, 17 deletions
diff --git a/cwd/assets/altcraft/scripts/init.lua b/cwd/assets/altcraft/scripts/init.lua
index 5fd9e11..2137464 100644
--- a/cwd/assets/altcraft/scripts/init.lua
+++ b/cwd/assets/altcraft/scripts/init.lua
@@ -4,6 +4,7 @@ local plugin = {
onLoad = nil,
onUnload = nil,
onChangeState = nil,
+ onTick = nil,
}
function plugin.onLoad ()
@@ -11,7 +12,19 @@ function plugin.onLoad ()
end
function plugin.onChangeState (newState)
- AC:LogWarning("New state: "..newState)
+ AC.LogWarning("New state: "..newState)
end
-AC:RegisterPlugin(plugin) \ No newline at end of file
+function plugin.onUnload ()
+ AC.LogInfo("AC Core unloaded")
+end
+
+function plugin.onTick (deltaTime)
+ if AC.GetGameState() and AC.GetGameState():GetPlayer() then
+ local player = AC.GetGameState():GetPlayer()
+ player.pos.x = player.pos.x + deltaTime * 0.5
+ end
+end
+
+AC.RegisterPlugin(plugin)
+plugin = nil \ No newline at end of file
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index ba2b4f4..eb3186a 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -115,7 +115,12 @@ void LoadScripts() {
LOG(ERROR) << "Unrecognised script file /" << it->name;
continue;
}
- PluginSystem::Execute(asset->code);
+ try {
+ PluginSystem::Execute(asset->code, true);
+ }
+ catch (std::exception& e) {
+ LOG(ERROR) << "Failed loading script '" << script->name << "' in '" << it->name << "'";
+ }
}
}
}
diff --git a/src/Game.cpp b/src/Game.cpp
index d67072e..c69ff5d 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -7,6 +7,7 @@
#include "Render.hpp"
#include "GameState.hpp"
#include "NetworkClient.hpp"
+#include "Plugin.hpp"
bool isRunning = true;
bool isMoving[5] = { 0,0,0,0,0 };
@@ -75,6 +76,12 @@ void InitEvents() {
listener.RegisterHandler("SendChatMessage", [](const Event& eventData) {
auto message = eventData.get<std::string>();
+ if (message[0] == '!' && message[1] != '!') {
+ PluginSystem::Execute(message.substr(1));
+ return;
+ }
+ if (message[0] == '!')
+ message = message.substr(1);
auto packet = std::static_pointer_cast<Packet>(std::make_shared<PacketChatMessageSB>(message));
PUSH_EVENT("SendPacket",packet);
});
@@ -182,8 +189,9 @@ void RunGame() {
SetState(State::MainMenu);
while (isRunning) {
- OPTICK_FRAME("MainThread");
+ OPTICK_FRAME("MainThread");
listener.HandleAllEvents();
+ PluginSystem::CallOnTick(timer->GetRealDeltaS());
if (gs) {
if (GetState() == State::Playing) {
if (isMoving[GameState::FORWARD])
@@ -196,8 +204,7 @@ void RunGame() {
gs->HandleMovement(GameState::RIGHT, timer->GetRealDeltaS());
if (isMoving[GameState::JUMP])
gs->HandleMovement(GameState::JUMP, timer->GetRealDeltaS());
- }
-
+ }
gs->Update(timer->GetRealDeltaS());
}
render->Update();
diff --git a/src/Plugin.cpp b/src/Plugin.cpp
index 41c690c..9641e0f 100644
--- a/src/Plugin.cpp
+++ b/src/Plugin.cpp
@@ -4,6 +4,11 @@
#include <easylogging++.h>
#include <sol.hpp>
+#include <optick.h>
+
+#include "GameState.hpp"
+#include "Game.hpp"
+
struct Plugin {
const std::string name;
@@ -11,6 +16,7 @@ struct Plugin {
const std::function<void()> onLoad;
const std::function<void()> onUnload;
const std::function<void(std::string)> onChangeState;
+ const std::function<void(double)> onTick;
};
@@ -20,13 +26,14 @@ sol::state lua;
namespace PluginApi {
- void RegisterPlugin(sol::table self, sol::table plugin) {
+ void RegisterPlugin(sol::table plugin) {
Plugin nativePlugin {
- plugin["name"].get_or<std::string>("75"),
+ plugin["name"].get_or<std::string>(""),
plugin["displayName"].get_or<std::string>(""),
plugin["onLoad"].get_or(std::function<void()>()),
plugin["onUnload"].get_or(std::function<void()>()),
- plugin["onChangeState"].get_or(std::function<void(std::string)>())
+ plugin["onChangeState"].get_or(std::function<void(std::string)>()),
+ plugin["onTick"].get_or(std::function<void(double)>())
};
plugins.push_back(nativePlugin);
nativePlugin.onLoad();
@@ -34,14 +41,25 @@ namespace PluginApi {
LOG(INFO) << "Loaded plugin " << (!nativePlugin.displayName.empty() ? nativePlugin.displayName : nativePlugin.name);
}
- void LogWarning(sol::table self, std::string text) {
+ void LogWarning(std::string text) {
LOG(WARNING) << text;
}
+ void LogInfo(std::string text) {
+ LOG(INFO) << text;
+ }
+
+ void LogError(std::string text) {
+ LOG(ERROR) << text;
+ }
+
+ GameState *GetGameState() {
+ return ::GetGameState();
+ }
}
-void PluginSystem::Init()
-{
+void PluginSystem::Init() {
+ OPTICK_EVENT();
LOG(INFO) << "Initializing plugin system";
for (Plugin &plugin : plugins) {
if (plugin.onUnload)
@@ -52,23 +70,47 @@ void PluginSystem::Init()
lua = sol::state();
lua.open_libraries();
+ lua.new_usertype<Entity>("Entity",
+ "pos", &Entity::pos);
+
+ lua.new_usertype<GameState>("GameState",
+ "GetPlayer", &GameState::GetPlayer,
+ "GetWorld", &GameState::GetWorld);
+
+ lua.new_usertype<World>("World");
+
+ lua.new_usertype<Vector>("Vector",
+ "x", &Vector::x,
+ "y", &Vector::y,
+ "z", &Vector::z);
+
+ lua.new_usertype<VectorF>("VectorF",
+ "x", &VectorF::x,
+ "y", &VectorF::y,
+ "z", &VectorF::z);
+
sol::table apiTable = lua["AC"].get_or_create<sol::table>();
apiTable["RegisterPlugin"] = PluginApi::RegisterPlugin;
apiTable["LogWarning"] = PluginApi::LogWarning;
+ apiTable["LogInfo"] = PluginApi::LogInfo;
+ apiTable["LogError"] = PluginApi::LogError;
+ apiTable["GetGameState"] = PluginApi::GetGameState;
}
-void PluginSystem::Execute(const std::string &luaCode)
-{
+void PluginSystem::Execute(const std::string &luaCode, bool except) {
+ OPTICK_EVENT();
try {
lua.safe_script(luaCode);
} catch (sol::error &e) {
LOG(ERROR) << e.what();
+ if (except)
+ throw;
}
}
-void PluginSystem::CallOnChangeState(std::string newState)
-{
+void PluginSystem::CallOnChangeState(std::string newState) {
+ OPTICK_EVENT();
for (Plugin &plugin : plugins) {
if (plugin.onChangeState)
try {
@@ -79,3 +121,16 @@ void PluginSystem::CallOnChangeState(std::string newState)
}
}
}
+
+void PluginSystem::CallOnTick(double deltaTime) {
+ OPTICK_EVENT();
+ for (Plugin& plugin : plugins) {
+ if (plugin.onTick)
+ try {
+ plugin.onTick(deltaTime);
+ }
+ catch (sol::error &e) {
+ LOG(ERROR) << e.what();
+ }
+ }
+}
diff --git a/src/Plugin.hpp b/src/Plugin.hpp
index a5f75e1..3b61011 100644
--- a/src/Plugin.hpp
+++ b/src/Plugin.hpp
@@ -5,7 +5,9 @@
namespace PluginSystem {
void Init();
- void Execute(const std::string &luaCode);
+ void Execute(const std::string &luaCode, bool except = false);
void CallOnChangeState(std::string newState);
+
+ void CallOnTick(double deltaTime);
} \ No newline at end of file