Giter Club home page Giter Club logo

compose's Introduction

Compose

Composition decorator pattern API

Luau





What is a decorator?

A decorator lets you add new features or 'decorations' to x without changing what it is at its core.

API

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 a Composition<T>

Composition:SetValue(T) -> nil
ExampleNumericalComposition:SetValue(64.0)



Getting the base value of type T of a Composition<T>

Composition:GetValue(T) -> T
local BaseValue = ExampleNumericalComposition:GetValue()



Appending a modifier function (T)->T to a Composition<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 a Composition<T>

Composition:Get() -> T
local Mult = ExampleNumericalComposition:Get() -- 64 * 2.0 = 128




API Examples

--!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

compose's People

Contributors

rt0mmy avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.