Skip to content

Commit e572921

Browse files
committed
Add targets argument to closest(), furthest()
And exclude `source` from targets
1 parent c46ab84 commit e572921

1 file changed

Lines changed: 52 additions & 17 deletions

File tree

src/physics/arcade/ArcadePhysics.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -367,75 +367,110 @@ var ArcadePhysics = new Class({
367367
},
368368

369369
/**
370-
* Finds the Dynamic Body closest to a source point or object.
370+
* Finds the Body or Game Object closest to a source point or object.
371371
*
372-
* If two or more bodies are the exact same distance from the source point, only the first body
372+
* If a `targets` argument is passed, this method finds the closest of those.
373+
* The targets can be Arcade Physics Game Objects, Dynamic Bodies, or Static Bodies.
374+
*
375+
* If no `targets` argument is passed, this method finds the closest Dynamic Body.
376+
*
377+
* If two or more targets are the exact same distance from the source point, only the first target
373378
* is returned.
374379
*
375380
* @method Phaser.Physics.Arcade.ArcadePhysics#closest
376381
* @since 3.0.0
377382
*
378383
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
384+
* @param {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[]|Phaser.GameObjects.GameObject[])} [targets] - The targets.
379385
*
380-
* @return {Phaser.Physics.Arcade.Body} The closest Dynamic Body to the given source point.
386+
* @return {?(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject)} The target closest to the given source point.
381387
*/
382-
closest: function (source)
388+
closest: function (source, targets)
383389
{
384-
var bodies = this.world.bodies;
390+
if (!targets)
391+
{
392+
targets = this.world.bodies.entries;
393+
}
385394

386395
var min = Number.MAX_VALUE;
387396
var closest = null;
388397
var x = source.x;
389398
var y = source.y;
399+
var len = targets.length;
390400

391-
bodies.iterate(function (target)
401+
for (var i = 0; i < len; i++)
392402
{
393-
var distance = DistanceSquared(x, y, target.center.x, target.center.y);
403+
var target = targets[i];
404+
var body = target.body || target;
405+
406+
if (source === target || source === body || source === body.gameObject || source === body.center)
407+
{
408+
continue;
409+
}
410+
411+
var distance = DistanceSquared(x, y, body.center.x, body.center.y);
394412

395413
if (distance < min)
396414
{
397415
closest = target;
398416
min = distance;
399417
}
400-
401-
});
418+
}
402419

403420
return closest;
404421
},
405422

406423
/**
407-
* Finds the Dynamic Body farthest from a source point or object.
424+
* Finds the Body or Game Object farthest from a source point or object.
408425
*
409-
* If two or more bodies are the exact same distance from the source point, only the first body
426+
* If a `targets` argument is passed, this method finds the farthest of those.
427+
* The targets can be Arcade Physics Game Objects, Dynamic Bodies, or Static Bodies.
428+
*
429+
* If no `targets` argument is passed, this method finds the farthest Dynamic Body.
430+
*
431+
* If two or more targets are the exact same distance from the source point, only the first target
410432
* is returned.
411433
*
412434
* @method Phaser.Physics.Arcade.ArcadePhysics#furthest
413435
* @since 3.0.0
414436
*
415437
* @param {any} source - Any object with public `x` and `y` properties, such as a Game Object or Geometry object.
438+
* @param {(Phaser.Physics.Arcade.Body[]|Phaser.Physics.Arcade.StaticBody[]|Phaser.GameObjects.GameObject[])} [targets] - The targets.
416439
*
417-
* @return {Phaser.Physics.Arcade.Body} The Dynamic Body furthest away from the given source point.
440+
* @return {?(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody|Phaser.GameObjects.GameObject)} The target farthest from the given source point.
418441
*/
419-
furthest: function (source)
442+
furthest: function (source, targets)
420443
{
421-
var bodies = this.world.bodies;
444+
if (!targets)
445+
{
446+
targets = this.world.bodies.entries;
447+
}
422448

423449
var max = -1;
424450
var farthest = null;
425451
var x = source.x;
426452
var y = source.y;
453+
var len = targets.length;
427454

428-
bodies.iterate(function (target)
455+
for (var i = 0; i < len; i++)
429456
{
430-
var distance = DistanceSquared(x, y, target.center.x, target.center.y);
457+
var target = targets[i];
458+
var body = target.body || target;
459+
460+
if (source === target || source === body || source === body.gameObject || source === body.center)
461+
{
462+
continue;
463+
}
464+
465+
var distance = DistanceSquared(x, y, body.center.x, body.center.y);
431466

432467
if (distance > max)
433468
{
434469
farthest = target;
435470
max = distance;
436471
}
437472

438-
});
473+
}
439474

440475
return farthest;
441476
},

0 commit comments

Comments
 (0)