Skip to content

Commit

Permalink
types overhaul and organization
Browse files Browse the repository at this point in the history
  • Loading branch information
jmesrje committed Nov 19, 2024
1 parent e1f5a4c commit 7cacbcb
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 205 deletions.
127 changes: 52 additions & 75 deletions src/Animation/Spring.luau
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
--[[
Forked from https://github.com/DervexDev/AdvancedSpring
Thank you @DervexDev!
]]
-- Forked from dervexdev/advancedspring
-- Variables
local Root = script.Parent.Parent
local Packages = script.Parent.Parent.Parent.Parent.Packages

-- SERVICES >
local RunService = game:GetService("RunService")

-- FOLDERS >
local Aegis = script.Parent.Parent
local Runtime = Aegis.Runtime
local Utility = Aegis.Utility

-- DEPENDENCIES >
local Runtime = Root.Runtime
local Scheduler = require(Runtime.Scheduler)
local Storage = require(Runtime.Storage)

local Debugger = require(Utility.Debugger)
local Utils = require(Utility.Utils)
local Is = require(Utility.Is)
local Debugger = require(Packages.debugger)
local Utils = require(Root.Utility)
local Is = require(Root.Is)
local Types = require(Root.Types)

local Types = require(Aegis.Types)

-- VARIABLES >
local Class = {} :: Types.SpringFunc
local Class = {}

local Epsilon = 1e-4

Expand All @@ -32,7 +24,7 @@ local cos = math.cos
local sin = math.sin
local pi = math.pi

