Class: add getInstance(). MapGen: fixes :new() methods. #18

This commit is contained in:
Van
2026-01-20 19:06:53 +03:00
parent c32d324bfb
commit ec614e6080
11 changed files with 72 additions and 33 deletions

View File

@@ -1,6 +1,8 @@
local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_id
local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
@@ -34,14 +36,14 @@ local Cavern = require('MapGen.Cavern')
local ff2luat = require('MapGen.ff2luat')
---@class MapGen
---@field layersByName table<string, MapGen.Layer>
---@field layersList MapGen.Layer[]
---@field staticsByName table<string, MapGen.StaticLayer>
---@field staticsList MapGen.StaticLayer[]
---@field isLayersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization.
---@field isCavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization.
---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, number>
---@field layersByName table<string, MapGen.Layer>
---@field layersList MapGen.Layer[]
---@field staticsByName table<string, MapGen.StaticLayer>
---@field staticsList MapGen.StaticLayer[]
---@field isLayersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization.
---@field isCavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization.
---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, number>
local MapGen = {
layersByName = {},
layersList = {},
@@ -56,7 +58,7 @@ local MapGen = {
---
---**Only for EmmyLua.**
---@class MapGenDef
---@field nodes table<string, string>
---@field nodes table<string, string>
---@param def MapGenDef
---@return MapGen
@@ -68,10 +70,10 @@ function MapGen:new(def)
end
---@type MapGen
local instance = setmetatable({
nodes = def.nodes,
nodeIDs = nodesIDs,
}, {__index = self})
local instance = getInstance(self, {
nodes = def.nodes,
nodeIDs = nodesIDs,
})
return instance
end

View File

@@ -1,5 +1,7 @@
local id = core.get_content_id
local getInstance = Class.getInstance
---@class MapGen.Biome
---@field name string
---@field tempPoint number
@@ -165,7 +167,7 @@ function Biome:new(name, def)
end
---@type MapGen.Biome
local instance = setmetatable({
local instance = getInstance(self, {
name = name,
tempPoint = def.tempPoint,
humidityPoint = def.humidityPoint,
@@ -181,7 +183,7 @@ function Biome:new(name, def)
generateSoil = def.generateSoil,
generateTurf = def.generateTurf,
generateBottom = def.generateBottom,
}, {__index = self})
})
return instance
end

View File

@@ -1,6 +1,8 @@
local mathMin, mathMax, mathAbs
= math.min, math.max, math.abs
local getInstance = Class.getInstance
---@class MapGen.Cavern
---@field name string
---@field minY number
@@ -50,13 +52,13 @@ function Cavern:new(name, def)
end
---@type MapGen.Cavern
local instance = setmetatable({
local instance = getInstance(self, {
name = name,
minY = def.minY,
maxY = def.maxY,
smoothDistance = def.smoothDistance,
threshold = def.threshold,
}, {__index = self})
})
function instance:getNoise()
return _noise

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---**NOT IMPLEMENTED**
---@class MapGen.Decoration
local Decoration = {}
@@ -5,9 +7,9 @@ local Decoration = {}
---@return MapGen.Decoration
function Decoration:new()
---@type MapGen.Decoration
local instance = setmetatable({
local instance = getInstance(self, {
}, {__index = self})
})
return instance
end

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Layer
---@field name string
---@field minY integer
@@ -89,7 +91,7 @@ function Layer:new(name, def)
end
---@type MapGen.Layer
local instance = setmetatable({
local instance = getInstance(self, {
name = name,
minY = def.minY,
maxY = def.maxY,
@@ -102,7 +104,7 @@ function Layer:new(name, def)
calcHumidity = def.calcHumidity,
biomesGorizontalScattering = def.biomesGorizontalScattering or 0,
biomesVerticalScattering = def.biomesVerticalScattering or 0,
}, {__index = self})
})
return instance
end

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Peak
---@field id number
---@field getPeakPos fun(self):vector
@@ -32,9 +34,8 @@ function Peak:new(def)
end
---@type MapGen.Peak
local instance = setmetatable({},
local instance = getInstance(self,{},
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})

View File

@@ -1,5 +1,7 @@
local id = core.get_content_id
local getInstance = Class.getInstance
---@class MapGen.StaticLayer
---@field name string
---@field minY number
@@ -43,14 +45,14 @@ function StaticLayer:new(name, def)
local staticNodeID = id(def.staticNode)
---@type MapGen.StaticLayer
local instance = setmetatable({
local instance = getInstance(self, {
name = name,
minY = def.minY,
maxY = def.maxY,
staticNode = def.staticNode,
staticNodeID = staticNodeID,
generateStaticNode = def.generateStaticNode
}, {__index = self})
})
return instance
end

View File

@@ -1,5 +1,7 @@
local mathAbs = math.abs
local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
@@ -31,7 +33,7 @@ local Tetrahedron = {}
---@param p4 MapGen.Peak
---@return MapGen.Tetrahedron
function Tetrahedron:new(p1, p2, p3, p4)
local instance = setmetatable({
local instance = getInstance(self, {
p1 = p1,
p2 = p2,
p3 = p3,
@@ -44,8 +46,8 @@ function Tetrahedron:new(p1, p2, p3, p4)
f2 = Triangle:new(p1, p2, p4),
f3 = Triangle:new(p1, p3, p4),
f4 = Triangle:new(p2, p3, p4),
}, {
__index = self,
},
{
__tostring = self.toString
})

View File

@@ -1,6 +1,8 @@
local mathSqrt, mathAbs
= math.sqrt, math.abs
local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
@@ -93,7 +95,7 @@ function Triangle:new(p1, p2, p3)
color = c1
end
local instance = setmetatable({
local instance = getInstance(self, {
p1 = p1,
p2 = p2,
p3 = p3,
@@ -106,7 +108,6 @@ function Triangle:new(p1, p2, p3)
color = color
},
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})

View File

@@ -1,3 +1,5 @@
local getInstance = Class.getInstance
---@class MapGen.Triangulation.Edge
---@field p1 MapGen.Peak
---@field p2 MapGen.Peak
@@ -8,12 +10,11 @@ local Edge = {}
---@return MapGen.Triangulation.Edge
function Edge:new( p1, p2 )
---@type MapGen.Triangulation.Edge
local instance = setmetatable({
local instance = getInstance(self, {
p1 = p1,
p2 = p2
},
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})

View File

@@ -1,3 +1,5 @@
local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables
---@class Class
Class = {}
@@ -5,5 +7,25 @@ Class = {}
---@param parent table
---@param child table
function Class.extend(parent ,child)
return setmetatable(child or {}, { __index = parent })
return setmetatable(child or {}, { __index = TableCopy(parent) })
end
---Intended for class constructors.
---
---Correctly creates an object of the class using `setmetatable()`.
---@generic T:table
---@param self T Parent class.
---@param params table Child object parameters.
---@param metatable table? The table that will become the metatable for the object. Note: The `__index` field will be overwritten by a copy of `self`.
---@return T
function Class.getInstance(self, params, metatable)
if metatable == nil then
metatable = {}
end
metatable.__index = TableCopy(self)
local instance = setMetaTable(params, metatable)
return instance
end