From e30b2a81b48f5e86474aa2c38d7034d63c0b7c4e Mon Sep 17 00:00:00 2001 From: Gene Pasquet Date: Sun, 18 May 2025 09:18:07 +0100 Subject: Enemies sort of working --- src/macroknight/systems.hy | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'src/macroknight/systems.hy') diff --git a/src/macroknight/systems.hy b/src/macroknight/systems.hy index 8c1bb9c..f503642 100644 --- a/src/macroknight/systems.hy +++ b/src/macroknight/systems.hy @@ -1,4 +1,4 @@ -(import utils [sub-points Direction]) +(import utils [sub-points distance Direction]) ;; Define systems here (setv GRAVITY 5) @@ -40,14 +40,17 @@ (= entity.type "enemy") ;; If the player is on the tile next to the enemy, attack. (let [player (next (entities-by-type entities ["player"])) - dist (sub-points entity.pos player.pos) - direction (.x-from-move Direction dist)] + delta (sub-points entity.pos player.pos) + dist (distance entity.pos player.pos) + direction (.x-from-move Direction delta)] ;; If facing the player and within reach, attack) (when (and (= direction entity.facing) - (< (get dist 1) 1) ;; Same level - (< (abs (get dist 0)) 2) + (< (get delta 1) 1) ;; Same level + (< (abs (get delta 0)) 2) (not entity.attacking)) - (setv entity.attacking True) - (print "attack") - ) + (.attack entity)) + ;; If not facing the player, turn to face + (when (and (< dist 5) + (!= direction entity.facing)) + (.move entity #((if (< (get delta 0) 0) -1 1) 0))) )) -- cgit v1.2.3