diff --git a/Core/GDCore/Extensions/Builtin/BaseObjectExtension.cpp b/Core/GDCore/Extensions/Builtin/BaseObjectExtension.cpp index 1ac581ddb8dc..2b26ebb51a44 100644 --- a/Core/GDCore/Extensions/Builtin/BaseObjectExtension.cpp +++ b/Core/GDCore/Extensions/Builtin/BaseObjectExtension.cpp @@ -1638,6 +1638,25 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension( .AddCodeOnlyParameter("conditionInverted", "") .MarkAsSimple(); + extension + .AddCondition("DistanceToPosition", + _("Distance between an object and a position"), + _("Compare the distance between the object and a " + "\"target\" position. If condition is " + "inverted, objects that are farther than the specified " + "distance will be picked."), + _("_PARAM0_ distance to _PARAM1_;_PARAM2_ " + "is below _PARAM3_ pixels"), + _("Position"), + "res/conditions/distance24.png", + "res/conditions/distance.png") + .AddParameter("objectList", _("Object")) + .AddParameter("expression", _("Target X position")) + .AddParameter("expression", _("Target Y position")) + .AddParameter("expression", _("Distance")) + .AddCodeOnlyParameter("conditionInverted", "") + .MarkAsSimple(); + extension .AddCondition( "AjoutObjConcern", diff --git a/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp b/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp index 3036b0ee2a2c..7b257731edc6 100644 --- a/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp +++ b/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp @@ -212,6 +212,8 @@ BaseObjectExtension::BaseObjectExtension() { "gdjs.evtTools.object.raycastObjectToPosition"); GetAllConditions()["Distance"].SetFunctionName( "gdjs.evtTools.object.distanceTest"); + GetAllConditions()["DistanceToPosition"].SetFunctionName( + "gdjs.evtTools.object.isCloseToPosition"); GetAllConditions()["SeDirige"].SetFunctionName( "gdjs.evtTools.object.movesTowardTest"); GetAllConditions()["EstTourne"].SetFunctionName( diff --git a/GDJS/Runtime/events-tools/objecttools.ts b/GDJS/Runtime/events-tools/objecttools.ts index adda409a006b..15b52c8172d0 100644 --- a/GDJS/Runtime/events-tools/objecttools.ts +++ b/GDJS/Runtime/events-tools/objecttools.ts @@ -253,6 +253,45 @@ namespace gdjs { ); }; + export const _distanceToPosition = function (obj, x, y, distance) { + return obj.getSqDistanceToPosition(x, y) <= distance; + }; + + export const isCloseToPosition = function ( + objectsLists1: ObjectsLists, + x: float, + y: float, + distance: float, + inverted: boolean + ) { + let isTrue = false; + const squaredDistance = distance * distance; + const lists = gdjs.staticArray(gdjs.evtTools.object.isCloseToPosition); + objectsLists1.values(lists); + + for (let i = 0, len = lists.length; i < len; ++i) { + const arr = lists[i]; + for (let k = 0, lenk = arr.length; k < lenk; ++k) { + const obj = arr[k]; + const withinDistance = + obj.getSqDistanceToPosition(x, y) <= squaredDistance; + if (inverted ? !withinDistance : withinDistance) { + isTrue = true; + obj.pick = true; + } else { + obj.pick = false; + } + } + } + + // Trim not picked objects from lists. + for (let i = 0, len = lists.length; i < len; ++i) { + gdjs.evtTools.object.filterPickedObjectsList(lists[i]); + } + + return isTrue; + }; + export const _movesToward = function (obj1, obj2, tolerance) { if (obj1.hasNoForces()) { return false;