Hey, so I’m trying to make a hexagonal grid base game in a shape of a bigger hexagon. I’ve already made the grid system, but I don’t know how to shape it into a hexagon. I would also like to specify a “render distance” sort of thing so I can change how large the hexagon is.
My code is an edit of this devforum post, although my edits may have been unoptimized.
Here is the shape I would like to create:
(the numbers is the ‘render distance’ I mentioned earlier, image found on google)
Here is how it currently is in game:
Current Code
local tweenService = game:GetService("TweenService")
local debris = game:GetService("Debris")
local ModelGen = {
Enabled = false,
tile = game.ReplicatedStorage:WaitForChild("tile"),
tileFolder = game.Workspace:WaitForChild("tiles"),
seed = game.Workspace:WaitForChild("seed"),
x = 1,
y = 7,
noiseScale = 50,
tileOffset = 0.05,
Space = {},
}
ModelGen.updateTime = (ModelGen.x + ModelGen.y)/128
function ModelGen:toVector2(v3)
return Vector2.new(math.floor(v3.X), math.floor(v3.Z))
end
-- set and get functions for Space, since it will error if the first index is nil.
function ModelGen.Space:set(index1,index2,value)
if self[index1] then
self[index1][index2] = value
else
self[index1] = {}
self[index1][index2] = value
end
end
function ModelGen.Space:get(index1,index2)
if self[index1] then
return self[index1][index2]
else
return nil
end
end
function ModelGen:SetEnabled(bool) -- simplest way to disable ModelGen:AutoUpdate
self.Enabled = bool
end
function ModelGen:Color(AtPos)
--this part isnt done
end
-- Base function for creating a model at a certain position in Space
function ModelGen:Create(AtPos, player)
if not self.Space:get(AtPos.X, AtPos.Y) then
print(tostring(player.x.Value - AtPos.X) .. ", " .. tostring(player.y.Value - AtPos.Y))
--local mid = math.clamp(math.noise(AtPos.X*math.pi + self.Seed.X,AtPos.Y*math.pi + self.Seed.Y),-0.499,0.5) + 0.5
--local rc = math.ceil(#self.Templates*mid)
local xPos = AtPos.X * self.tile.Size.X
local yPos = (AtPos.Y * self.tile.Size.Z) * 0.5
if AtPos.Y % 2 == 1 then
xPos += self.tile.Size.X * 0.5
end
xPos *= 1.5
xPos *= 1 + self.tileOffset
yPos *= 1 + self.tileOffset
local clone = self.tile:Clone() --self.Templates[rc]:Clone()
clone.Position = Vector3.new(xPos, 0.25, yPos)
clone.Transparency = 1
clone.Parent = self.tileFolder
clone.Name = AtPos.X .. ", " .. -AtPos.Y
tweenService:Create(clone, TweenInfo.new(0.5), {Transparency = 0}):Play()
self.Space:set(AtPos.X, AtPos.Y, clone)
self:Color(AtPos)
end
end
-- Base function for destroying a model at a certain position in Space
function ModelGen:Destroy(AtPos)
local model = self.Space:get(AtPos.X, AtPos.Y)
if model then
tweenService:Create(model, TweenInfo.new(0.5), {Transparency = 1}):Play()
debris:AddItem(model, 0.5)
self.Space:set(AtPos.X, AtPos.Y, nil)
end
end
function ModelGen:Update(AtPos, player)
-- Destroy any chunks that are outside of the Render_Radius and still loaded
for x, tab in pairs(self.Space) do
if x ~= "set" and x ~= "get" then
for y, value in pairs(tab) do
if math.abs(AtPos.X - x) > self.x or math.abs(AtPos.Y - y) > self.y then
self:Destroy(Vector2.new(x, y))
end
end
end
end
-- Create any chunks that are in the Render_Radius and not loaded
for x = -self.x, self.x do
for y = -self.y, self.y do
self:Create(Vector2.new(AtPos.X + x, AtPos.Y + y), player)
end
end
end
function ModelGen:AutoUpdate(rootPart)
local player = game.Players:GetPlayerFromCharacter(rootPart.Parent)
local playerX = player:WaitForChild("x")
local playerY = player:WaitForChild("y")
for i, part in pairs(self.tileFolder:GetChildren()) do
part:Destroy()
end
self.Enabled = true
self:Update(Vector2.new(0, 0), player)
--self:Update(self:toVector2(rootPart.Position/self.tile.Size))-- - Vector2.new(0, math.floor(self.y/2)))
while wait(self.updateTime) and self.Enabled do
self:Update(Vector2.new(playerX.Value, -playerY.Value), player)
--self:Update(self:toVector2(rootPart.Position/self.tile.Size))-- - Vector2.new(0, math.floor(self.y/2)))
end
end
return ModelGen