Files
Ornament-Generator-RNG/GUI/GarlandPlacer.luau
T
2026-11-16 23:50:40 +03:00

185 lines
8.4 KiB
Luau

-- GarlandPlacer: "Обвить ёлку" mode. The player clicks points on their own tree one after another, the garland
-- (ToyModels.BuildGarland) is previewed through them live; "Готово" (Enter) sends the points to the server
-- (Shop remote: "Place", id, { {x, y, z}, ... } in tree space). "Назад" (Backspace) removes the last point,
-- "Отмена" (Esc) leaves without changes. Limits: Config.GARLAND (points, wire length).
-- Usage (from OrnamentCraftUI): require(GarlandPlacer).start(id, ctx)
-- ctx = { player, Config, ToyModels, remote, myPlot(), toast(text, color), closeWindow(), gui,
-- new, corner, stroke, text, button, colors = { NAVY, GOLD, WHITE, MUTED, WARN, OK, GREEN, RED, GREY } }
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local M = {}
local ui -- built once: { bar, label, ok, undo, cancel }
local state -- active session or nil
local function stop(restore)
if not state then return end
local s = state
state = nil
for _, c in s.conns do c:Disconnect() end
if s.preview then s.preview:Destroy() end
s.dot:Destroy()
s.line:Destroy()
if restore and s.hidden and s.hiddenParent and s.hiddenParent.Parent then s.hidden.Parent = s.hiddenParent end
ui.bar.Visible = false
end
local function buildUI(ctx)
local c = ctx.colors
local bar = ctx.new("Frame", { Name = "GarlandBar", AnchorPoint = Vector2.new(0.5, 1), Position = UDim2.new(0.5, 0, 1, -24), Size = UDim2.fromOffset(700, 64),
BackgroundColor3 = c.NAVY, BackgroundTransparency = 0.05, Visible = false, ZIndex = 30, Parent = ctx.gui }, { ctx.corner(14), ctx.stroke(c.GOLD, 2) })
local label = ctx.text({ Position = UDim2.fromOffset(16, 0), Size = UDim2.new(1, -380, 1, 0), Text = "", TextSize = 14, TextWrapped = true, ZIndex = 31, Parent = bar })
local ok = ctx.button({ Name = "GarlandDone", AnchorPoint = Vector2.new(1, 0.5), Position = UDim2.new(1, -244, 0.5, 0), Size = UDim2.fromOffset(116, 44), Text = "Done", TextSize = 15, ZIndex = 31, Parent = bar }, c.GREEN)
local undo = ctx.button({ Name = "GarlandUndo", AnchorPoint = Vector2.new(1, 0.5), Position = UDim2.new(1, -128, 0.5, 0), Size = UDim2.fromOffset(108, 44), Text = "Back", TextSize = 15, ZIndex = 31, Parent = bar }, c.GREY)
local cancel = ctx.button({ Name = "GarlandCancel", AnchorPoint = Vector2.new(1, 0.5), Position = UDim2.new(1, -12, 0.5, 0), Size = UDim2.fromOffset(108, 44), Text = "Cancel", TextSize = 15, ZIndex = 31, Parent = bar }, c.RED)
ui = { bar = bar, label = label, ok = ok, undo = undo, cancel = cancel }
ok.Activated:Connect(function() if state then state.finish() end end)
undo.Activated:Connect(function() if state then state.undo() end end)
cancel.Activated:Connect(function() stop(true) end)
end
function M.active() return state ~= nil end
function M.stop() stop(true) end
function M.start(id, ctx)
local Config, ToyModels, c = ctx.Config, ctx.ToyModels, ctx.colors
local G = Config.GARLAND
local plot = ctx.myPlot()
local tree = plot and plot:FindFirstChild("Decor") and plot.Decor:FindFirstChild("Tree")
if not tree or not tree:FindFirstChild("Needles") then
ctx.toast("Place a tree on your plot first (Inventory → Items)", c.WARN)
return
end
if not ui then buildUI(ctx) end
stop(true)
ctx.closeWindow()
local hum = ctx.player.Character and ctx.player.Character:FindFirstChildOfClass("Humanoid")
if hum then hum:UnequipTools() end
local s = { points = {}, conns = {} }
state = s
-- the garland already on the tree is hidden while a new one is being strung
local real = plot.Decor:FindFirstChild(id)
if real then s.hidden, s.hiddenParent = real, real.Parent; real.Parent = nil end
local function part(props)
local p = Instance.new("Part")
p.Anchored, p.CanCollide, p.CanQuery, p.CanTouch, p.CastShadow = true, false, false, false, false
for k, v in props do p[k] = v end
return p
end
s.dot = part({ Name = "GarlandAimDot", Shape = Enum.PartType.Ball, Size = Vector3.one * 0.7, Material = Enum.Material.Neon, Color = Color3.fromRGB(255, 220, 120), Transparency = 0.25 })
s.line = part({ Name = "GarlandAimLine", Material = Enum.Material.Neon, Color = Color3.fromRGB(255, 220, 120), Transparency = 0.45 })
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Include
params.FilterDescendantsInstances = { tree.Needles, tree:FindFirstChild("Trunk") }
local aim -- tree-space point under the cursor (or nil)
local function treeCF() return tree:GetPivot() end
local function length() return ToyModels.GarlandLength(s.points) end
local function aimAt(pos, isScreen) -- viewport coords (mouse) or screen coords (touch, without the top inset)
local cam = workspace.CurrentCamera
local ray = isScreen and cam:ScreenPointToRay(pos.X, pos.Y) or cam:ViewportPointToRay(pos.X, pos.Y)
local hit = workspace:Raycast(ray.Origin, ray.Direction * 300, params)
if not hit then return nil end
-- a bit in front of the needles, so the wire lies on top of them
return treeCF():PointToObjectSpace(hit.Position + hit.Normal * 0.25)
end
local function refreshLabel()
local n, len = #s.points, length()
local text
if n == 0 then
text = "Click the tree: the first point starts the garland"
elseif n == 1 then
text = "Keep clicking — the garland follows your cursor"
else
text = string.format("Points %d · length %d / %d. Done — Enter, undo — Backspace", n, math.floor(len + 0.5), G.MAX_LENGTH)
end
ui.label.Text = text
ui.label.TextColor3 = c.WHITE
ui.ok.BackgroundTransparency = n >= 2 and 0 or 0.5
ui.undo.BackgroundTransparency = n >= 1 and 0 or 0.5
end
local function rebuild()
if s.preview then s.preview:Destroy() s.preview = nil end
if #s.points >= 2 then
s.preview = ToyModels.BuildGarland(s.points)
s.preview.Name = "GarlandPreview"
s.preview:PivotTo(treeCF())
s.preview.Parent = workspace
end
refreshLabel()
end
local function add(p)
if #s.points >= G.MAX_POINTS then ctx.toast("No more points — press “Done”", c.WARN) return end
local last = s.points[#s.points]
if last and (p - last).Magnitude < 0.4 then return end
if last and length() + (p - last).Magnitude > G.MAX_LENGTH then ctx.toast("Out of wire — press “Done” or remove a point", c.WARN) return end
table.insert(s.points, p)
rebuild()
end
s.undo = function()
if #s.points == 0 then return end
table.remove(s.points)
rebuild()
end
s.finish = function()
if #s.points < 2 then ctx.toast("Place at least 2 points on the tree", c.WARN) return end
local pts = {}
for k, p in s.points do pts[k] = { p.X, p.Y, p.Z } end
ui.ok.Text = "…"
task.spawn(function()
local okCall, res = pcall(ctx.remote.InvokeServer, ctx.remote, "Place", id, pts)
res = (okCall and type(res) == "table") and res or { ok = false, msg = "No connection" }
ui.ok.Text = "Done"
ctx.toast(res.msg or (res.ok and "Done" or "Failed"), res.ok and c.OK or c.WARN)
if res.ok and state == s then
if s.hidden then s.hidden:Destroy() s.hidden = nil end -- the server already replaced it
stop(false)
end
end)
end
table.insert(s.conns, RunService.RenderStepped:Connect(function()
if not tree.Parent then stop(true) return end
if UIS.TouchEnabled and not UIS.MouseEnabled then return end
aim = aimAt(UIS:GetMouseLocation())
if aim then
local cf = treeCF()
local w = cf * aim
s.dot.Position = w
s.dot.Parent = workspace
local last = s.points[#s.points]
if last then
local a = cf * last
local d = (w - a).Magnitude
s.line.Size = Vector3.new(0.1, 0.1, d)
s.line.CFrame = CFrame.lookAt((a + w) / 2, w)
s.line.Parent = workspace
else
s.line.Parent = nil
end
else
s.dot.Parent = nil
s.line.Parent = nil
end
end))
table.insert(s.conns, UIS.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape then stop(true) return end
if processed then return end
if input.KeyCode == Enum.KeyCode.Return or input.KeyCode == Enum.KeyCode.KeypadEnter then
s.finish()
elseif input.KeyCode == Enum.KeyCode.Backspace or input.KeyCode == Enum.KeyCode.Z then
s.undo()
elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
if aim then add(aim) else ctx.toast("Click on your tree", c.WARN) end
elseif input.UserInputType == Enum.UserInputType.Touch then
local p = aimAt(Vector2.new(input.Position.X, input.Position.Y), true)
if p then add(p) end
end
end))
ui.bar.Visible = true
refreshLabel()
end
return M