Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ComputeLinearIndexing(index, size)
- local row = math.floor((index - 1) / size) + 1
- local column = (index - 1) % size + 1
- return column, row
- end
- --[[
- Generates a 3D tilemap subdivided into chunks of equal size
- ]]
- function GenerateChunkedTileMap(gridSize: number, subdivisionBase: number)
- gridSize = math.floor((gridSize/4) * 4) --> optional, this keeps the map size a multiple of 4
- --# The number of chunks is derived this way because I have to guarantee
- --# that it is gonna be a square number(1, 4, 9, 16, etc). this is so the
- --# map remains square shaped, and so each chunk has an equal ammount of cells
- --! ↓ ↓ ↓ ↓ ↓ ↓
- local numberOfChunks = subdivisionBase^2
- local chunkSize = gridSize/numberOfChunks --> area of the chunk
- local Map: Model = Instance.new("Model")
- Map.Name = "Map"
- -- Generate an array of chunk matrixes.
- local chunks = table.create(numberOfChunks)
- for chunkIndex = 1, numberOfChunks do
- local ChunkModel = Instance.new("Model")
- local collumns = table.create(chunkSize)
- -- Generate the chunk grid/matrix
- for x = 1, chunkSize do
- local rows = table.create(chunkSize)
- for z = 1, chunkSize do
- local tile = {X = x, Z = z}
- rows[z] = tile
- local Part = Instance.new("Part")
- Part.Name = x..","..z
- Part.Size = Vector3.one
- Part.Position = Vector3.new(x * Part.Size.X, 10, z * Part.Size.Z)
- Part.Anchored = true
- Part.Material = Enum.Material.SmoothPlastic
- Part.Parent = ChunkModel
- end
- collumns[x] = rows
- end
- --[[
- Since Chunks are represented as models in the workspace... I got to position them
- But I have no X or Z position to do so! Therefor I use Linear indexing so I can get
- an X & Z index from an array ↓ ↓ ↓ ↓ ↓ ↓
- --]]
- local x, z = ComputeLinearIndexing(chunkIndex, subdivisionBase)
- ChunkModel.Name = chunkIndex
- ChunkModel.Parent = Map
- ChunkModel:PivotTo(CFrame.new(x * ChunkModel:GetExtentsSize().X, 10, z * ChunkModel:GetExtentsSize().Z))
- chunks[chunkIndex] = collumns
- end
- Map.Parent = workspace
- return chunks
- end
- print(GenerateChunkedTileMap(144, 3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement