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?)))
|
(('describe) `(gate ,position ,($ open?)))
|
||||||
(('collide other offset grid-info) #f)))
|
(('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 position (vector x y 0))
|
||||||
(define state (spawn ^cell 'copper))
|
(define state (spawn ^cell 'copper))
|
||||||
(match-lambda*
|
(match-lambda*
|
||||||
|
@ -365,6 +365,13 @@
|
||||||
(('exit obj grid-info) #f)
|
(('exit obj grid-info) #f)
|
||||||
(('wire-state grid-info) ($ state))
|
(('wire-state grid-info) ($ state))
|
||||||
(('update-wire-state grid-info)
|
(('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)
|
(match ($ state)
|
||||||
('electron-head ($ state 'electron-tail))
|
('electron-head ($ state 'electron-tail))
|
||||||
('electron-tail ($ state 'copper))
|
('electron-tail ($ state 'copper))
|
||||||
|
@ -377,22 +384,10 @@
|
||||||
'electron-head #f #f)
|
'electron-head #f #f)
|
||||||
($ state 'electron-head))
|
($ state 'electron-head))
|
||||||
(_ ($ state 'copper))))))
|
(_ ($ state 'copper))))))
|
||||||
(('alive?) #t)
|
(^logic-gate bcom x y 'and-gate update-wire-state))
|
||||||
(('describe) `(and-gate ,position ,($ state)))
|
|
||||||
(('collide other offset grid-info) #f)))
|
|
||||||
|
|
||||||
(define (^xor-gate bcom x y)
|
(define (^xor-gate bcom x y)
|
||||||
(define position (vector x y 0))
|
(define (update-wire-state state grid-info)
|
||||||
(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)
|
|
||||||
(match ($ state)
|
(match ($ state)
|
||||||
('electron-head ($ state 'electron-tail))
|
('electron-head ($ state 'electron-tail))
|
||||||
('electron-tail ($ state 'copper))
|
('electron-tail ($ state 'copper))
|
||||||
|
@ -417,22 +412,10 @@
|
||||||
'electron-head #f #f)
|
'electron-head #f #f)
|
||||||
($ state 'electron-head))
|
($ state 'electron-head))
|
||||||
(_ ($ state 'copper))))))
|
(_ ($ state 'copper))))))
|
||||||
(('alive?) #t)
|
(^logic-gate bcom x y 'xor-gate update-wire-state))
|
||||||
(('describe) `(xor-gate ,position ,($ state)))
|
|
||||||
(('collide other offset grid-info) #f)))
|
|
||||||
|
|
||||||
(define (^or-gate bcom x y)
|
(define (^or-gate bcom x y)
|
||||||
(define position (vector x y 0))
|
(define (update-wire-state state grid-info)
|
||||||
(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)
|
|
||||||
(match ($ state)
|
(match ($ state)
|
||||||
('electron-head ($ state 'electron-tail))
|
('electron-head ($ state 'electron-tail))
|
||||||
('electron-tail ($ state 'copper))
|
('electron-tail ($ state 'copper))
|
||||||
|
@ -461,9 +444,7 @@
|
||||||
'electron-head #f #f)
|
'electron-head #f #f)
|
||||||
($ state 'electron-head))
|
($ state 'electron-head))
|
||||||
(_ ($ state 'copper))))))
|
(_ ($ state 'copper))))))
|
||||||
(('alive?) #t)
|
(^logic-gate bcom x y 'or-gate update-wire-state))
|
||||||
(('describe) `(or-gate ,position ,($ state)))
|
|
||||||
(('collide other offset grid-info) #f)))
|
|
||||||
|
|
||||||
(define (^player bcom x y)
|
(define (^player bcom x y)
|
||||||
(define position (spawn ^cell (vector x y 2)))
|
(define position (spawn ^cell (vector x y 2)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue