player.lua 980 B

12345678910111213141516171819202122232425262728293031323334353637
  1. -- DONE: implementar função keyDown e keyReleased no bloco main
  2. local player = {}
  3. p = love.physics
  4. g = love.graphics
  5. function player.new(world, x, y, width, height)
  6. local player1 = {}
  7. player1.x = x or 50
  8. player1.y = y or 400
  9. player1.width = width or 20
  10. player1.height = height or 20
  11. local newPlayer = {}
  12. newPlayer.body = p.newBody(world, x, y, "dynamic")
  13. newPlayer.shape = p.newRectangleShape(width, height)
  14. newPlayer.fixture = p.newFixture(newPlayer.body, newPlayer.shape)
  15. newPlayer.body:setLinearDamping(5)
  16. newPlayer.update = function(self, keys)
  17. if keys.up then self.body:applyForce(0, -600) end
  18. if keys.down then self.body:applyForce(0, 600) end
  19. if keys.left then self.body:applyForce(-600, 0) end
  20. if keys.right then self.body:applyForce(600, 0) end
  21. end
  22. newPlayer.draw = function(self)
  23. love.graphics.setColor(255, 0, 0)
  24. love.graphics.polygon("fill", self.body:getWorldPoints(self.shape:getPoints()))
  25. end
  26. return newPlayer
  27. end
  28. return player