Add bombs and explodable bricks

This commit is contained in:
Juliana Sims 2024-05-24 12:46:15 -04:00
parent 23f034a868
commit feb5b8f3d6
No known key found for this signature in database
GPG key ID: 2A00BD4B0090029E
6 changed files with 86 additions and 2 deletions

View file

@ -9,6 +9,8 @@
^switched-emitter
^floor-switch
^gate
^bomb
^brick
^gem
^ghost-gem
^and-gate
@ -28,6 +30,13 @@
('electron-head 'electron-tail)
('electron-tail 'copper)))
(define (grid-electrified? neighbor-grid)
(let lp ((i 0))
(cond
((= i (vector-length neighbor-grid)) #f)
((eq? (vector-ref neighbor-grid i) 'electron-head) #t)
(else (lp (+ i 1))))))
(define (electron-head-count neighbor-grid)
(define (check state)
(match state
@ -92,6 +101,27 @@
(('describe) `(wall ,position ,type))
(('collide other offset grid-info) #f)))
(define (^brick bcom x y)
(define position (vector x y 1))
(define alive? (spawn ^cell #t))
(define exploding? (spawn ^cell))
(match-lambda*
(('type) 'brick)
(('position) position)
(('tick grid-info) #f)
(('post-tick grid-info)
(when ($ exploding?)
($ alive? #f)))
(('enter obj grid-info) #f)
(('exit obj grid-info) #f)
(('wire-state grid-info from from-x from-y) #f)
(('update-wire-state grid-info neighbor-grid) #f)
(('alive?) ($ alive?))
(('explode)
($ exploding? #t))
(('describe) `(brick ,position ,($ exploding?)))
(('collide other offset grid-info) #f)))
;; TODO: Maybe make separate actors for conductive vs. inert blocks.
(define (^block bcom x y type)
(define position (spawn ^cell (vector x y 1)))
@ -315,6 +345,43 @@
(('describe) `(electron-warp ,position ,($ state)))
(('collide other offset grid-info) #f)))
(define (^bomb bcom x y)
(define position (vector x y 2))
(define alive? (spawn ^cell #t))
(define lit? (spawn ^cell))
(define exploding? (spawn ^cell))
(match-lambda*
(('type) 'bomb)
(('position) position)
(('tick grid-info)
(cond
(($ exploding?)
($ alive? #f)
(do ((ix (- x 1) (+ ix 1)))
((> ix (+ x 1)))
(do ((iy (- y 1) (+ iy 1)))
((> iy (+ y 1)))
(unless (and (= ix x) (= iy y))
(let ((obj (match ($ grid-info 'occupants ix iy)
(() #f)
((obj . rest) obj))))
(when (and obj (eq? ($ obj 'type) 'brick))
($ obj 'explode)))))))
(($ lit?)
($ exploding? #t))
(else #f)))
(('post-tick grid-info) #f)
(('enter obj grid-info) #f)
(('exit obj grid-info) #f)
(('wire-state grid-info from from-x from-y) #f)
(('update-wire-state grid-info neighbor-grid)
(when (and (not ($ lit?))
(grid-electrified? neighbor-grid))
($ lit? #t)))
(('alive?) ($ alive?))
(('describe) `(bomb ,position ,($ exploding?)))
(('collide other offset grid-info) #f)))
(define (^gem bcom x y)
(define position (vector x y 1))
(define picked-up? (spawn ^cell))