From bfca13ed6edcf30017726247f79812b8179de44c Mon Sep 17 00:00:00 2001 From: interpreterK Date: Tue, 19 Dec 2023 22:35:22 -0500 Subject: [PATCH] Tween module and KeyBinds module --- src/shared/Client/KeyBinds.lua | 48 +++++++++++++++++++++++++----- src/shared/Tween.lua | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 src/shared/Tween.lua diff --git a/src/shared/Client/KeyBinds.lua b/src/shared/Client/KeyBinds.lua index 86f3a19..f01a8db 100644 --- a/src/shared/Client/KeyBinds.lua +++ b/src/shared/Client/KeyBinds.lua @@ -1,34 +1,66 @@ local Binds = {} +local BindsMap = {} Binds.__index = Binds - -local BindsMap = { - Keys = {} -} BindsMap.__index = BindsMap local UIS = game:GetService("UserInputService") +export type KeyBindMap = { + [string]: { + [Enum.KeyCode]: () -> () + } +} + function Binds.constructor() --Allow multiple bindings of the same keys, no overwrites - return setmetatable({}, BindsMap) + local RegisteredKeys: KeyBindMap = { + Began = {}, + Ended = {} + } + return setmetatable({ + RegisteredKeys = RegisteredKeys + }, BindsMap) end -function BindsMap.new() +type CallbackFunction = () -> () + +function Binds:Add(Key: Enum.KeyCode, Callback: CallbackFunction) + local k = self.RegisteredKeys[Key] + if k then + warn(`Key "{Key.Name}" is already binded on this KeyBind map`) + end + self.RegisteredKeys[Key] = Callback +end + +function BindsMap.NewBindState(RegisteredKeys: KeyBindMap) local InputBegan = UIS.InputBegan:Connect(function(input, gameProcessedEvent) if not gameProcessedEvent then - + local switch = RegisteredKeys.Began[input.KeyCode] + if switch then + switch() + else + --switch.default() + end end end) local InputEnded = UIS.InputEnded:Connect(function(input, gameProcessedEvent) if not gameProcessedEvent then - + local switch = RegisteredKeys.Ended[input.KeyCode] + if switch then + switch() + else + --switch.default() + end end end) return setmetatable({ + --Return these for convenience InputBegan = InputBegan, InputEnded = InputEnded }, BindsMap) end + + return Binds \ No newline at end of file diff --git a/src/shared/Tween.lua b/src/shared/Tween.lua new file mode 100644 index 0000000..8777ffd --- /dev/null +++ b/src/shared/Tween.lua @@ -0,0 +1,54 @@ +local Tween = {} +Tween.__index = Tween + +local TS = game:GetService("TweenService") + +type TweenAnimation = {[string]: any} + +function Tween.constructor( + TweenSettings: TweenInfo, + Object: Instance | nil, + PreProperties: TweenAnimation | nil +) + return setmetatable({ + TweenInfo = TweenSettings, + Instance = Object, + PreProperties = PreProperties + }, Tween) +end + +function Tween:Start( + PostInstance: Instance | nil, + PostProperties: TweenAnimation | nil, + PostTweenSettings: TweenInfo | nil +): Tween + local Props = self.PreProperties + local Object = self.Instance + local TweenSettings = self.TweenInfo + + if PostProperties and self.PreProperties then + for tween_prop, tween_value in PostProperties do + Props[tween_prop] = tween_value + end + print("Tween library: Combining PostProperties and PreProperties") + end + if PostInstance then + if Object then + print("Tween library: Overwriting an already defined animating object old=", Object, "new=", PostInstance) + end + Object = PostInstance + end + if PostTweenSettings then + if TweenSettings then + print("Tween library: Overwriting already defined Tween settings") + end + TweenSettings = PostTweenSettings + end + + local Animation = TS:Create(Object, TweenSettings, Props) + Animation:Play() + + return Animation +end + +return Tween \ No newline at end of file