From 6490663c97ca86d7bd178d638261cf41fb7d2fd4 Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 5 Aug 2021 22:33:23 +0200 Subject: [PATCH] Added helpers for easy checking the target's range for interaction. --- entities/entity.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++++- entities/entity.h | 40 ++++++++++++---------- 2 files changed, 104 insertions(+), 19 deletions(-) diff --git a/entities/entity.cpp b/entities/entity.cpp index c0cd23c..a76d992 100644 --- a/entities/entity.cpp +++ b/entities/entity.cpp @@ -710,7 +710,7 @@ void Entity::_setup() { Ref stat_data = cc->get_stat_data(); ERR_FAIL_COND(!stat_data.is_valid()); - + for (int i = 0; i < ESS::get_singleton()->stat_get_count(); ++i) { stat_set_base(i, stat_data->get_base(i)); } @@ -2884,6 +2884,79 @@ void Entity::copen_window(int window_id) { emit_signal("onc_open_winow_request", window_id); } +bool Entity::iss_target_in_interact_range() { + return call("_iss_target_in_interact_range"); +} +bool Entity::isc_target_in_interact_range() { + return call("_isc_target_in_interact_range"); +} +bool Entity::_iss_target_in_interact_range() { + Entity *t = gets_target(); + + if (!ObjectDB::instance_validate(t)) { + return false; + } + + Node2D *b2d = get_body_2d(); + + if (b2d) { + Node2D *tb = t->get_body_2d(); + + if (!tb) { + return false; + } + + return (b2d->get_transform().get_origin() - tb->get_transform().get_origin()).length_squared() > EntityEnums::ENTITY_INTERACT_RANGE_SQUARED; + } + + Spatial *b3d = get_body_3d(); + + if (b3d) { + Spatial *tb = t->get_body_3d(); + + if (!tb) { + return false; + } + + return (b3d->get_transform().get_origin() - tb->get_transform().get_origin()).length_squared() > EntityEnums::ENTITY_INTERACT_RANGE_SQUARED; + } + + return false; +} +bool Entity::_isc_target_in_interact_range() { + Entity *t = getc_target(); + + if (!ObjectDB::instance_validate(t)) { + return false; + } + + Node2D *b2d = get_body_2d(); + + if (b2d) { + Node2D *tb = t->get_body_2d(); + + if (!tb) { + return false; + } + + return (b2d->get_transform().get_origin() - tb->get_transform().get_origin()).length_squared() > EntityEnums::ENTITY_INTERACT_RANGE_SQUARED; + } + + Spatial *b3d = get_body_3d(); + + if (b3d) { + Spatial *tb = t->get_body_3d(); + + if (!tb) { + return false; + } + + return (b3d->get_transform().get_origin() - tb->get_transform().get_origin()).length_squared() > EntityEnums::ENTITY_INTERACT_RANGE_SQUARED; + } + + return false; +} + //XP Operations void Entity::xp_adds(int value) { _s_xp += value; @@ -6972,6 +7045,14 @@ void Entity::_bind_methods() { ClassDB::bind_method(D_METHOD("ssend_open_window", "window_id"), &Entity::ssend_open_window); ClassDB::bind_method(D_METHOD("copen_window", "window_id"), &Entity::copen_window); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_iss_target_in_interact_range")); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::BOOL, "ret"), "_isc_target_in_interact_range")); + + ClassDB::bind_method(D_METHOD("iss_target_in_interact_range"), &Entity::iss_target_in_interact_range); + ClassDB::bind_method(D_METHOD("isc_target_in_interact_range"), &Entity::isc_target_in_interact_range); + ClassDB::bind_method(D_METHOD("_iss_target_in_interact_range"), &Entity::_iss_target_in_interact_range); + ClassDB::bind_method(D_METHOD("_isc_target_in_interact_range"), &Entity::_isc_target_in_interact_range); + //XP Operations ADD_SIGNAL(MethodInfo("notification_sxp_gained", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::INT, "value"))); ADD_SIGNAL(MethodInfo("notification_cxp_gained", PropertyInfo(Variant::OBJECT, "entity", PROPERTY_HINT_RESOURCE_TYPE, "Entity"), PropertyInfo(Variant::INT, "value"))); diff --git a/entities/entity.h b/entities/entity.h index 090d465..803f4e8 100644 --- a/entities/entity.h +++ b/entities/entity.h @@ -27,15 +27,14 @@ SOFTWARE. #if VERSION_MAJOR > 3 #include "core/object/object.h" -#include "core/templates/vector.h" #include "core/string/ustring.h" +#include "core/templates/vector.h" #else #include "core/object.h" -#include "core/vector.h" #include "core/ustring.h" +#include "core/vector.h" #endif - #include "core/io/networked_multiplayer_peer.h" #include "scene/main/node.h" @@ -620,6 +619,11 @@ public: void ssend_open_window(int window_id); void copen_window(int window_id); + bool iss_target_in_interact_range(); + bool isc_target_in_interact_range(); + virtual bool _iss_target_in_interact_range(); + virtual bool _isc_target_in_interact_range(); + //XP Operations void xp_adds(int value); void xp_addc(int value); @@ -1196,13 +1200,13 @@ private: //// Equipment //// - Vector > _s_equipment; - Vector > _c_equipment; + Vector> _s_equipment; + Vector> _c_equipment; //// Resources //// - Vector > _s_resources; - Vector > _c_resources; + Vector> _s_resources; + Vector> _c_resources; //// GCD //// @@ -1223,8 +1227,8 @@ private: //// AuraComponent //// - Vector > _s_auras; - Vector > _c_auras; + Vector> _s_auras; + Vector> _c_auras; int _s_entity_type; int _c_entity_type; @@ -1267,8 +1271,8 @@ private: //// Data //// - Vector > _s_data; - Vector > _c_data; + Vector> _s_data; + Vector> _c_data; //// Actionbars //// @@ -1277,21 +1281,21 @@ private: //// Crafting System //// - Vector > _s_craft_recipes; - Vector > _c_craft_recipes; + Vector> _s_craft_recipes; + Vector> _c_craft_recipes; //// Known Spells //// int _s_free_spell_points; int _c_free_spell_points; - Vector > _s_spells; - Vector > _c_spells; + Vector> _s_spells; + Vector> _c_spells; //// Skills //// - Vector > _s_skills; - Vector > _c_skills; + Vector> _s_skills; + Vector> _c_skills; //// Stat Allocations //// @@ -1335,7 +1339,7 @@ private: // Callbacks - Vector > _physics_process_scis; + Vector> _physics_process_scis; }; #endif