-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathisMovingNearObjects.js
27 lines (25 loc) · 1.01 KB
/
isMovingNearObjects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function isNearPlayer(target, player, distance = 2) {
const { x, y } = player
return (
target.x >= x - distance &&
target.x <= x + distance &&
target.y >= y - distance &&
target.y <= y + distance
)
}
/* ------ subscription() can be called to cancel the subscription later ----- */
const subscription = game.subscribeToEvent(
'playerMoves',
(data, context) => {
const { player } = context
const objs = game.filterObjectsInMap(player.map, (obj) =>
isNearPlayer(obj, player)
)
if (objs) console.log(objs)
}
/* -------------------------------------------------------------------------- */
/* optional filter parameter demonstration */
/* -------------------------------------------------------------------------- */
/* , (data, context) => player.name !== 'Facility Managementbot' */
/* -------------------------------------------------------------------------- */
)