Mothback/data/scripts/entities/player.lua

184 lines
4.8 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.3
o.zeroSpeed = 0.01
o.move_x = 0
o.airFriction = 0.01
o.groundFriction = 0.3
o.jumpImpulse = 3.5
o.dashCooldownTime = 0.1
o.dashCooldownTimer = 0
o.dashTimer = 0
o.dashTime = 0.15
o.dashDistance = 40
o.dashSpeed = o.dashDistance / (o.dashTime*60)
o.dashCount = 1
2021-10-29 00:17:18 +00:00
o.boxCollision = {
from = {x = -8, y = -16},
to = {x = 8, y = 0}
}
o.lightRange = 0--32
2021-10-25 23:19:22 +00:00
-- status
o.isDashing = false
2021-10-25 23:19:22 +00:00
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.target_offset = {x = 0, y = 12}
o.body = Animation:New(animation.nancy.idle)
o.mask = Animation:New(animation.moth_mask.idle)
o:centerOffset(o.body)
o:getBoundingBox(o.body, 3,-3,0,-1)
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()
-- light
self.light.pos.x = self.pos.x-self.target_offset.x
self.light.pos.y = self.pos.y-self.target_offset.y
if self.dashTimer <= 0 then
if self.isOnGround then
self.vel.x = self.vel.x * (1-self.groundFriction)
else
self.vel.x = self.vel.x * (1-self.airFriction)
end
if math.abs(self.vel.x) < self.zeroSpeed then self.vel.x = 0 end
if Keybind:CheckDown(Keybind.move.left) then
self.move_x = -self.moveSpeed
elseif Keybind:CheckDown(Keybind.move.right) then
self.move_x = self.moveSpeed
else
self.move_x = 0
end
self.vel.x = self.vel.x
if Keybind:CheckDown(Keybind.move.jump) then
if self.isOnGround then
self.vel.y = -self.jumpImpulse
end
end
end
self.dashCooldownTimer = math.max(0,self.dashCooldownTimer - current_dt)
if Keybind:CheckDown(Keybind.move.dash) then
if self.dashCooldownTimer == 0
and not self.isDashing
and self.dashCount > 0 then
self.dashCount = self.dashCount - 1
self.isDashing = true
local vertical = 0
if Keybind:CheckDown(Keybind.move.down) then vertical = vertical + 1 end
if Keybind:CheckDown(Keybind.move.up) then vertical = vertical - 1 end
local horizontal = 0
if Keybind:CheckDown(Keybind.move.right) then horizontal = horizontal + 1 end
if Keybind:CheckDown(Keybind.move.left) then horizontal = horizontal - 1 end
if horizontal == 0 and vertical == 0 then
horizontal = self.sprite_flip.x
end
self.dashDirection = GetAngleFromVector(horizontal, vertical)
self.dashTimer = self.dashTime
end
else
self.isDashing = false
end
end
2021-10-25 23:19:22 +00:00
function Player:DoPhysics()
self.isOnGround = false
self.dashTimer = self.dashTimer - current_dt
if self.dashTimer > 0 then
self.dashCooldownTimer = self.dashCooldownTime
self.vel.x = self.dashSpeed * math.cos(self.dashDirection)
self.vel.y = self.dashSpeed * math.sin(self.dashDirection)
else
self.dashTimer = 0
self.vel.y = self.vel.y + gravity
end
if not self:isCollidingAt(self.pos.x + self.vel.x + self.move_x, self.pos.y, objects.collisions) then
self.pos.x = self.pos.x + self.vel.x + self.move_x
else
self.vel.x = 0
end
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
self.pos.y = self.pos.y + self.vel.y
else
if self.vel.y > 0 then
self.isOnGround = true
self.dashCount = 1
self.vel.y = 0
end
end
2021-10-25 23:19:22 +00:00
end
function Player:HandleAnimation()
if self.dashTimer > 0 then
self.sprite_tint = {1,1,0}
else
self.sprite_tint = {1,1,1}
end
2021-10-25 23:19:22 +00:00
-- flip sprite to look in the direction is moving
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
2021-10-25 23:19:22 +00:00
-- 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 + self.move_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:Draw(self.body)
if self.dashCount > 0 then
self:Draw(self.mask)
end
2021-10-25 23:19:22 +00:00
end