This strategy works for a square map, filled with square models. You can create a some preset models that are all the same size, and place these models in your world based on the 2d array that you generate.
A 2-dimensional array is just an array of arrays. It will look something like this:
local map_layout = {
{"grass", "forest", "forest"},
{"grass", "forest", "forest"},
{"forest", "forest", "forest"},
}
Each item on the grid will have an ‘x’ and ‘y’ position. For example, the item in the top left corner of this 3x3 grid would be at position (1,3).
The GetOffsetPosition
function will return a Vector3 for an ‘x’ and ‘y’ coordinate on your map.
Assuming your map is square, and each model in your map is square, you can use this function to get the world position to place each model in your map.
-- This function takes an x and y coordinate for the map and returns a Vector3 position.
-- blockWidth is the width/height in studs of each model. All models should be the same size.
-- numBlocksXY is the width/height of your map, if your map is 8x8 this number will be 8.
function GetOffsetPosition(x, y, numBlocksXY, blockWidth)
local studWidth = numBlocksXY * blockWidth
local studHeight = numBlocksXY * blockWidth
return Vector3.new(x * blockWidth - studWidth/2 - blockWidth/2, blockWidth/2, y * blockWidth - studHeight/2 - blockWidth/2)
end
The Vector3 returned from this function can be used to set the model’s position in the world after it is cloned.
For each item in the 2-dimensional array, you can select a map model from ReplicatedStorage, clone it, and move the cloned model to a Vector3 position.
This should be enough to get you started on map generation.