Player always ticks first.

This commit is contained in:
David Thompson 2024-05-22 13:36:00 -04:00
parent 6b9edbca25
commit f1afd9e177

View file

@ -377,8 +377,6 @@
('down #(0 1)) ('down #(0 1))
(_ (error "invalid direction" dir))))) (_ (error "invalid direction" dir)))))
(('tick grid-info) (('tick grid-info)
;; Search for objects that were fine to step onto last turn, but
;; have become deadly this turn. If
(match ($ position) (match ($ position)
(#(x y z) (#(x y z)
($ event #f) ($ event #f)
@ -387,16 +385,13 @@
($ position (vector (+ x dx) (+ y dy) z)) ($ position (vector (+ x dx) (+ y dy) z))
($ velocity #(0 0))))))) ($ velocity #(0 0)))))))
(('post-tick grid-info) (('post-tick grid-info)
;; Search for objects that were fine to step onto last turn, but
;; have become deadly this turn.
(match ($ position) (match ($ position)
(#(x y z) (#(x y z)
(let lp ((objs ($ grid-info 'occupants x y))) (let lp ((objs ($ grid-info 'occupants x y)))
(match objs (match objs
;; All is well. Move by current velocity. (() #f)
(()
(match ($ velocity)
(#(dx dy)
($ position (vector (+ x dx) (+ y dy) z))
($ velocity #(0 0)))))
((obj . rest) ((obj . rest)
(match ($ obj 'type) (match ($ obj 'type)
('gate ('gate
@ -438,6 +433,7 @@
(('event) ($ event)))) (('event) ($ event))))
(define (^level bcom width height) (define (^level bcom width height)
(define player (spawn ^cell))
(define objects (spawn ^cell '())) (define objects (spawn ^cell '()))
;; Spatial partition ;; Spatial partition
@ -579,30 +575,41 @@
(if ($ obj 'alive?) (if ($ obj 'alive?)
(cons obj (lp rest)) (cons obj (lp rest))
(lp rest))))))) (lp rest)))))))
(define (tick-object obj)
(let ((prev-pos ($ obj 'position)))
($ obj 'tick grid-info)
;; Only check collisions for movable objects.
(let ((desired-pos ($ obj 'position)))
(unless (equal? prev-pos desired-pos)
(collide obj desired-pos prev-pos)))))
(define (tick) (define (tick)
;; Tick each object and check for collisions. (let ((player ($ player)))
(iter-objects ;; Player goes first.
(lambda (obj) (when ($ player 'alive?)
(let ((prev-pos ($ obj 'position))) (tick-object player))
($ obj 'tick grid-info) ;; Tick all the non-player objects.
;; Only check collisions for movable objects. (iter-objects tick-object)
(let ((desired-pos ($ obj 'position))) ;; Advance Wirewold simulation.
(unless (equal? prev-pos desired-pos) (refresh-wire-grid)
(collide obj desired-pos prev-pos)))))) (for-each (lambda (obj)
;; Advance Wirewold simulation. ($ obj 'update-wire-state grid-info))
(refresh-wire-grid) ($ objects))
(for-each (lambda (obj) ;; Run post-tick hooks.
($ obj 'update-wire-state grid-info)) ($ player 'post-tick grid-info)
($ objects)) (iter-objects (lambda (obj) ($ obj 'post-tick grid-info)))))
;; Run post-tick hooks.
(iter-objects (lambda (obj) ($ obj 'post-tick grid-info))))
(match-lambda* (match-lambda*
(('tick) (tick)) (('tick) (tick))
(('describe) (('describe)
(map (lambda (obj) ($ obj 'describe)) ($ objects))) (let ((player ($ player))
(obj-descs (map (lambda (obj) ($ obj 'describe)) ($ objects))))
(if ($ player 'alive?)
(cons ($ player 'describe) obj-descs)
obj-descs)))
(('add-object obj) (('add-object obj)
($ objects (cons obj ($ objects))) (if (eq? ($ obj 'type) 'player)
($ player obj)
($ objects (cons obj ($ objects))))
(match ($ obj 'position) (match ($ obj 'position)
(#(x y _) (#(x y _)
(let ((cell (grid-ref grid x y))) (let ((cell (grid-ref grid x y)))