[LuaCore] Added. Only two functions to get World and Player.

This commit is contained in:
Quentin Bazin 2019-01-07 04:56:42 +01:00
parent 3875b73a1d
commit 50bf84acf6
7 changed files with 100 additions and 20 deletions

4
TODO
View File

@ -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`

39
include/lua/LuaCore.hpp Normal file
View File

@ -0,0 +1,39 @@
/*
* =====================================================================================
*
* Filename: LuaCore.hpp
*
* Description:
*
* Created: 07/01/2019 04:35:13
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef LUACORE_HPP_
#define LUACORE_HPP_
#include <functional>
#include <sol.hpp>
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_

View File

@ -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_

View File

@ -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

24
source/lua/LuaCore.cpp Normal file
View File

@ -0,0 +1,24 @@
/*
* =====================================================================================
*
* Filename: LuaCore.cpp
*
* Description:
*
* Created: 07/01/2019 04:41:11
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "LuaCore.hpp"
#include "Player.hpp"
#include "World.hpp"
void LuaCore::initUsertype(sol::state &lua) {
lua.new_usertype<LuaCore>("LuaCore",
"world", &LuaCore::world,
"player", &LuaCore::player
);
}

View File

@ -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>("World");
m_lua.new_usertype<Player>("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);
}

View File

@ -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) {