瀏覽代碼

start of entities system and some minor changes to world generation

master
Isabelle L. 5 年之前
父節點
當前提交
71a8bd9d3e
共有 4 個檔案被更改,包括 83 行新增6 行删除
  1. +47
    -0
      entities.lua
  2. 二進制
      img/building.png
  3. +12
    -1
      main.lua
  4. +24
    -5
      world.lua

+ 47
- 0
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

二進制
img/building.png 查看文件

Before After
Width: 32  |  Height: 32  |  Size: 252 B

+ 12
- 1
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

+ 24
- 5
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


Loading…
取消
儲存