From 41a22938d6b3d7554350eecbc7c8355a3bdd9477 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jul 2024 07:35:02 +0300 Subject: [PATCH] add 'entity.despawn' command --- res/layouts/console.xml.lua | 4 ++++ res/scripts/stdcmd.lua | 13 +++++++++++++ src/frontend/debug_panel.cpp | 6 +++--- src/logic/CommandsInterpreter.cpp | 6 ++++++ src/logic/PlayerController.cpp | 1 + src/logic/scripting/lua/libplayer.cpp | 4 ++-- src/objects/Player.cpp | 8 ++++++++ src/objects/Player.hpp | 4 ++++ 8 files changed, 41 insertions(+), 5 deletions(-) diff --git a/res/layouts/console.xml.lua b/res/layouts/console.xml.lua index 0f69bdc9..e23ce357 100644 --- a/res/layouts/console.xml.lua +++ b/res/layouts/console.xml.lua @@ -11,6 +11,10 @@ function setup_variables() if pentity ~= 0 then console.set('entity.id', pentity) end + local sentity = player.get_selected_entity(pid) + if sentity ~= nil then + console.set('entity.selected', sentity) + end end function on_history_up() diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 8d27e77e..1933ae03 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -156,3 +156,16 @@ console.add_command( return "spawned new player entity #"..tostring(eid) end ) + +console.add_command( + "entity.despawn entity:sel=$entity.selected", + "Despawn entity", + function (args, kwargs) + local eid = args[1] + local entity = entities.get(eid) + if entity ~= nil then + entity:despawn() + return "despawned entity #"..tostring(eid) + end + end +) diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index d472c3e5..1397d335 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -105,10 +105,10 @@ std::shared_ptr create_debug_panel( } })); panel->add(create_label([=]() { - const auto& selection = player->selection; - if (selection.entity == ENTITY_NONE) { + auto eid = player->getSelectedEntity(); + if (eid == ENTITY_NONE) { return std::wstring {L"entity: -"}; - } else if (auto entity = level->entities->get(selection.entity)) { + } else if (auto entity = level->entities->get(eid)) { return L"entity: "+util::str2wstr_utf8(entity->getDef().name)+ L" uid: "+std::to_wstring(entity->getUID()); } else { diff --git a/src/logic/CommandsInterpreter.cpp b/src/logic/CommandsInterpreter.cpp index 524fd6a4..85a082e7 100644 --- a/src/logic/CommandsInterpreter.cpp +++ b/src/logic/CommandsInterpreter.cpp @@ -373,6 +373,12 @@ public: if (!arg->optional) { throw error("missing argument "+util::quote(arg->name)); } else { + if (auto string = std::get_if(&arg->def)) { + if ((*string)[0] == '$') { + args->put((*interpreter)[string->substr(1)]); + continue; + } + } args->put(arg->def); } } diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 3a80f41d..91801d79 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -241,6 +241,7 @@ void PlayerController::update(float delta, bool input, bool pause) { if (!pause) { if (input) { updateKeyboard(); + player->updateSelectedEntity(); } else { resetKeyboard(); } diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index 38194290..06724901 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -140,8 +140,8 @@ static int l_get_selected_block(lua::State* L) { static int l_get_selected_entity(lua::State* L) { if (auto player = get_player(L, 1)) { - if (player->selection.entity) { - return lua::pushinteger(L, player->selection.entity); + if (auto eid = player->getSelectedEntity()) { + return lua::pushinteger(L, eid); } } return 0; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 2f1dfc7e..7fc93d4c 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -135,6 +135,10 @@ void Player::updateInput(PlayerInput& input, float delta) { input.flight = false; } +void Player::updateSelectedEntity() { + selectedEid = selection.entity; +} + void Player::postUpdate() { auto entity = level->entities->get(eid); if (!entity.has_value()) { @@ -225,6 +229,10 @@ void Player::setEntity(entityid_t eid) { this->eid = eid; } +entityid_t Player::getSelectedEntity() const { + return selectedEid; +} + std::shared_ptr Player::getInventory() const { return inventory; } diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index dd80236d..7fa62c5a 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -52,6 +52,7 @@ class Player : public Object, public Serializable { bool flight = false; bool noclip = false; entityid_t eid; + entityid_t selectedEid; public: std::shared_ptr camera, spCamera, tpCamera; std::shared_ptr currentCamera; @@ -66,6 +67,7 @@ public: void teleport(glm::vec3 position); void updateEntity(); void updateInput(PlayerInput& input, float delta); + void updateSelectedEntity(); void postUpdate(); void attemptToFindSpawnpoint(); @@ -83,6 +85,8 @@ public: entityid_t getEntity() const; void setEntity(entityid_t eid); + + entityid_t getSelectedEntity() const; std::shared_ptr getInventory() const;