Add smooth sub-tile interpolation for Pac-Man and ghosts

Entities now move smoothly between tiles instead of snapping. Previous
positions are tracked in pacman and ghost ADTs; the draw layer linearly
interpolates between prev and current based on movement timer progress.

Residual time is carried across movement ticks for consistent speed at
varying frame rates. Teleportation and ghost house exits call sync-prev!
to prevent cross-map interpolation artifacts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
joren
2026-03-23 11:52:00 +01:00
parent 9028dd031c
commit f251478dd6
4 changed files with 88 additions and 22 deletions

View File

@@ -4,8 +4,9 @@
;; Pac-Man ADT ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Manages the logical state of the player: grid position and current
;; direction. Contains NO graphics code.
;; Manages the logical state of the player: grid position, direction, and
;; previous position for smooth rendering interpolation. Contains NO
;; graphics code.
(define-library (pacman-project adt pacman)
(import (scheme base)
@@ -18,7 +19,9 @@
;; Creates a Pac-Man object at the given start position (row, col).
(define (make-pacman start-row start-col)
(let ((position (make-position start-row start-col))
(direction 'right))
(direction 'right)
(prev-row start-row)
(prev-col start-col))
;; position! :: position -> /
(define (position! new-position)
@@ -29,11 +32,20 @@
(set! direction new-direction))
;; move! :: number, number -> /
;; Moves Pac-Man by a delta on the grid.
;; Saves previous position, then moves by delta on the grid.
(define (move! delta-row delta-col)
(set! prev-row (position 'row))
(set! prev-col (position 'col))
((position 'row!) (+ (position 'row) delta-row))
((position 'col!) (+ (position 'col) delta-col)))
;; sync-prev! :: -> /
;; Sets previous position to current. Call after teleportation
;; to prevent interpolation across the map.
(define (sync-prev!)
(set! prev-row (position 'row))
(set! prev-col (position 'col)))
;; dispatch-pacman :: symbol -> any
(define (dispatch-pacman msg)
(cond ((eq? msg 'position) position)
@@ -41,6 +53,9 @@
((eq? msg 'direction) direction)
((eq? msg 'direction!) direction!)
((eq? msg 'move!) move!)
((eq? msg 'prev-row) prev-row)
((eq? msg 'prev-col) prev-col)
((eq? msg 'sync-prev!) sync-prev!)
(else (error "Pac-Man ADT -- Unknown message:" msg))))
dispatch-pacman))))