Add inclusive property to triggers

This commit is contained in:
James Campbell
2026-07-25 12:59:33 -04:00
parent 59e4bcce1c
commit 3ad36d2911

View File

@@ -67,11 +67,13 @@ class _Directional:
range: cap the distance in that direction (tiles); omit = anywhere.
aligned: also require overlap on the perpendicular axis, i.e. *directly*
left/right (same rows) or *directly* above/below (same columns).
inclusive: specify if the trap tile itself counts in the given direction.
"""
def __init__(self, direction, rng, aligned):
def __init__(self, direction, rng, aligned, inclusive):
self.dir = direction
self.rng = None if rng is None else float(rng)
self.aligned = bool(aligned)
self.inclusive = bool(inclusive)
def evaluate(self, trap, game, dt):
r = trap.sensor_rect()
@@ -81,24 +83,28 @@ class _Directional:
if self.aligned and not (p.bottom > r.top and p.top < r.bottom):
return False
if d == "left":
if p.centerx >= r.left:
ref = r.right if self.inclusive else r.right
if p.centerx >= ref:
return False
dist = r.left - p.centerx
dist = ref - p.centerx
else:
if p.centerx <= r.right:
ref = r.left if self.inclusive else r.right
if p.centerx <= ref:
return False
dist = p.centerx - r.right
dist = p.centerx - ref
else: # above / below
if self.aligned and not (p.right > r.left and p.left < r.right):
return False
if d == "above":
if p.centery >= r.top:
ref = r.bottom if self.inclusive else r.top
if p.centery >= ref:
return False
dist = r.top - p.centery
else:
if p.centery <= r.bottom:
ref = r.top if self.inclusive else r.bottom
if p.centery <= ref:
return False
dist = p.centery - r.bottom
dist = p.centery - ref
return self.rng is None or dist <= self.rng * trap.tile
def reset(self):
@@ -160,7 +166,7 @@ def make_condition(spec):
if "within" in spec:
return _Within(spec["within"])
if "dir" in spec:
return _Directional(spec["dir"], spec.get("range"), spec.get("aligned", False))
return _Directional(spec["dir"], spec.get("range"), spec.get("aligned", False), spec.get("inclusive", False))
raise ValueError(f"unrecognized trigger condition: {spec!r}")