an unnamed video game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.7 KiB

  1. -- bring in the util module
  2. local util = require("util")
  3. -- world stuffs
  4. local world = {}
  5. -- create a new world with given depth and width
  6. function world.new(width, depth, height)
  7. world.width = width
  8. world.depth = depth
  9. world.height = height
  10. world.hover = nil
  11. world.select = love.graphics.newImage("img/tiles/select.png")
  12. for x = 1, world.width do
  13. world[x] = {}
  14. for y = 1, world.depth do
  15. world[x][y] = {}
  16. for z = 1, world.height do
  17. world[x][y][z] = "air"
  18. end
  19. end
  20. end
  21. end
  22. -- generates a world, should be called after world.new()
  23. function world.generate()
  24. -- draw the bottom layer of dirt
  25. for x = 1, world.width do
  26. for y = 1, world.depth do
  27. world[x][y][1] = "dirt"
  28. end
  29. end
  30. -- draw the grass
  31. for x = 1, world.width do
  32. for y = 1, world.depth do
  33. world[x][y][2] = "grass"
  34. end
  35. end
  36. -- draw a stream
  37. for x = 1, world.width do
  38. world[x][5][2] = "water"
  39. end
  40. end
  41. -- load the tile files
  42. function world.loadTiles()
  43. world.tiles = {}
  44. world.tiles.grass = love.graphics.newImage("img/tiles/grass.png")
  45. world.tiles.dirt = love.graphics.newImage("img/tiles/dirt.png")
  46. world.tiles.water = love.graphics.newImage("img/tiles/water.png")
  47. world.tiles.nothing = love.graphics.newImage("img/tiles/nothing.png")
  48. end
  49. -- render the world
  50. function world.render(offset)
  51. for x = 1, world.width do
  52. for y = 1, world.depth do
  53. for z = 1, world.height do
  54. local tile = world[x][y][z]
  55. local loc = util.cartToIso({x = x, y = y})
  56. if tile == "grass" then
  57. image = world.tiles.grass
  58. elseif tile == "dirt" then
  59. image = world.tiles.dirt
  60. elseif tile == "water" then
  61. image = world.tiles.water
  62. elseif tile == "air" then
  63. image = nil
  64. else
  65. image = world.tiles.nothing
  66. end
  67. if image ~= nil then
  68. love.graphics.draw(image, loc.x * 16 + offset.x, loc.y * 16 + offset.y - z * 16)
  69. end
  70. end
  71. end
  72. end
  73. if world.hover ~= nil then
  74. love.graphics.draw(world.select, world.hover.x, world.hover.y)
  75. end
  76. end
  77. return world