115 lines
4.5 KiB
Luau
115 lines
4.5 KiB
Luau
--!strict
|
|
-- ModuleScript in ReplicatedStorage.
|
|
-- The shape of the sniper reload, shared by the first-person viewmodel (SniperViewmodel) and the
|
|
-- third-person pose seen by other players (ThirdPersonRifle). Times are fractions of the reload
|
|
-- (0 = start, 1 = end); positions are in the rifle's grip space (-Z = barrel, +Y = up, +X = right).
|
|
-- 0.00-0.18 the weapon goes into pose A (magazine change: muzzle up, canted)
|
|
-- 0.04-0.30 left hand: handguard -> magazine -> pulls it down -> throws it away to the left
|
|
-- 0.30-0.38 left hand out of view, takes a new magazine
|
|
-- 0.38-0.68 left hand brings it up from below, pushes it in, back to the handguard
|
|
-- 0.55-0.65 the weapon swings into pose B (bolt: to the left, rolled over)
|
|
-- 0.62-0.93 right hand: grip -> bolt -> lifts it -> pulls it back -> pushes it forward ->
|
|
-- locks it down -> back to the grip
|
|
-- 0.85-1.00 the weapon returns to the normal pose
|
|
|
|
local ReloadTimeline = {}
|
|
|
|
ReloadTimeline.MAG_GRAB = 0.14 -- the left hand reaches the old magazine
|
|
ReloadTimeline.MAG_RELEASE = 0.30 -- the old magazine is thrown
|
|
ReloadTimeline.MAG_NEW = 0.38 -- the new magazine appears in the hand (out of view)
|
|
ReloadTimeline.MAG_IN = 0.55 -- the new magazine is in the rifle
|
|
|
|
local function smooth(a: number): number
|
|
a = math.clamp(a, 0, 1)
|
|
return a * a * (3 - 2 * a)
|
|
end
|
|
ReloadTimeline.smooth = smooth
|
|
|
|
type Key = { number | Vector3 }
|
|
|
|
local function track(keys: { Key }, u: number): Vector3
|
|
local first = keys[1]
|
|
if u <= (first[1] :: number) then
|
|
return first[2] :: Vector3
|
|
end
|
|
for i = 2, #keys do
|
|
local k0, k1 = keys[i - 1], keys[i]
|
|
local u0, u1 = k0[1] :: number, k1[1] :: number
|
|
if u <= u1 then
|
|
local a = smooth((u - u0) / math.max(u1 - u0, 1e-3))
|
|
return (k0[2] :: Vector3):Lerp(k1[2] :: Vector3, a)
|
|
end
|
|
end
|
|
return keys[#keys][2] :: Vector3
|
|
end
|
|
|
|
-- How strongly the weapon is in pose A and pose B. Apply as
|
|
-- CFrame.identity:Lerp(A, a):Lerp(B, b) (B fully replaces A while b = 1).
|
|
function ReloadTimeline.weapon(u: number): (number, number)
|
|
local b = math.min(smooth((u - 0.55) / 0.10), 1 - smooth((u - 0.85) / 0.15))
|
|
local a = if u < 0.65 then smooth(u / 0.18) else 0
|
|
return a, b
|
|
end
|
|
|
|
-- Left hand position at time u. rest = its normal place on the handguard, mag = the bottom of
|
|
-- the magazine, drop = how far down the magazine is pulled out.
|
|
function ReloadTimeline.leftHand(u: number, rest: Vector3, mag: Vector3, drop: number): Vector3
|
|
local down = mag - Vector3.new(0, drop, 0)
|
|
local toss = down + Vector3.new(-drop * 0.5, -drop * 0.2, 0)
|
|
local away = mag + Vector3.new(-drop * 0.4, -drop * 1.6, drop * 0.3) -- below the view
|
|
local push = mag + Vector3.new(0, drop * 0.05, 0)
|
|
return track({
|
|
{ 0.04, rest },
|
|
{ ReloadTimeline.MAG_GRAB, mag },
|
|
{ 0.24, down },
|
|
{ ReloadTimeline.MAG_RELEASE, toss },
|
|
{ ReloadTimeline.MAG_NEW, away },
|
|
{ 0.48, down },
|
|
{ ReloadTimeline.MAG_IN, mag },
|
|
{ 0.58, push },
|
|
{ 0.60, mag },
|
|
{ 0.68, rest },
|
|
}, u)
|
|
end
|
|
|
|
-- Right hand position at time u. rest = its normal place on the pistol grip, bolt = the bolt
|
|
-- handle, lift = how far the bolt is lifted, pull = how far it is pulled back.
|
|
function ReloadTimeline.rightHand(u: number, rest: Vector3, bolt: Vector3, lift: number, pull: number): Vector3
|
|
return ReloadTimeline.boltCycle((u - 0.62) / 0.31, rest, bolt, lift, pull)
|
|
end
|
|
|
|
-- One working of the bolt by the right hand, a = 0..1 (also used after every shot):
|
|
-- grip -> bolt -> lift -> pull back -> push forward -> lock down -> grip.
|
|
function ReloadTimeline.boltCycle(a: number, rest: Vector3, bolt: Vector3, lift: number, pull: number): Vector3
|
|
local up = bolt + Vector3.new(0, lift, 0)
|
|
local back = up + Vector3.new(0, 0, pull)
|
|
return track({
|
|
{ 0.00, rest },
|
|
{ 0.19, bolt },
|
|
{ 0.29, up },
|
|
{ 0.48, back },
|
|
{ 0.61, up },
|
|
{ 0.71, bolt },
|
|
{ 1.00, rest },
|
|
}, a)
|
|
end
|
|
|
|
-- The loose magazines.
|
|
-- Old one: "held" while the left hand pulls it out, then "flying" after the throw (2nd value =
|
|
-- seconds since the throw), nil otherwise. New one (3rd value): true while it is in the left
|
|
-- hand on its way into the rifle.
|
|
function ReloadTimeline.magazine(u: number, reloadTime: number): (string?, number, boolean)
|
|
local old: string? = nil
|
|
local flyTime = 0
|
|
if u >= ReloadTimeline.MAG_GRAB and u < ReloadTimeline.MAG_RELEASE then
|
|
old = "held"
|
|
elseif u >= ReloadTimeline.MAG_RELEASE and u < ReloadTimeline.MAG_RELEASE + 0.25 then
|
|
old = "flying"
|
|
flyTime = (u - ReloadTimeline.MAG_RELEASE) * reloadTime
|
|
end
|
|
local new = u >= ReloadTimeline.MAG_NEW and u < ReloadTimeline.MAG_IN
|
|
return old, flyTime, new
|
|
end
|
|
|
|
return ReloadTimeline
|