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
|
2022-01-29 07:49:45 +00:00
|
|
|
o.moveSpeed = 1.3 -- gameworld pixels
|
|
|
|
o.zeroSpeed = 0.01 -- gameworld pixels
|
|
|
|
o.move_x = 0 -- gameworld pixels
|
2022-01-17 23:14:54 +00:00
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
o.airFriction = 0.01 -- gameworld pixels
|
|
|
|
o.groundFriction = 0.3 -- gameworld pixels
|
2022-01-17 23:14:54 +00:00
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
o.jumpImpulse = 3.5 -- gameworld pixels
|
2022-01-17 23:14:54 +00:00
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
o.coyoteAmount = 5 -- int
|
|
|
|
o.coyoteValue = 5 -- frames
|
2022-01-17 23:14:54 +00:00
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
o.dashCooldownTime = 0.1 -- seconds
|
|
|
|
o.dashCooldownTimer = 0 -- seconds
|
|
|
|
|
|
|
|
o.dashTimer = 0 -- seconds
|
|
|
|
o.dashTime = 0.15 -- seconds
|
|
|
|
o.dashDistance = 40 -- gameworld pixels
|
|
|
|
o.dashSpeed = o.dashDistance / (o.dashTime*60) -- pixels
|
|
|
|
o.dashCount = 1 -- int
|
|
|
|
o.dashAmount = 0 -- int
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
o.boxCollision = {
|
2022-01-29 07:49:45 +00:00
|
|
|
from = {x = -8, y = -16}, --gameworld pixels
|
|
|
|
to = {x = 8, y = 0} -- gameworld pixels
|
2021-11-28 02:38:30 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
o.lightRange = 344 -- screen pixels
|
2022-01-17 23:14:54 +00:00
|
|
|
|
2021-10-25 23:19:22 +00:00
|
|
|
-- status
|
2022-01-17 23:14:54 +00:00
|
|
|
o.isDashing = false
|
2021-10-25 23:19:22 +00:00
|
|
|
o.isJumping = false
|
2022-01-29 07:49:45 +00:00
|
|
|
o.isOnGround = true
|
|
|
|
o.isOnLadder = false
|
2021-10-25 23:19:22 +00:00
|
|
|
o.canJump = true
|
|
|
|
o.canFall = true
|
|
|
|
o.canFriction = true
|
2021-11-28 02:38:30 +00:00
|
|
|
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)
|
2022-01-17 23:14:54 +00:00
|
|
|
o:centerOffset(o.body)
|
2022-01-29 07:49:45 +00:00
|
|
|
o:getBoundingBox(o.body,0,3,-1,-3)
|
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
|
|
|
|
2022-01-19 22:29:02 +00:00
|
|
|
table.insert(LoadedEntities,o)
|
|
|
|
o.id = #LoadedEntities
|
|
|
|
|
2021-10-25 23:19:22 +00:00
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function Player:Smart()
|
2022-01-17 23:14:54 +00:00
|
|
|
-- 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
|
|
|
|
|
2022-01-19 13:55:15 +00:00
|
|
|
if Keybind:CheckDown(Keybind.move.left) then
|
2022-01-17 23:14:54 +00:00
|
|
|
self.move_x = -self.moveSpeed
|
2022-01-19 13:55:15 +00:00
|
|
|
elseif Keybind:CheckDown(Keybind.move.right) then
|
2022-01-17 23:14:54 +00:00
|
|
|
self.move_x = self.moveSpeed
|
|
|
|
else
|
|
|
|
self.move_x = 0
|
|
|
|
end
|
|
|
|
self.vel.x = self.vel.x
|
|
|
|
|
2022-01-19 13:55:15 +00:00
|
|
|
if Keybind:CheckDown(Keybind.move.jump) then
|
2022-01-29 07:49:45 +00:00
|
|
|
if self.coyoteValue > 0 then
|
2022-01-17 23:14:54 +00:00
|
|
|
self.vel.y = -self.jumpImpulse
|
2022-01-29 07:49:45 +00:00
|
|
|
self.coyoteValue = 0
|
2022-01-17 23:14:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-29 07:49:45 +00:00
|
|
|
if self.isOnGround then
|
|
|
|
self.coyoteValue = self.coyoteAmount
|
|
|
|
elseif self.coyoteValue > 0 then
|
|
|
|
self.coyoteValue = self.coyoteValue - 1
|
|
|
|
end
|
|
|
|
|
2022-01-17 23:14:54 +00:00
|
|
|
self.dashCooldownTimer = math.max(0,self.dashCooldownTimer - current_dt)
|
2022-01-19 13:55:15 +00:00
|
|
|
if Keybind:CheckDown(Keybind.move.dash) then
|
2022-01-17 23:14:54 +00:00
|
|
|
if self.dashCooldownTimer == 0
|
|
|
|
and not self.isDashing
|
|
|
|
and self.dashCount > 0 then
|
|
|
|
self.dashCount = self.dashCount - 1
|
|
|
|
self.isDashing = true
|
|
|
|
local vertical = 0
|
2022-01-19 13:55:15 +00:00
|
|
|
if Keybind:CheckDown(Keybind.move.down) then vertical = vertical + 1 end
|
|
|
|
if Keybind:CheckDown(Keybind.move.up) then vertical = vertical - 1 end
|
2022-01-17 23:14:54 +00:00
|
|
|
local horizontal = 0
|
2022-01-19 13:55:15 +00:00
|
|
|
if Keybind:CheckDown(Keybind.move.right) then horizontal = horizontal + 1 end
|
|
|
|
if Keybind:CheckDown(Keybind.move.left) then horizontal = horizontal - 1 end
|
2022-01-17 23:14:54 +00:00
|
|
|
|
|
|
|
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
|
2021-11-28 02:38:30 +00:00
|
|
|
end
|
2021-10-25 23:19:22 +00:00
|
|
|
|
2021-11-28 02:38:30 +00:00
|
|
|
function Player:DoPhysics()
|
2022-01-29 07:49:45 +00:00
|
|
|
-- reset state
|
2022-01-17 23:14:54 +00:00
|
|
|
self.isOnGround = false
|
2022-01-29 07:49:45 +00:00
|
|
|
-- adjust timers
|
2022-01-17 23:14:54 +00:00
|
|
|
self.dashTimer = self.dashTimer - current_dt
|
2022-01-19 23:53:59 +00:00
|
|
|
|
|
|
|
-- DASH STATE
|
2022-01-17 23:14:54 +00:00
|
|
|
if self.dashTimer > 0 then
|
2022-01-29 07:49:45 +00:00
|
|
|
-- dash particle
|
2022-01-19 23:53:59 +00:00
|
|
|
local particle_data = {
|
|
|
|
animation = self.body,
|
|
|
|
sprite_tint = HEX2RGB("#fed100"),
|
2022-01-21 14:56:25 +00:00
|
|
|
sprite_alpha = 0.5,
|
2022-01-19 23:53:59 +00:00
|
|
|
sprite_flip = {
|
|
|
|
x = self.sprite_flip.x,
|
|
|
|
y = self.sprite_flip.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Particle:New(self.pos.x,self.pos.y,particle_data)
|
2022-01-17 23:14:54 +00:00
|
|
|
self.dashCooldownTimer = self.dashCooldownTime
|
2022-01-29 07:49:45 +00:00
|
|
|
-- dash movement
|
2022-01-17 23:14:54 +00:00
|
|
|
self.vel.x = self.dashSpeed * math.cos(self.dashDirection)
|
|
|
|
self.vel.y = self.dashSpeed * math.sin(self.dashDirection)
|
|
|
|
else
|
2022-01-29 07:49:45 +00:00
|
|
|
-- not in dash; fall normally
|
2022-01-17 23:14:54 +00:00
|
|
|
self.dashTimer = 0
|
|
|
|
self.vel.y = self.vel.y + gravity
|
|
|
|
end
|
|
|
|
|
2022-01-22 22:10:21 +00:00
|
|
|
if not self:isCollidingAt(self.pos.x + self.vel.x + self.move_x, self.pos.y, LoadedObjects.Collisions) then
|
2022-01-17 23:14:54 +00:00
|
|
|
self.pos.x = self.pos.x + self.vel.x + self.move_x
|
|
|
|
else
|
|
|
|
self.vel.x = 0
|
|
|
|
end
|
|
|
|
|
2022-01-22 22:10:21 +00:00
|
|
|
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
|
2022-01-17 23:14:54 +00:00
|
|
|
self.pos.y = self.pos.y + self.vel.y
|
|
|
|
else
|
|
|
|
if self.vel.y > 0 then
|
|
|
|
self.isOnGround = true
|
2022-01-29 07:49:45 +00:00
|
|
|
self.dashCount = self.dashAmount
|
2022-01-17 23:14:54 +00:00
|
|
|
self.vel.y = 0
|
|
|
|
end
|
|
|
|
end
|
2021-10-25 23:19:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Player:HandleAnimation()
|
|
|
|
-- flip sprite to look in the direction is moving
|
2022-01-17 23:14:54 +00:00
|
|
|
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
|
2021-10-25 23:19:22 +00:00
|
|
|
|
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)
|
2022-01-17 23:14:54 +00:00
|
|
|
elseif self.vel.x + self.move_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()
|
2022-01-17 23:14:54 +00:00
|
|
|
self:Draw(self.body)
|
|
|
|
if self.dashCount > 0 then
|
|
|
|
self:Draw(self.mask)
|
|
|
|
end
|
2021-10-25 23:19:22 +00:00
|
|
|
end
|