Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new condition to check the distance between an object and a position #6738

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Core/GDCore/Extensions/Builtin/BaseObjectExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions GDJS/GDJS/Extensions/Builtin/BaseObjectExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 41 additions & 0 deletions GDJS/Runtime/events-tools/objecttools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,47 @@ namespace gdjs {
);
};

export const _distanceToPosition = function (obj, x, y, distance) {
return obj.getSqDistanceToPosition(x, y) <= distance;
};

export const distanceToPositionTest = function (
tristanbob marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
13 changes: 13 additions & 0 deletions GDJS/Runtime/runtimeobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
tristanbob marked this conversation as resolved.
Show resolved Hide resolved

/**
* Return true if the cursor, or any touch, is on the object.
*
Expand Down