From 9027df5f208099e01e27820675cbbb10c303d854 Mon Sep 17 00:00:00 2001
From: David Thompson <dthompson2@worcester.edu>
Date: Sun, 26 May 2024 15:36:39 -0400
Subject: [PATCH] Add logic gate animations.

---
 game.scm | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/game.scm b/game.scm
index 9b486c8..42994c3 100644
--- a/game.scm
+++ b/game.scm
@@ -388,6 +388,21 @@
   (56 .25)
   (76 .25)
   (96 .25))
+(define-animation anim:and-gate
+  (42  .4)
+  (115 .4)
+  (135 .4)
+  (155 .4))
+(define-animation anim:or-gate
+  (43  .4)
+  (116 .4)
+  (136 .4)
+  (156 .4))
+(define-animation anim:xor-gate
+  (44  .4)
+  (117 .4)
+  (137 .4)
+  (157 .4))
 
 (define number->string*
   (let ((cache (make-eq-hashtable)))    ; assuming fixnums only
@@ -397,13 +412,13 @@
             (hashtable-set! cache x str)
             str)))))
 
-(define (draw-rotated-tile id pos angle)
+(define (draw-rotated-animation anim pos angle)
   (let ((hw (* tile-width 0.5))
         (hh (* tile-height 0.5)))
     (save! context)
     (translate! context (+ (vec2-x pos) hw) (+ (vec2-y pos) hh))
     (rotate! context angle)
-    (draw-tile context tileset id (- hw) (- hh))
+    (draw-animation context anim (- hw) (- hh))
     (restore! context)))
 
 (define (draw-player pos alive?)
@@ -467,15 +482,15 @@
 (define (draw-gate pos open?)
   (draw-tile context tileset (if open? 46 45) (vec2-x pos) (vec2-y pos)))
 
-(define (draw-logic-gate pos direction state id)
+(define (draw-logic-gate pos direction state anim)
   (let ((x (vec2-x pos))
         (y (vec2-y pos)))
     (draw-tile context tileset 2 x y)
     (match direction
-      ('right (draw-tile context tileset id x y))
-      ('left (draw-rotated-tile id pos pi))
-      ('up (draw-rotated-tile id pos (* pi 1.5)))
-      ('down (draw-rotated-tile id pos (* pi 0.5))))
+      ('right (draw-animation context anim x y))
+      ('left (draw-rotated-animation anim pos pi))
+      ('up (draw-rotated-animation anim pos (* pi 1.5)))
+      ('down (draw-rotated-animation anim pos (* pi 0.5))))
     (draw-wire-state pos state)))
 
 (define (draw-electric-switch pos on?)
@@ -500,9 +515,9 @@
     (('gem pos) (draw-gem pos))
     (('ghost-gem pos) (draw-ghost-gem pos))
     (('gate pos open?) (draw-gate pos open?))
-    (('and-gate pos direction state) (draw-logic-gate pos direction state 42))
-    (('or-gate pos direction state) (draw-logic-gate pos direction state 43))
-    (('xor-gate pos direction state) (draw-logic-gate pos direction state 44))
+    (('and-gate pos direction state) (draw-logic-gate pos direction state anim:and-gate))
+    (('or-gate pos direction state) (draw-logic-gate pos direction state anim:or-gate))
+    (('xor-gate pos direction state) (draw-logic-gate pos direction state anim:xor-gate))
     (('electric-switch pos on?) (draw-electric-switch pos on?))
     (('electron-warp pos state) (draw-electron-warp pos state))))
 
@@ -623,7 +638,10 @@
       (update-animation anim:electron-head dt)
       (update-animation anim:electron-tail dt)
       (update-animation anim:gem dt)
-      (update-animation anim:ghost-gem dt))
+      (update-animation anim:ghost-gem dt)
+      (update-animation anim:and-gate dt)
+      (update-animation anim:or-gate dt)
+      (update-animation anim:xor-gate dt))
     (clear-rect context 0.0 0.0 *canvas-width* *canvas-height*)
     (set-transform! context 1.0 0.0 0.0 1.0 0.0 0.0)
     (scale! context *canvas-scale* *canvas-scale*)