-- FUNCTIONS >
-- Functions
local function IsSpringSettled(currentLinearPosition: { number }, targetLinearPosition: { number }): boolean
for index, value in currentLinearPosition do
if math.abs(value - targetLinearPosition[index]) > Epsilon then
Expand All @@ -44,19 +36,19 @@ local function IsSpringSettled(currentLinearPosition: { number }, targetLinearPo
return true
end

function Class._bind(self: Types.Spring, prop: string, instance: Instance)
function Class._Bind(self: Types.Spring, prop: string, instance: Instance)
local Mode = RunService:IsClient() and "Client" or "Server"
local Connection: ((number) -> ())?

local TargetLinearPosition = self._type.ToLinear(self._state:Get())
local CurrentValue = self._state:Get()
local TargetLinearPosition = self._type.ToLinear(self._State:Get())
local CurrentValue = self._State:Get()

if CurrentValue ~= nil then
(instance :: any)[prop] = CurrentValue
end
self._currentLinearPosition = TargetLinearPosition
self._CurrentLinearPosition = TargetLinearPosition

self._state:Listen(function(newValue)
;(self._State :: any):Listen(function(newValue)
TargetLinearPosition = self._type.ToLinear(newValue)

if Connection ~= nil then
Expand All @@ -65,31 +57,31 @@ function Class._bind(self: Types.Spring, prop: string, instance: Instance)

if not Connection then
Connection = Scheduler:Add(Mode, function(delta: number)
if self and self._update then
self:_update(TargetLinearPosition, delta);
(instance :: any)[prop] = self._type.FromLinear(self._currentLinearPosition)
if self and (self :: any)._Update then
(self :: any):_Update(TargetLinearPosition, delta);
(instance :: any)[prop] = self._type.FromLinear(self._CurrentLinearPosition)
else
if Connection then
Scheduler:Remove(Mode, Connection)
return
end
end

if IsSpringSettled(self._currentLinearPosition, TargetLinearPosition) == true and Connection then
if IsSpringSettled(self._CurrentLinearPosition, TargetLinearPosition) == true and Connection then
Scheduler:Remove(Mode, Connection)
Connection = nil;
(instance :: any)[prop] = self._state:Get()
(instance :: any)[prop] = (self :: any)._State:Get()

for index = 1, #self._velocity do
self._velocity[index] = 0
for index = 1, #self._Velocity do
self._Velocity[index] = 0
end
end
end)
end
end)
end

function Class._update(self: Types.Spring, targetLinearPosition: { number }, delta: number)
function Class._Update(self: Types.Spring, targetLinearPosition: { number }, delta: number)
for index = 1, #targetLinearPosition do
-- DO NOT CHANGE ANYTHING IN THE CALCULATION UNLESS YOU KNOW WHAT YOU ARE DOING
--[[
Expand All @@ -107,25 +99,25 @@ function Class._update(self: Types.Spring, targetLinearPosition: { number }, del
e2 = exp2
]]

local d = self._damping
local f = self._frequency * pi * 2
local d = self._Damping
local f = self._Frequency * pi * 2
local g = targetLinearPosition[index]
local p = self._currentLinearPosition[index]
local v = self._velocity[index]
local p = self._CurrentLinearPosition[index]
local v = self._Velocity[index]

local offset = p - g
local decay = exp(-delta * d * f)

if d == 1 then
self._currentLinearPosition[index] = (v * delta + offset * (f * delta + 1)) * decay + g
self._velocity[index] = (v - (offset * f + v) * (f * delta)) * decay
self._CurrentLinearPosition[index] = (v * delta + offset * (f * delta + 1)) * decay + g
self._Velocity[index] = (v - (offset * f + v) * (f * delta)) * decay
elseif d < 1 then
local c = sqrt(1 - d * d)
local i = cos(delta * f * c)
local j = sin(delta * f * c)

self._currentLinearPosition[index] = (offset * i + (v + offset * (d * f)) * j / (f * c)) * decay + g
self._velocity[index] = (v * (i * c) - (v * d + offset * f) * j) * (decay / c)
self._CurrentLinearPosition[index] = (offset * i + (v + offset * (d * f)) * j / (f * c)) * decay + g
self._Velocity[index] = (v * (i * c) - (v * d + offset * f) * j) * (decay / c)
else
local c = sqrt(d * d - 1)
local r1 = -f * (d - c)
Expand All @@ -135,8 +127,8 @@ function Class._update(self: Types.Spring, targetLinearPosition: { number }, del
local e1 = co1 * exp(r1 * delta)
local e2 = co2 * exp(r2 * delta)

self._currentLinearPosition[index] = e1 + e2 + g
self._velocity[index] = r1 * e1 + r2 * e2
self._CurrentLinearPosition[index] = e1 + e2 + g
self._Velocity[index] = r1 * e1 + r2 * e2
end
end
end
Expand All @@ -157,7 +149,7 @@ end
]]
function Class.Get(self: Types.Spring): Types.Animatable
-- Return the unpacked format for improved clarity
return self._type.FromLinear(self._currentLinearPosition)
return self._type.FromLinear(self._CurrentLinearPosition)
end

--[[
Expand All @@ -174,38 +166,23 @@ end
```
]]
function Class.Destroy(self: Types.Spring): nil
self._state:Destroy()
(self :: any)._State:Destroy()
Utils.CleanMetatable(self :: any)
return nil
end

--[[
## `Aegis.spring`
A constructor to create new spring objects.
#### Parameters
- **springInfo:** Table of information to create a new spring object.
#### Returns
- [`spring`](https://lumin-dev.github.io/Aegis/api/state) | [`Tutorial`](https://lumin-dev.github.io/Aegis/guide/managing-springs)
#### Example
```luau
local MyState = Aegis.state(Vector3.new(0, 5, 0))
local CustomSpring = Aegis.spring({
State = MyState,
Damping = 0.3,
Frequency = 1.5,
})
Aegis.new("Part", {
Size = Vector3.new(4, 4, 4),
Anchored = true,
Parent = workspace,
Position = CustomSpring,
})
```
]]
return function(springInfo: Types.SpringInfo): Types.Spring
--[=[
Creates a new spring.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/#spring)
]=]
return function(springInfo: {
State: Types.State,
Damping: number?,
Frequency: number?,
}): Types.Spring
-- Checks
if Is.AegisConstructor(springInfo.State :: any) == false then
if Is.Constructor(springInfo.State :: any) == false then
Debugger.Error("TypeMismatch", "Aegis.State", typeof(springInfo.State))
end

Expand All @@ -219,14 +196,14 @@ return function(springInfo: Types.SpringInfo): Types.Spring

self.ConstructorClass = "spring"

self._damping = springInfo.Damping or 1
self._frequency = springInfo.Frequency or 1
self._state = springInfo.State
self._Damping = springInfo.Damping or 1
self._Frequency = springInfo.Frequency or 1
self._State = springInfo.State

self._type = Type
self._currentLinearPosition = self._type.ToLinear(springInfo.State:Get())
self._CurrentLinearPosition = self._type.ToLinear(springInfo.State:Get())

self._velocity = table.create(#self._currentLinearPosition, 0)
self._Velocity = table.create(#self._CurrentLinearPosition, 0)

return self
end
5 changes: 2 additions & 3 deletions src/Instances/New.luau
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
-- Variables
local Root = script.Parent.Parent
local Packages = script.Parent.Parent.Parent.Parent.Packages

local Debugger = require(Packages.debugger)
local Utility = require(Root.Utility)
local Types = require(Root.Types)

local Debugger = require(Root.Parent.debugger)
local Default = require(Root.Instances.Default)

-- Module
Expand Down
5 changes: 3 additions & 2 deletions src/Instances/Update.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Variables
local Utility = require(script.Parent.Parent.Utility)
local Types = require(script.Parent.Parent.Types)
local Root = script.Parent.Parent
local Utility = require(Root.Utility)
local Types = require(Root.Types)

-- Module

Expand Down
2 changes: 1 addition & 1 deletion src/Is.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local Types = require(script.Parent.Types)

-- Functions
local function Constructor(item: Types.Constructor)
local function Constructor(item: Types.Constructor<any>)
return if type(item) == "table" and item._Type and item._Bind and item.Get then true else false
end

Expand Down
15 changes: 8 additions & 7 deletions src/Keys/Change.luau
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
-- Variables
local Packages = script.Parent.Parent.Parent.Parent.Packages
local Utility = script.Parent.Utility
local Keys = require(Utility.Keys)
local Debugger = require(Packages.debugger)
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Debugger = require(Root.Parent.debugger)

-- Module

--[=[
Connects a callback to a certain property change event.
Connects a callback to a certain property change event, and supplies the changed prop as an arg.
[Learn More](https://luminlabsdev.github.io/ui-framework/api/keys/#change)
]=]
return function(prop: string)
if not Keys["Change"] then
Keys["Change"] = {
Name = "Change",
Apply = function(instance: Instance, callback: (...any) -> ())
Apply = function(instance: Instance, callback: (changed: any) -> ())
local Success, Event = pcall(instance.GetPropertyChangedSignal, instance :: any, prop :: any)
if not Success or type(callback) ~= "function" then
Debugger.Fatal("")
end
Event:Connect(callback)
Event:Connect(function()
callback((instance :: any)[prop])
end)
end,
}
end
Expand Down
7 changes: 3 additions & 4 deletions src/Keys/Clean.luau
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
-- Variables
local Packages = script.Parent.Parent.Parent.Parent.Packages
local Utility = script.Parent.Utility
local Keys = require(Utility.Keys)
local Debugger = require(Packages.debugger)
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Debugger = require(Root.Parent.debugger)

-- Module

Expand Down
15 changes: 9 additions & 6 deletions src/Keys/Event.luau
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
-- Variables
local Packages = script.Parent.Parent.Parent.Parent.Packages
local Utility = script.Parent.Utility
local FindProp = require(Utility.FindProp)
local Keys = require(Utility.Keys)
local Debugger = require(Packages.debugger)
local Root = script.Parent.Parent
local Keys = require(script.Parent.List)
local Debugger = require(Root.Parent.debugger)

-- Functions
local function Find(instance: Instance, property: string)
return (instance :: any)[property]
end

-- Module

Expand All @@ -17,7 +20,7 @@ return function(event: string)
Keys["Event"] = {
Name = "Event",
Apply = function(instance: Instance, callback: (...any) -> ())
local Success, Event = pcall(FindProp, instance :: any, event :: any)
local Success, Event = pcall(Find, instance :: any, event :: any)
if not Success or type(callback) ~= "function" then
Debugger.Fatal("")
end
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions src/Keys/Tag.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- Variables
local Utility = script.Parent.Utility
local Keys = require(Utility.Keys)
local Keys = require(script.Parent.List)

-- Module

Expand Down
4 changes: 0 additions & 4 deletions src/Keys/Utility/FindProp.luau

This file was deleted.

Loading

0 comments on commit 7cacbcb

Please sign in to comment.