- More cleanup
- Added kupos entity and sprites (body, bow) - Finally understood my code
This commit is contained in:
35
data/scripts/entities/kupo.lua
Normal file
35
data/scripts/entities/kupo.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
Kupo = Entity:New(x,y)
|
||||
|
||||
function Kupo:New(x,y)
|
||||
local o = Entity:New(x,y)
|
||||
|
||||
o.pos = {x = x, y = y}
|
||||
o.speed = 20
|
||||
o.range = 1000
|
||||
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
|
||||
return o
|
||||
end
|
||||
|
||||
function Kupo:DoInput()
|
||||
|
||||
end
|
||||
|
||||
function Kupo:HandleAnimation()
|
||||
-- flip sprite to look in the direction is moving
|
||||
if self.vel.x ~= 0 then self.flip.x = math.sign(self.vel.x) end
|
||||
self:LoadAnimation(animation.kupo.body)
|
||||
end
|
||||
|
||||
function Kupo:DoPhysics()
|
||||
|
||||
-- horizontal collisions
|
||||
if not isThereAnyCollisionAt(self.pos.x + self.vel.x, self.pos.y) then
|
||||
self.pos.x = self.pos.x + self.vel.x
|
||||
end
|
||||
if not isThereAnyCollisionAt(self.pos.x, self.pos.y + self.vel.y) then
|
||||
self.pos.y = self.pos.y + self.vel.y
|
||||
end
|
||||
end
|
||||
@@ -1,38 +1,8 @@
|
||||
player = entity:newEntity(x,y)
|
||||
Player = Entity:New(x,y)
|
||||
|
||||
function InitPlayer(id)
|
||||
player.health = 3
|
||||
player.coins = 0
|
||||
|
||||
-- physics
|
||||
player.vel = {
|
||||
x = 0,
|
||||
y = 0
|
||||
}
|
||||
-- constants
|
||||
player.acc = 90
|
||||
player.friction = 20
|
||||
player.gravity = 9.81
|
||||
player.climbHeight = 3
|
||||
player.jumpForce = 5
|
||||
player.maxSpeed = 600
|
||||
player.jumpMaxSpeed = 9.5
|
||||
player.zeroSpeed = 0.001
|
||||
-- bools
|
||||
player.isJumping = false
|
||||
player.isOnGround = false
|
||||
player.isOnLadder = false
|
||||
player.canJump = true
|
||||
player.canFall = true
|
||||
player.canFriction = true
|
||||
|
||||
-- sprite
|
||||
player.offset = {x = -8, y = -16}
|
||||
end
|
||||
|
||||
function player:DoInput()
|
||||
function Player:DoInput()
|
||||
-- PLATFORMER INPUT
|
||||
if self.isOnGround then
|
||||
if self.isOnGround > 0 then
|
||||
-- apply friction
|
||||
|
||||
-- horizontal input (slide~~)
|
||||
@@ -46,7 +16,7 @@ function player:DoInput()
|
||||
-- vertical input (jump!)
|
||||
if love.keyboard.isDown("up", "w") and self.isJumping ~= true then
|
||||
self.vel.y = self.vel.y - self.jumpForce
|
||||
self.isOnGround = false
|
||||
self.isOnGround = 0
|
||||
self.isJumping = true
|
||||
end
|
||||
end
|
||||
@@ -65,11 +35,11 @@ function player:DoInput()
|
||||
) and love.keyboard.isDown("down", "s")
|
||||
then
|
||||
self.pos.y = self.pos.y + tileProperties.height/3
|
||||
self.isOnGround = false
|
||||
self.isOnGround = 0
|
||||
end
|
||||
end
|
||||
|
||||
function player:HandleAnimation()
|
||||
function Player:HandleAnimation()
|
||||
|
||||
-- flip sprite to look in the direction is moving
|
||||
if self.vel.x ~= 0 then self.flip.x = math.sign(self.vel.x) end
|
||||
@@ -77,9 +47,9 @@ function player:HandleAnimation()
|
||||
-- animation manager
|
||||
if self.isOnLadder then
|
||||
self:LoadAnimation(animation.nancy.jump)
|
||||
elseif not self.isOnGround and self.isJumping and self.vel.y > 1.25 then
|
||||
elseif self.isOnGround == 0 and self.isJumping and self.vel.y > 1.25 then
|
||||
self:LoadAnimation(animation.nancy.fall)
|
||||
elseif not self.isOnGround and self.vel.y < 0 then
|
||||
elseif self.isOnGround == 0 and self.vel.y < 0 then
|
||||
self:LoadAnimation(animation.nancy.jump)
|
||||
elseif self.vel.x ~= 0 then
|
||||
self:LoadAnimation(animation.nancy.run)
|
||||
@@ -93,13 +63,12 @@ function player:HandleAnimation()
|
||||
end
|
||||
end
|
||||
|
||||
function player:DoPhysics()
|
||||
function Player:DoPhysics()
|
||||
-- reset physics resolution
|
||||
self.canFall = true
|
||||
self.canJump = true
|
||||
self.canFriction = true
|
||||
-- reset flags
|
||||
self.isOnGround = false
|
||||
self.isOnLadder = false
|
||||
-- truncate to max & min values
|
||||
if math.abs(self.vel.x) > self.maxSpeed then
|
||||
@@ -126,7 +95,7 @@ function player:DoPhysics()
|
||||
self.pos.x,
|
||||
self.pos.y + self.vel.y
|
||||
) then
|
||||
self.isOnGround = true
|
||||
self.isOnGround = self.coyoteValue
|
||||
self.isJumping = false
|
||||
end
|
||||
end
|
||||
@@ -147,15 +116,15 @@ function player:DoPhysics()
|
||||
self.canFriction = false
|
||||
|
||||
self.isOnLadder = true
|
||||
self.isOnGround = true
|
||||
self.isOnGround = self.coyoteValue
|
||||
end
|
||||
|
||||
-- checks for slopes
|
||||
for i = 1, self.climbHeight do
|
||||
if not isThereCollisionAt(self.pos.x + self.vel.x, self.pos.y - i * game.scale)
|
||||
and self.isOnGround then
|
||||
and self.isOnGround > 0 then
|
||||
|
||||
self.pos.x = self.pos.x + self.vel.x
|
||||
self.pos.x = self.pos.x + self.vel.x * 4/5
|
||||
self.pos.y = self.pos.y - i * game.scale
|
||||
|
||||
self.canFriction = false
|
||||
@@ -174,11 +143,12 @@ function player:DoPhysics()
|
||||
-- vertical collision
|
||||
if self.vel.y > 0
|
||||
and isThereAnyCollisionAt(self.pos.x, self.pos.y + self.vel.y) then
|
||||
self.isOnGround = true
|
||||
self.isOnGround = self.coyoteValue
|
||||
self.isJumping = false
|
||||
self.vel.y = 0
|
||||
else
|
||||
self.pos.y = self.pos.y + self.vel.y
|
||||
self.isOnGround = math.max(self.isOnGround - 1, 0)
|
||||
end
|
||||
|
||||
-- drop.
|
||||
@@ -187,7 +157,7 @@ function player:DoPhysics()
|
||||
end
|
||||
|
||||
-- friction hard in ground, soft in air
|
||||
if self.isOnGround then
|
||||
if self.isOnGround > 0 then
|
||||
self.vel.x = self.vel.x * (1 - math.min(current_dt * self.friction, 1))
|
||||
else
|
||||
self.vel.x = self.vel.x * (1 - math.min(current_dt * self.friction/20, 1))
|
||||
@@ -195,9 +165,36 @@ function player:DoPhysics()
|
||||
end
|
||||
|
||||
|
||||
function player:newPlayer(x,y)
|
||||
local o = entity:newEntity(x,y)
|
||||
function Player:New(x,y)
|
||||
local o = Entity:New(x,y)
|
||||
Player.health = 3
|
||||
Player.coins = 0
|
||||
|
||||
-- physics
|
||||
o.vel = {
|
||||
x = 0,
|
||||
y = 0
|
||||
}
|
||||
-- constants
|
||||
o.acc = 90
|
||||
o.friction = 20
|
||||
o.gravity = 9.81
|
||||
o.climbHeight = 4
|
||||
o.jumpForce = 5
|
||||
o.maxSpeed = 600
|
||||
o.jumpMaxSpeed = 9.5
|
||||
o.zeroSpeed = 0.001
|
||||
-- bools
|
||||
o.isJumping = false
|
||||
o.isOnGround = 0
|
||||
o.coyoteValue = 10
|
||||
o.isOnLadder = false
|
||||
o.canJump = true
|
||||
o.canFall = true
|
||||
o.canFriction = true
|
||||
|
||||
-- sprite
|
||||
o.offset = {x = -8, y = -16}
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
|
||||
|
||||
Reference in New Issue
Block a user