Code: Select all
local Class = require 'lib.hump.class'
local Entity = require 'entities.entity'
local player = Class{
__includes = Entity
}
function player:init(world, x, y)
self.img = love.graphics.newImage('/assets/character_block.png')
Entity.init(self, world, x, y, self.img:getWidth(), self.img:getHeight())
-- player values
self.xVelocity = 0 -- current velocity on x, y axes
self.yVelocity = 0
self.acc = 100 -- acceleration of player
self.maxSpeed = 600 -- the top speed
self.friction = 20 -- slow player down - could toggle this situationally to create icy or slick platforms
self.gravity = 80 -- towards the bottom
self.isJumping = false -- in the process of jumping?
self.doubleJumps = 3
self.isGrounded = false -- on the ground?
self.hasReachedMax = false -- is this as high as player can go?
self.jumpAcc = 500 -- how fast does player accelerate towards the top
self.jumpMaxSpeed = 11 -- speed limit while jumping
self.world:add(self, self:getRect())
end
function player:collisionFilter(other)
local x, y, w, h = self.world:getRect(other)
local playerBottom = self.y + self.h
local otherBottom = y + h
if playerBottom <= y then -- bottom of player collides with top of platform.
return 'slide'
end
end
function player:update(dt)
local prevX, prevY = self.x, self.y
-- Apply Friction
self.xVelocity = self.xVelocity * (1 - math.min(dt * self.friction, 1))
self.yVelocity = self.yVelocity * (1 - math.min(dt * self.friction, 1))
-- Apply gravity
self.yVelocity = self.yVelocity + self.gravity * dt
if love.keyboard.isDown("a") and self.xVelocity > -self.maxSpeed then
self.xVelocity = self.xVelocity - self.acc * dt
elseif love.keyboard.isDown("d") and self.xVelocity < self.maxSpeed then
self.xVelocity = self.xVelocity + self.acc * dt
end
-- The Jump code gets a lttle bit crazy. Bare with me.
if love.keyboard.isDown("w") then
if -self.yVelocity < self.jumpMaxSpeed and not self.hasReachedMax then
self.yVelocity = self.yVelocity - self.jumpAcc * dt
elseif math.abs(self.yVelocity) > self.jumpMaxSpeed then
self.hasReachedMax = true
end
self.isGrounded = false -- no longer in contact with the ground
end
-- double-jump script
function love.keyreleased(key)
if key == 'w' then
function love.keypressed(key)
if key == 'w' and self.doubleJumps > 0 and self.doubleJumps ~= 0 then
self.yVelocity = self.yVelocity - self.jumpAcc * dt
self.doubleJumps = self.doubleJumps - 1
end
end
end
end
-- store the location the player will arrive at should
local goalX = self.x + self.xVelocity
local goalY = self.y + self.yVelocity
-- move player while testing for collisions
self.x, self.y, collisions, len = self.world:move(self, goalX, goalY, self.collisionFilter)
-- loop through those collisions to see if anything important is happening
for i, coll in ipairs(collisions) do
if coll.touch.y > goalY then -- touched below intended target.
self.hasReachedMax = true
self.isGrounded = false
elseif coll.normal.y < 0 then
self.hasReachedMax = false
self.isGrounded = true
self.doubleJumps = 3
end
end
end
function player:draw()
love.graphics.draw(self.img, self.x, self.y)
end
return player