local C=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local J=require(C.Shared.components)local E=require(script.Parent.removeMissingModels)local function l(C)for E,l in C:queryChanged(J.Transform)do if not C:contains(E)then continue end local f=C:get(E,J.Model)if not f then continue end if l.new and not l.new.doNotReconcile then f.model:SetPrimaryPartCFrame(l.new.cframe)end end for E,l in C:queryChanged(J.Model)do if not C:contains(E)then continue end local f=C:get(E,J.Transform)if not f then continue end if l.new then l.new.model:SetPrimaryPartCFrame(f.cframe)end end for E,l,f in C:query(J.Model,J.Transform)do if l.model.PrimaryPart.Anchored then continue end local T=f.cframe local w=l.model.PrimaryPart.CFrame if w.Y<-400 then C:despawn(E)continue end if w~=T then C:insert(E,J.Transform({cframe=w,doNotReconcile=true}))end end end return{system=l;after={E}}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local removeMissingModels = require(script.Parent.removeMissingModels)

local function updateTransforms(world)
	-- Handle Transform added/changed to existing entity with Model
	for id, transformRecord in world:queryChanged(Components.Transform) do
		if not world:contains(id) then
			continue
		end

		local model = world:get(id, Components.Model)

		if not model then
			continue
		end

		if transformRecord.new and not transformRecord.new.doNotReconcile then
			model.model:SetPrimaryPartCFrame(transformRecord.new.cframe)
		end
	end

	-- Handle Model added/changed on existing entity with Transform
	for id, modelRecord in world:queryChanged(Components.Model) do
		if not world:contains(id) then
			continue
		end

		local transform = world:get(id, Components.Transform)

		if not transform then
			continue
		end

		if modelRecord.new then
			modelRecord.new.model:SetPrimaryPartCFrame(transform.cframe)
		end
	end

	-- Update Transform on unanchored Models
	for id, model, transform in world:query(Components.Model, Components.Transform) do
		if model.model.PrimaryPart.Anchored then
			continue
		end

		local existingCFrame = transform.cframe
		local currentCFrame = model.model.PrimaryPart.CFrame

		-- Despawn models that fall into the void
		if currentCFrame.Y < -400 then
			world:despawn(id)
			continue
		end

		if currentCFrame ~= existingCFrame then
			world:insert(
				id,
				Components.Transform({
					cframe = currentCFrame,
					doNotReconcile = true,
				})
			)
		end
	end
end

return {
	system = updateTransforms,
	after = { removeMissingModels },
}
