Nasamos

Random generated tile map LÖVE

Jul 14th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.69 KB | None | 0 0
  1. --main.lua
  2.  
  3. require 'Files/camera'
  4. require 'Files/player'
  5. require 'Files/map'
  6. require 'Files/Tiles'
  7. local inspect = require('Files/inspect')
  8. io.stdout:setvbuf("no")
  9.  
  10. function love.load()
  11.   playerload()
  12.   mapload()
  13.   mapcollision()
  14.   image = love.graphics.newImage('Textures/bg.jpg')
  15.   song = love.audio.newSource("Sounds/BackgroundSong.mp3")
  16.   song:setLooping(true)
  17.   song:play()
  18. end
  19.  
  20. function love.update()
  21.   playercollision(collision)
  22.   playerupdate()
  23. end
  24.  
  25. function love.draw()
  26.   camera:set()
  27.   love.graphics.draw(image)
  28.   mapdraw()
  29.   playerdraw()
  30.   camera:unset()
  31. end
  32.  
  33. function love.keypressed(k)
  34.   if k == 'escape' then
  35.     love.event.quit()
  36.   end
  37.   if k == 'p' then
  38.     for row = 1,9 do
  39.       print(inspect(collision[row]))
  40.     end
  41.   end
  42.   playermovement(k)
  43.   if k == 'space' then
  44.     mapload()
  45.     mapcollision()
  46.     mapdraw()
  47.   end
  48. end
  49.  
  50. --end of main.lua
  51.  
  52. -- Files/Tiles.lua
  53.  
  54. Tiles = {}
  55.  
  56. function tilesload()
  57.   Tiles[1] = love.graphics.newImage("Textures/Tile1.png")
  58.   Tiles[2] = love.graphics.newImage("Textures/Tile2.png")
  59. end
  60.  
  61. --end of Tiles.lua
  62.  
  63.  
  64. --Files/camera.lua
  65.  
  66. camera = {}
  67. camera.x = -(64 * 3)
  68. camera.y = -(64 * 3)
  69. camera.scaleX = 1
  70. camera.scaleY = 1
  71. camera.rotation = 0
  72.  
  73. function camera:set()
  74. love.graphics.push()
  75. love.graphics.rotate(-self.rotation)
  76. love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
  77. love.graphics.translate(-self.x, -self.y)
  78. end
  79.  
  80. function camera:unset()
  81. love.graphics.pop()
  82. end
  83.  
  84. function camera:move(dx, dy)
  85. self.x = self.x + (dx or 0)
  86. self.y = self.y + (dy or 0)
  87. end
  88.  
  89. function camera:rotate(dr)
  90. self.rotation = self.rotation + dr
  91. end
  92.  
  93. function camera:scale(sx, sy)
  94. sx = sx or 1
  95. self.scaleX = self.scaleX * sx
  96. self.scaleY = self.scaleY * (sy or sx)
  97. end
  98.  
  99. function camera:setPosition(x, y)
  100. self.x = x or self.x
  101. self.y = y or self.y
  102. end
  103.  
  104. function camera:setScale(sx, sy)
  105. self.scaleX = sx or self.scaleX
  106. self.scaleY = sy or self.scaleY
  107. end
  108.  
  109. --end of camera.lua
  110.  
  111. --Files/conf.lua
  112.  
  113. function love.conf(t)
  114.   t.window.heigth = 640
  115.   t.window.width = 640
  116. end
  117.  
  118. --end of conf.lua
  119.  
  120. --Files/map.lua
  121.  
  122. require 'Files/Tiles'
  123.  
  124. map = {}
  125. collision = {}
  126.  
  127. function mapload()
  128.   tilesload()
  129. end
  130.  
  131. function mapcollision()
  132.   collision[1] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  133.   for row = 2, 8 do
  134.     collision[row] = {}
  135.     for column = 2, 10 do
  136.       collision[row][column] = love.math.random(2)
  137.     end
  138.     collision[row][1] = 1
  139.     collision[row][11] = 1
  140.   end
  141.   collision[9] = collision[1]
  142. end
  143.  
  144. function mapdraw()
  145.   for row = 1, 9 do
  146.     for column = 1, 11 do
  147.       if collision[row][column] == 1 then
  148.         love.graphics.draw(Tiles[1], (column-1)*64, (row-1)*64)
  149.       elseif collision[row][column] == 2 then
  150.         love.graphics.draw(Tiles[2], (column-1)*64, (row-1)*64)
  151.       end
  152.     end
  153.   end
  154. end
  155.  
  156. --end of map.lua
  157.  
  158. --Files/player.lua
  159.  
  160. require 'Files/map'
  161.  
  162. player = {}
  163. move = {}
  164. text = {}
  165.  
  166. text.x = 32
  167. text.y = - 64
  168.  
  169. function moveload()
  170.   move.up = true
  171.   move.down = true
  172.   move.left = true
  173.   move.right = true
  174. end
  175.  
  176. function playerload()
  177.   tilesload()
  178.   moveload()
  179.   player.x = 64
  180.   player.y = 64
  181.   player.color = 0, 255, 255
  182.   Male = love.graphics.newImage("Textures/test.png")
  183.   Female = love.graphics.newImage("Textures/Playertest.png")
  184. end
  185.  
  186. function playerupdate()
  187.   if currenttile == 2 then
  188.     playerimage = Female
  189.   elseif currenttile == 1 then
  190.     playerimage = Male
  191.   end
  192. end
  193.  
  194. function playermovement(k)
  195.   if k == 'w' and move.up then
  196.     player.y = player.y - 64
  197.     camera.y = camera.y - 64
  198.     text.y = text.y - 64
  199.   elseif k == 's' and move.down then
  200.     player.y = player.y + 64
  201.     camera.y = camera.y + 64
  202.     text.y = text.y + 64
  203.   end  
  204.   if k == 'a' and move.left then
  205.     player.x = player.x - 64
  206.     camera.x = camera.x - 64
  207.     text.x = text.x - 64
  208.   elseif k == 'd' and move.right then
  209.     player.x = player.x + 64
  210.     camera.x = camera.x + 64
  211.     text.x = text.x + 64
  212.   end
  213. end
  214.  
  215.  
  216. function playercollision(collision)
  217.   currenttile = collision[player.y / 64 + 1][player.x / 64 + 1]
  218.   if collision[player.y / 64][player.x / 64 + 1] == 1 then
  219.     move.up = false
  220.   else
  221.     move.up = true
  222.   end
  223.   if collision[player.y / 64 + 2][player.x / 64 + 1] == 1 then
  224.     move.down = false
  225.   else
  226.     move.down = true
  227.   end
  228.   if collision[player.y / 64 + 1][player.x / 64 + 2] == 1 then
  229.     move.right = false
  230.   else
  231.     move.right = true
  232.   end
  233.   if collision[player.y / 64 + 1][player.x / 64] == 1 then
  234.     move.left = false
  235.   else
  236.     move.left = true
  237.   end
  238. end
  239.  
  240. function playerdraw()
  241.   love.graphics.draw(playerimage, player.x, player.y)
  242. end
  243.  
  244. --end of player.lua
  245.  
  246. --https://github.com/Ohwrake/Random-Generated-tiles-love-lua
Advertisement
Add Comment
Please, Sign In to add comment