From ca870dab91daee38b8d55ac6d2f2b4fd6959c6ac Mon Sep 17 00:00:00 2001 From: equa Date: Sun, 18 Apr 2021 20:22:20 -0500 Subject: entities et al --- lib/game.fnl | 81 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 25 deletions(-) (limited to 'lib/game.fnl') diff --git a/lib/game.fnl b/lib/game.fnl index 01f882d..b75a254 100644 --- a/lib/game.fnl +++ b/lib/game.fnl @@ -1,19 +1,14 @@ (local state (require :lib.state)) (local cell (require :lib.cell)) (local cells (require :lib.cells)) +(local vec (require :lib.vec)) +(local entity (require :lib.entity)) +(local player (require :lib.player)) (local fv (require :fennel.view)) (fn lerp* [a b c d x] (+ c (* (/ (- x a) (- b a)) (- d c)))) -(fn vec-sub [a b] - {:x (- a.x b.x) - :y (- a.y b.y)}) - -(fn vec-lerp* [a b c d x] - {:x (lerp* a.x b.x c.x d.x x.x) - :y (lerp* a.y b.y c.y d.y x.y)}) - (fn new-grid [w h f] (var t {}) (for [x 0 (- w 1)] @@ -23,8 +18,8 @@ t) (fn update [self] - (set self.ship.x (+ self.ship.x 0.02)) - (set self.ship.y (+ self.ship.y 0.005)) + ;; (set ship-pos.x (+ self.ship.x 0.02)) + ;; (set ship-pos.y (+ self.ship.y 0.005)) (set self.radius (lerp* 0 1 self.radius self.target-radius 0.3)) (when (= self.tick 0) (for [x 0 (- self.width 1)] @@ -48,15 +43,37 @@ (cell.birth (. neighbors (math.random (length neighbors))) get)) (tset self.grid-alt x y nil)))))) (set (self.grid self.grid-alt) (values self.grid-alt self.grid))) - ;; TODO - (set self.tick (% (+ self.tick 1) self.rate))) + (set self.tick (% (+ self.tick 1) self.rate)) + ;; player steering + (when self.entities.player + (entity.steer self.entities.player + self + :player + {:up (love.keyboard.isDown :up) + :right (love.keyboard.isDown :right) + :left (love.keyboard.isDown :left)})) + ;; entities + (each [id e (pairs self.entities)] + (tset e entity.position (vec.wrap + (vec.add (entity.position e) (entity.velocity e)) + {:x self.width :y self.height})) + (let [x (math.floor (. e entity.position :x)) + y (math.floor (. e entity.position :y)) + t (. self.grid x y)] + (when t + (entity.collide e self id x y))) + ) + ) (fn id [x] x) (fn draw [self] (local (width height) (love.graphics.getDimensions)) ;; (love.graphics.scale width height) - (let [camera-size (math.min width height) + (let [ship-pos (if self.entities.player + (entity.position self.entities.player) + self.ship-pos) + camera-size (math.min width height) radius-x (* self.radius (/ width camera-size)) radius-y (* self.radius (/ height camera-size)) clipped-x (math.min radius-x self.max-radius) @@ -64,18 +81,20 @@ display-a {:x (* width (- 1 (/ clipped-x radius-x)) 0.5) :y (* height (- 1 (/ clipped-y radius-y)) 0.5)} display-b {:x (- width display-a.x) :y (- height display-a.y)} - display-size (vec-sub display-b display-a) - camera-a {:x (- self.ship.x clipped-x) - :y (- self.ship.y clipped-y)} - camera-b {:x (+ self.ship.x clipped-x) - :y (+ self.ship.y clipped-y)} - cell-box (vec-lerp* {:x 0 :y 0} + display-size (vec.sub display-b display-a) + camera-a {:x (- ship-pos.x clipped-x) + :y (- ship-pos.y clipped-y)} + camera-b {:x (+ ship-pos.x clipped-x) + :y (+ ship-pos.y clipped-y)} + cell-box (vec.lerp {:x 0 :y 0} {:x (* 2 clipped-x) :y (* 2 clipped-y)} ;; TODO: this is wrong {:x 0 :y 0} display-size {:x 1 :y 1})] + ;; TODO: this is ugly and weird + (set self.ship-pos ship-pos) (love.graphics.setScissor (- display-a.x 1) (- display-a.y 1) (- display-b.x display-a.x -2) (- display-b.y display-a.y -2)) @@ -91,12 +110,12 @@ (love.graphics.clear) (for [x (math.floor camera-a.x) (math.floor camera-b.x)] (for [y (math.floor camera-a.y) (math.floor camera-b.y)] - (let [vec {:x (% x self.width) :y (% y self.height)} - render-a (vec-lerp* camera-a camera-b display-a display-b + (let [pos {:x (% x self.width) :y (% y self.height)} + render-a (vec.lerp camera-a camera-b display-a display-b {: x : y}) - render-b (vec-lerp* camera-a camera-b display-a display-b + render-b (vec.lerp camera-a camera-b display-a display-b {:x (+ x 1) :y (+ y 1)}) - the (. self.grid vec.x vec.y) + the (. self.grid pos.x pos.y) color (and the (cell.color the))] (when color (love.graphics.setColor (unpack color)) @@ -106,6 +125,15 @@ (id cell-box.x) (id cell-box.y)))))) ;; draw other stuff + (each [id v (pairs self.entities)] + (love.graphics.push) + (let [pos (entity.position v) + render-pos (vec.lerp camera-a camera-b display-a display-b pos)] + (love.graphics.translate render-pos.x render-pos.y) + (love.graphics.scale cell-box.x + cell-box.y)) + (entity.draw v self id) + (love.graphics.pop)) )) ;; (love.graphics.setLineWidth 0.1) ;; (love.graphics.line 0 0 0.3 0.3) @@ -142,10 +170,13 @@ :min-radius 16 :tick 0 :rate 6 + :entities {:player (entity.init player {:x 32 :y 32})} :grid (new-grid width height #(if (= (math.random 6) 1) - (if (< $1 52) + (if (> $1 34) (cell.init cells.life) - (cell.init cells.brain)) + (< $1 00) + (cell.init cells.brain) + nil) nil)) :grid-alt (new-grid width height #nil) } -- cgit 1.3.0-6-gf8a5