diff options
Diffstat (limited to 'physics.scm')
| -rw-r--r-- | physics.scm | 40 |
1 files changed, 19 insertions, 21 deletions
diff --git a/physics.scm b/physics.scm index 773d922..d418539 100644 --- a/physics.scm +++ b/physics.scm @@ -2,7 +2,7 @@ (resolve-entity-collisions resolve-pair aabb-overlap? push-apart push-along-axis aabb-overlap-on-axis entity-center-on-axis push-entity axis->velocity axis->dimension - index-pairs list-set apply-jump detect-on-solid + index-pairs list-set detect-on-solid resolve-tile-collisions-y resolve-tile-collisions-x resolve-tile-collisions-axis tile-push-pos entity-tile-cells pixel->tile build-cell-list apply-velocity apply-velocity-y apply-velocity-x apply-gravity apply-acceleration @@ -32,25 +32,25 @@ ;; for #:skip-pipelines symbol names). ;; Consume #:ay into #:vy and clear it (one-shot acceleration) - (define-pipeline (apply-acceleration acceleration) (entity) + (define-pipeline (apply-acceleration acceleration) (entity scene dt) guard: (entity-ref entity #:gravity? #f) (let ((ay (entity-ref entity #:ay 0)) (vy (entity-ref entity #:vy 0))) (entity-set (entity-set entity #:vy (+ vy ay)) #:ay 0))) ;; Apply gravity to an entity if it has gravity enabled - (define-pipeline (apply-gravity gravity) (entity) + (define-pipeline (apply-gravity gravity) (entity scene dt) guard: (entity-ref entity #:gravity? #f) (entity-set entity #:vy (+ (entity-ref entity #:vy) *gravity*))) ;; Update entity's x by its vx velocity - (define-pipeline (apply-velocity-x velocity-x) (entity) + (define-pipeline (apply-velocity-x velocity-x) (entity scene dt) (let ((x (entity-ref entity #:x 0)) (vx (entity-ref entity #:vx 0))) (entity-set entity #:x (+ x vx)))) ;; Update entity's y by its vy velocity - (define-pipeline (apply-velocity-y velocity-y) (entity) + (define-pipeline (apply-velocity-y velocity-y) (entity scene dt) (let ((y (entity-ref entity #:y 0)) (vy (entity-ref entity #:vy 0))) (entity-set entity #:y (+ y vy)))) @@ -128,16 +128,20 @@ (entity-tile-cells entity tilemap))))) ;; Resolve horizontal collisions with solid tiles - (define-pipeline (resolve-tile-collisions-x tile-collisions-x) (entity tilemap) - (let ((w (entity-ref entity #:width 0)) - (tw (tilemap-tilewidth tilemap))) + (define-pipeline (resolve-tile-collisions-x tile-collisions-x) (entity scene dt) + guard: (scene-tilemap scene) + (let* ((tilemap (scene-tilemap scene)) + (w (entity-ref entity #:width 0)) + (tw (tilemap-tilewidth tilemap))) (resolve-tile-collisions-axis entity tilemap #:vx #:x (lambda (v col row) (tile-push-pos v col tw w))))) ;; Resolve vertical collisions with solid tiles - (define-pipeline (resolve-tile-collisions-y tile-collisions-y) (entity tilemap) - (let ((h (entity-ref entity #:height 0)) - (th (tilemap-tileheight tilemap))) + (define-pipeline (resolve-tile-collisions-y tile-collisions-y) (entity scene dt) + guard: (scene-tilemap scene) + (let* ((tilemap (scene-tilemap scene)) + (h (entity-ref entity #:height 0)) + (th (tilemap-tileheight tilemap))) (resolve-tile-collisions-axis entity tilemap #:vy #:y (lambda (v col row) (tile-push-pos v row th h))))) @@ -175,19 +179,13 @@ (or (not (zero? (tilemap-tile-at tilemap col-left row))) (not (zero? (tilemap-tile-at tilemap col-right row)))))) - (define-pipeline (detect-on-solid on-solid) - (entity tilemap #!optional (other-entities #f)) + (define-pipeline (detect-on-solid on-solid) (entity scene dt) guard: (entity-ref entity #:gravity? #f) - (let* ((on-tile? (and tilemap (tile-ground-below? entity tilemap))) - (on-entity? (and other-entities - (entity-solid-support-below? entity other-entities)))) + (let* ((tilemap (scene-tilemap scene)) + (on-tile? (and tilemap (tile-ground-below? entity tilemap))) + (on-entity? (entity-solid-support-below? entity (scene-entities scene)))) (entity-set entity #:on-ground? (or on-tile? on-entity?)))) - ;; Set vertical acceleration for jump (consumed next frame by apply-acceleration) - (define-pipeline (apply-jump jump) (entity jump-pressed?) - guard: (and jump-pressed? (entity-ref entity #:on-ground? #f)) - (entity-set entity #:ay (- (entity-ref entity #:jump-force *jump-force*)))) - ;; Replace element at idx in lst with val (define (list-set lst idx val) (let loop ((lst lst) (i 0) (acc '())) |
