add beholder and get drawing working

"
This commit is contained in:
Travis Shears 2026-04-23 18:41:57 +02:00
parent 0a1d655267
commit c4c33e4726
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
6 changed files with 246 additions and 26 deletions

View file

@ -0,0 +1,58 @@
(local colors (require "src/colors.fnl"))
(local levels (require "levels.fnl"))
(local assets (require "src/assets.fnl"))
(lambda angle-to-direction [angle]
"Convert angle (radians) to compass direction keyword"
(local tau (* 2 math.pi))
; Normalize angle to 0-2π range
(local normalized (% (+ angle tau) tau))
; Offset by 22.5° (π/8) so section boundaries align with directions
(local offset (+ normalized (/ math.pi 8)))
; Find which 45° section (π/4) it falls into
(local section (math.floor (/ offset (/ math.pi 4))))
; Map to compass directions
(local directions [:e :se :s :sw :w :nw :n :ne])
(. directions (+ (% section 8) 1)))
(local player { :x 50 :y 50 :w 25 :h 25 :speed 80 :battery 100 :rot 0 })
(fn player.load [self level-key]
(set self.battery 100)
(set self.x (. levels :levels level-key :spawns :player-1 :x))
(set self.y (. levels :levels level-key :spawns :player-1 :y))
; (bump-world:add player player.x player.y player.w player.h))
(set self.quads
(let [
(w h) (assets.player-sprite:getDimensions)]
{
:n (love.graphics.newQuad 0 0 25 25 w h)
:s (love.graphics.newQuad 25 0 25 25 w h)
:ne (love.graphics.newQuad 50 0 25 25 w h)
:e (love.graphics.newQuad 75 0 25 25 w h)
:se (love.graphics.newQuad 100 0 25 25 w h)
:sw (love.graphics.newQuad 125 0 25 25 w h)
:w (love.graphics.newQuad 150 0 25 25 w h)
:nw (love.graphics.newQuad 175 0 25 25 w h)
}))
)
(fn player.draw [self state]
"draw player sprite and hitbox"
(colors:reset-color)
(love.graphics.draw
assets.player-sprite
(. self.quads (angle-to-direction player.rot))
self.x self.y)
;; draw player hitbox and direction line
(when state.debug
(colors:set-color :black)
(love.graphics.rectangle "line" self.x self.y self.w self.h)
(love.graphics.push)
(let [ox (+ self.x (/ 25 2)) oy (+ self.y (/ 25 2))]
(love.graphics.translate ox oy)
(love.graphics.rotate self.rot)
(love.graphics.line 0 0 35 0))
(love.graphics.pop)))
player

View file

@ -1,6 +1,9 @@
(local color (require "src/colors.fnl"))
(local nata (require "libs/nata"))
(local utils (require "src/utils.fnl"))
(local beholder (require "libs/beholder"))
(local player (require "src/entities/player.fnl"))
(lambda gen-renderer [screen canvas]
(let [renderer {}]
@ -23,15 +26,18 @@
; (local debug true)
(fn load [screen]
(let [canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h)]
(nata.new {
(let [
canvas (love.graphics.newCanvas screen.canvas-w screen.canvas-h)
pool (nata.new {
; :groups {
; gravity = {filter = {'gravity'}},
; },
:systems [
(gen-renderer screen canvas)
]
})))
]})]
(pool:queue player)
pool
))
{
: load