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