A decorator lets you add new features or 'decorations' to x without changing what it is at its core.
local Compose = require(...)
Creating a new
Composition<T>
Compose(T) -> Composition<T>
local ExampleNumericalComposition = Composition(24.0)
Setting the base value of type
T
of aComposition<T>
Composition:SetValue(T) -> nil
ExampleNumericalComposition:SetValue(64.0)
Getting the base value of type
T
of aComposition<T>
Composition:GetValue(T) -> T
local BaseValue = ExampleNumericalComposition:GetValue()
Appending a modifier function
(T)->T
to aComposition<T>
Composition:Modifier(f:(a0: T)->T) -> nil
ExampleNumericalComposition:Modifier(function(a0: number): number
return a0 * 2.0
end)
Getting the fully composed value
T
of aComposition<T>
Composition:Get() -> T
local Mult = ExampleNumericalComposition:Get() -- 64 * 2.0 = 128
--!strict
--!native
--!optimize 2
local Compose = require(script.Parent:WaitForChild("Compose"))
local Camera = workspace.CurrentCamera
local BASE_FOV = 60.0
local ZOOM_FOV_MULT = 0.5
local BOOST_FOV_MULT = 1.2
local FOV_ALPHA = 0.5
local CameraFOVComposition = Compose(BASE_FOV)
local Boost = false
local function onBoost(fov: number)
return fov * (Boost and BOOST_FOV_MULT or 1)
end
local Zoom = false
local function onZoom(fov: number)
return fov * (Zoom and ZOOM_FOV_MULT or 1)
end
CameraFOVComposition:Modifier(onBoost)
CameraFOVComposition:Modifier(onZoom)
CameraFOVComposition:Modifier(function(fov: number): number
return fov + (math.sin(tick()) * 0.5 + 0.5) * 8
end)
game:GetService("UserInputService").InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then
return
end
if input.KeyCode == Enum.KeyCode.Z then
Zoom = true
end
if input.KeyCode == Enum.KeyCode.X then
Boost = true
end
end)
game:GetService("UserInputService").InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
if gameProcessedEvent then
return
end
if input.KeyCode == Enum.KeyCode.Z then
Zoom = false
end
if input.KeyCode == Enum.KeyCode.X then
Boost = false
end
end)
game:GetService("RunService").RenderStepped:Connect(function(deltaTime: number)
local FieldOfView = CameraFOVComposition:Get()
Camera.FieldOfView = Camera.FieldOfView + (FieldOfView - Camera.FieldOfView) * FOV_ALPHA
end)
--!strict
--!native
--!optimize 2
local Compose = require(script.Parent:WaitForChild("Compose"))
local BASE_STRING = "Hello World!"
local MODIFIERS = {
"Cheeseburger!!!",
"Taco!!!",
"Cookies??!",
"Cakes!"
}
local Modifier = MODIFIERS[1]
local StringComposition = Compose("Hello world!")
StringComposition:Modifier(function(a0: string): string
return `{a0} {Modifier}`
end)
while task.wait(1.0) do
Modifier = MODIFIERS[math.random(#MODIFIERS)]
warn(StringComposition:Get())
end