tile & level refactor
This commit is contained in:
parent
cf30985756
commit
7d082f80b8
@ -1,40 +0,0 @@
|
||||
local utils = require "lib.utils.utils"
|
||||
|
||||
--- @class Level
|
||||
--- @field size Vec3
|
||||
--- @field characters Character[]
|
||||
--- @field characterGrid CharacterGrid
|
||||
--- @field selector Selector
|
||||
--- @field camera Camera
|
||||
local level = {}
|
||||
level.__index = level
|
||||
|
||||
local function new()
|
||||
local size = Vec3 { 30, 30 } -- magic numbers for testing purposes only
|
||||
return setmetatable({
|
||||
size = size,
|
||||
characters = {},
|
||||
characterGrid = (require "lib/grid").new(),
|
||||
selector = (require "lib/selector").new(),
|
||||
camera = (require "lib/camera").new()
|
||||
}, level)
|
||||
end
|
||||
|
||||
function level:update(dt)
|
||||
self.characterGrid:reset()
|
||||
utils.each(self.characters, function(el)
|
||||
el:update(dt)
|
||||
end)
|
||||
self.camera:update(dt)
|
||||
self.selector:update(dt)
|
||||
end
|
||||
|
||||
function level:draw()
|
||||
utils.each(self.characters, function(el)
|
||||
el:draw()
|
||||
end)
|
||||
end
|
||||
|
||||
return {
|
||||
new = new
|
||||
}
|
||||
1
lib/level/handmaded.lua
Normal file
1
lib/level/handmaded.lua
Normal file
@ -0,0 +1 @@
|
||||
--- @alias Handmaded "hub"|"something_else"
|
||||
61
lib/level/level.lua
Normal file
61
lib/level/level.lua
Normal file
@ -0,0 +1,61 @@
|
||||
local utils = require "lib.utils.utils"
|
||||
|
||||
--- @class Level
|
||||
--- @field size Vec3
|
||||
--- @field characters Character[]
|
||||
--- @field characterGrid CharacterGrid
|
||||
--- @field selector Selector
|
||||
--- @field camera Camera
|
||||
--- @field map Map
|
||||
local level = {}
|
||||
level.__index = level
|
||||
|
||||
--- @param type "procedural"|"handmaded"
|
||||
--- @param template Procedural|Handmaded
|
||||
local function new(type, template)
|
||||
local size = Vec3 { 30, 30 } -- magic numbers for testing purposes only
|
||||
print(type, template, size)
|
||||
return setmetatable({
|
||||
size = size,
|
||||
characters = {},
|
||||
characterGrid = (require "lib.level.grid").new(),
|
||||
selector = (require "lib.level.selector").new(),
|
||||
camera = (require "lib.level.camera").new(),
|
||||
map = (require "lib.level.map").new(type, template, size)
|
||||
}, level)
|
||||
end
|
||||
|
||||
function level:update(dt)
|
||||
self.characterGrid:reset()
|
||||
utils.each(self.characters, function(el)
|
||||
el:update(dt)
|
||||
end)
|
||||
self.camera:update(dt)
|
||||
self.selector:update(dt)
|
||||
end
|
||||
|
||||
function level:draw()
|
||||
self.map:draw()
|
||||
|
||||
--- Это отрисовка пути персонажа к мышке
|
||||
if self.selector.id then
|
||||
local charPos = self.characters[self.selector.id].logic.mapLogic.position:floor()
|
||||
--- @type Vec3
|
||||
local mpos = self.camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor()
|
||||
local path = (require "lib.pathfinder")(charPos, mpos)
|
||||
|
||||
love.graphics.setColor(0.6, 0.75, 0.5)
|
||||
for p in path:values() do
|
||||
love.graphics.circle("fill", p.x + 0.45, p.y + 0.45, 0.1)
|
||||
end
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
end
|
||||
|
||||
utils.each(self.characters, function(el)
|
||||
el:draw()
|
||||
end)
|
||||
end
|
||||
|
||||
return {
|
||||
new = new
|
||||
}
|
||||
26
lib/level/map.lua
Normal file
26
lib/level/map.lua
Normal file
@ -0,0 +1,26 @@
|
||||
--- @class Map
|
||||
--- @field atlas love.Image
|
||||
--- @field size Vec3|nil
|
||||
local map = {}
|
||||
map.__index = map
|
||||
|
||||
--- create new map
|
||||
--- @param type "procedural"|"handmaded"
|
||||
--- @param template Procedural|Handmaded
|
||||
--- @param size? Vec3
|
||||
local function new(type, template, size)
|
||||
map.size = size
|
||||
local tMap = require('lib.level.' .. type).new(template, size)
|
||||
return setmetatable(tMap, map)
|
||||
end
|
||||
|
||||
function map:draw()
|
||||
for y = 0, self.size.y - 1 do
|
||||
for x = 0, self.size.x - 1 do
|
||||
love.graphics.draw(self.atlas, self[x][y], x, y,
|
||||
nil, 1 / 32, 1 / 32)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return { new = new }
|
||||
21
lib/level/procedural.lua
Normal file
21
lib/level/procedural.lua
Normal file
@ -0,0 +1,21 @@
|
||||
--- @alias Procedural "flower_plains"
|
||||
|
||||
--- @param template Procedural
|
||||
local function new(template, size)
|
||||
local map = {}
|
||||
-- паттерн-матчинг для самых маленьких
|
||||
if template == "flower_plains" then
|
||||
local tileMap = require("lib.level.tile").load(Tree.assets.files.tiles.grass)
|
||||
map.atlas = tileMap.atlas
|
||||
for y = 0, size.y - 1 do
|
||||
map[y] = {}
|
||||
for x = 0, size.x - 1 do
|
||||
local type = tileMap["flower_grass"]
|
||||
map[y][x] = type[math.random(1, #type)]
|
||||
end
|
||||
end
|
||||
end
|
||||
return map
|
||||
end
|
||||
|
||||
return { new = new }
|
||||
@ -4,8 +4,8 @@
|
||||
|
||||
|
||||
Tree = {
|
||||
panning = require "lib/panning",
|
||||
assets = (require "lib.utils.asset_bundle"):load(),
|
||||
controls = require "lib/controls",
|
||||
level = (require "lib/level"):new() -- для теста у нас только один уровень, который сразу же загружен
|
||||
assets = (require "lib.utils.asset_bundle"):load()
|
||||
}
|
||||
Tree.panning = require "lib/panning"
|
||||
Tree.controls = require "lib.controls"
|
||||
Tree.level = (require "lib.level.level").new("procedural", "flower_plains") -- для теста у нас только один уровень, который сразу же загружен
|
||||
|
||||
33
main.lua
33
main.lua
@ -14,16 +14,6 @@ local height = 30
|
||||
function love.load()
|
||||
character.spawn("Hero", "warrior", Tree.assets.files.sprites.character)
|
||||
|
||||
Map = {}
|
||||
TileMap2 = require("lib.tile").load(Tree.assets.files.tiles.grass)
|
||||
for y = 0, height - 1 do
|
||||
Map[y] = {}
|
||||
for x = 0, width - 1 do
|
||||
local type = TileMap2["flower_grass"]
|
||||
Map[y][x] = type[math.random(1, #type)]
|
||||
end
|
||||
end
|
||||
|
||||
-- PlayerFaction.characters = { Hero1, Hero2 }
|
||||
love.window.setMode(1080, 720, { resizable = true, msaa = 4, vsync = true })
|
||||
end
|
||||
@ -60,30 +50,9 @@ function love.draw()
|
||||
love.graphics.pop()
|
||||
|
||||
Tree.level.camera:attach()
|
||||
for y = 0, height - 1 do
|
||||
for x = 0, width - 1 do
|
||||
love.graphics.draw(TileMap2.atlas, Map[x][y], x, y,
|
||||
nil, 1 / 32, 1 / 32)
|
||||
end
|
||||
end
|
||||
|
||||
--- Это отрисовка пути персонажа к мышке
|
||||
if Tree.level.selector.id then
|
||||
local charPos = Tree.level.characters[Tree.level.selector.id].logic.mapLogic.position:floor()
|
||||
--- @type Vec3
|
||||
local mpos = Tree.level.camera:toWorldPosition(Vec3 { love.mouse.getX(), love.mouse.getY() }):floor()
|
||||
local path = (require "lib.pathfinder")(charPos, mpos)
|
||||
|
||||
love.graphics.setColor(0.6, 0.75, 0.5)
|
||||
for p in path:values() do
|
||||
love.graphics.circle("fill", p.x + 0.45, p.y + 0.45, 0.1)
|
||||
end
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
end
|
||||
|
||||
|
||||
|
||||
Tree.level:draw()
|
||||
|
||||
Tree.level.camera:detach()
|
||||
|
||||
local stats = "fps: " .. love.timer.getFPS() .. " lt: " .. lt .. " dt: " .. dt
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user