142 lines
6.1 KiB
Luau
142 lines
6.1 KiB
Luau
--!strict
|
|
-- Crosshair in the centre of the screen: four short white lines with a dark outline and a centre dot.
|
|
-- Hidden while aiming down the sights. Hides the default mouse cursor while it is shown (the cursor comes back while the
|
|
-- ViewmodelTuner panel is open, so its buttons can be clicked). Plus a hit marker on confirmed hits.
|
|
|
|
local Players = game:GetService("Players")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
local RunService = game:GetService("RunService")
|
|
local UserInputService = game:GetService("UserInputService")
|
|
local UI = require(ReplicatedStorage:WaitForChild("HunterUI") :: ModuleScript)
|
|
|
|
local GAP = 7 -- px from the centre to the start of each line
|
|
local LENGTH = 9
|
|
local THICK = 2
|
|
local OUTLINE = Color3.new(0, 0, 0)
|
|
local HIT_COLOR = Color3.fromRGB(255, 60, 50)
|
|
|
|
local player = Players.LocalPlayer
|
|
local playerGui = player:WaitForChild("PlayerGui")
|
|
|
|
local gui = Instance.new("ScreenGui")
|
|
gui.Name = "Crosshair"
|
|
gui.IgnoreGuiInset = true -- true screen centre, matching the camera
|
|
gui.ResetOnSpawn = false
|
|
gui.DisplayOrder = 10
|
|
gui.Parent = playerGui
|
|
|
|
local root = Instance.new("Frame")
|
|
root.Name = "Reticle"
|
|
root.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
root.Position = UDim2.fromScale(0.5, 0.5)
|
|
root.Size = UDim2.fromOffset(0, 0)
|
|
root.BackgroundTransparency = 1
|
|
root.Parent = gui
|
|
|
|
local function bar(x: number, y: number, w: number, h: number): Frame
|
|
return UI.element("CrosshairTick", root, UDim2.fromOffset(x, y), UDim2.fromOffset(w, h))
|
|
end
|
|
|
|
local mid = GAP + LENGTH / 2
|
|
local top = bar(0, -mid, THICK, LENGTH)
|
|
local bottom = bar(0, mid, THICK, LENGTH)
|
|
local left = bar(-mid, 0, LENGTH, THICK)
|
|
local right = bar(mid, 0, LENGTH, THICK)
|
|
bar(0, 0, THICK, THICK) -- centre dot
|
|
|
|
-- Hit marker: four short strokes on the diagonals (an "X" around the centre), shown on top of the
|
|
-- crosshair for a moment after each confirmed hit, also while aiming. Body / limb: white and small;
|
|
-- head or a kill: red and bigger. Pops in larger and fades out.
|
|
local MARKER_TIME = 0.28
|
|
type MarkerStyle = { color: Color3, radius: number, length: number, thick: number }
|
|
local MARKER_HIT: MarkerStyle = { color = Color3.new(1, 1, 1), radius = 13, length = 8, thick = 2 }
|
|
local MARKER_CRIT: MarkerStyle = { color = HIT_COLOR, radius = 17, length = 12, thick = 3 }
|
|
|
|
local marker = Instance.new("Frame")
|
|
marker.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
marker.Position = UDim2.fromScale(0.5, 0.5)
|
|
marker.Size = UDim2.fromOffset(0, 0)
|
|
marker.BackgroundTransparency = 1
|
|
marker.Visible = false
|
|
marker.Parent = gui
|
|
local markerScale = Instance.new("UIScale")
|
|
markerScale.Parent = marker
|
|
local strokes: { { frame: Frame, stroke: UIStroke, angle: number } } = {}
|
|
for _, angle in { 45, 135, 225, 315 } do -- clockwise from straight up
|
|
local f = Instance.new("Frame")
|
|
f.AnchorPoint = Vector2.new(0.5, 0.5)
|
|
f.BorderSizePixel = 0
|
|
f.Rotation = angle -- a vertical stroke turned to lie along its diagonal
|
|
local s = Instance.new("UIStroke")
|
|
s.Color = OUTLINE
|
|
s.Thickness = 1
|
|
s.ApplyStrokeMode = Enum.ApplyStrokeMode.Border
|
|
s.Parent = f
|
|
f.Parent = marker
|
|
table.insert(strokes, { frame = f, stroke = s, angle = angle })
|
|
end
|
|
local markerStart: number? = nil
|
|
local markerBig = false
|
|
|
|
-- gap: how far the crosshair lines are open now (the marker sits outside them)
|
|
local function updateMarker(gap: number)
|
|
local start = markerStart
|
|
local t = if start then os.clock() - start else math.huge
|
|
if t > MARKER_TIME then
|
|
marker.Visible = false
|
|
return
|
|
end
|
|
marker.Visible = true
|
|
local style = if markerBig then MARKER_CRIT else MARKER_HIT
|
|
local radius = math.max(style.radius, gap - 2) + style.length / 2
|
|
for _, s in strokes do
|
|
local a = math.rad(s.angle)
|
|
s.frame.Size = UDim2.fromOffset(style.thick, style.length)
|
|
s.frame.Position = UDim2.fromOffset(math.sin(a) * radius, -math.cos(a) * radius)
|
|
s.frame.BackgroundColor3 = style.color
|
|
local fade = math.clamp((t - MARKER_TIME * 0.45) / (MARKER_TIME * 0.55), 0, 1)
|
|
s.frame.BackgroundTransparency = fade
|
|
s.stroke.Transparency = 0.35 + 0.65 * fade
|
|
end
|
|
markerScale.Scale = 1 + 0.5 * math.max(1 - t / 0.08, 0) -- pops in from 1.5x
|
|
end
|
|
|
|
-- hidden while aiming down the sights (SniperViewmodel sets its "Aiming" attribute)
|
|
local viewmodelScript = player:WaitForChild("PlayerScripts"):WaitForChild("SniperViewmodel")
|
|
local renderName = "HunterCrosshair"
|
|
RunService:BindToRenderStep(renderName, Enum.RenderPriority.Camera.Value + 2, function()
|
|
local tuner = playerGui:FindFirstChild("ViewmodelTuner") :: ScreenGui?
|
|
local menu = playerGui:FindFirstChild("MainMenu") :: ScreenGui?
|
|
local tunerShown = tuner ~= nil and tuner.Enabled
|
|
local menuShown = menu ~= nil and menu.Enabled
|
|
local inArena = player:GetAttribute("InArena") == true
|
|
UserInputService.MouseIconEnabled = not inArena or tunerShown or menuShown
|
|
if not inArena and not UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) then
|
|
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
|
|
end
|
|
local character = player.Character
|
|
local hasWeapon = character ~= nil and character:FindFirstChild("SniperRifle") ~= nil
|
|
root.Visible = inArena and hasWeapon and not menuShown
|
|
and viewmodelScript:GetAttribute("Aiming") ~= true
|
|
and viewmodelScript:GetAttribute("ThirdPersonView") ~= true
|
|
-- The gap follows the next shot's deflection angle from centre.
|
|
local cam = workspace.CurrentCamera
|
|
if not cam then return end
|
|
local spread = (viewmodelScript:GetAttribute("SpreadDeg") :: number?) or 0
|
|
local px = math.tan(math.rad(spread)) / math.tan(math.rad(cam.FieldOfView / 2)) * cam.ViewportSize.Y / 2
|
|
local m = math.max(GAP, px) + LENGTH / 2
|
|
top.Position = UDim2.fromOffset(0, -m)
|
|
bottom.Position = UDim2.fromOffset(0, m)
|
|
left.Position = UDim2.fromOffset(-m, 0)
|
|
right.Position = UDim2.fromOffset(m, 0)
|
|
if inArena then updateMarker(m) else marker.Visible = false end
|
|
end)
|
|
script.Destroying:Once(function() RunService:UnbindFromRenderStep(renderName) end)
|
|
|
|
-- the server confirms each own hit (SniperHit: position, damage, zone, killed)
|
|
local hitRemote = ReplicatedStorage:WaitForChild("SniperHit") :: RemoteEvent
|
|
hitRemote.OnClientEvent:Connect(function(_position: unknown, _damage: unknown, zone: unknown, killed: unknown)
|
|
markerStart = os.clock()
|
|
markerBig = zone == "Head" or killed == true
|
|
end)
|