switch repo to just cleaning game focus
This commit is contained in:
parent
276c7c562b
commit
6d5406c6c5
49 changed files with 0 additions and 150 deletions
117
game/src/entities/player.fnl
Normal file
117
game/src/entities/player.fnl
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
(local utils (require "src/utils.fnl"))
|
||||
(local beholder (require "libs/beholder"))
|
||||
(local color (require "src/colors.fnl"))
|
||||
(local levels (require "levels.fnl"))
|
||||
(local assets (require "src/assets.fnl"))
|
||||
|
||||
(var bump-world nil)
|
||||
(var level-key nil)
|
||||
|
||||
(lambda bump-filter [item other]
|
||||
(if (= other.behavior "block") :slide :cross))
|
||||
|
||||
(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 width 19)
|
||||
(local height 15)
|
||||
(local player {
|
||||
:x 50 :y 50 :w 25 :h 25 :speed 80 :battery 100 :rot 0
|
||||
:hitbox [50 50 width height]
|
||||
:init-move false ; nudge the player at start of game to trigger starting collisions
|
||||
})
|
||||
|
||||
|
||||
(lambda handle-collisions [cols]
|
||||
(each [_ col (pairs cols)]
|
||||
(when col.other.on-hit (col.other:on-hit))
|
||||
(beholder.trigger "PLAYER.HIT" col.other)))
|
||||
|
||||
(fn player.update [self dt]
|
||||
(when (= self.init-move false)
|
||||
(set self.init-move true)
|
||||
(let [(x y cols len) (bump-world:move self self.x self.y bump-filter)]
|
||||
(handle-collisions cols)))
|
||||
(let [
|
||||
d-key (love.keyboard.isDown :d)
|
||||
a-key (love.keyboard.isDown :a)
|
||||
e-key (love.keyboard.isDown :e)
|
||||
q-key (love.keyboard.isDown :q)]
|
||||
(match {:d-key d-key :a-key a-key :e-key e-key :q-key q-key}
|
||||
{:d-key true :a-key false :e-key false :q-key false} (set self.rot (+ self.rot (* dt 2)))
|
||||
{:d-key false :a-key true :e-key false :q-key false} (set self.rot (- self.rot (* dt 2)))
|
||||
{:d-key false :a-key false :e-key true :q-key false} (set self.rot (- self.rot (* dt 2)))
|
||||
{:d-key false :a-key false :e-key false :q-key true} (set self.rot (+ self.rot (* dt 2)))
|
||||
)
|
||||
(when (and (> self.battery 0) (or d-key a-key e-key q-key))
|
||||
(let [
|
||||
dir-fn (if (or d-key a-key) #(+ $1 $2) #(- $1 $2))
|
||||
new-x (dir-fn self.x (* self.speed dt (math.cos self.rot)))
|
||||
new-y (dir-fn self.y (* self.speed dt (math.sin self.rot)))
|
||||
(x y cols len) (bump-world:move self new-x new-y bump-filter)]
|
||||
(handle-collisions cols)
|
||||
(set self.x x)
|
||||
(set self.y y))
|
||||
(if (> self.battery 0)
|
||||
(set self.battery (- self.battery (* dt 2))))))
|
||||
(beholder.trigger "PLAYER.POS" self.x self.y)
|
||||
(beholder.trigger "PLAYER.BATTERY" self.battery))
|
||||
|
||||
(fn player.load [self]
|
||||
(set self.battery 100)
|
||||
(let [
|
||||
spawn-x (-> (. levels :levels level-key :spawns :player-1 :x) (- (/ width 2)))
|
||||
spawn-y (-> (. levels :levels level-key :spawns :player-1 :y) (- (/ height 2)))
|
||||
(x y cols len) (bump-world:move self spawn-x spawn-y bump-filter)]
|
||||
(set self.x x)
|
||||
(set self.y y))
|
||||
(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.draw50 [self]
|
||||
"draw player sprite and hitbox"
|
||||
(color:reset-color)
|
||||
(love.graphics.draw
|
||||
assets.player-sprite
|
||||
(. self.quads (angle-to-direction player.rot))
|
||||
(- self.x 3) (- self.y 7)))
|
||||
|
||||
(fn player.draw-debug [self]
|
||||
"draw player hitbox and direction line"
|
||||
(color.set-color :black)
|
||||
(love.graphics.rectangle "line" self.x self.y width height)
|
||||
(love.graphics.rectangle "line" self.x self.y 1 1)
|
||||
(love.graphics.push)
|
||||
(let [ox (+ self.x (/ width 2)) oy (+ self.y (/ height 2))]
|
||||
(love.graphics.translate ox oy)
|
||||
(love.graphics.rotate self.rot)
|
||||
(love.graphics.line 0 0 35 0))
|
||||
(love.graphics.pop))
|
||||
|
||||
|
||||
(lambda [{:bump-world bw :level-key key}]
|
||||
(set bump-world bw)
|
||||
(set level-key key)
|
||||
player)
|
||||
Loading…
Add table
Add a link
Reference in a new issue