From 84840ede6646ed793b61cdd889d3f57ab05e9311 Mon Sep 17 00:00:00 2001 From: Gene Pasquet Date: Wed, 8 Apr 2026 01:32:55 +0100 Subject: Refactor to be functional --- world.scm | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) (limited to 'world.scm') diff --git a/world.scm b/world.scm index e637e90..14840c8 100644 --- a/world.scm +++ b/world.scm @@ -42,31 +42,29 @@ background) ; #f or (r g b) / (r g b a) for framebuffer clear (define (scene-add-entity scene entity) - (scene-entities-set! scene (append (scene-entities scene) (list entity))) - scene) + (update-scene scene + entities: (append (scene-entities scene) (list entity)))) (define (scene-update-entities scene . procs) - "Apply each proc in sequence to the scene's entities; each proc maps over all entities. - The scene's entity list is replaced once with the final result." - (scene-entities-set! scene - (fold (lambda (proc es) (map proc es)) - (scene-entities scene) - procs)) - scene) + "Apply each proc in sequence to the scene's entities; returns a new scene." + (update-scene scene + entities: (fold (lambda (proc es) (map proc es)) + (scene-entities scene) + procs))) (define (scene-filter-entities scene pred) - "Remove all entities from scene that do not satisfy pred." - (scene-entities-set! scene - (filter pred (scene-entities scene))) - scene) + "Keep only entities satisfying pred; returns a new scene." + (update-scene scene + entities: (filter pred (scene-entities scene)))) ;; Center camera on entity. Clamps to >= 0 on both axes. - ;; viewport-w and viewport-h are the game window dimensions (pixels). - (define (camera-follow! camera entity viewport-w viewport-h) + ;; Returns a new camera struct. + (define (camera-follow camera entity viewport-w viewport-h) (let* ((entity-x (entity-ref entity #:x 0)) (entity-y (entity-ref entity #:y 0))) - (camera-x-set! camera (inexact->exact (floor (max 0 (- entity-x (/ viewport-w 2)))))) - (camera-y-set! camera (inexact->exact (floor (max 0 (- entity-y (/ viewport-h 2)))))))) + (update-camera camera + x: (inexact->exact (floor (max 0 (- entity-x (/ viewport-w 2))))) + y: (inexact->exact (floor (max 0 (- entity-y (/ viewport-h 2)))))))) ;; Returns the first entity in scene whose #:tags list contains tag, or #f. (define (scene-find-tagged scene tag) @@ -107,11 +105,10 @@ (entity-ref e #:group-local-y 0)))) e))) - ;; Snap member #:x/#:y to origin + #:group-local-x/y. Call after moving origins (tweens, etc.). - (define (scene-sync-groups! scene) + ;; Snap member #:x/#:y to origin + #:group-local-x/y. Returns a new scene. + (define (scene-sync-groups scene) (let ((origins (group-origin-alist (scene-entities scene)))) - (scene-entities-set! scene - (map (lambda (e) (sync-member-to-origin e origins)) - (scene-entities scene))) - scene)) + (update-scene scene + entities: (map (cut sync-member-to-origin <> origins) + (scene-entities scene))))) ) -- cgit v1.2.3