PATTERN 2
unknown
lua
10 months ago
2.8 kB
4
Indexable
-- ObjectSpawnerService.lua
local ObjectSpawnerService = {}
ObjectSpawnerService.__index = ObjectSpawnerService
-- Método construtor (new)
function ObjectSpawnerService.new()
local self = setmetatable({}, ObjectSpawnerService)
-- Inicializa as propriedades
self.SpawnRegion = Workspace.ObjectSpawner.SpawnRegion
self.ObjectClone = Workspace.ObjectSpawner.ObjectClone
self.ObjectsFolder = ReplicatedStorage:WaitForChild("ObjectSpawner")
self.SPAWN_INTERVAL = 0.3
self.MAX_OBJECTS = 200
self.MOVE_DIRECTION = Vector3.new(-1, 0, 0)
self.MOVE_SPEED = 15
return self
end
-- Métodos (IsWithinSpawnRegion, SpawnObject, OnStart)
function ObjectSpawnerService:IsWithinSpawnRegion(objectPosition: Vector3): boolean
local minX = self.SpawnRegion.Position.X - self.SpawnRegion.Size.X / 2
local maxX = self.SpawnRegion.Position.X + self.SpawnRegion.Size.X / 2
local minZ = self.SpawnRegion.Position.Z - self.SpawnRegion.Size.Z / 2
local maxZ = self.SpawnRegion.Position.Z + self.SpawnRegion.Size.Z / 2
return objectPosition.X >= minX and objectPosition.X <= maxX and
objectPosition.Z >= minZ and objectPosition.Z <= maxZ
end
function ObjectSpawnerService:SpawnObject()
if #self.ObjectClone:GetChildren() >= self.MAX_OBJECTS then
return
end
local objects = self.ObjectsFolder:GetChildren()[math.random(1, #self.ObjectsFolder:GetChildren())]
local newObject = objects:Clone()
-- Posição inicial ajustada
local spawnPosition = Vector3.new(
math.random(self.SpawnRegion.Position.X - self.SpawnRegion.Size.X / 2, self.SpawnRegion.Position.X + self.SpawnRegion.Size.X / 2),
self.SpawnRegion.Position.Y + 5,
math.random(self.SpawnRegion.Position.Z - self.SpawnRegion.Size.Z / 2, self.SpawnRegion.Position.Z + self.SpawnRegion.Size.Z / 2)
)
newObject:PivotTo(CFrame.new(spawnPosition))
newObject.Anchored = false
newObject.Parent = self.ObjectClone
-- Lógica de movimento
task.spawn(function()
while newObject.Parent == self.ObjectClone do
local delta = self.MOVE_DIRECTION * self.MOVE_SPEED * task.wait() -- Multiplica pela espera do `task.wait()`
newObject:PivotTo(newObject.CFrame * CFrame.new(delta))
-- Se o objeto sair da área de spawn, destrua-o
if not self:IsWithinSpawnRegion(newObject.Position) then
newObject:Destroy()
end
end
end)
end
-- Método que inicia o spawn
function ObjectSpawnerService:OnStart()
task.spawn(function()
while true do
self:SpawnObject()
task.wait(self.SPAWN_INTERVAL)
end
end)
end
return ObjectSpawnerService
Editor is loading...
Leave a Comment