Now using ServerConfig in Lua + Collision callback: OK.
This commit is contained in:
parent
2f82101fb8
commit
4ff7714ece
@ -174,17 +174,16 @@ mod:entity {
|
||||
hitbox = {0, 0, 0, 0.25, 0.25, 0.25},
|
||||
},
|
||||
|
||||
-- on_collision = function(entity, other)
|
||||
-- if other:type() == "player" then
|
||||
-- other:inventory():add_item(entity:properties():itemstack())
|
||||
-- end
|
||||
-- end,
|
||||
on_collision = function(item_stack, entity, entity_net_id, player, registry, server)
|
||||
player:inventory():add_stack(item_stack:item():string_id(), item_stack:amount())
|
||||
server:send_player_inv_update(player:client_id(), player:client());
|
||||
server:send_entity_despawn(entity_net_id, nil)
|
||||
registry:destroy(entity)
|
||||
end,
|
||||
}
|
||||
|
||||
useItemDrops = true
|
||||
|
||||
openminer:add_listener(EventType.OnBlockDigged, function(pos, block, player, world, client, server)
|
||||
if useItemDrops then
|
||||
if ServerConfig.useItemDrops then
|
||||
mods["default"]:spawn_entity("default:item_drop", {
|
||||
position = {pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
|
||||
dimension = world:dimension():id(),
|
||||
|
@ -34,7 +34,6 @@
|
||||
#include "ClientCommandHandler.hpp"
|
||||
#include "ClientPlayer.hpp"
|
||||
#include "ClientWorld.hpp"
|
||||
#include "ItemDropFactory.hpp"
|
||||
#include "Registry.hpp"
|
||||
#include "Sky.hpp"
|
||||
#include "TextureAtlas.hpp"
|
||||
|
@ -26,6 +26,7 @@
|
||||
*/
|
||||
#include "LuaCore.hpp"
|
||||
#include "Registry.hpp"
|
||||
#include "ServerConfig.hpp"
|
||||
#include "ServerModLoader.hpp"
|
||||
#include "ServerWorld.hpp"
|
||||
|
||||
@ -40,6 +41,10 @@ void LuaCore::initUsertype(sol::state &lua) {
|
||||
"OnBlockActivated", LuaEventType::OnBlockActivated
|
||||
);
|
||||
|
||||
lua["ServerConfig"] = lua.create_table_with(
|
||||
"useItemDrops", ServerConfig::useItemDrops
|
||||
);
|
||||
|
||||
lua.new_usertype<LuaCore>("LuaCore",
|
||||
"add_listener", &LuaCore::addListener,
|
||||
|
||||
|
@ -51,6 +51,10 @@ void ScriptEngine::init() {
|
||||
}
|
||||
|
||||
void ScriptEngine::initUsertypes() {
|
||||
m_lua.new_usertype<entt::registry>("entt_registry",
|
||||
"destroy", static_cast<void (entt::registry::*)(const entt::entity)>(&entt::registry::destroy)
|
||||
);
|
||||
|
||||
m_lua.new_usertype<Registry>("Registry",
|
||||
"get_block", &Registry::getBlock,
|
||||
"get_item", &Registry::getItem,
|
||||
@ -190,7 +194,8 @@ void ScriptEngine::initUsertypes() {
|
||||
m_lua.new_usertype<ServerCommandHandler>("ServerCommandHandler",
|
||||
"send_player_change_dimension", &ServerCommandHandler::sendPlayerChangeDimension,
|
||||
"send_player_inv_update", &ServerCommandHandler::sendPlayerInvUpdate,
|
||||
"send_chat_message", &ServerCommandHandler::sendChatMessage
|
||||
"send_chat_message", &ServerCommandHandler::sendChatMessage,
|
||||
"send_entity_despawn", &ServerCommandHandler::sendEntityDespawn
|
||||
);
|
||||
|
||||
m_lua.new_usertype<ServerModLoader>("ServerModLoader",
|
||||
|
@ -28,6 +28,7 @@
|
||||
|
||||
#include "AnimationComponent.hpp"
|
||||
#include "DrawableDef.hpp"
|
||||
#include "LuaCallbackComponent.hpp"
|
||||
#include "LuaEntityLoader.hpp"
|
||||
#include "LuaMod.hpp"
|
||||
#include "NetworkComponent.hpp"
|
||||
@ -41,9 +42,11 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
|
||||
entt::registry ®istry = Registry::getInstance().entityRegistry();
|
||||
entt::entity entity = Registry::getInstance().registerEntity(stringID);
|
||||
|
||||
// TODO: Create a struct to store these functions
|
||||
sol::unsafe_function onSpawn = table["on_spawn"];
|
||||
sol::unsafe_function onCollision = table["on_collision"];
|
||||
sol::optional<sol::unsafe_function> onCollision = table["on_collision"];
|
||||
if (onCollision != sol::nullopt) {
|
||||
auto &luaCallbackComponent = registry.emplace<LuaCallbackComponent>(entity);
|
||||
luaCallbackComponent.collisionCallback = onCollision.value();
|
||||
}
|
||||
|
||||
sol::optional<sol::table> properties = table["properties"];
|
||||
if (properties != sol::nullopt) {
|
||||
@ -90,7 +93,7 @@ void LuaEntityLoader::loadEntity(const sol::table &table) const {
|
||||
|
||||
sol::optional<sol::table> hitboxTable = properties.value()["hitbox"];
|
||||
if (hitboxTable != sol::nullopt) {
|
||||
registry.emplace<gk::FloatBox>(entity,
|
||||
registry.emplace<gk::DoubleBox>(entity,
|
||||
hitboxTable.value()[1], hitboxTable.value()[2], hitboxTable.value()[3],
|
||||
hitboxTable.value()[4], hitboxTable.value()[5], hitboxTable.value()[6]
|
||||
);
|
||||
@ -136,6 +139,10 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table
|
||||
if (registry.has<RotationComponent>(entityModel))
|
||||
sceneRegistry.emplace<RotationComponent>(entity);
|
||||
|
||||
auto *luaCallbackComponent = registry.try_get<LuaCallbackComponent>(entityModel);
|
||||
if (luaCallbackComponent)
|
||||
sceneRegistry.emplace<LuaCallbackComponent>(entity, *luaCallbackComponent);
|
||||
|
||||
auto *drawable = registry.try_get<DrawableDef>(entityModel);
|
||||
if (drawable) {
|
||||
auto &drawableDef = sceneRegistry.emplace<DrawableDef>(entity, *drawable);
|
||||
@ -149,9 +156,9 @@ void LuaEntityLoader::spawnEntity(const std::string &entityID, const sol::table
|
||||
if (animation)
|
||||
sceneRegistry.emplace<AnimationComponent>(entity, *animation);
|
||||
|
||||
auto *hitbox = registry.try_get<gk::FloatBox>(entityModel);
|
||||
auto *hitbox = registry.try_get<gk::DoubleBox>(entityModel);
|
||||
if (hitbox)
|
||||
sceneRegistry.emplace<gk::FloatBox>(entity, *hitbox);
|
||||
sceneRegistry.emplace<gk::DoubleBox>(entity, *hitbox);
|
||||
|
||||
if (stack.amount())
|
||||
sceneRegistry.emplace<ItemStack>(entity, stack);
|
||||
|
@ -284,7 +284,6 @@ void ServerCommandHandler::setupCallbacks() {
|
||||
|
||||
ServerWorld &world = getWorldForClient(client.id);
|
||||
const Block &blockDef = Registry::getInstance().getBlock(world.getBlock(x, y, z));
|
||||
world.onBlockDigged(x, y, z, blockDef, *player);
|
||||
world.setBlock(x, y, z, 0);
|
||||
|
||||
m_scriptEngine.luaCore().onEvent(LuaEventType::OnBlockDigged, glm::ivec3{x, y, z}, blockDef, *player, world, client, *this);
|
||||
|
@ -24,16 +24,13 @@
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#ifndef ITEMDROPFACTORY_HPP_
|
||||
#define ITEMDROPFACTORY_HPP_
|
||||
#ifndef LUACALLBACKCOMPONENT_HPP_
|
||||
#define LUACALLBACKCOMPONENT_HPP_
|
||||
|
||||
#include <gk/core/IntTypes.hpp>
|
||||
#include <sol.hpp>
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
class ItemDropFactory {
|
||||
public:
|
||||
static void create(entt::registry ®istry, double x, double y, double z, u16 dimension, const std::string &itemID, u16 amount = 1);
|
||||
struct LuaCallbackComponent {
|
||||
sol::unsafe_function collisionCallback;
|
||||
};
|
||||
|
||||
#endif // ITEMDROPFACTORY_HPP_
|
||||
#endif // LUACALLBACKCOMPONENT_HPP_
|
@ -24,25 +24,24 @@
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include <gk/core/Debug.hpp>
|
||||
|
||||
#include "CollisionController.hpp"
|
||||
#include "ItemStack.hpp"
|
||||
#include "LuaCallbackComponent.hpp"
|
||||
#include "NetworkComponent.hpp"
|
||||
#include "PlayerList.hpp"
|
||||
#include "PositionComponent.hpp"
|
||||
#include "ServerCommandHandler.hpp"
|
||||
|
||||
void CollisionController::update(entt::registry ®istry) {
|
||||
// FIXME: This should be a callback in a CollisionComponent
|
||||
registry.view<PositionComponent, gk::DoubleBox, ItemStack, NetworkComponent>().each([&](auto entity, auto &position, auto &box, auto &itemStack, auto &network) {
|
||||
registry.view<PositionComponent, gk::DoubleBox, ItemStack, NetworkComponent, LuaCallbackComponent>().each([&](auto entity, auto &position, auto &box, auto &itemStack, auto &network, auto &luaCallbackComponent) {
|
||||
for (auto &it : m_players) {
|
||||
if (it.second.dimension() == position.dimension) {
|
||||
gk::DoubleBox hitbox = box + gk::Vector3d{position.x, position.y, position.z};
|
||||
gk::DoubleBox playerHitbox = it.second.hitbox() + gk::Vector3d{it.second.x(), it.second.y(), it.second.z()};
|
||||
if (hitbox.intersects(playerHitbox)) {
|
||||
it.second.inventory().addStack(itemStack.item().stringID(), itemStack.amount());
|
||||
m_server->sendPlayerInvUpdate(it.second.clientID(), &it.second.client());
|
||||
m_server->sendEntityDespawn(network.entityID);
|
||||
registry.destroy(entity);
|
||||
luaCallbackComponent.collisionCallback(itemStack, entity, network.entityID, it.second, registry, *m_server);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* OpenMiner
|
||||
*
|
||||
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
||||
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
||||
*
|
||||
* This file is part of OpenMiner.
|
||||
*
|
||||
* OpenMiner is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* OpenMiner is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include "AnimationComponent.hpp"
|
||||
#include "DrawableComponent.hpp"
|
||||
#include "DrawableDef.hpp"
|
||||
#include "ItemDropFactory.hpp"
|
||||
#include "ItemStack.hpp"
|
||||
#include "NetworkComponent.hpp"
|
||||
#include "PositionComponent.hpp"
|
||||
#include "Registry.hpp"
|
||||
#include "RotationComponent.hpp"
|
||||
|
||||
void ItemDropFactory::create(entt::registry ®istry, double x, double y, double z, u16 dimension, const std::string &itemID, u16 amount) {
|
||||
auto entity = registry.create();
|
||||
registry.emplace<PositionComponent>(entity, x, y, z, dimension);
|
||||
registry.emplace<RotationComponent>(entity);
|
||||
registry.emplace<NetworkComponent>(entity, entity);
|
||||
|
||||
auto &drawableDef = registry.emplace<DrawableDef>(entity);
|
||||
auto &cube = drawableDef.addInventoryCube();
|
||||
cube.size = 0.25f;
|
||||
cube.origin = gk::Vector3f{cube.size / 2.f, cube.size / 2.f, cube.size / 2.f};
|
||||
cube.blockID = itemID;
|
||||
|
||||
auto &animationComponent = registry.emplace<AnimationComponent>(entity);
|
||||
animationComponent.addRotation(0.f, 0.f, 1.f, 0.5f);
|
||||
animationComponent.addTranslation(0.f, 0.f, -0.0005f, -0.2f, 0.f, true);
|
||||
|
||||
registry.emplace<gk::DoubleBox>(entity, 0., 0., 0., cube.size, cube.size, cube.size);
|
||||
registry.emplace<ItemStack>(entity, itemID, amount);
|
||||
}
|
||||
|
@ -28,7 +28,6 @@
|
||||
|
||||
#include "Dimension.hpp"
|
||||
#include "EngineConfig.hpp"
|
||||
#include "ItemDropFactory.hpp"
|
||||
#include "Network.hpp"
|
||||
#include "Server.hpp"
|
||||
#include "ServerCommandHandler.hpp"
|
||||
@ -151,17 +150,6 @@ void ServerWorld::sendRequestedData(ClientInfo &client, int cx, int cy, int cz)
|
||||
sendChunkData(client, chunk);
|
||||
}
|
||||
|
||||
void ServerWorld::onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player) {
|
||||
// FIXME: lua-api-entity
|
||||
// if (ServerConfig::useItemDrops) {
|
||||
// ItemDropFactory::create(m_scene.registry(), x + 0.5, y + 0.5, z + 0.5, m_dimension.id(), block.getItemDrop().item().stringID(), block.getItemDrop().amount());
|
||||
// }
|
||||
// else {
|
||||
// player.inventory().addStack(block.getItemDrop().item().stringID(), block.getItemDrop().amount());
|
||||
// m_server->sendPlayerInvUpdate(player.clientID(), &player.client());
|
||||
// }
|
||||
}
|
||||
|
||||
ServerChunk &ServerWorld::createChunk(s32 cx, s32 cy, s32 cz) {
|
||||
ServerChunk *chunk = (ServerChunk *)getChunk(cx, cy, cz);
|
||||
if (!chunk) {
|
||||
|
@ -57,8 +57,6 @@ class ServerWorld : public World {
|
||||
void sendChunkData(const ClientInfo &client, ServerChunk &chunk);
|
||||
void sendRequestedData(ClientInfo &client, s32 cx, s32 cy, s32 cz);
|
||||
|
||||
void onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player);
|
||||
|
||||
ServerChunk &createChunk(s32 cx, s32 cy, s32 cz);
|
||||
|
||||
Chunk *getChunk(int cx, int cy, int cz) const override;
|
||||
|
Loading…
x
Reference in New Issue
Block a user