2021-10-25 23:19:22 +00:00
|
|
|
Player = Entity:New(x,y)
|
|
|
|
|
|
|
|
|
|
|
|
function Player:New(x,y)
|
|
|
|
local o = Entity:New(x,y)
|
2021-11-28 02:38:30 +00:00
|
|
|
Player.health = 3
|
2021-10-25 23:19:22 +00:00
|
|
|
Player.coins = 0
|
|
|
|
|
|
|
|
-- physics
|
2021-11-28 02:38:30 +00:00
|
|
|
o.moveSpeed = 1.5
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2021-11-28 02:38:30 +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
|
2021-11-28 02:38:30 +00:00
|
|
|
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
|
|
|
|
2021-11-28 02:38:30 +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()
|
2021-11-28 02:38:30 +00:00
|
|
|
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
|
|
|
|
2021-11-28 02:38:30 +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()
|
2021-11-28 02:38:30 +00:00
|
|
|
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
|
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
-- animation priority
|
|
|
|
if self.vel.y > 1.25 then
|
2021-10-29 02:15:53 +00:00
|
|
|
self.body = self.body:ChangeTo(animation.nancy.fall)
|
2021-11-28 02:38:30 +00:00
|
|
|
self.mask = self.mask:ChangeTo(self.maskType.fall)
|
|
|
|
elseif self.vel.y < 0 then
|
2021-10-29 02:15:53 +00:00
|
|
|
self.body = self.body:ChangeTo(animation.nancy.jump)
|
2021-11-28 02:38:30 +00:00
|
|
|
self.mask = self.mask:ChangeTo(self.maskType.jump)
|
2021-10-25 23:19:22 +00:00
|
|
|
elseif self.vel.x ~= 0 then
|
2021-10-29 02:15:53 +00:00
|
|
|
self.body = self.body:ChangeTo(animation.nancy.run)
|
2021-11-28 02:38:30 +00:00
|
|
|
self.mask = self.mask:ChangeTo(self.maskType.run)
|
2021-10-25 23:19:22 +00:00
|
|
|
else
|
2021-10-29 02:15:53 +00:00
|
|
|
self.body = self.body:ChangeTo(animation.nancy.idle)
|
2021-11-28 02:38:30 +00:00
|
|
|
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
|
2021-10-29 02:15:53 +00:00
|
|
|
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
|
2021-10-29 02:15:53 +00:00
|
|
|
|
|
|
|
self.body:Animate()
|
|
|
|
self.body:Draw(
|
2021-11-28 02:38:30 +00:00
|
|
|
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,
|
2021-10-29 02:15:53 +00:00
|
|
|
self.sprite_rotation,
|
|
|
|
self.sprite_scale.x * self.sprite_flip.x,
|
|
|
|
self.sprite_scale.y * self.sprite_flip.y
|
|
|
|
)
|
|
|
|
self.mask:Draw(
|
2021-11-28 02:38:30 +00:00
|
|
|
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,
|
2021-10-29 02:15:53 +00:00
|
|
|
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
|