Rotate logic gate sprites based on direction.

This commit is contained in:
Juliana Sims 2024-05-23 16:08:25 -04:00 committed by David Thompson
parent 00df10dc12
commit bb4d2df5f5
5 changed files with 33 additions and 5 deletions

View file

@ -67,8 +67,11 @@ window.addEventListener("load", async () => {
fillRect: (ctx, x, y, w, h) => ctx.fillRect(x, y, w, h), fillRect: (ctx, x, y, w, h) => ctx.fillRect(x, y, w, h),
fillText: (ctx, text, x, y) => ctx.fillText(text, x, y), fillText: (ctx, text, x, y) => ctx.fillText(text, x, y),
drawImage: (ctx, image, sx, sy, sw, sh, dx, dy, dw, dh) => ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh), drawImage: (ctx, image, sx, sy, sw, sh, dx, dy, dw, dh) => ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh),
restore: (ctx) => ctx.restore(),
save: (ctx) => ctx.save(),
scale: (ctx, sx, sy) => ctx.scale(sx, sy), scale: (ctx, sx, sy) => ctx.scale(sx, sy),
translate: (ctx, x, y) => ctx.translate(x, y), translate: (ctx, x, y) => ctx.translate(x, y),
rotate: (ctx, angle) => ctx.rotate(angle),
setTransform: (ctx, a, b, c, d, e, f) => ctx.setTransform(a, b, c, d, e, f), setTransform: (ctx, a, b, c, d, e, f) => ctx.setTransform(a, b, c, d, e, f),
setImageSmoothingEnabled: (ctx, enabled) => ctx.imageSmoothingEnabled = (enabled == 1) setImageSmoothingEnabled: (ctx, enabled) => ctx.imageSmoothingEnabled = (enabled == 1)
}, },

View file

@ -317,6 +317,15 @@
(hashtable-set! cache x str) (hashtable-set! cache x str)
str))))) str)))))
(define (draw-rotated-tile id 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))
(restore! context)))
(define (draw-player pos alive?) (define (draw-player pos alive?)
(draw-tile context tileset (if alive? 0 20) (vec2-x pos) (vec2-y pos))) (draw-tile context tileset (if alive? 0 20) (vec2-x pos) (vec2-y pos)))
@ -368,7 +377,11 @@
(let ((x (vec2-x pos)) (let ((x (vec2-x pos))
(y (vec2-y pos))) (y (vec2-y pos)))
(draw-tile context tileset 2 x y) (draw-tile context tileset 2 x y)
(draw-tile context tileset id 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))))
(draw-wire-state pos state))) (draw-wire-state pos state)))
(define (draw-electric-switch pos on?) (define (draw-electric-switch pos on?)

View file

@ -31,8 +31,11 @@
fill-rect fill-rect
fill-text fill-text
draw-image draw-image
restore!
save!
scale! scale!
translate! translate!
rotate!
set-transform! set-transform!
set-image-smoothing-enabled!)) set-image-smoothing-enabled!))
@ -66,12 +69,21 @@
(define-foreign draw-image (define-foreign draw-image
"canvas" "drawImage" "canvas" "drawImage"
(ref extern) (ref extern) f64 f64 f64 f64 f64 f64 f64 f64 -> none) (ref extern) (ref extern) f64 f64 f64 f64 f64 f64 f64 f64 -> none)
(define-foreign restore!
"canvas" "restore"
(ref extern) -> none)
(define-foreign save!
"canvas" "save"
(ref extern) -> none)
(define-foreign scale! (define-foreign scale!
"canvas" "scale" "canvas" "scale"
(ref extern) f64 f64 -> none) (ref extern) f64 f64 -> none)
(define-foreign translate! (define-foreign translate!
"canvas" "translate" "canvas" "translate"
(ref extern) f64 f64 -> none) (ref extern) f64 f64 -> none)
(define-foreign rotate!
"canvas" "rotate"
(ref extern) f64 -> none)
(define-foreign set-transform! (define-foreign set-transform!
"canvas" "setTransform" "canvas" "setTransform"
(ref extern) f64 f64 f64 f64 f64 f64 -> none) (ref extern) f64 f64 f64 f64 f64 f64 -> none)

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.8" tiledversion="1.8.6" orientation="orthogonal" renderorder="right-down" width="20" height="15" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="27"> <map version="1.8" tiledversion="1.8.6" orientation="orthogonal" renderorder="right-down" width="20" height="15" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="30">
<tileset firstgid="1" source="tiles.tsx"/> <tileset firstgid="1" source="tiles.tsx"/>
<layer id="1" name="background" width="20" height="15"> <layer id="1" name="background" width="20" height="15">
<data encoding="csv"> <data encoding="csv">

View file

@ -19,15 +19,15 @@
;;; Code: ;;; Code:
(define-module (math) (define-module (math)
#:pure
#:use-module (scheme base)
#:use-module (hoot ffi) #:use-module (hoot ffi)
#:export (random clamp)) #:export (random pi clamp))
(define-foreign random (define-foreign random
"math" "random" "math" "random"
-> f64) -> f64)
(define pi (* 4.0 (atan 1.0)))
(define (clamp x min max) (define (clamp x min max)
(cond ((< x min) min) (cond ((< x min) min)
((> x max) max) ((> x max) max)