From 50bf84acf66b2dcfea0f43038c25622adaa465e0 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Mon, 7 Jan 2019 04:56:42 +0100 Subject: [PATCH] [LuaCore] Added. Only two functions to get World and Player. --- TODO | 4 ++++ include/lua/LuaCore.hpp | 39 +++++++++++++++++++++++++++++++++++ include/states/GameState.hpp | 3 +++ mods/test.lua | 40 +++++++++++++++++++----------------- source/lua/LuaCore.cpp | 24 ++++++++++++++++++++++ source/lua/ScriptEngine.cpp | 5 +++++ source/states/GameState.cpp | 5 ++++- 7 files changed, 100 insertions(+), 20 deletions(-) create mode 100644 include/lua/LuaCore.hpp create mode 100644 source/lua/LuaCore.cpp diff --git a/TODO b/TODO index 2c90e1a0..2dca32e9 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,9 @@ TODO +# Issues + +• TODO: `Workbench` is not scaled correctly with lower GUI scale settings + # Code improvements • TODO: If possible, `Cube` should inherit from `ItemWidget` or `Widget` diff --git a/include/lua/LuaCore.hpp b/include/lua/LuaCore.hpp new file mode 100644 index 00000000..316262e8 --- /dev/null +++ b/include/lua/LuaCore.hpp @@ -0,0 +1,39 @@ +/* + * ===================================================================================== + * + * Filename: LuaCore.hpp + * + * Description: + * + * Created: 07/01/2019 04:35:13 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef LUACORE_HPP_ +#define LUACORE_HPP_ + +#include + +#include + +class Player; +class World; + +class LuaCore { + public: + World *world() { return m_world; } + void setWorld(World &world) { m_world = &world; } + + Player *player() { return m_player; } + void setPlayer(Player &player) { m_player = &player; } + + static void initUsertype(sol::state &lua); + + private: + Player *m_player = nullptr; + World *m_world = nullptr; +}; + +#endif // LUACORE_HPP_ diff --git a/include/states/GameState.hpp b/include/states/GameState.hpp index 13543867..227f7b55 100644 --- a/include/states/GameState.hpp +++ b/include/states/GameState.hpp @@ -21,6 +21,7 @@ #include "Config.hpp" #include "HUD.hpp" +#include "LuaCore.hpp" #include "Player.hpp" #include "Skybox.hpp" #include "World.hpp" @@ -49,6 +50,8 @@ class GameState : public gk::ApplicationState { Player m_player{m_camera}; HUD m_hud{m_player, m_world}; + + LuaCore m_luaCore; }; #endif // GAMESTATE_HPP_ diff --git a/mods/test.lua b/mods/test.lua index d4bf8526..ace62d68 100644 --- a/mods/test.lua +++ b/mods/test.lua @@ -12,25 +12,27 @@ dofile("mods/items.lua") dofile("mods/recipes.lua") function init() - player:inventory():add_stack("default:workbench", 1); - player:inventory():add_stack("default:dirt", 64); - player:inventory():add_stack("default:grass", 64); - player:inventory():add_stack("default:stone", 64); - player:inventory():add_stack("default:glass", 64); - player:inventory():add_stack("default:glowstone", 64); - player:inventory():add_stack("default:furnace", 1); - player:inventory():add_stack("default:slab_planks", 64); - player:inventory():add_stack("default:pickaxe_stone", 1); + local player_inv = openminer:player():inventory() - player:inventory():add_stack("default:wood", 64); - player:inventory():add_stack("default:planks", 64); - player:inventory():add_stack("default:cobblestone", 64); - player:inventory():add_stack("default:stick", 64); - player:inventory():add_stack("default:axe_stone", 1); - player:inventory():add_stack("default:hoe_stone", 1); - player:inventory():add_stack("default:pickaxe_stone", 1); - player:inventory():add_stack("default:shovel_stone", 1); - player:inventory():add_stack("default:ore_iron", 64); - player:inventory():add_stack("default:coal", 64); + player_inv:add_stack("default:workbench", 1); + player_inv:add_stack("default:dirt", 64); + player_inv:add_stack("default:grass", 64); + player_inv:add_stack("default:stone", 64); + player_inv:add_stack("default:glass", 64); + player_inv:add_stack("default:glowstone", 64); + player_inv:add_stack("default:furnace", 1); + player_inv:add_stack("default:slab_planks", 64); + player_inv:add_stack("default:pickaxe_stone", 1); + + player_inv:add_stack("default:wood", 64); + player_inv:add_stack("default:planks", 64); + player_inv:add_stack("default:cobblestone", 64); + player_inv:add_stack("default:stick", 64); + player_inv:add_stack("default:axe_stone", 1); + player_inv:add_stack("default:hoe_stone", 1); + player_inv:add_stack("default:pickaxe_stone", 1); + player_inv:add_stack("default:shovel_stone", 1); + player_inv:add_stack("default:ore_iron", 64); + player_inv:add_stack("default:coal", 64); end diff --git a/source/lua/LuaCore.cpp b/source/lua/LuaCore.cpp new file mode 100644 index 00000000..42da07a2 --- /dev/null +++ b/source/lua/LuaCore.cpp @@ -0,0 +1,24 @@ +/* + * ===================================================================================== + * + * Filename: LuaCore.cpp + * + * Description: + * + * Created: 07/01/2019 04:41:11 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#include "LuaCore.hpp" +#include "Player.hpp" +#include "World.hpp" + +void LuaCore::initUsertype(sol::state &lua) { + lua.new_usertype("LuaCore", + "world", &LuaCore::world, + "player", &LuaCore::player + ); +} + diff --git a/source/lua/ScriptEngine.cpp b/source/lua/ScriptEngine.cpp index bd8e37b0..d82dd690 100644 --- a/source/lua/ScriptEngine.cpp +++ b/source/lua/ScriptEngine.cpp @@ -11,11 +11,13 @@ * * ===================================================================================== */ +#include "LuaCore.hpp" #include "LuaGUI.hpp" #include "LuaMod.hpp" #include "Player.hpp" #include "Registry.hpp" #include "ScriptEngine.hpp" +#include "World.hpp" ScriptEngine *ScriptEngine::s_instance = nullptr; @@ -34,6 +36,8 @@ void ScriptEngine::init() { } void ScriptEngine::initUsertypes() { + m_lua.new_usertype("World"); + m_lua.new_usertype("Player", "inventory", &Player::inventory); @@ -47,6 +51,7 @@ void ScriptEngine::initUsertypes() { "z", &glm::ivec3::z ); + LuaCore::initUsertype(m_lua); LuaMod::initUsertype(m_lua); LuaGUI::initUsertype(m_lua); } diff --git a/source/states/GameState.cpp b/source/states/GameState.cpp index 722d5e8e..f0e9bdcc 100644 --- a/source/states/GameState.cpp +++ b/source/states/GameState.cpp @@ -40,9 +40,12 @@ GameState::GameState() { } void GameState::testLuaAPI() { + m_luaCore.setPlayer(m_player); + m_luaCore.setWorld(m_world); + try { auto &lua = ScriptEngine::getInstance().lua(); - lua["player"] = &m_player; + lua["openminer"] = &m_luaCore; lua.script("init()"); } catch (const sol::error &e) {