main.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. local sti = require"./Simple-Tiled-Implementation/sti"
  2. function love.load()
  3. --love.window.updateMode( 320, 320)
  4. love.window.setTitle("red man goes dancing")
  5. source = love.audio.newSource( "audio/ramesesb-deeper.mp3", "static")
  6. source:setLooping(true)
  7. source:play()
  8. -- load map
  9. map = sti("map/map.lua")
  10. -- create sprites layer
  11. local layer = map:addCustomLayer("sprites", 3)
  12. -- get player spawn object
  13. local player
  14. for k, object in pairs(map.objects) do
  15. if object.name == "player" then
  16. player = object
  17. break
  18. end
  19. end
  20. -- create player object
  21. local sprite = love.graphics.newImage("img/sprite.png")
  22. layer.player = {
  23. sprite = sprite,
  24. x = player.x,
  25. y = player.y,
  26. ox = sprite:getWidth() / 2,
  27. oy = sprite:getHeight() / 1
  28. }
  29. -- add controls
  30. layer.update = function(self, dt)
  31. local speed = 72
  32. if love.keyboard.isDown("w") or love.keyboard.isDown("up") then
  33. self.player.y = self.player.y - speed * dt
  34. end
  35. if love.keyboard.isDown("s") or love.keyboard.isDown("down") then
  36. self.player.y = self.player.y + speed * dt
  37. end
  38. if love.keyboard.isDown("a") or love.keyboard.isDown("left") then
  39. self.player.x = self.player.x - speed * dt
  40. end
  41. if love.keyboard.isDown("d") or love.keyboard.isDown("right") then
  42. self.player.x = self.player.x + speed * dt
  43. end
  44. end
  45. -- draw player
  46. layer.draw = function(self)
  47. love.graphics.draw(
  48. self.player.sprite,
  49. math.floor(self.player.x),
  50. math.floor(self.player.y),
  51. 0,
  52. 1,
  53. 1,
  54. self.player.ox,
  55. self.player.oy
  56. )
  57. -- temp
  58. love.graphics.setPointSize(5)
  59. love.graphics.points(math.floor(self.player.x), math.floor(self.player.y))
  60. end
  61. map:removeLayer("spawn point")
  62. end
  63. function love.update(dt)
  64. map:update(dt)
  65. end
  66. function love.draw()
  67. coloredtext = {
  68. {255,0,0,255}, "OBJECTIVE: keep dancing and dont float off into space"
  69. }
  70. -- scale world
  71. local scale = 2
  72. local screen_width = love.graphics.getWidth() / scale
  73. local screen_height = love.graphics.getHeight() / scale
  74. -- translate world for centering player
  75. local player = map.layers["sprites"].player
  76. local tx = math.floor(player.x - screen_width / 2)
  77. local ty = math.floor(player.y - screen_height / 2)
  78. map:draw(-tx, -ty, scale)
  79. love.graphics.polygon('fill', 20, 20, 720, 20, 720, 85, 20, 85)
  80. love.graphics.print(coloredtext, 36, 36, 0, 2, 2)
  81. end