From 71a8bd9d3e5ddc27f1a6c8470c2f328b30712a61 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Fri, 3 Jul 2020 15:04:38 -0500 Subject: [PATCH] start of entities system and some minor changes to world generation --- entities.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ img/building.png | Bin 0 -> 252 bytes main.lua | 13 ++++++++++++- world.lua | 29 ++++++++++++++++++++++++----- 4 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 entities.lua create mode 100644 img/building.png diff --git a/entities.lua b/entities.lua new file mode 100644 index 0000000..53c40d4 --- /dev/null +++ b/entities.lua @@ -0,0 +1,47 @@ +-- bring in the util module +local util = require("util") + +local entities = {} + +-- initialize the entities table +function entities.init() + entities.staticEntities = {} + entities.dynamicEntities = {} +end + +-- load all sprites to memory +function entities.loadSprites() + entities.sprites = {} + entities.sprites.town = love.graphics.newImage("img/building.png") +end + +-- create a new static entity that will always align with the map grid +function entities.newStatic(x, y, z, type) + local static = {} + static.x = x + static.y = y + static.z = z + static.type = type + + table.insert(entities.staticEntities, static) +end + +-- render all entities +function entities.render(offset) + -- render static entities + for _, entity in ipairs(entities.staticEntities) do + local type = entity.type + local loc = util.cartToIso({x = entity.x, y = entity.y}) + + if type == "town" then + image = entities.sprites.town + end + + if image ~= nil then + love.graphics.draw(image, loc.x * 16 + offset.x, loc.y * 16 + offset.y - entity.z * 16) + end + end + +end + +return entities \ No newline at end of file diff --git a/img/building.png b/img/building.png new file mode 100644 index 0000000000000000000000000000000000000000..286a1b74efd201e311ca023745c42e766a59b3ef GIT binary patch literal 252 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}+dN$yLn2z= zPT9!YY#`w3o5i-kXF6lXgi9O6L%$WZyG&+$p<&xly<&pW(Qkk2Exd}?v%J2yIj7*9 z0ncHBe?EtG>h8%`zx=yr?G+m>he_Ga3tD^T@Cu4&Jk9VhT+kNNb%HJ9Y4Y_t+iKnS zYo340-sxhLaPL~%-z6rWat=HXNZAmk$EKhd{-nS`Pet{+qLsXojj+}8%oh*6D^DF? zr+cqXtAX>Q;FVdQ&MBb@06`;W A_y7O^ literal 0 HcmV?d00001 diff --git a/main.lua b/main.lua index 5804149..61d51f5 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,7 @@ -- world module local world = require("world") +-- entities module +local entities = require("entities") -- love's load function function love.load() @@ -8,10 +10,16 @@ function love.load() x = 304, y = 48 } - + -- generate the world and load the files world.new(20, 20, 3) + world.generate() world.loadTiles() + + -- entities initialization + entities.init() + entities.loadSprites() + entities.newStatic(7, 7, 3, "town") end -- love's update function @@ -50,4 +58,7 @@ function love.draw() -- draw the map world.render(offset) + + -- draw the entities + entities.render(offset) end \ No newline at end of file diff --git a/world.lua b/world.lua index f40ed77..4563728 100644 --- a/world.lua +++ b/world.lua @@ -16,7 +16,7 @@ local world = {} for y = 1, world.depth do world[x][y] = {} for z = 1, world.height do - world[x][y][z] = "nothing" + world[x][y][z] = "air" end end end @@ -24,7 +24,22 @@ local world = {} -- 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 @@ -50,16 +65,20 @@ local world = {} image = world.tiles.dirt elseif tile == "water" then image = world.tiles.water + elseif tile == "air" then + image = nil else image = world.tiles.nothing end - - love.graphics.draw(image, loc.x * 16 + offset.x, loc.y * 16 + offset.y - z * 16) + + 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 not world.hover == nil then + if world.hover ~= nil then love.graphics.draw(world.select, world.hover.x, world.hover.y) end end