summaryrefslogtreecommitdiff
path: root/player.lua
diff options
context:
space:
mode:
authorcursed22bc <admin@pixeldawn.org>2026-03-10 19:27:58 +0200
committercursed22bc <admin@pixeldawn.org>2026-03-10 19:27:58 +0200
commit9d95968c3e732be915f10841d1d659e37b3b5d03 (patch)
tree1932c4fef554a350303c75cda830503efdaa144c /player.lua
parentdb9454cb632a3ba0b39018513b0d5bd444c8ffd3 (diff)
collision death state and basic HUD
Diffstat (limited to 'player.lua')
-rw-r--r--player.lua48
1 files changed, 48 insertions, 0 deletions
diff --git a/player.lua b/player.lua
index 14f0885..a09e887 100644
--- a/player.lua
+++ b/player.lua
@@ -15,6 +15,9 @@ function Player.new(world, spawnX, spawnY)
local pw, ph = 16, 16 -- physics body size
local self = setmetatable(Entity:new(spawnX or 0, spawnY or 0, w, h, pw, ph), Player)
+ self.spawnX = spawnX or 0
+ self.spawnY = spawnY or 0
+
self.directionState = { 'side', 'up', 'down', 'side_up', 'side_down' }
self.directionIndex = 1
self.direction = self.directionState[self.directionIndex]
@@ -65,6 +68,9 @@ function Player.new(world, spawnX, spawnY)
self.isInLiquid = false
self.wasInLiquid = false
self.waterSurfaceContact = false
+
+ self.isDead = false
+ self.respawnTimer = 0
return self
end
@@ -98,6 +104,17 @@ end
function Player:update(dt)
self:syncFromPhysicsBody()
+
+ if self.isDead then
+ self.respawnTimer = self.respawnTimer - dt
+ if self.respawnTimer <= 0 then
+ self:respawn()
+ end
+ self.currentAnim = self.animations.dead or self.animations.idle
+ self.currentAnim:update(dt)
+ return
+ end
+
self:trackDirectionByKeyPressed()
local vx, vy = self.body:getLinearVelocity()
@@ -196,12 +213,43 @@ function Player:update(dt)
swimming = self.animations.swimming,
stop_running = self.animations.stop_running,
ground_hit = self.animations.ground_hit,
+ dead = self.animations.dead or self.animations.idle,
}
self.currentAnim = animMap[self.state] or self.animations.idle
self.currentAnim:update(dt)
end
+function Player:die(nx, ny)
+ if self.isDead then return end
+ self.isDead = true
+ self.state = "dead"
+ self.respawnTimer = 4
+
+ local len = math.sqrt(nx * nx + ny * ny)
+ if len < 0.01 then
+ nx, ny = 0, -1
+ else
+ nx, ny = nx / len, ny / len
+ end
+
+ local BOUNCE_SPEED = 200
+ self.body:setLinearVelocity(nx * BOUNCE_SPEED, ny * BOUNCE_SPEED)
+end
+
+function Player:respawn()
+ self.isDead = false
+ self.respawnTimer = 0
+ self.state = "idle"
+ self.jumpsUsed = 0
+ local cx = self.spawnX + self.physicsWidth / 2
+ local cy = self.spawnY + self.height - self.physicsHeight / 2
+ self.body:setPosition(cx, cy)
+ self.body:setLinearVelocity(0, 0)
+ self:syncFromPhysicsBody()
+end
+
function Player:jump()
+ if self.isDead then return false end
local c = self.contact or { floor = 0, wall = 0, ceiling = 0 }
local onFloor = c.floor == 1
local onWall = c.wall == 1