Advertisement
Guest User

Untitled

a guest
Jul 25th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.65 KB | None | 0 0
  1. local gameObject = require('gameObjects/gameObject')
  2. local player = gameObject:new()
  3.  
  4. player.image = love.graphics.newImage('assets/player/testRectangle.png')
  5. player.x = 32
  6. player.y = 32
  7. player.prevX = 32
  8. player.prevY = 32
  9. player.speed = 0
  10. player.acceleration = 15
  11. player.deceleration = 7
  12. player.maxSpeed = 25
  13. player.velocityX = 0
  14. player.velocityY = 0
  15. player.height = 8
  16. player.width = 8
  17.  
  18. player.boundingRectangle = {
  19.     x = player.x,                
  20.     y = player.y,              
  21.     width = player.width,        
  22.     height = player.height
  23. }
  24.  
  25. player.directions = {
  26.     UP = 'UP',
  27.     DOWN = 'DOWN',
  28.     LEFT = 'LEFT',
  29.     RIGHT = 'RIGHT',
  30.     RIGHTUP = 'RIGHTUP',
  31.     RIGHTDOWN = 'RIGHTDOWN',
  32.     LEFTUP = 'LEFTUP',
  33.     LEFTDOWN = 'LEFTDOWN',
  34.     NONE = 'NONE'
  35. }
  36.  
  37. player.direction = player.directions.NONE
  38.  
  39. player.states = {
  40.     IDLE = 'IDLE'
  41. }
  42.  
  43. player.state = player.states.IDLE
  44.  
  45. function player:new()
  46.     local obj = {}
  47.     setmetatable(obj, self)
  48.     self.__index = self
  49.     return obj
  50. end
  51.  
  52. function player:applyVelocity(dt)
  53.  
  54.     if self.direction == self.directions.UP then
  55.         self.velocityY = self.velocityY - self.acceleration * dt
  56.     end
  57.  
  58.     if self.direction == self.directions.LEFT then
  59.         self.velocityX = self.velocityX - self.acceleration * dt
  60.     end
  61.  
  62.     if self.direction == self.directions.DOWN then
  63.         self.velocityY = self.velocityY + self.acceleration * dt
  64.     end
  65.  
  66.     if self.direction == self.directions.RIGHT then
  67.         self.velocityX = self.velocityX + self.acceleration * dt
  68.     end
  69.  
  70.     if self.direction == self.directions.RIGHTUP then
  71.         self.velocityX = self.velocityX + self.acceleration * dt
  72.         self.velocityY = self.velocityY - self.acceleration * dt
  73.     end
  74.  
  75.     if self.direction == self.directions.LEFTUP then
  76.         self.velocityX = self.velocityX - self.acceleration * dt
  77.         self.velocityY = self.velocityY - self.acceleration * dt
  78.     end
  79.  
  80.     if self.direction == self.directions.RIGHTDOWN then
  81.         self.velocityX = self.velocityX + self.acceleration * dt
  82.         self.velocityY = self.velocityY + self.acceleration * dt
  83.     end
  84.  
  85.     if self.direction == self.directions.LEFTDOWN then
  86.         self.velocityX = self.velocityX - self.acceleration * dt
  87.         self.velocityY = self.velocityY + self.acceleration * dt
  88.     end
  89.  
  90.     self:normalizeVector()
  91.  
  92. end
  93.  
  94. function player:applyDeceleration(dt)
  95.     self.velocityX = self.velocityX * (1 - self.deceleration * dt)
  96.     self.velocityY = self.velocityY * (1 - self.deceleration * dt)
  97.    
  98.     local threshold = 0.01
  99.  
  100.     if math.abs(self.velocityX) < threshold then
  101.         self.velocityX = 0
  102.     end
  103.  
  104.     if math.abs(self.velocityY) < threshold then
  105.         self.velocityY = 0
  106.     end
  107. end
  108.  
  109. function player:setPosition()
  110.     self.prevX = self.x
  111.     self.prevY = self.y
  112.  
  113.     self.x = self.x + self.velocityX
  114.     self.y = self.y + self.velocityY
  115.  
  116.     self.boundingRectangle.x = self.x
  117.     self.boundingRectangle.y = self.y
  118. end
  119.  
  120. function player:checkForInput(dt)
  121.     self.direction = self.directions.NONE
  122.  
  123.     if love.keyboard.isDown('w') then
  124.         self.direction = self.directions.UP
  125.     end
  126.  
  127.     if love.keyboard.isDown('a') then
  128.         self.direction = self.directions.LEFT
  129.     end
  130.  
  131.     if love.keyboard.isDown('s') then
  132.         self.direction = self.directions.DOWN
  133.     end
  134.  
  135.     if love.keyboard.isDown('d') then
  136.         self.direction = self.directions.RIGHT
  137.     end
  138.  
  139.     if love.keyboard.isDown('w') and love.keyboard.isDown('d') then
  140.         self.direction = self.directions.RIGHTUP
  141.     end
  142.  
  143.     if love.keyboard.isDown('w') and love.keyboard.isDown('a') then
  144.         self.direction = self.directions.LEFTUP
  145.     end
  146.  
  147.     if love.keyboard.isDown('s') and love.keyboard.isDown('d') then
  148.         self.direction = self.directions.RIGHTDOWN
  149.     end
  150.  
  151.     if love.keyboard.isDown('s') and love.keyboard.isDown('a') then
  152.         self.direction = self.directions.LEFTDOWN
  153.     end
  154.  
  155.     if love.keyboard.isDown('return') then
  156.         print(self.direction)
  157.     end
  158.  
  159. end
  160.  
  161. function player:getFuturePosition()
  162.     local collidableObject = {}
  163.    
  164.     collidableObject.boundingRectangle = {
  165.         x = self.x + self.velocityX,
  166.         y = self.y + self.velocityY,
  167.         width = self.width,
  168.         height = self.height
  169.     }
  170.     return collidableObject
  171. end
  172.  
  173. function player:normalizeVector()
  174.     local length = math.sqrt(self.velocityX * self.velocityX + self.velocityY * self.velocityY)
  175.     if length > self.maxSpeed then
  176.         self.velocityX = (self.velocityX / length) * self.maxSpeed
  177.         self.velocityY = (self.velocityY / length) * self.maxSpeed
  178.     end
  179. end
  180.  
  181. function player:checkForCollision()
  182.     local didCollide, overlapX, overlapY = checkForCollisionWithMap(self:getFuturePosition())
  183.     if didCollide then
  184.         print('overlapX and Y', overlapX, overlapY)
  185.         self:revertCollision(overlapX, overlapY)
  186.     end
  187. end
  188.  
  189. function player:revertCollision(overlapX, overlapY)
  190.    
  191.     if overlapX > 0 then
  192.         self.x = self.prevX
  193.         self.velocityX = 0
  194.     end
  195.  
  196.     if overlapY > 0 then
  197.         self.y = self.prevY
  198.         self.velocityY = 0
  199.     end
  200.  
  201.     self.boundingRectangle.x = self.x
  202.     self.boundingRectangle.y = self.y
  203. end
  204.  
  205. function player:update(dt)
  206.     self:checkForInput()
  207.     self:applyVelocity(dt)
  208.     self:checkForCollision()  
  209.     self:setPosition()
  210.     self:applyDeceleration(dt)
  211. end
  212.  
  213. function player:draw()
  214.     love.graphics.draw(self.image, self.x, self.y)
  215.     love.graphics.print(self.velocityX, 0, 0)
  216.     love.graphics.print(self.velocityY, 0, 10)
  217. end
  218.  
  219. function player:sayHi()
  220. end
  221.  
  222. return player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement