aboutsummaryrefslogtreecommitdiff
path: root/demo/sandbox.scm
diff options
context:
space:
mode:
Diffstat (limited to 'demo/sandbox.scm')
-rw-r--r--demo/sandbox.scm58
1 files changed, 14 insertions, 44 deletions
diff --git a/demo/sandbox.scm b/demo/sandbox.scm
index d7bd53f..f757c7f 100644
--- a/demo/sandbox.scm
+++ b/demo/sandbox.scm
@@ -27,41 +27,7 @@
;; ── Mutable demo state ──────────────────────────────────────────────────────
-(define *demo-t* 0.0)
-(define *shelf-endpoints* #f)
-(define *shelf-tween* #f)
-(define *shelf-origin* #f)
-
-;; ── Tween helpers ────────────────────────────────────────────────────────────
-
-(define (other-endpoint x endpoints)
- (let ((lo (car endpoints))
- (hi (cdr endpoints)))
- (if (< (abs (- x lo)) (abs (- x hi))) hi lo)))
-
-(define (make-ping-pong-tween leader endpoints)
- (make-tween leader
- props: `((#:x . ,(other-endpoint (entity-ref leader #:x 0) endpoints)))
- duration: 3500 ease: 'sine-in-out))
-
-(define (scene-replace-group-origin! scene gid new-origin)
- (scene-entities-set! scene
- (map (lambda (e)
- (if (and (entity-ref e #:group-origin? #f)
- (eq? (entity-ref e #:group-id) gid))
- new-origin
- e))
- (scene-entities scene))))
-
-(define (advance-shelf-tween! scene dt)
- (when (and *shelf-tween* *shelf-origin*)
- (let ((gid (entity-ref *shelf-origin* #:group-id)))
- (receive (tw2 e0) (tween-step *shelf-tween* *shelf-origin* dt)
- (set! *shelf-tween* (if (tween-finished? tw2)
- (make-ping-pong-tween e0 *shelf-endpoints*)
- tw2))
- (set! *shelf-origin* e0)
- (scene-replace-group-origin! scene gid e0)))))
+(define *demo-t* 0.0)
;; ── Tilemap builder ──────────────────────────────────────────────────────────
@@ -143,13 +109,16 @@
;; ── Scene builder ───────────────────────────────────────────────────────────
-(define (init-shelf-tween! shelf-list tw)
+(define (attach-shelf-tween! shelf-list tw)
(let* ((origin (car shelf-list))
(x-left (entity-ref origin #:x 0))
- (x-right (+ x-left (* 6 tw))))
- (set! *shelf-origin* origin)
- (set! *shelf-endpoints* (cons x-left x-right))
- (set! *shelf-tween* (make-ping-pong-tween origin *shelf-endpoints*))))
+ (x-right (+ x-left (* 6 tw)))
+ (tweened (entity-set origin #:tween
+ (make-tween origin
+ props: `((#:x . ,x-right))
+ duration: 3500 ease: 'sine-in-out
+ repeat: -1 yoyo?: #t))))
+ (cons tweened (cdr shelf-list))))
(define (make-sandbox-scene game)
(let* ((reg (load-prefabs "demo/assets/sandbox-groups.scm" (engine-mixins) '()))
@@ -160,14 +129,15 @@
(gw (game-width game))
(gh (game-height game))
(tm (make-sandbox-tilemap ts tw th gw gh))
- (shelf-list (instantiate-group-prefab reg 'shelf-platform
- (* 10 tw) (- gh (* 6 th))))
+ (shelf-list (attach-shelf-tween!
+ (instantiate-group-prefab reg 'shelf-platform
+ (* 10 tw) (- gh (* 6 th)))
+ tw))
(raft-list (instantiate-group-prefab reg 'collision-raft
120 (- gh (* 14 th))))
(bots (list (make-demo-bot 80 80 tw th 0)
(make-demo-bot 220 60 tw th 1)
(make-demo-bot 380 100 tw th 2))))
- (init-shelf-tween! shelf-list tw)
(make-scene
entities: (append shelf-list raft-list (spawn-boxes tw th) bots)
tilemap: tm
@@ -191,7 +161,7 @@
(set! *demo-t* (+ *demo-t* dt))
(let* ((scene (game-scene game))
(tm (scene-tilemap scene)))
- (advance-shelf-tween! scene dt)
+ (scene-update-entities scene (lambda (e) (step-tweens e dt)))
(scene-update-entities scene (lambda (e) (integrate-entity e dt tm)))
(scene-sync-groups! scene)
(scene-resolve-collisions scene)