Add "Pick Nearest" and "Rotate toward object" action (#7883)

This commit is contained in:
Florian Rival
2025-10-07 11:59:07 +02:00
committed by GitHub
parent d5c96d74ed
commit 3c0bb83032
5 changed files with 95 additions and 40 deletions

View File

@@ -293,6 +293,25 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
.AddCodeOnlyParameter("currentScene", "")
.MarkAsAdvanced();
obj.AddAction(
"RotateTowardObject",
_("Rotate toward another object"),
_("Rotate an object towards another object, with the specified speed. "
"Note that if multiple instances of the target object are picked, "
"only the first one will be used. Use a For Each event or actions "
"like \"Pick nearest object\", \"Pick a random object\" to refine "
"the choice of the target object."),
_("Rotate _PARAM0_ towards _PARAM1_ at speed _PARAM2_ deg/second"),
_("Angle"),
"res/actions/rotate24_black.png",
"res/actions/rotate_black.png")
.AddParameter("object", _("Object"))
.AddParameter("objectPtr", _("Target object"))
.AddParameter("expression", _("Angular speed (in degrees per second)"))
.SetParameterLongDescription(_("Enter 0 for an immediate rotation."))
.AddCodeOnlyParameter("currentScene", "")
.MarkAsAdvanced();
obj.AddAction(
"AddForceXY",
_("Add a force"),
@@ -1617,7 +1636,7 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
extension
.AddAction("AjoutObjConcern",
_("Pick all instances"),
_("Pick all object instances"),
_("Pick all instances of the specified object(s). When you "
"pick all instances, "
"the next conditions and actions of this event work on all "
@@ -1631,20 +1650,34 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
.MarkAsAdvanced();
extension
.AddAction(
"AjoutHasard",
_("Pick a random object"),
_("Pick one object from all the specified objects. When an object "
"is picked, the next conditions and actions of this event work "
"only on that object."),
_("Pick a random _PARAM1_"),
_("Objects"),
"res/actions/ajouthasard24.png",
"res/actions/ajouthasard.png")
.AddAction("AjoutHasard",
_("Pick a random object"),
_("Pick one instance from all the specified objects. When an "
"instance is picked, the next conditions and actions of "
"this event work only on that object instance."),
_("Pick a random _PARAM1_"),
_("Objects"),
"res/actions/ajouthasard24.png",
"res/actions/ajouthasard.png")
.AddCodeOnlyParameter("objectsContext", "")
.AddParameter("objectList", _("Object"))
.MarkAsSimple();
extension
.AddAction(
"PickNearest",
_("Pick nearest object"),
_("Pick the instance of this object that is nearest to the specified "
"position."),
_("Pick the _PARAM0_ that is nearest to _PARAM1_;_PARAM2_"),
_("Objects"),
"res/conditions/distance24.png",
"res/conditions/distance.png")
.AddParameter("objectList", _("Object"))
.AddParameter("expression", _("X position"))
.AddParameter("expression", _("Y position"))
.MarkAsSimple();
extension
.AddAction(
"MoveObjects",
@@ -1694,11 +1727,12 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
extension
.AddCondition(
"AjoutObjConcern",
_("Pick all objects"),
_("Pick all the specified objects. When you pick all objects, "
_("Pick all object instances"),
_("Pick all instances of the specified object(s). When you "
"pick all instances, "
"the next conditions and actions of this event work on all "
"of them."),
_("Pick all _PARAM1_ objects"),
_("Pick all instances of _PARAM1_"),
_("Objects"),
"res/conditions/add24.png",
"res/conditions/add.png")
@@ -1707,16 +1741,15 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
.MarkAsAdvanced();
extension
.AddCondition(
"AjoutHasard",
_("Pick a random object"),
_("Pick one object from all the specified objects. When an object "
"is picked, the next conditions and actions of this event work "
"only on that object."),
_("Pick a random _PARAM1_"),
_("Objects"),
"res/conditions/ajouthasard24.png",
"res/conditions/ajouthasard.png")
.AddCondition("AjoutHasard",
_("Pick a random object"),
_("Pick one instance from all the specified objects. When "
"an instance is picked, the next conditions and actions "
"of this event work only on that object instance."),
_("Pick a random _PARAM1_"),
_("Objects"),
"res/conditions/ajouthasard24.png",
"res/conditions/ajouthasard.png")
.AddCodeOnlyParameter("objectsContext", "")
.AddParameter("objectList", _("Object"))
.MarkAsSimple();
@@ -1725,9 +1758,9 @@ void GD_CORE_API BuiltinExtensionsImplementer::ImplementsBaseObjectExtension(
.AddCondition(
"PickNearest",
_("Pick nearest object"),
_("Pick the object of this type that is nearest to the specified "
"position. If the condition is inverted, the object farthest from "
"the specified position is picked instead."),
_("Pick the instance of this object that is nearest to the specified "
"position. If the condition is inverted, the instance farthest "
"from the specified position is picked instead."),
_("Pick the _PARAM0_ that is nearest to _PARAM1_;_PARAM2_"),
_("Objects"),
"res/conditions/distance24.png",

View File

@@ -53,6 +53,7 @@ BaseObjectExtension::BaseObjectExtension() {
"runtimeobject.js");
objectActions["RotateTowardAngle"].SetFunctionName("rotateTowardAngle");
objectActions["RotateTowardPosition"].SetFunctionName("rotateTowardPosition");
objectActions["RotateTowardObject"].SetFunctionName("rotateTowardObject");
objectActions["ChangeLayer"].SetFunctionName("setLayer");
objectConditions["Layer"].SetFunctionName("isOnLayer");
objectActions["ChangePlan"]
@@ -229,6 +230,8 @@ BaseObjectExtension::BaseObjectExtension() {
"gdjs.evtTools.object.pickRandomObject");
GetAllConditions()["AjoutHasard"].SetFunctionName(
"gdjs.evtTools.object.pickRandomObject");
GetAllActions()["PickNearest"].SetFunctionName(
"gdjs.evtTools.object.pickNearestObject");
GetAllConditions()["PickNearest"].SetFunctionName(
"gdjs.evtTools.object.pickNearestObject");

View File

@@ -410,7 +410,12 @@ namespace gdjs {
return true;
};
export const pickNearestObject = function (objectsLists, x, y, inverted) {
export const pickNearestObject = function (
objectsLists: ObjectsLists,
x: float,
y: float,
inverted?: boolean | undefined
) {
let bestObject = null;
let best = 0;
let first = true;
@@ -421,8 +426,7 @@ namespace gdjs {
for (let j = 0; j < list.length; ++j) {
const object = list[j];
const distance = object.getSqDistanceToPosition(x, y);
// @ts-ignore
if (first || (distance < best) ^ inverted) {
if (first || distance < best !== inverted) {
best = distance;
bestObject = object;
}

View File

@@ -842,6 +842,12 @@ namespace gdjs {
return this.getY();
}
/**
* Rotate the object towards another object position.
* @param x The target x position
* @param y The target y position
* @param speed The rotation speed. 0 for an immediate rotation to the target position.
*/
rotateTowardPosition(x: float, y: float, speed: float): void {
this.rotateTowardAngle(
gdjs.toDegrees(
@@ -855,8 +861,24 @@ namespace gdjs {
}
/**
* @param angle The targeted direction angle.
* @param speed The rotation speed.
* Rotate the object towards another object position (aiming at the center of the object).
* @param target The target object
* @param speed The rotation speed. 0 for an immediate rotation to the target object.
*/
rotateTowardObject(target: gdjs.RuntimeObject | null, speed: float): void {
if (target === null) {
return;
}
this.rotateTowardPosition(
target.getDrawableX() + target.getCenterX(),
target.getDrawableY() + target.getCenterY(),
speed
);
}
/**
* @param angle The targeted angle.
* @param speed The rotation speed. 0 for an immediate rotation to the target angle.
*/
rotateTowardAngle(angle: float, speed: float): void {
if (speed === 0) {

View File

@@ -953,14 +953,7 @@ namespace gdjs {
* @deprecated
*/
turnTowardObject(obj: gdjs.RuntimeObject | null) {
if (obj === null) {
return;
}
this.rotateTowardPosition(
obj.getDrawableX() + obj.getCenterX(),
obj.getDrawableY() + obj.getCenterY(),
0
);
return this.rotateTowardObject(obj, 0);
}
}
gdjs.registerObject(