aboutsummaryrefslogtreecommitdiff
path: root/src/macroknight/systems.hy
diff options
context:
space:
mode:
authorGene Pasquet <dev@etenil.net>2025-05-18 09:18:07 +0100
committerGene Pasquet <dev@etenil.net>2025-05-18 09:18:07 +0100
commite30b2a81b48f5e86474aa2c38d7034d63c0b7c4e (patch)
tree585e05b0238b36761a6616fa2c6c8627e96876fc /src/macroknight/systems.hy
parent1cf28e193729ee64a17e6464ce7310c67f5eae34 (diff)
Enemies sort of working
Diffstat (limited to 'src/macroknight/systems.hy')
-rw-r--r--src/macroknight/systems.hy19
1 files changed, 11 insertions, 8 deletions
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)))
))