Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local gameObject = require('gameObjects/gameObject')
- local player = gameObject:new()
- player.image = love.graphics.newImage('assets/player/testRectangle.png')
- player.x = 32
- player.y = 32
- player.prevX = 32
- player.prevY = 32
- player.speed = 0
- player.acceleration = 15
- player.deceleration = 7
- player.maxSpeed = 25
- player.velocityX = 0
- player.velocityY = 0
- player.height = 8
- player.width = 8
- player.boundingRectangle = {
- x = player.x,
- y = player.y,
- width = player.width,
- height = player.height
- }
- player.directions = {
- UP = 'UP',
- DOWN = 'DOWN',
- LEFT = 'LEFT',
- RIGHT = 'RIGHT',
- RIGHTUP = 'RIGHTUP',
- RIGHTDOWN = 'RIGHTDOWN',
- LEFTUP = 'LEFTUP',
- LEFTDOWN = 'LEFTDOWN',
- NONE = 'NONE'
- }
- player.direction = player.directions.NONE
- player.states = {
- IDLE = 'IDLE'
- }
- player.state = player.states.IDLE
- function player:new()
- local obj = {}
- setmetatable(obj, self)
- self.__index = self
- return obj
- end
- function player:applyVelocity(dt)
- if self.direction == self.directions.UP then
- self.velocityY = self.velocityY - self.acceleration * dt
- end
- if self.direction == self.directions.LEFT then
- self.velocityX = self.velocityX - self.acceleration * dt
- end
- if self.direction == self.directions.DOWN then
- self.velocityY = self.velocityY + self.acceleration * dt
- end
- if self.direction == self.directions.RIGHT then
- self.velocityX = self.velocityX + self.acceleration * dt
- end
- if self.direction == self.directions.RIGHTUP then
- self.velocityX = self.velocityX + self.acceleration * dt
- self.velocityY = self.velocityY - self.acceleration * dt
- end
- if self.direction == self.directions.LEFTUP then
- self.velocityX = self.velocityX - self.acceleration * dt
- self.velocityY = self.velocityY - self.acceleration * dt
- end
- if self.direction == self.directions.RIGHTDOWN then
- self.velocityX = self.velocityX + self.acceleration * dt
- self.velocityY = self.velocityY + self.acceleration * dt
- end
- if self.direction == self.directions.LEFTDOWN then
- self.velocityX = self.velocityX - self.acceleration * dt
- self.velocityY = self.velocityY + self.acceleration * dt
- end
- self:normalizeVector()
- end
- function player:applyDeceleration(dt)
- self.velocityX = self.velocityX * (1 - self.deceleration * dt)
- self.velocityY = self.velocityY * (1 - self.deceleration * dt)
- local threshold = 0.01
- if math.abs(self.velocityX) < threshold then
- self.velocityX = 0
- end
- if math.abs(self.velocityY) < threshold then
- self.velocityY = 0
- end
- end
- function player:setPosition()
- self.prevX = self.x
- self.prevY = self.y
- self.x = self.x + self.velocityX
- self.y = self.y + self.velocityY
- self.boundingRectangle.x = self.x
- self.boundingRectangle.y = self.y
- end
- function player:checkForInput(dt)
- self.direction = self.directions.NONE
- if love.keyboard.isDown('w') then
- self.direction = self.directions.UP
- end
- if love.keyboard.isDown('a') then
- self.direction = self.directions.LEFT
- end
- if love.keyboard.isDown('s') then
- self.direction = self.directions.DOWN
- end
- if love.keyboard.isDown('d') then
- self.direction = self.directions.RIGHT
- end
- if love.keyboard.isDown('w') and love.keyboard.isDown('d') then
- self.direction = self.directions.RIGHTUP
- end
- if love.keyboard.isDown('w') and love.keyboard.isDown('a') then
- self.direction = self.directions.LEFTUP
- end
- if love.keyboard.isDown('s') and love.keyboard.isDown('d') then
- self.direction = self.directions.RIGHTDOWN
- end
- if love.keyboard.isDown('s') and love.keyboard.isDown('a') then
- self.direction = self.directions.LEFTDOWN
- end
- if love.keyboard.isDown('return') then
- print(self.direction)
- end
- end
- function player:getFuturePosition()
- local collidableObject = {}
- collidableObject.boundingRectangle = {
- x = self.x + self.velocityX,
- y = self.y + self.velocityY,
- width = self.width,
- height = self.height
- }
- return collidableObject
- end
- function player:normalizeVector()
- local length = math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY)
- if length > self.maxSpeed then
- self.velocityX = (self.velocityX / length) * self.maxSpeed
- self.velocityY = (self.velocityY / length) * self.maxSpeed
- end
- end
- function player:checkForCollision()
- local didCollide, overlapX, overlapY = checkForCollisionWithMap(self:getFuturePosition())
- if didCollide then
- print('overlapX and Y', overlapX, overlapY)
- self:revertCollision(overlapX, overlapY)
- end
- end
- function player:revertCollision(overlapX, overlapY)
- if overlapX > 0 then
- self.x = self.prevX
- self.velocityX = 0
- end
- if overlapY > 0 then
- self.y = self.prevY
- self.velocityY = 0
- end
- self.boundingRectangle.x = self.x
- self.boundingRectangle.y = self.y
- end
- function player:update(dt)
- self:checkForInput()
- self:applyVelocity(dt)
- self:checkForCollision()
- self:setPosition()
- self:applyDeceleration(dt)
- end
- function player:draw()
- love.graphics.draw(self.image, self.x, self.y)
- love.graphics.print(self.velocityX, 0, 0)
- love.graphics.print(self.velocityY, 0, 10)
- end
- function player:sayHi()
- end
- return player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement