Mothback/data/scripts/entities/player.lua

100 lines
3.4 KiB
Lua

Player = Entity:New(x,y)
function Player:New(x,y)
local o = Entity:New(x,y)
Player.health = 3
Player.coins = 0
-- physics
o.moveSpeed = 1.5
o.boxCollision = {
from = {x = -8, y = -16},
to = {x = 8, y = 0}
}
o.lightRange = 32
-- status
o.isJumping = false
o.isOnGround = 0
o.coyoteValue = 5
o.isOnLadder = false
o.canJump = true
o.canFall = true
o.canFriction = true
o.maskType = animation.moth_mask
-- sprite
o.sprite_offset = {x = 8, y = 16}
o.target_offset = {x = 0, y = 12}
o.body = Animation:New(animation.nancy.idle)
o.mask = Animation:New(animation.moth_mask.idle)
-- lights
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
setmetatable(o, self)
self.__index = self
return o
end
function Player:Smart()
if love.keyboard.isDown("a") then self.vel.x = -self.moveSpeed
elseif love.keyboard.isDown("d") then self.vel.x = self.moveSpeed
else self.vel.x = 0 end
if love.keyboard.isDown("w") then self.vel.y = -self.moveSpeed
elseif love.keyboard.isDown("s") then self.vel.y = self.moveSpeed
else self.vel.y = 0 end
end
function Player:DoPhysics()
self.pos.x = self.pos.x + self.vel.x
self.pos.y = self.pos.y + self.vel.y
end
function Player:HandleAnimation()
self.light.pos.x = self.pos.x-self.target_offset.x
self.light.pos.y = self.pos.y-self.target_offset.y
-- flip sprite to look in the direction is moving
if self.vel.x ~= 0 then self.sprite_flip.x = math.sign(self.vel.x) end
-- animation priority
if self.vel.y > 1.25 then
self.body = self.body:ChangeTo(animation.nancy.fall)
self.mask = self.mask:ChangeTo(self.maskType.fall)
elseif self.vel.y < 0 then
self.body = self.body:ChangeTo(animation.nancy.jump)
self.mask = self.mask:ChangeTo(self.maskType.jump)
elseif self.vel.x ~= 0 then
self.body = self.body:ChangeTo(animation.nancy.run)
self.mask = self.mask:ChangeTo(self.maskType.run)
else
self.body = self.body:ChangeTo(animation.nancy.idle)
self.mask = self.mask:ChangeTo(self.maskType.idle)
end
-- special case: idle animation gets slower by time
if self.body.anim_path == animation.nancy.idle.path then
if self.body.anim_speed < 0.5 then self.body.anim_speed = self.body.anim_speed + 0.001 end
end
self.body:Animate()
self.body:Draw(
math.floor(self.pos.x) - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x,
math.floor(self.pos.y)- Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y,
self.sprite_rotation,
self.sprite_scale.x * self.sprite_flip.x,
self.sprite_scale.y * self.sprite_flip.y
)
self.mask:Draw(
math.floor(self.pos.x) - Camera.pos.x - ( (self.sprite_offset.x) * math.cos(self.sprite_rotation) - (self.sprite_offset.y) * math.sin(self.sprite_rotation)) * self.sprite_scale.x * self.sprite_flip.x,
math.floor(self.pos.y)- Camera.pos.y - ( (self.sprite_offset.x) * math.sin(self.sprite_rotation) + (self.sprite_offset.y) * math.cos(self.sprite_rotation)) * self.sprite_scale.y * self.sprite_flip.y,
self.sprite_rotation,
self.sprite_scale.x * self.sprite_flip.x,
self.sprite_scale.y * self.sprite_flip.y
)
end