Advertisement
Guest User

Untitled

a guest
Jan 13th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. --player.lua
  2.  
  3. --require
  4.  
  5. local gameObject = require('gameObject')
  6.  
  7. -- class
  8.  
  9. local player = gameObject:new()
  10.  
  11. --enums
  12.  
  13. player.playerStateMovementState = {
  14.     onGround = 'onGround',
  15.     inAir = 'inAir',
  16.     onLadder = 'onLadder',
  17.     onPlatform = 'onPlatform'  
  18. }
  19.  
  20. function player:new(x, y, path)
  21.     local obj = {
  22.         xPosition = x or 0,
  23.         yPosition = y or 0,
  24.         xVelocity = 0,
  25.         yVelocity = 0,
  26.         gravity = 9.81 * 7,
  27.         speed = 30,
  28.         jumpStrength = -7,
  29.         state = self.playerStateMovementState.inAir,
  30.         playerSprite = love.graphics.newImage(path)
  31.     }
  32.  
  33.     setmetatable(obj, self)
  34.     self.__index = self
  35.  
  36.     return obj
  37. end
  38.  
  39. function player:move(direction)
  40.     if direction == 'left' then
  41.         self.xVelocity = self.xVelocity - self.speed
  42.     end
  43.  
  44.     if direction == 'right' then
  45.         self.xVelocity = self.xVelocity + self.speed
  46.     end
  47. end
  48.  
  49. function player:jump()
  50.     self.yVelocity = self.yVelocity + self.jumpStrength
  51. end
  52.  
  53. function player:checkForInput()
  54.  
  55.     if love.keyboard.isDown('d') then
  56.         self:move('right')
  57.     end
  58.  
  59.     if love.keyboard.isDown('a') then
  60.         self:move('left')
  61.     end
  62.  
  63.     if love.keyboard.isDown('space') then
  64.         self:jump()
  65.     end
  66. end
  67.  
  68. function player:applyVelocity(dt)
  69.     self.xPosition = self.xPosition + self.xVelocity * dt
  70.     self.yPosition = self.yPosition + self.yVelocity * dt
  71.     self.yVelocity = self.yVelocity + self.gravity * dt
  72. end
  73.  
  74. function player:load()
  75. end
  76.  
  77. function player:update(dt)
  78.     self:checkForInput()
  79.     self:applyVelocity(dt)
  80. end
  81.  
  82. function player:draw()
  83.     love.graphics.draw(self.playerSprite, self.xPosition, self.yPosition)
  84. end
  85.  
  86. return player
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement