From fbbf9335a4a61aee959be44614404d517ab45ef0 Mon Sep 17 00:00:00 2001 From: Tristan Rhodes Date: Mon, 1 Jul 2024 22:06:29 -0600 Subject: [PATCH 1/2] Add a new condition to check the distance between an object and a position --- .../Builtin/BaseObjectExtension.cpp | 19 +++++++++ .../Builtin/BaseObjectExtension.cpp | 2 + GDJS/Runtime/events-tools/objecttools.ts | 41 +++++++++++++++++++ GDJS/Runtime/runtimeobject.ts | 13 ++++++ 4 files changed, 75 insertions(+) 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..8cc0794295e5 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.distanceToPositionTest"); 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..166c6d3a9c7b 100644 --- a/GDJS/Runtime/events-tools/objecttools.ts +++ b/GDJS/Runtime/events-tools/objecttools.ts @@ -253,6 +253,47 @@ namespace gdjs { ); }; + export const _distanceToPosition = function (obj, x, y, distance) { + return obj.getSqDistanceToPosition(x, y) <= distance; + }; + + export const distanceToPositionTest = 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.distanceToPositionTest + ); + 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; diff --git a/GDJS/Runtime/runtimeobject.ts b/GDJS/Runtime/runtimeobject.ts index b046b953febe..feb1392f5da5 100644 --- a/GDJS/Runtime/runtimeobject.ts +++ b/GDJS/Runtime/runtimeobject.ts @@ -2719,6 +2719,19 @@ namespace gdjs { return obj1.getSqDistanceToObject(obj2) <= distance; } + /** + * Check the distance between an object and a position. + * @static + */ + static distanceToPositionTest( + obj: RuntimeObject, + x: float, + y: float, + distance: float + ): boolean { + return obj.getSqDistanceToPosition(x, y) <= distance * distance; + } + /** * Return true if the cursor, or any touch, is on the object. * From db2f09693d5155cba1f49a462b11dc149e07811d Mon Sep 17 00:00:00 2001 From: Tristan Rhodes Date: Tue, 2 Jul 2024 23:59:55 -0600 Subject: [PATCH 2/2] Renamed function and removed unused code in runtimeobject.ts --- .../GDJS/Extensions/Builtin/BaseObjectExtension.cpp | 2 +- GDJS/Runtime/events-tools/objecttools.ts | 6 ++---- GDJS/Runtime/runtimeobject.ts | 13 ------------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp b/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp index 8cc0794295e5..7b257731edc6 100644 --- a/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp +++ b/GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp @@ -213,7 +213,7 @@ BaseObjectExtension::BaseObjectExtension() { GetAllConditions()["Distance"].SetFunctionName( "gdjs.evtTools.object.distanceTest"); GetAllConditions()["DistanceToPosition"].SetFunctionName( - "gdjs.evtTools.object.distanceToPositionTest"); + "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 166c6d3a9c7b..15b52c8172d0 100644 --- a/GDJS/Runtime/events-tools/objecttools.ts +++ b/GDJS/Runtime/events-tools/objecttools.ts @@ -257,7 +257,7 @@ namespace gdjs { return obj.getSqDistanceToPosition(x, y) <= distance; }; - export const distanceToPositionTest = function ( + export const isCloseToPosition = function ( objectsLists1: ObjectsLists, x: float, y: float, @@ -266,9 +266,7 @@ namespace gdjs { ) { let isTrue = false; const squaredDistance = distance * distance; - const lists = gdjs.staticArray( - gdjs.evtTools.object.distanceToPositionTest - ); + const lists = gdjs.staticArray(gdjs.evtTools.object.isCloseToPosition); objectsLists1.values(lists); for (let i = 0, len = lists.length; i < len; ++i) { diff --git a/GDJS/Runtime/runtimeobject.ts b/GDJS/Runtime/runtimeobject.ts index feb1392f5da5..b046b953febe 100644 --- a/GDJS/Runtime/runtimeobject.ts +++ b/GDJS/Runtime/runtimeobject.ts @@ -2719,19 +2719,6 @@ namespace gdjs { return obj1.getSqDistanceToObject(obj2) <= distance; } - /** - * Check the distance between an object and a position. - * @static - */ - static distanceToPositionTest( - obj: RuntimeObject, - x: float, - y: float, - distance: float - ): boolean { - return obj.getSqDistanceToPosition(x, y) <= distance * distance; - } - /** * Return true if the cursor, or any touch, is on the object. *