inertia and refactor
This commit is contained in:
@@ -5,7 +5,7 @@ function Player:init(x, y)
|
||||
Player.super.init(self, "assets/sprites/player", true)
|
||||
|
||||
-- Animation properties
|
||||
self.animation:addState("run", 1, 7)
|
||||
self.animation:addState("run", 8, 14)
|
||||
self.animation.run.frameDuration = 2
|
||||
self.animation:addState("up", 1, 7)
|
||||
self.animation.up.frameDuration = 6
|
||||
@@ -22,17 +22,27 @@ function Player:init(x, y)
|
||||
-- Collision properties
|
||||
self:setSize(64, 64)
|
||||
self:moveTo(x, y)
|
||||
self:setZIndex(10)
|
||||
self:setCollideRect(3, 19, 63, 33)
|
||||
self:setZIndex(ZIndex.player)
|
||||
self:setGroups(CollideGroups.player)
|
||||
self:setCollidesWithGroups(
|
||||
{
|
||||
CollideGroups.enemy,
|
||||
CollideGroups.wall
|
||||
})
|
||||
self:setCollideRect(3, 19, 60, 33)
|
||||
self:setTag(1)
|
||||
|
||||
-- Physics properties
|
||||
self.fallSpeed = 0.05
|
||||
self.xVelocity = 0
|
||||
self.yVelocity = 0
|
||||
self.yVelocity = self.fallSpeed
|
||||
self.maxXSpeed = 2
|
||||
self.maxYSpeed = 5
|
||||
self.fallSpeed = 0.05
|
||||
self.maxFallSpeed = 0.4
|
||||
self.acceleration = 0.05
|
||||
self.friction = 0.95
|
||||
self.yInertia = 0
|
||||
self.maxFallSpeed = 0.7
|
||||
self.frictionAccel = 0.3
|
||||
|
||||
-- Player State
|
||||
self.touchingGround = false
|
||||
@@ -41,8 +51,9 @@ function Player:init(x, y)
|
||||
Player.dead = false
|
||||
Player.bat = 10000
|
||||
Player.dischargeRate = 1
|
||||
self.cantDown = false
|
||||
Player.moveRight = false
|
||||
self.lastDirection = nil
|
||||
self.targetDone = false
|
||||
end
|
||||
|
||||
function Player:handleInput()
|
||||
@@ -52,54 +63,53 @@ function Player:handleInput()
|
||||
|
||||
local crankChange, acceleratedChange = playdate.getCrankChange()
|
||||
|
||||
if playdate.buttonJustReleased(playdate.kButtonLeft) or playdate.buttonJustReleased(playdate.kButtonRight) then
|
||||
self.cantDown = false
|
||||
end
|
||||
|
||||
if playdate.buttonIsPressed(playdate.kButtonLeft) then
|
||||
self:changeToRunState("left")
|
||||
self:setRotation(10)
|
||||
self.cantDown = true
|
||||
-- X velocity
|
||||
if not playdate.buttonIsPressed(playdate.kButtonLeft) and not playdate.buttonIsPressed(playdate.kButtonRight) then
|
||||
self.xVelocity = self.xVelocity * self.friction
|
||||
Player.moveRight = false
|
||||
elseif playdate.buttonIsPressed(playdate.kButtonRight) then
|
||||
self:changeToRunState("right")
|
||||
self:setRotation(10)
|
||||
self.cantDown = true
|
||||
Player.moveRight = true
|
||||
end
|
||||
|
||||
local isChangingDirection = false
|
||||
if playdate.buttonIsPressed(playdate.kButtonLeft) then
|
||||
if self.lastDirection == "right" then
|
||||
isChangingDirection = true
|
||||
end
|
||||
self:changeToRunState("left")
|
||||
self.lastDirection = "left"
|
||||
self.xVelocity = self.xVelocity - self.acceleration
|
||||
elseif playdate.buttonIsPressed(playdate.kButtonRight) then
|
||||
if self.lastDirection == "left" then
|
||||
isChangingDirection = true
|
||||
end
|
||||
self:changeToRunState("right")
|
||||
Player.moveRight = true
|
||||
self.lastDirection = "right"
|
||||
self.xVelocity = self.xVelocity + self.acceleration
|
||||
end
|
||||
|
||||
if isChangingDirection then
|
||||
self.xVelocity = self.xVelocity * self.frictionAccel
|
||||
end
|
||||
|
||||
-- Y velocity
|
||||
if crankChange ~= 0 then
|
||||
local force = 0.01
|
||||
if crankChange > 0 then
|
||||
self.animation:setState("up")
|
||||
else
|
||||
self.animation:setState("down")
|
||||
force = 0.05
|
||||
end
|
||||
self.yVelocity = self.yVelocity - acceleratedChange * force
|
||||
elseif self.cantDown == false then
|
||||
self.cantDown = false
|
||||
self:changeToDownState()
|
||||
self.yInertia = self.yInertia - (acceleratedChange * 0.007)
|
||||
else
|
||||
self.yVelocity = self.fallSpeed
|
||||
self.yInertia = self.yInertia * self.friction
|
||||
self.animation:setState("down")
|
||||
end
|
||||
end
|
||||
|
||||
function Player:changeToDownState()
|
||||
Player.moveRight = false
|
||||
self.yVelocity = self.fallSpeed
|
||||
self.xVelocity = 0
|
||||
self.animation:setState("down")
|
||||
self:setRotation(0)
|
||||
end
|
||||
|
||||
function Player:changeToRunState(direction)
|
||||
if direction == "left" then
|
||||
self.xVelocity = -self.maxXSpeed
|
||||
self.animation.direction = Noble.Animation.DIRECTION_LEFT
|
||||
self.animation:setState("run")
|
||||
elseif direction == "right" then
|
||||
self.xVelocity = self.maxXSpeed
|
||||
self.animation.direction = Noble.Animation.DIRECTION_RIGHT
|
||||
self.animation:setState("run")
|
||||
end
|
||||
@@ -129,12 +139,13 @@ function Player:handleMovementAndCollisions()
|
||||
end
|
||||
|
||||
local xVel = self.xVelocity
|
||||
local yVel = self.yVelocity + self.yInertia + self.fallSpeed
|
||||
|
||||
if (self.x < 20 and xVel < 0) or (self.x > 380 and xVel > 0) then -- Screen bounds
|
||||
xVel = 0
|
||||
end
|
||||
|
||||
local _, _, collisions, length = self:checkCollisions(self.x + xVel, self.y + self.yVelocity)
|
||||
local _, _, collisions, length = self:checkCollisions(self.x + xVel, self.y + yVel)
|
||||
|
||||
self.touchingGround = false
|
||||
self.touchingCeiling = false
|
||||
@@ -161,12 +172,13 @@ function Player:handleMovementAndCollisions()
|
||||
self:boom()
|
||||
return
|
||||
elseif collisionTag == 154 then -- Baleba
|
||||
-- self:boom(collisionObject)
|
||||
self:boom(collisionObject)
|
||||
return
|
||||
elseif collisionTag == 2 then -- Tank
|
||||
self:boom()
|
||||
BigBoom()
|
||||
|
||||
self.targetDone = true
|
||||
local particleC = ParticlePoly(collisionObject.x, collisionObject.y)
|
||||
particleC:setThickness(5)
|
||||
particleC:setAngular(-15, 15)
|
||||
@@ -180,7 +192,8 @@ function Player:handleMovementAndCollisions()
|
||||
return
|
||||
end
|
||||
end
|
||||
self:moveTo(self.x + xVel, self.y + self.yVelocity)
|
||||
|
||||
self:moveTo(self.x + xVel, self.y + yVel)
|
||||
end
|
||||
|
||||
function Player:handleDischarge(state)
|
||||
@@ -190,7 +203,7 @@ function Player:handleDischarge(state)
|
||||
if Player.bat <= 0 then
|
||||
Player.bat = 0
|
||||
self.fallSpeed = 2
|
||||
self:changeToDownState()
|
||||
self.animation:setState("down")
|
||||
return
|
||||
end
|
||||
|
||||
@@ -198,6 +211,10 @@ function Player:handleDischarge(state)
|
||||
self.fallSpeed = self.maxFallSpeed
|
||||
end
|
||||
|
||||
if Player.bat < 3000 then
|
||||
self.frictionAccel = 0.4
|
||||
end
|
||||
|
||||
if state == "run" then
|
||||
Player.dischargeRate = 5
|
||||
elseif state == "up" then
|
||||
@@ -216,7 +233,6 @@ end
|
||||
function Player:update()
|
||||
self:handleMovementAndCollisions()
|
||||
if Player.dead then
|
||||
print("Player is dead")
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user