Remake MapGen (based on polyhedrons)

This commit is contained in:
2025-10-12 03:44:37 +07:00
parent 9f4cc443c5
commit ca56b703e3
19 changed files with 842 additions and 322 deletions

View File

@@ -0,0 +1,5 @@
local OBJLoader = Mod:new()
OBJLoader.Class = OBJLoader.require('OBJLoader')
Api.addModToGlobalSpace(OBJLoader, 'Core.OBJLoader')

View File

@@ -0,0 +1,2 @@
name = smc__core__obj_loader
depends = smc__api__api_loader, smc__core__polyhedron

View File

@@ -0,0 +1,37 @@
local OBJLoader = {}
function OBJLoader:load(filename)
local file = io.open(filename, "r")
if not file then error("Cannot open OBJ file: " .. filename) end
local vertices = {}
local faces = {}
for line in file:lines() do
line = tostring(line) -- to make EmmyLua shut up
if line:sub(1, 2) == "v " then
local x, y, z = line:match("v (%S+) (%S+) (%S+)")
---@diagnostic disable-next-line: param-type-not-match -- TODO: tell alek to fix his ide-helper's vector.new annotation
table.insert(vertices, vector.new(tonumber(x), tonumber(y), tonumber(z)))
elseif line:sub(1, 2) == "f " then
local faceVertices = {}
for vertex in line:gmatch("%S+") do
if vertex ~= "f" then
local index = tonumber(vertex:match("(%d+)"))
table.insert(faceVertices, index)
end
end
table.insert(faces, faceVertices)
end
end
file:close()
---@diagnostic disable-next-line: need-check-nil -- TODO: idk wtf to do but something has to be done
local polyhedron = Core.Polyhedron.Class:new(vertices, faces)
return polyhedron
end
return OBJLoader