Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --player.lua
- --require
- local gameObject = require('gameObject')
- -- class
- local player = gameObject:new()
- --enums
- player.playerStateMovementState = {
- onGround = 'onGround',
- inAir = 'inAir',
- onLadder = 'onLadder',
- onPlatform = 'onPlatform'
- }
- function player:new(x, y, path)
- local obj = {
- xPosition = x or 0,
- yPosition = y or 0,
- xVelocity = 0,
- yVelocity = 0,
- gravity = 9.81 * 7,
- speed = 30,
- jumpStrength = -7,
- state = self.playerStateMovementState.inAir,
- playerSprite = love.graphics.newImage(path)
- }
- setmetatable(obj, self)
- self.__index = self
- return obj
- end
- function player:move(direction)
- if direction == 'left' then
- self.xVelocity = self.xVelocity - self.speed
- end
- if direction == 'right' then
- self.xVelocity = self.xVelocity + self.speed
- end
- end
- function player:jump()
- self.yVelocity = self.yVelocity + self.jumpStrength
- end
- function player:checkForInput()
- if love.keyboard.isDown('d') then
- self:move('right')
- end
- if love.keyboard.isDown('a') then
- self:move('left')
- end
- if love.keyboard.isDown('space') then
- self:jump()
- end
- end
- function player:applyVelocity(dt)
- self.xPosition = self.xPosition + self.xVelocity * dt
- self.yPosition = self.yPosition + self.yVelocity * dt
- self.yVelocity = self.yVelocity + self.gravity * dt
- end
- function player:load()
- end
- function player:update(dt)
- self:checkForInput()
- self:applyVelocity(dt)
- end
- function player:draw()
- love.graphics.draw(self.playerSprite, self.xPosition, self.yPosition)
- end
- return player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement