MapGen: add Region class and MapGen:pointInRegion2D. close #17

This commit is contained in:
Van
2026-01-20 20:59:58 +03:00
parent 1d1591accc
commit 8c7aaefe09
5 changed files with 167 additions and 2 deletions

View File

@@ -33,6 +33,9 @@ local Triangulation = require('MapGen.Triangulation')
---@type MapGen.Cavern
local Cavern = require('MapGen.Cavern')
---@type MapGen.Region
local Region = require('MapGen.Region')
local ff2luat = require('MapGen.ff2luat')
---@class MapGen
@@ -189,6 +192,20 @@ function MapGen:getTriangleByPos2D(layer, category, pos)
end
end
---Checks if a point is inside a region.
---If the point is inside the region, it will also return the distance to the nearest border.
---@param layer MapGen.Layer The name of the layer in which the region is located.
---@param regionName string The name of the region in which the location of the point is checked.
---@param point Position Point position.
---@return boolean, number?
function MapGen:pointInRegion2D(layer, regionName, point)
local region = layer.regionsByName[regionName]
assert(layer ~= nil, ('There is no region named `%s` registered.'):format(regionName))
return region:pointInRegion(point)
end
-- --- REGISTRATION METHODS ---
@@ -365,6 +382,7 @@ end
function MapGen:registerBiome(layerName, biomeName, def)
local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
---TODO: перенести проверку на уникальность в конструктор
assert(layer.biomesByName[biomeName] == nil, ('A biome named `%s` already exists in the `%s` layer.'):format(biomeName, layerName))
local biome = Biome:new(biomeName, def)
@@ -379,6 +397,7 @@ end
function MapGen:registerCavern(layerName, cavernName, def)
local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
---TODO: перенести проверку на уникальность в конструктор
assert(layer.cavernsByName[cavernName] == nil, ('A cavern named `%s` already exists in the `%s` layer.'):format(cavernName, layerName))
local cavern = Cavern:new(cavernName, def)
@@ -386,6 +405,19 @@ function MapGen:registerCavern(layerName, cavernName, def)
layer:addCavern(cavernName, cavern)
end
---Designate the world region between the vertices as `MapGen.Region` and give it a name.
---@param layerName string The name of the layer in which the new region will be included.
---@param regionName string The name to be assigned to the region. The name must be unique for each region.
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
function MapGen:registerRegion(layerName, regionName, def)
local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
assert(layer.regionsByName[regionName] == nil, ('A region named `%s` already exists in the `%s` layer.'):format(regionName, layerName))
local region = Region:new(regionName, def)
layer:addRegion(regionName, region)
end
-- --- INITIALIZATION METHODS ---