rope, moved entities from LoadedEntities to LoadedObjects.Entities

This commit is contained in:
lustlion
2022-02-10 18:18:37 +01:00
parent 7f42dea6fa
commit ff99b79563
15 changed files with 143 additions and 42 deletions

View File

@@ -3,9 +3,8 @@
function Player:New(x,y)
local o = Entity:New(x,y)
Player.health = 3
Player.coins = 0
o.type = "player"
-- physics
o.moveSpeed = 1.3 -- gameworld pixels
o.zeroSpeed = 0.01 -- gameworld pixels
@@ -22,6 +21,7 @@
o.dashCooldownTime = 0.1 -- seconds
o.dashCooldownTimer = 0 -- seconds
-- dash values
o.dashTimer = 0 -- seconds
o.dashTime = 0.15 -- seconds
o.dashDistance = 40 -- gameworld pixels
@@ -29,30 +29,36 @@
o.dashCount = 1 -- int
o.dashAmount = 10 -- int
-- hook values
o.hookDistance = 100
o.hookedDistance = 80
o.hookAnchor = {
x = nil,
y = nil
}
o.boxCollision = {
from = {x = -8, y = -16}, --gameworld pixels
to = {x = 8, y = 0} -- gameworld pixels
}
o.lightRange = 0 -- screen pixels
o.lightRange = 10 -- screen pixels
-- status
o.isDashing = false
o.isJumping = false
o.isHooked = false
o.isOnGround = true
o.isOnLadder = false
o.canJump = true
o.canFall = true
o.canFriction = true
o.maskType = animation.moth_mask
o.anchorRespawn = {
x = o.pos.x,
y = o.pos.y
}
o.anchorRope = {
x = nil,
y = nil
}
-- sprite
o.target_offset = {x = 0, y = 0}
@@ -64,8 +70,8 @@
-- lights
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
table.insert(LoadedEntities,o)
o.id = #LoadedEntities
table.insert(LoadedObjects.Entities,o)
o.id = #LoadedObjects.Entities
setmetatable(o, self)
self.__index = self
@@ -73,15 +79,24 @@
end
function Player:Smart()
self:LightAdjust(-self.target_offset.x,-self.target_offset.y)
self:LightAdjust(self.target_offset.x,self.target_offset.y)
-- reset coyoteValue
if self.isOnGround then
self.coyoteValue = self.coyoteAmount
elseif self.coyoteValue > 0 then
self.coyoteValue = self.coyoteValue - 1
end
if self.dashTimer <= 0 then
-- horizontal movement
if Keybind:CheckDown(Keybind.move.left) then
self.move_x = -self.moveSpeed
elseif Keybind:CheckDown(Keybind.move.right) then
self.move_x = self.moveSpeed
end
-- jump if on ground (coyotevalue)
if Keybind:CheckDown(Keybind.move.jump) then
if self.coyoteValue > 0 then
self.vel.y = -self.jumpImpulse
@@ -90,19 +105,20 @@ function Player:Smart()
end
end
if self.isOnGround then
self.coyoteValue = self.coyoteAmount
elseif self.coyoteValue > 0 then
self.coyoteValue = self.coyoteValue - 1
end
-- dash timer
self.dashCooldownTimer = math.max(0,self.dashCooldownTimer - current_dt)
-- try to dash
if Keybind:CheckDown(Keybind.move.dash) then
if self.dashCooldownTimer == 0
and not self.isDashing
and self.dashCount > 0 then
-- state player
self.dashCount = self.dashCount - 1
self.isDashing = true
-- get dash direction
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
@@ -110,16 +126,32 @@ function Player:Smart()
if Keybind:CheckDown(Keybind.move.right) then horizontal = horizontal + 1 end
if Keybind:CheckDown(Keybind.move.left) then horizontal = horizontal - 1 end
-- if no direction, then dash forward
if horizontal == 0 and vertical == 0 then
horizontal = self.sprite_flip.x
end
-- set dash values
self.dashDirection = GetAngleFromVector(horizontal, vertical)
self.dashTimer = self.dashTime
end
else
-- not dashing!
self.isDashing = false
end
if Keybind:CheckDown(Keybind.move.hook) then
local anchor = self:CheckNearest("decoration",self.hookDistance)
if anchor then
self.isHooked = true
self.hookAnchor = {
x = anchor.pos.x,
y = anchor.pos.y
}
end
else
self.isHooked = false
end
end
function Player:DoPhysics()
@@ -131,13 +163,16 @@ function Player:DoPhysics()
end
if math.abs(self.vel.x) < self.zeroSpeed then self.vel.x = 0 end
end
-- reset state
self.canFall = true
self.isOnGround = false
-- adjust timers
self.dashTimer = self.dashTimer - current_dt
-- DASH STATE
if self.dashTimer > 0 then
self.canFall = false
-- dash particle
local particle_data = {
animation = self.body,
@@ -153,28 +188,44 @@ function Player:DoPhysics()
-- dash movement
self.vel.x = self.dashSpeed * math.cos(self.dashDirection)
self.vel.y = self.dashSpeed * math.sin(self.dashDirection)
else
-- not in dash; fall normally
end
-- hook state
if self.isHooked then
local hook = Vector(self.pos.x, self.pos.y, self.hookAnchor.x, self.hookAnchor.y)
if GetVectorValue(hook) > self.hookedDistance then
local hook_angle = GetAngleFromVector(hook[1],hook[2])
self.pos.x = self.hookAnchor.x + self.hookedDistance * math.cos(-math.rad(180)+hook_angle)
self.pos.y = self.hookAnchor.y + self.hookedDistance * math.sin(-math.rad(180)+hook_angle)
end
end
if self.canFall then
-- not in dash or hook; fall normally
self.dashTimer = 0
self.vel.y = self.vel.y + gravity
end
-- horizontal collision
if not self:isCollidingAt(self.pos.x + self.vel.x + self.move_x, self.pos.y, LoadedObjects.Collisions) then
self.pos.x = self.pos.x + self.vel.x + self.move_x
else
self.vel.x = 0
end
-- vertical collision
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
self.pos.y = self.pos.y + self.vel.y
else
if self.vel.y > 0 then
self.isOnGround = true
self.dashCount = self.dashAmount
self.vel.y = 0
end
self.vel.y = 0
end
-- if u collision w hazard, respawn
if self:isCollidingAt(self.pos.x, self.pos.y, LoadedObjects.Hazards) then
self:Respawn()
end
@@ -190,8 +241,8 @@ function Player:HandleAnimation()
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
-- animation priority
if self.vel.y > 1.25 then
self.body = self.body:ChangeTo(animation.nancy.fall)
if self.vel.y > 1.25 then
self.mask = self.mask:ChangeTo(self.maskType.fall)
elseif self.vel.y < 0 then
self.body = self.body:ChangeTo(animation.nancy.jump)
@@ -209,6 +260,15 @@ function Player:HandleAnimation()
if self.body.anim_speed < 0.5 then self.body.anim_speed = self.body.anim_speed + 0.001 end
end
if self.isHooked then
love.graphics.line(
-Camera.pos.x + self.pos.x,
-Camera.pos.y + self.pos.y,
-Camera.pos.x + self.hookAnchor.x,
-Camera.pos.y + self.hookAnchor.y
)
end
self.body:Animate()
self:Draw(self.body)
if self.dashCount > 0 then