pyxen.GridBody
A GridBody is a grid-based collision component that interacts with a GridMap.
For collision concepts, see Collision.
Constructor
GridBody(
*,
pos=None,
size=None,
move=None,
tag=None,
mask=None
)
All parameters are keyword-only.
Parameters
| Parameter | Type | Description |
|---|---|---|
pos | (x, y) | Initial position in pixels |
size | (w, h) | Body size in pixels |
move | (x, y) | Movement vector |
tag | int | Body tag |
mask | (int, …) | Tuple of tile tags to collide with |
Example
e = world.spawn(
name="hero",
sprite=sprite,
layer=LAYER_HERO,
body=GridBody(
pos=(64, 64),
size=(16, 16),
move=(0, 0),
tag=TAG_PLAYER,
mask=(TAG_WALL, TAG_MONSTER)
)
)
Attaching a GridBody
During spawn():
player = world.spawn(
body=GridBody(pos=(32, 32), size=(16, 16))
)
Or later:
entity = world.spawn()
entity.body = GridBody(size=(16, 16))
Properties
move
Set the movement vector with a tuple or Vector2i:
body.move = (1, 0)
Reading move returns a Vector2i:
m = body.move
print(m.x, m.y)
Planned Properties
| Property | Description |
|---|---|
pos | Body position |
size | Body size |
tag | Body tag |
mask | Collision mask |
Tag System
tag
Each body has a single tag:
body.tag = TAG_PLAYER
mask
The mask defines which tags this body collides with:
body.mask = (TAG_WALL, TAG_MONSTER)
GridBody + GridMap Interaction
A GridBody collides with tiles in a GridMap and other GridBodies based on tag filtering.
GridBody must be a child of a GridMap entity for collisions to resolve.
Movement Example
body = player.body
if input.keyboard.left.down:
body.move = (-1, 0)
if input.keyboard.right.down:
body.move = (1, 0)
Minimal Setup
level = world.spawn(
map=GridMap(
rows=10,
columns=10,
size=(16, 16)
)
)
player = world.spawn(
parent=level,
body=GridBody(
pos=(16, 16),
size=(16, 16),
tag=1,
mask=(2,)
)
)
Common Pattern: Player vs Walls
TAG_PLAYER = 1
TAG_WALL = 2
player = world.spawn(
body=GridBody(
pos=(32, 32),
size=(16, 16),
tag=TAG_PLAYER,
mask=(TAG_WALL,)
)
)
If a tile is set with:
map.set(row=0, column=5, tag=TAG_WALL)
The player will collide with it.