diff options
Diffstat (limited to 'player.lua')
| -rw-r--r-- | player.lua | 39 |
1 files changed, 28 insertions, 11 deletions
@@ -22,6 +22,9 @@ function Player.new(world, spawnX, spawnY) self.availableJumps = 0 self:enablePhysics(world, "dynamic") + self.fixture:setFriction(0) + self.doubleJump = true + self.jumpsUsed = 0 self.animations = { idle = Animation.new("assets/player/idle.png", 16, 16, 0.6), running = Animation.new("assets/player/running.png", 16, 16, 0.9), @@ -139,7 +142,7 @@ function Player:update(dt) self.body:applyForce(move * SWIM_SPEED, moveY * SWIM_SPEED) elseif onFloor or onWaterSurface then self.body:setLinearDamping(0) - self.availableJumps = 1 + self.jumpsUsed = 0 if move ~= 0 then self.lastFacing = move self.body:setLinearVelocity(move * MOVE_SPEED, vy) @@ -198,18 +201,32 @@ end function Player:jump() local c = self.contact or { floor = 0, wall = 0, ceiling = 0 } + local onFloor = c.floor == 1 + local onWall = c.wall == 1 local onWaterSurface = self.waterSurfaceContact - local canJump = (c.floor == 1 or c.wall == 1 or onWaterSurface) and (self.availableJumps or 0) > 0 - if not canJump then return false end - if self.isInLiquid and not onWaterSurface then return false end - if canJump then - self.body:setLinearVelocity(self.body:getLinearVelocity(), JUMP_FORCE) - self.state = "jumping" - self.animations.going_up:reset() - self.availableJumps = self.availableJumps - 1 - return true + + if onWall and not onFloor then + return false + end + + local maxJumps = self.doubleJump and 2 or 1 + + if self.jumpsUsed >= maxJumps then + return false end - return false + + if self.isInLiquid and not onWaterSurface then + return false + end + + self.body:setLinearVelocity(self.body:getLinearVelocity(), JUMP_FORCE) + + self.state = "jumping" + self.animations.going_up:reset() + + self.jumpsUsed = self.jumpsUsed + 1 + + return true end function Player:draw() |
