Refactor logic gates to reduce code duplication
This commit is contained in:
parent
50b6c67db8
commit
91ba2fab66
1 changed files with 75 additions and 94 deletions
|
@ -353,7 +353,7 @@
|
|||
(('describe) `(gate ,position ,($ open?)))
|
||||
(('collide other offset grid-info) #f)))
|
||||
|
||||
(define (^and-gate bcom x y)
|
||||
(define (^logic-gate bcom x y name update-wire-state)
|
||||
(define position (vector x y 0))
|
||||
(define state (spawn ^cell 'copper))
|
||||
(match-lambda*
|
||||
|
@ -365,6 +365,13 @@
|
|||
(('exit obj grid-info) #f)
|
||||
(('wire-state grid-info) ($ state))
|
||||
(('update-wire-state grid-info)
|
||||
(update-wire-state state grid-info))
|
||||
(('alive?) #t)
|
||||
(('describe) `(,name ,position ,($ state)))
|
||||
(('collide other offset grid-info) #f)))
|
||||
|
||||
(define (^and-gate bcom x y)
|
||||
(define (update-wire-state state grid-info)
|
||||
(match ($ state)
|
||||
('electron-head ($ state 'electron-tail))
|
||||
('electron-tail ($ state 'copper))
|
||||
|
@ -377,22 +384,10 @@
|
|||
'electron-head #f #f)
|
||||
($ state 'electron-head))
|
||||
(_ ($ state 'copper))))))
|
||||
(('alive?) #t)
|
||||
(('describe) `(and-gate ,position ,($ state)))
|
||||
(('collide other offset grid-info) #f)))
|
||||
(^logic-gate bcom x y 'and-gate update-wire-state))
|
||||
|
||||
(define (^xor-gate bcom x y)
|
||||
(define position (vector x y 0))
|
||||
(define state (spawn ^cell 'copper))
|
||||
(match-lambda*
|
||||
(('type) 'emitter)
|
||||
(('position) position)
|
||||
(('tick grid-info) #f)
|
||||
(('post-tick grid-info) #f)
|
||||
(('enter obj grid-info) #f)
|
||||
(('exit obj grid-info) #f)
|
||||
(('wire-state grid-info) ($ state))
|
||||
(('update-wire-state grid-info)
|
||||
(define (update-wire-state state grid-info)
|
||||
(match ($ state)
|
||||
('electron-head ($ state 'electron-tail))
|
||||
('electron-tail ($ state 'copper))
|
||||
|
@ -417,22 +412,10 @@
|
|||
'electron-head #f #f)
|
||||
($ state 'electron-head))
|
||||
(_ ($ state 'copper))))))
|
||||
(('alive?) #t)
|
||||
(('describe) `(xor-gate ,position ,($ state)))
|
||||
(('collide other offset grid-info) #f)))
|
||||
(^logic-gate bcom x y 'xor-gate update-wire-state))
|
||||
|
||||
(define (^or-gate bcom x y)
|
||||
(define position (vector x y 0))
|
||||
(define state (spawn ^cell 'copper))
|
||||
(match-lambda*
|
||||
(('type) 'emitter)
|
||||
(('position) position)
|
||||
(('tick grid-info) #f)
|
||||
(('post-tick grid-info) #f)
|
||||
(('enter obj grid-info) #f)
|
||||
(('exit obj grid-info) #f)
|
||||
(('wire-state grid-info) ($ state))
|
||||
(('update-wire-state grid-info)
|
||||
(define (update-wire-state state grid-info)
|
||||
(match ($ state)
|
||||
('electron-head ($ state 'electron-tail))
|
||||
('electron-tail ($ state 'copper))
|
||||
|
@ -461,9 +444,7 @@
|
|||
'electron-head #f #f)
|
||||
($ state 'electron-head))
|
||||
(_ ($ state 'copper))))))
|
||||
(('alive?) #t)
|
||||
(('describe) `(or-gate ,position ,($ state)))
|
||||
(('collide other offset grid-info) #f)))
|
||||
(^logic-gate bcom x y 'or-gate update-wire-state))
|
||||
|
||||
(define (^player bcom x y)
|
||||
(define position (spawn ^cell (vector x y 2)))
|
||||
|
|
Loading…
Add table
Reference in a new issue