Mothback/data/scripts/entities/player.lua

100 lines
3.4 KiB
Lua
Raw Normal View History

2021-10-25 23:19:22 +00:00
Player = Entity:New(x,y)
function Player:New(x,y)
local o = Entity:New(x,y)
Player.health = 3
2021-10-25 23:19:22 +00:00
Player.coins = 0
-- physics
o.moveSpeed = 1.5
2021-10-29 00:17:18 +00:00
o.boxCollision = {
from = {x = -8, y = -16},
to = {x = 8, y = 0}
}
o.lightRange = 32
2021-10-25 23:19:22 +00:00
-- 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)
2021-10-29 00:17:18 +00:00
-- lights
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
2021-10-29 00:17:18 +00:00
2021-10-25 23:19:22 +00:00
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
2021-10-25 23:19:22 +00:00
function Player:DoPhysics()
self.pos.x = self.pos.x + self.vel.x
self.pos.y = self.pos.y + self.vel.y
2021-10-25 23:19:22 +00:00
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
2021-10-25 23:19:22 +00:00
-- 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)
2021-10-25 23:19:22 +00:00
elseif self.vel.x ~= 0 then
self.body = self.body:ChangeTo(animation.nancy.run)
self.mask = self.mask:ChangeTo(self.maskType.run)
2021-10-25 23:19:22 +00:00
else
self.body = self.body:ChangeTo(animation.nancy.idle)
self.mask = self.mask:ChangeTo(self.maskType.idle)
2021-10-25 23:19:22 +00:00
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
2021-10-25 23:19:22 +00:00
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
)
2021-10-25 23:19:22 +00:00
end