58 lines
2 KiB
Fennel
58 lines
2 KiB
Fennel
(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
|