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/player.fnl | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/player.fnl (limited to 'lib/player.fnl') diff --git a/lib/player.fnl b/lib/player.fnl new file mode 100644 index 0000000..0633727 --- /dev/null +++ b/lib/player.fnl @@ -0,0 +1,39 @@ +(local entity (require :lib.entity)) +(local vec (require :lib.vec)) +(local cell (require :lib.cell)) +(local cells (require :lib.cells)) + +(fn init [self pos] + (setmetatable {entity.position pos + entity.velocity {:x 0 :y 0} + :target-spin 0 + :direction 0} + self)) + +(fn steer [self game id controls] + ;; TODO: smooth turning + (when controls.left + (set self.target-spin (- self.target-spin 0.15))) + ;; (set self.direction (% (- self.direction 0.1) (* math.pi 2))) + (when controls.right + (set self.target-spin (+ self.target-spin 0.15))) + (set self.direction (% (+ self.direction (* self.target-spin 0.1)) (* math.pi 2))) + (set self.target-spin (* self.target-spin 0.9)) + (when controls.up + (local v (entity.velocity self)) + (set v.x (+ v.x (* (math.cos self.direction) 0.02))) + (set v.y (+ v.y (* (math.sin self.direction) 0.02))) + (when (> (vec.mag v) 0.8) + (tset self entity.velocity (vec.mul v (/ 0.8 (vec.mag v))))))) + +(fn draw [self game id] + (love.graphics.setColor 1 1 1) + (love.graphics.rotate self.direction) + (love.graphics.polygon :fill -0.6 -0.4 0.6 0 -0.6 0.4)) + +(fn collide [self game id x y] + (print "ow!") + (tset game.entities id nil) + (tset game.grid x y (cell.init cells.boom))) + +{entity.init init entity.steer steer entity.draw draw entity.collide collide} -- cgit 1.3.0-6-gf8a5