From 84c617b7bae507abff6305a55a39a23bce1af9ba Mon Sep 17 00:00:00 2001 From: Peter Tillemans Date: Thu, 23 May 2024 23:05:09 +0200 Subject: [PATCH] make other avoid each other, update readme and remove pk's --- README.org | 6 +++--- game/model/hero.scm | 5 ++--- game/model/key.scm | 2 +- game/model/other.scm | 32 ++++++++++++++++++++++++++++---- game/model/runner.scm | 2 +- game/render/key.scm | 1 - 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.org b/README.org index 580f224..5db08e5 100644 --- a/README.org +++ b/README.org @@ -36,7 +36,7 @@ let the _others_ fall in so you can pass over their head. So now, go out, collect the keys and maintain a healthy diet! -* Game Plan [11/18] +* Game Plan [12/20] - [X] start project organization - [X] select some assets to start with @@ -53,6 +53,8 @@ So now, go out, collect the keys and maintain a healthy diet! - [ ] automate upload to itch.io - [X] create other's AI - [X] add keys and portal + - [X] make other avoid each other + - [ ] Detect success reaching the goal and progress to next level - [ ] add foods and bloat indicator - [ ] scale hero waist related to bloat level - [ ] create more levels @@ -75,8 +77,6 @@ Untar the filename, enter the newly created folder and launch `bloatrunner/bloat #+END_SRC - - *** TODO make an upload script which cleans up the filename The current filename contains the GUIX UUID which is ugly and not very diff --git a/game/model/hero.scm b/game/model/hero.scm index 9f9ec89..3dda191 100644 --- a/game/model/hero.scm +++ b/game/model/hero.scm @@ -58,9 +58,8 @@ (define (blocked-by-door? level keys position) "return true if the hero is blocked by a door" (let ((door-position (level-find-goal level))) - (pk 'blocked-by-door? keys (null? keys) position - (and (collides? position door-position) - (not (null? keys)))))) + (and (collides? position door-position) + (not (null? keys))))) (define (hero-update hero level inputs keys dt) (let ((new-runner (runner-update (hero-runner hero) level inputs dt))) diff --git a/game/model/key.scm b/game/model/key.scm index 03dd285..748378e 100644 --- a/game/model/key.scm +++ b/game/model/key.scm @@ -21,7 +21,7 @@ ) (define (key-collides? key position) - (< (pk "key distance" (vec2-magnitude (vec2- (key-position key) position))) 4)) + (< (vec2-magnitude (vec2- (key-position key) position)) 4)) (define (keys-load level) diff --git a/game/model/other.scm b/game/model/other.scm index bc9ec0b..55e0a6f 100644 --- a/game/model/other.scm +++ b/game/model/other.scm @@ -73,10 +73,31 @@ #t ;; if not on ladder, it's safe ))) +(define (other-distance a b) + (vec2-magnitude (vec2- (other-position a) (other-position b)))) + +;; find closest other to given other +(define (find-closest-other other others) + (let ((other-others (remove (lambda (o) (equal? o other)) others))) + (fold (lambda (a b) + (if (< (other-distance a other) (other-distance b other)) a b)) + (car other-others) + (cdr other-others)))) + ;; Generate inputs for the other based on the level and ;; the hero and other others. (define (determine-actions other level hero others) - (let ((actions '())) + (let ((actions '()) + (closest (find-closest-other other others))) + ;; keep distance from each other + (if (< (other-distance closest other) (* 2 level-cell-size)) + (let ((diff (vec2- (other-position closest) (other-position other)))) + (when (< (vec2-x diff) 0) (set! actions (cons 'right actions))) + (when (> (vec2-x diff) 0) (set! actions (cons 'left actions))) + (when (< (vec2-y diff) 0) (set! actions (cons 'up actions))) + (when (> (vec2-y diff) 0) (set! actions (cons 'down actions))))) + + ;; move towards hero without falling off ladders (when (safe-ladder-position? (other-position other) level) (when (< (hero-x hero) (other-x other)) (set! actions (cons 'left actions))) @@ -86,11 +107,14 @@ (set! actions (cons 'down actions))) (when (> (hero-y hero) (other-y other)) (set! actions (cons 'up actions))) + actions)) + + -(define (other-update other level hero others dt) -(let ((inputs (determine-actions other level hero others))) - (other-with-runner other (runner-update (other-runner other) level inputs dt)))) + (define (other-update other level hero others dt) + (let ((inputs (determine-actions other level hero others))) + (other-with-runner other (runner-update (other-runner other) level inputs dt)))) diff --git a/game/model/runner.scm b/game/model/runner.scm index 9d66416..3274380 100644 --- a/game/model/runner.scm +++ b/game/model/runner.scm @@ -72,7 +72,7 @@ (define (safe-move level pos move) (let ((new-pos (vec2+ pos move))) (if (level-tile-blocked? level new-pos) - (pk "snap position " pos "to" (round-position pos)) ;; snap to grid on collision with hard scenery + (round-position pos) ;; snap to grid on collision with hard scenery new-pos ))) diff --git a/game/render/key.scm b/game/render/key.scm index 8b3d345..c9abe52 100644 --- a/game/render/key.scm +++ b/game/render/key.scm @@ -15,7 +15,6 @@ (texture-atlas-ref tile-atlas key-index) (vec2+ (key-position key) (vec2 -8 0)))) - (define (render-goal keys goal-position) (let ((goal-index (if (null? keys) open-goal-index closed-goal-index))) (draw-sprite