diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index d5fc9591..eb2781a5 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -111,3 +111,7 @@ function on_update() body:set_vel(dir) end end + +function on_attacked(attacker, pid) + body:set_vel({0, 10, 0}) +end diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index e7be8c36..3a80f41d 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -449,6 +449,20 @@ void PlayerController::processRightClick(Block* def, Block* target) { } } +void PlayerController::updateEntityInteraction(entityid_t eid, bool lclick, bool rclick) { + auto entityOpt = level->entities->get(eid); + if (!entityOpt.has_value()) { + return; + } + auto entity = entityOpt.value(); + if (lclick) { + scripting::on_attacked(entity, player.get(), player->getEntity()); + } + if (rclick) { + scripting::on_entity_used(entity, player.get()); + } +} + void PlayerController::updateInteraction() { auto indices = level->content->getIndices(); auto chunks = level->chunks.get(); @@ -468,6 +482,9 @@ void PlayerController::updateInteraction() { if (rclick && item->rt.funcsset.on_use) { scripting::on_item_use(player.get(), item); } + if (selection.entity) { + updateEntityInteraction(selection.entity, lclick, rclick); + } return; } diff --git a/src/logic/PlayerController.hpp b/src/logic/PlayerController.hpp index 69938fd4..79363f61 100644 --- a/src/logic/PlayerController.hpp +++ b/src/logic/PlayerController.hpp @@ -55,6 +55,7 @@ class PlayerController { void updateKeyboard(); void resetKeyboard(); void updatePlayer(float delta); + void updateEntityInteraction(entityid_t eid, bool lclick, bool rclick); void updateInteraction(); float stepsTimer = 0.0f; diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index e85323f9..dd07620c 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -348,6 +348,8 @@ void scripting::on_entity_spawn( funcsset.on_save = lua::hasfield(L, "on_save"); funcsset.on_aim_on = lua::hasfield(L, "on_aim_on"); funcsset.on_aim_off = lua::hasfield(L, "on_aim_off"); + funcsset.on_attacked = lua::hasfield(L, "on_attacked"); + funcsset.on_used = lua::hasfield(L, "on_used"); lua::pop(L, 2); component->env = compenv; @@ -439,6 +441,22 @@ void scripting::on_aim_off(const Entity& entity, Player* player) { }); } +void scripting::on_attacked(const Entity& entity, Player* player, entityid_t attacker) { + process_entity_callback(entity, "on_attacked", + &entity_funcs_set::on_attacked, [player, attacker](auto L) { + lua::pushinteger(L, attacker); + lua::pushinteger(L, player->getId()); + return 2; + }); +} + +void scripting::on_entity_used(const Entity& entity, Player* player) { + process_entity_callback(entity, "on_used", + &entity_funcs_set::on_used, [player](auto L) { + return lua::pushinteger(L, player->getId()); + }); +} + void scripting::on_entities_update() { auto L = lua::get_main_thread(); lua::get_from(L, STDCOMP, "update", true); diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index f2c64fdd..6064f132 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -95,6 +95,8 @@ namespace scripting { void on_sensor_exit(const Entity& entity, size_t index, entityid_t oid); void on_aim_on(const Entity& entity, Player* player); void on_aim_off(const Entity& entity, Player* player); + void on_attacked(const Entity& entity, Player* player, entityid_t attacker); + void on_entity_used(const Entity& entity, Player* player); /// @brief Called on UI view show void on_ui_open( diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 8ff693d1..b8e03a17 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -24,6 +24,8 @@ struct entity_funcs_set { bool on_save; bool on_aim_on; bool on_aim_off; + bool on_attacked; + bool on_used; }; struct EntityDef;