|
- -- bring in the util module
- local util = require("util")
-
- -- world stuffs
- local world = {}
- -- create a new world with given depth and width
- function world.new(width, depth, height)
- world.width = width
- world.depth = depth
- world.height = height
- world.hover = nil
- world.select = love.graphics.newImage("img/tiles/select.png")
-
- for x = 1, world.width do
- world[x] = {}
- for y = 1, world.depth do
- world[x][y] = {}
- for z = 1, world.height do
- world[x][y][z] = "air"
- end
- end
- end
- end
-
- -- generates a world, should be called after world.new()
- function world.generate()
- -- draw the bottom layer of dirt
- for x = 1, world.width do
- for y = 1, world.depth do
- world[x][y][1] = "dirt"
- end
- end
- -- draw the grass
- for x = 1, world.width do
- for y = 1, world.depth do
- world[x][y][2] = "grass"
- end
- end
- -- draw a stream
- for x = 1, world.width do
- world[x][5][2] = "water"
- end
- end
-
- -- load the tile files
- function world.loadTiles()
- world.tiles = {}
- world.tiles.grass = love.graphics.newImage("img/tiles/grass.png")
- world.tiles.dirt = love.graphics.newImage("img/tiles/dirt.png")
- world.tiles.water = love.graphics.newImage("img/tiles/water.png")
- world.tiles.nothing = love.graphics.newImage("img/tiles/nothing.png")
- end
-
- -- render the world
- function world.render(offset)
- for x = 1, world.width do
- for y = 1, world.depth do
- for z = 1, world.height do
- local tile = world[x][y][z]
- local loc = util.cartToIso({x = x, y = y})
-
- if tile == "grass" then
- image = world.tiles.grass
- elseif tile == "dirt" then
- image = world.tiles.dirt
- elseif tile == "water" then
- image = world.tiles.water
- elseif tile == "air" then
- image = nil
- else
- image = world.tiles.nothing
- end
-
- if image ~= nil then
- love.graphics.draw(image, loc.x * 16 + offset.x, loc.y * 16 + offset.y - z * 16)
- end
- end
- end
- end
-
- if world.hover ~= nil then
- love.graphics.draw(world.select, world.hover.x, world.hover.y)
- end
- end
-
- return world
|