pyxen.world.near()
The entities within a radius of a point, nearest first.
world.near(x, y, radius, *components, without=None)
Returns the entities whose position is within radius of (x, y) (a distance of radius or less), nearest first. Two at the same distance always come in the same order. The components and without work as in world.all(); with no component, every entity counts.
It is fast with many entities: Pyxen keeps them in a grid by position and looks only where the circle is, so a shot that asks for the foes near it costs about the same with 50 foes as with 5000.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | float | The point’s x, in world pixels |
y | float | The point’s y, in world pixels |
radius | float | How far from the point, in pixels. 0 finds what stands on it |
*components | str | Component names an entity must all have, as in world.all() |
without | tuple | Component names an entity must not have |
A Shot That Hits
Each shot hurts the foes it touches, without testing every foe in the game:
for shot in world.all("shot"):
for foe in world.near(shot.x, shot.y, 10, "foe"):
foe.foe.hp -= 1
if foe.foe.hp <= 0:
world.destroy(foe)
A foe destroyed here is never found again, by this shot or the next.
Auto-Aim
The first entity is the nearest one, so next takes it, or None when there’s none in range:
hero = world.get(name="hero")
target = next(world.near(hero.x, hero.y, 120, "foe", without=("dead",)), None)
if target is not None:
fire_at(target.x, target.y)
A tower picks its target the same way, from its own position and its range.
Positions
The distance is measured between the point and each entity’s position in the world: its x and y, through its parents’ positions, rotations and scales, where it is drawn. For entities at the root, or under a parent at (0, 0) that doesn’t turn or scale (a level’s foes, as is usual), that is their x and y.
A position written earlier in the same frame is seen at once: move a shot, then ask what is near it.
Edge Cases
- A negative radius finds nothing.
math.inffinds every entity, nearest first. - The result says where things were when it was asked: ask again after they move.