From a36ffaacd95cc5ed315fcde6eda150f11d948344 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 16 Jul 2024 13:42:58 +0300 Subject: [PATCH] add entity events: on_aim_on, on_aim_off --- src/logic/PlayerController.cpp | 13 +++++++++++++ src/logic/scripting/scripting.cpp | 16 ++++++++++++++++ src/logic/scripting/scripting.hpp | 2 ++ 3 files changed, 31 insertions(+) diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 13c53f08..e7be8c36 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -357,6 +357,7 @@ voxel* PlayerController::updateSelection(float maxDistance) { if (vox) { maxDistance = glm::distance(camera->position, end); } + auto prevEntity = selection.entity; selection.entity = ENTITY_NONE; selection.actualPosition = iend; if (auto result = level->entities->rayCast( @@ -367,6 +368,18 @@ voxel* PlayerController::updateSelection(float maxDistance) { selection.actualPosition = selection.position; selection.normal = result->normal; } + if (selection.entity != prevEntity) { + if (prevEntity != ENTITY_NONE) { + if (auto pentity = level->entities->get(prevEntity)) { + scripting::on_aim_off(*pentity, player.get()); + } + } + if (selection.entity != ENTITY_NONE) { + if (auto pentity = level->entities->get(selection.entity)) { + scripting::on_aim_on(*pentity, player.get()); + } + } + } if (vox == nullptr || selection.entity) { selection.vox = {BLOCK_VOID, {}}; return nullptr; diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index ffbf3f47..e85323f9 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -346,6 +346,8 @@ void scripting::on_entity_spawn( funcsset.on_sensor_enter = lua::hasfield(L, "on_sensor_enter"); funcsset.on_sensor_exit = lua::hasfield(L, "on_sensor_exit"); 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"); lua::pop(L, 2); component->env = compenv; @@ -423,6 +425,20 @@ void scripting::on_sensor_exit(const Entity& entity, size_t index, entityid_t oi }); } +void scripting::on_aim_on(const Entity& entity, Player* player) { + process_entity_callback(entity, "on_aim_on", + &entity_funcs_set::on_aim_on, [player](auto L) { + return lua::pushinteger(L, player->getId()); + }); +} + +void scripting::on_aim_off(const Entity& entity, Player* player) { + process_entity_callback(entity, "on_aim_off", + &entity_funcs_set::on_aim_off, [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 b8792111..f2c64fdd 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -93,6 +93,8 @@ namespace scripting { void on_entities_render(); void on_sensor_enter(const Entity& entity, size_t index, entityid_t oid); 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); /// @brief Called on UI view show void on_ui_open(