Fix(Animation): Drive Pac-Man mouth animation from game loop, not draw callback
The draw callback receives no delta-time, so animation was stuck at 0. Split draw-pacman! into draw (position/rotation) and animate-pacman! (sprite sequence advancement). Animation is now called from the game loop which has the real delta-time. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -147,9 +147,17 @@
|
||||
((key-sprite 'set-x!) (grid->pixel-x (pos 'col)))
|
||||
((key-sprite 'set-y!) (grid->pixel-y (pos 'row))))))
|
||||
|
||||
;; draw-pacman! :: pacman, number -> /
|
||||
;; animate-pacman! :: number -> /
|
||||
;; Advances the Pac-Man sprite animation based on elapsed time.
|
||||
(define (animate-pacman! delta-time)
|
||||
(set! time-since-last-animation (+ time-since-last-animation delta-time))
|
||||
(when (>= time-since-last-animation animation-interval-ms)
|
||||
((pacman-sprite 'set-next!))
|
||||
(set! time-since-last-animation 0)))
|
||||
|
||||
;; draw-pacman! :: pacman -> /
|
||||
;; Draws Pac-Man at its current position with correct rotation.
|
||||
(define (draw-pacman! pacman delta-time)
|
||||
(define (draw-pacman! pacman)
|
||||
(let* ((pos (pacman 'position))
|
||||
(direction (pacman 'direction)))
|
||||
;; Set position
|
||||
@@ -159,12 +167,7 @@
|
||||
(cond ((eq? direction 'right) ((pacman-sprite 'rotate!) rotation-right))
|
||||
((eq? direction 'left) ((pacman-sprite 'rotate!) rotation-left))
|
||||
((eq? direction 'up) ((pacman-sprite 'rotate!) rotation-up))
|
||||
((eq? direction 'down) ((pacman-sprite 'rotate!) rotation-down)))
|
||||
;; Animation
|
||||
(set! time-since-last-animation (+ time-since-last-animation delta-time))
|
||||
(when (>= time-since-last-animation animation-interval-ms)
|
||||
((pacman-sprite 'set-next!))
|
||||
(set! time-since-last-animation 0))))
|
||||
((eq? direction 'down) ((pacman-sprite 'rotate!) rotation-down)))))
|
||||
|
||||
;; draw-ui! :: score, timer -> /
|
||||
;; Draws the score and time limit on screen.
|
||||
@@ -202,7 +205,7 @@
|
||||
;; Draws the full game (registered as draw callback).
|
||||
(define (draw-game! game)
|
||||
(let ((level (game 'level)))
|
||||
(draw-pacman! (level 'pacman) 0)
|
||||
(draw-pacman! (level 'pacman))
|
||||
(draw-key! (level 'key))
|
||||
(draw-coins! (level 'maze))
|
||||
(draw-ui! (level 'score) (level 'timer))
|
||||
@@ -237,6 +240,7 @@
|
||||
(cond ((eq? msg 'set-game-loop!) set-game-loop!)
|
||||
((eq? msg 'set-key-callback!) set-key-callback!)
|
||||
((eq? msg 'start-drawing!) start-drawing!)
|
||||
((eq? msg 'animate-pacman!) animate-pacman!)
|
||||
(else (error "Draw ADT -- Unknown message:" msg))))
|
||||
|
||||
dispatch-draw))))
|
||||
|
||||
@@ -28,9 +28,10 @@
|
||||
((level 'key-press!) key)))
|
||||
|
||||
;; game-loop :: number -> /
|
||||
;; Called each frame for game state updates.
|
||||
;; Called each frame for game state updates and animation.
|
||||
(define (game-loop delta-time)
|
||||
((level 'update!) delta-time))
|
||||
((level 'update!) delta-time)
|
||||
((draw 'animate-pacman!) delta-time))
|
||||
|
||||
;; start! :: -> /
|
||||
;; Starts the game by registering all callbacks.
|
||||
|
||||
Reference in New Issue
Block a user