2021-10-22 16:23:05 +00:00
|
|
|
Player = Entity:New(x,y)
|
2021-10-16 23:06:11 +00:00
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
function Player:DoInput()
|
2021-10-16 23:06:11 +00:00
|
|
|
-- PLATFORMER INPUT
|
2021-10-22 16:23:05 +00:00
|
|
|
if self.isOnGround > 0 then
|
2021-10-16 23:06:11 +00:00
|
|
|
-- apply friction
|
|
|
|
|
|
|
|
-- horizontal input (slide~~)
|
|
|
|
if love.keyboard.isDown('a',"left") then
|
|
|
|
self.vel.x = self.vel.x - self.acc*current_dt
|
|
|
|
end
|
|
|
|
if love.keyboard.isDown('d',"right") then
|
|
|
|
self.vel.x = self.vel.x + self.acc*current_dt
|
|
|
|
end
|
|
|
|
if self.canJump then
|
|
|
|
-- vertical input (jump!)
|
|
|
|
if love.keyboard.isDown("up", "w") and self.isJumping ~= true then
|
|
|
|
self.vel.y = self.vel.y - self.jumpForce
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = 0
|
2021-10-16 23:06:11 +00:00
|
|
|
self.isJumping = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- fall if down input on platforms
|
|
|
|
if not isThereCollisionAt(
|
|
|
|
self.pos.x,
|
|
|
|
self.pos.y + self.vel.y
|
|
|
|
) and not isThereLadderAt(
|
|
|
|
self.pos.x,
|
|
|
|
self.pos.y + self.vel.y
|
|
|
|
) and isTherePlatformAt(
|
|
|
|
self.pos.x,
|
|
|
|
self.pos.y + self.vel.y
|
|
|
|
) and love.keyboard.isDown("down", "s")
|
|
|
|
then
|
|
|
|
self.pos.y = self.pos.y + tileProperties.height/3
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = 0
|
2021-10-16 23:06:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
function Player:HandleAnimation()
|
2021-10-16 23:06:11 +00:00
|
|
|
|
|
|
|
-- flip sprite to look in the direction is moving
|
|
|
|
if self.vel.x ~= 0 then self.flip.x = math.sign(self.vel.x) end
|
|
|
|
|
|
|
|
-- animation manager
|
|
|
|
if self.isOnLadder then
|
|
|
|
self:LoadAnimation(animation.nancy.jump)
|
2021-10-22 16:23:05 +00:00
|
|
|
elseif self.isOnGround == 0 and self.isJumping and self.vel.y > 1.25 then
|
2021-10-16 23:06:11 +00:00
|
|
|
self:LoadAnimation(animation.nancy.fall)
|
2021-10-22 16:23:05 +00:00
|
|
|
elseif self.isOnGround == 0 and self.vel.y < 0 then
|
2021-10-16 23:06:11 +00:00
|
|
|
self:LoadAnimation(animation.nancy.jump)
|
|
|
|
elseif self.vel.x ~= 0 then
|
|
|
|
self:LoadAnimation(animation.nancy.run)
|
|
|
|
else
|
|
|
|
self:LoadAnimation(animation.nancy.idle)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- special case: idle animation gets slower by time
|
|
|
|
if self.anim_path == animation.nancy.idle.path then
|
|
|
|
if self.anim_speed < 0.5 then self.anim_speed = self.anim_speed + 0.001 end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
function Player:DoPhysics()
|
2021-10-16 23:06:11 +00:00
|
|
|
-- reset physics resolution
|
|
|
|
self.canFall = true
|
|
|
|
self.canJump = true
|
|
|
|
self.canFriction = true
|
|
|
|
-- reset flags
|
|
|
|
self.isOnLadder = false
|
|
|
|
-- truncate to max & min values
|
|
|
|
if math.abs(self.vel.x) > self.maxSpeed then
|
|
|
|
self.vel.x = self.maxSpeed * math.sign(self.vel.x)
|
|
|
|
end
|
|
|
|
if math.abs(self.vel.y) > self.maxSpeed then
|
|
|
|
self.vel.y = self.maxSpeed * math.sign(self.vel.y)
|
|
|
|
end
|
|
|
|
if math.abs(self.vel.x) < self.zeroSpeed then
|
|
|
|
self.vel.x = 0
|
|
|
|
end
|
|
|
|
if math.abs(self.vel.y) < self.zeroSpeed then
|
|
|
|
self.vel.y = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if on air, say so!
|
|
|
|
if self.vel.y > 5 then
|
|
|
|
self.isJumping = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if its on ground, then say so.
|
|
|
|
if self.vel.y > 0 then
|
|
|
|
if isThereAnyCollisionAt(
|
|
|
|
self.pos.x,
|
|
|
|
self.pos.y + self.vel.y
|
|
|
|
) then
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = self.coyoteValue
|
2021-10-16 23:06:11 +00:00
|
|
|
self.isJumping = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- horizontal collisions
|
|
|
|
if isThereAnyCollisionAt(self.pos.x + self.vel.x, self.pos.y) then
|
|
|
|
-- checks for ladders
|
|
|
|
if isThereLadderAt(self.pos.x + self.vel.x, self.pos.y)
|
|
|
|
and self.vel.x ~= 0
|
|
|
|
and not isThereLadderAt(self.pos.x, self.pos.y)
|
|
|
|
then
|
|
|
|
self.vel.y = 0
|
|
|
|
self.vel.x = 0
|
|
|
|
|
|
|
|
self.pos.y = self.pos.y - 4*game.scale * current_dt
|
|
|
|
|
|
|
|
self.canFall = false
|
|
|
|
self.canJump = false
|
|
|
|
self.canFriction = false
|
|
|
|
|
|
|
|
self.isOnLadder = true
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = self.coyoteValue
|
2021-10-16 23:06:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- checks for slopes
|
|
|
|
for i = 1, self.climbHeight do
|
|
|
|
if not isThereCollisionAt(self.pos.x + self.vel.x, self.pos.y - i * game.scale)
|
2021-10-22 16:23:05 +00:00
|
|
|
and self.isOnGround > 0 then
|
2021-10-16 23:06:11 +00:00
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
self.pos.x = self.pos.x + self.vel.x * 4/5
|
2021-10-16 23:06:11 +00:00
|
|
|
self.pos.y = self.pos.y - i * game.scale
|
|
|
|
|
|
|
|
self.canFriction = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- hey, you arent permanently stopped while collisioning, just lose a bit of force!
|
|
|
|
if self.canFriction then
|
|
|
|
self.vel.x = self.vel.x * (1 - math.min(current_dt * self.friction/15, 1))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.pos.x = self.pos.x + self.vel.x
|
|
|
|
end
|
|
|
|
|
|
|
|
-- vertical collision
|
|
|
|
if self.vel.y > 0
|
|
|
|
and isThereAnyCollisionAt(self.pos.x, self.pos.y + self.vel.y) then
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = self.coyoteValue
|
2021-10-16 23:06:11 +00:00
|
|
|
self.isJumping = false
|
|
|
|
self.vel.y = 0
|
|
|
|
else
|
|
|
|
self.pos.y = self.pos.y + self.vel.y
|
2021-10-22 16:23:05 +00:00
|
|
|
self.isOnGround = math.max(self.isOnGround - 1, 0)
|
2021-10-16 23:06:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- drop.
|
|
|
|
if self.canFall then
|
|
|
|
self.vel.y = self.vel.y + 2*self.gravity * current_dt
|
|
|
|
end
|
|
|
|
|
|
|
|
-- friction hard in ground, soft in air
|
2021-10-22 16:23:05 +00:00
|
|
|
if self.isOnGround > 0 then
|
2021-10-16 23:06:11 +00:00
|
|
|
self.vel.x = self.vel.x * (1 - math.min(current_dt * self.friction, 1))
|
|
|
|
else
|
|
|
|
self.vel.x = self.vel.x * (1 - math.min(current_dt * self.friction/20, 1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
function Player:New(x,y)
|
|
|
|
local o = Entity:New(x,y)
|
|
|
|
Player.health = 3
|
|
|
|
Player.coins = 0
|
2021-10-16 23:06:11 +00:00
|
|
|
|
2021-10-22 16:23:05 +00:00
|
|
|
-- physics
|
|
|
|
o.vel = {
|
|
|
|
x = 0,
|
|
|
|
y = 0
|
|
|
|
}
|
|
|
|
-- constants
|
|
|
|
o.acc = 90
|
|
|
|
o.friction = 20
|
|
|
|
o.gravity = 9.81
|
|
|
|
o.climbHeight = 4
|
|
|
|
o.jumpForce = 5
|
|
|
|
o.maxSpeed = 600
|
|
|
|
o.jumpMaxSpeed = 9.5
|
|
|
|
o.zeroSpeed = 0.001
|
|
|
|
-- bools
|
|
|
|
o.isJumping = false
|
|
|
|
o.isOnGround = 0
|
|
|
|
o.coyoteValue = 10
|
|
|
|
o.isOnLadder = false
|
|
|
|
o.canJump = true
|
|
|
|
o.canFall = true
|
|
|
|
o.canFriction = true
|
|
|
|
|
|
|
|
-- sprite
|
|
|
|
o.offset = {x = -8, y = -16}
|
2021-10-16 23:06:11 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
|
|
|
|
return o
|
|
|
|
end
|