2024-08-21 15:57:40 +00:00
|
|
|
--!optimize 2
|
|
|
|
--!native
|
2024-08-18 14:46:52 +00:00
|
|
|
--!strict
|
2024-08-21 15:57:40 +00:00
|
|
|
|
2024-08-07 16:45:56 +00:00
|
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
local blink = require(game:GetService("ServerScriptService").net)
|
|
|
|
local jecs = require(ReplicatedStorage.ecs)
|
|
|
|
local __ = jecs.Wildcard
|
|
|
|
|
|
|
|
local std = require(ReplicatedStorage.std)
|
2024-09-09 01:38:47 +00:00
|
|
|
local ref = std.ref
|
|
|
|
local interval = std.interval
|
|
|
|
|
2024-08-18 14:46:52 +00:00
|
|
|
local world: std.World = std.world
|
2024-08-07 16:45:56 +00:00
|
|
|
local cts = std.components
|
|
|
|
|
|
|
|
local Mob = cts.Mob
|
|
|
|
local Transform = cts.Transform
|
|
|
|
local Velocity = cts.Velocity
|
|
|
|
local Player = cts.Player
|
|
|
|
local Character = cts.Character
|
|
|
|
|
|
|
|
local function mobsMove(dt: number)
|
2024-10-12 20:18:11 +00:00
|
|
|
local targets = {}
|
|
|
|
for _, character in world:query(Character):with(Player):iter() do
|
|
|
|
table.insert(targets, (character.PrimaryPart :: Part).Position)
|
|
|
|
end
|
|
|
|
|
|
|
|
for mob, transform, v in world:query(Transform, Velocity):with(Mob):iter() do
|
|
|
|
local cf = transform.new
|
|
|
|
local p = cf.Position
|
|
|
|
|
|
|
|
local target
|
|
|
|
local closest
|
|
|
|
|
|
|
|
for _, pos in targets do
|
|
|
|
local distance = (p - pos).Magnitude
|
|
|
|
if not target or distance < closest then
|
|
|
|
target = pos
|
|
|
|
closest = distance
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not target then
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
|
|
|
|
local moving = CFrame.new(p + (target - p).Unit * dt * v)
|
|
|
|
transform.new = moving
|
|
|
|
blink.UpdateTransform.FireAll(mob, moving)
|
|
|
|
end
|
2024-08-07 16:45:56 +00:00
|
|
|
end
|
|
|
|
|
2024-09-09 01:38:47 +00:00
|
|
|
local throttle = interval(5)
|
|
|
|
|
|
|
|
local function spawnMobs()
|
2024-10-12 20:18:11 +00:00
|
|
|
if throttle() then
|
|
|
|
local p = Vector3.new(0, 5, 0)
|
|
|
|
local cf = CFrame.new(p)
|
|
|
|
local v = 5
|
2024-09-09 01:38:47 +00:00
|
|
|
|
2024-10-12 20:18:11 +00:00
|
|
|
local id = ref():set(Velocity, v):set(Transform, { new = cf }):add(Mob).id()
|
2024-09-09 01:38:47 +00:00
|
|
|
|
2024-10-12 20:18:11 +00:00
|
|
|
blink.SpawnMob.FireAll(id, cf, v)
|
|
|
|
end
|
2024-09-09 01:38:47 +00:00
|
|
|
end
|
|
|
|
|
2024-08-21 15:57:40 +00:00
|
|
|
return function(scheduler: std.Scheduler)
|
2024-10-12 20:18:11 +00:00
|
|
|
local phases = scheduler.phases
|
|
|
|
local system_new = scheduler.systems.new
|
|
|
|
system_new(mobsMove, phases.Heartbeat)
|
|
|
|
system_new(spawnMobs, phases.Heartbeat)
|
2024-08-21 15:57:40 +00:00
|
|
|
end
|