ludic/examples/library/physics_tiles.ludic
Orkuncakilkaya bf36bc8a8f refactor(runtime,packages,examples): named constants, package enums, idiom sweep
- runtime: HEADLESS_FRAME_PATH, STICK_DEADZONE / STICK_LEFT_X/Y, key and
  byte codes as char literals throughout (`k == 'w'`, `fill(rt_map, ' ', …)`)
- ludic.gameplay/stats: drop the duplicate `stat_field` (it answered "atk"
  for every build stat); Stats.base uses stats_field_name
- ludic.shooter: compare aim modes and fire patterns with AimMode.* and
  WeaponPattern.* instead of raw ints; STICK_RIGHT_X/Y
- ludic.npcai: DecisionMade / brain_set_state use AiState.*
- examples/games/menu.ludic uses Font.load / Ui.* with FONT_PATH and
  BACKDROP named; strings.ludic header says what it prints
- whole tree: `x = x + 1` → `x += 1` (single-term right-hand sides only),
  `0 - x` → `-x`, ASCII codes → char literals; every .ludic and every
  ```ludic fence reformatted with the fixed formatter (whitespace only)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 01:12:26 +03:00

84 lines
3.8 KiB
Text

# physics_tiles.ludic — the tile-grid broadphase of the #65 movement system:
# `esys_move` resolves a Body against the Map.* tilemap (the platformer /
# top-down world), configured by a single `Solids { tile, wall, oneway }` entity.
# Reuses the same deterministic swept-AABB core as entity collision; verifies a
# tile floor, a tile wall, and a one-way tile platform (land from above, rise
# through from below).
#
# Deterministic, so a full run prints: 112 1 80 1 48 1 60
program PhysicsTiles {
property Position { x: int = 0, y: int = 0 }
property Body {
vx: fixed = 0.0, vy: fixed = 0.0,
gravity: fixed = 0.0, max_fall: fixed = 0.0,
rx: fixed = 0.0, ry: fixed = 0.0,
policy: int = 0,
on_ground: int = 0, hit_wall: int = 0, hit_ceiling: int = 0
}
property Collider { w: int = 0, h: int = 0, offx: int = 0, offy: int = 0, is_trigger: int = 0, one_way: int = 0, layer: int = 0, mask: int = 0 }
property Solids { tile: int = 0, wall: int = 0, oneway: int = 0 }
function clear_prop(name: pointer) -> void {
let p = World.prop_id(name)
if p < 0 { return }
var e = World.query_next(p, 0)
while e >= 0 { despawn e; e = World.query_next(p, 0) }
}
function clear() -> void { clear_prop("Body"); clear_prop("Collider") }
entry {
let pb = World.prop_id("Body")
let pp = World.prop_id("Position")
let f_x = World.field_id(pp, "x")
let f_y = World.field_id(pp, "y")
let f_vx = World.field_id(pb, "vx")
let f_grd = World.field_id(pb, "on_ground")
let f_hw = World.field_id(pb, "hit_wall")
# 16px tiles; the map is now a loaded Tiled map (issue #69). Its `collision`
# layer projects to the same byte tilemap: a non-zero GID becomes '#' (35,
# solid), and a GID whose tile carries the bool `oneway` property becomes
# '=' (61), a one-way platform — so esys_move sees the identical grid.
# one-way platform, cols 10-12 (y 64); wall column, col 6, rows 5-7;
# solid floor, row 8 (top edge y 128).
Tiled.load("assets/tiled-fixtures/physics_map.tmx")
spawn Config { Solids { tile: 16, wall: 35, oneway: 61 } }
# ---- T1. fall and land on the tile floor -------------------------------
clear()
spawn Runner { Position { x: 32, y: 16 }, Body { gravity: fixed(1), max_fall: fixed(6) }, Collider { w: 16, h: 16 } }
var a = World.query_next(pb, 0)
var i = 0
while i < 80 { tick_fixed(); i += 1 }
print(World.get(a, pp, f_y)) # 112 — feet rest on the tile floor top (128)
print(World.get(a, pb, f_grd)) # 1
# ---- T2. run into a tile wall column -----------------------------------
clear()
spawn Runner { Position { x: 16, y: 112 }, Body { gravity: fixed(1), max_fall: fixed(6) }, Collider { w: 16, h: 16 } }
a = World.query_next(pb, 0)
i = 0
while i < 60 { World.set(a, pb, f_vx, fixed(4)); tick_fixed(); i += 1 }
print(World.get(a, pp, f_x)) # 80 — right edge stops at the wall column (96)
print(World.get(a, pb, f_hw)) # 1
# ---- T3a. land on a one-way tile platform from above -------------------
clear()
spawn Runner { Position { x: 176, y: 16 }, Body { gravity: fixed(1), max_fall: fixed(6) }, Collider { w: 16, h: 16 } }
a = World.query_next(pb, 0)
i = 0
while i < 40 { tick_fixed(); i += 1 }
print(World.get(a, pp, f_y)) # 48 — feet on the one-way top (64)
print(World.get(a, pb, f_grd)) # 1
# ---- T3b. rise straight up through the one-way tile --------------------
clear()
spawn Riser { Position { x: 176, y: 120 }, Body { vy: fixed(-4), policy: 1 }, Collider { w: 16, h: 16 } }
a = World.query_next(pb, 0)
i = 0
while i < 15 { tick_fixed(); i += 1 }
print(World.get(a, pp, f_y)) # 60 — passed through (120 - 4*15), above the platform
quit()
}
}