fix spawn and better match hitbox to player sprite

This commit is contained in:
Travis Shears 2026-04-25 20:05:01 +02:00
parent 414006b667
commit 7e75b665f5
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
6 changed files with 42 additions and 23 deletions

View file

@ -6,6 +6,9 @@
(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))
@ -19,7 +22,10 @@
(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 })
(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] })
(fn player.update [self dt]
(let [
@ -38,9 +44,7 @@
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)))
col-filter-fn (lambda [item other]
(if (= other.behavior "block") :slide :cross))
(x y cols len) (bump-world:move self new-x new-y col-filter-fn)]
(x y cols len) (bump-world:move self new-x new-y bump-filter)]
(set self.x x)
(set self.y y))
(if (> self.battery 0)
@ -50,8 +54,12 @@
(fn player.load [self]
(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))
(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)]
@ -64,8 +72,7 @@
: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)
}))
(bump-world:add self self.x self.y self.w self.h))
})))
(fn player.draw50 [self]
"draw player sprite and hitbox"
@ -73,14 +80,15 @@
(love.graphics.draw
assets.player-sprite
(. self.quads (angle-to-direction player.rot))
self.x self.y))
(- 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 self.w self.h)
(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 (/ 25 2)) oy (+ self.y (/ 25 2))]
(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))