@quenty/spring 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/spring@2.1.0...@quenty/spring@2.2.0) (2021-12-18)
7
+
8
+
9
+ ### Features
10
+
11
+ * Allow transformation of Color3 into a linear value for interpolation ([a770697](https://github.com/Quenty/NevermoreEngine/commit/a7706970e4de8969bdd0f5a8b54e1803c89f9e87))
12
+
13
+
14
+
15
+
16
+
6
17
  # [2.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/spring@2.0.0...@quenty/spring@2.1.0) (2021-09-22)
7
18
 
8
19
 
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2014 Quenty
3
+ Copyright (c) 2014-2021 Quenty
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/spring",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Spring implementation for Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -28,5 +28,8 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "6288c63115f31544fce6663d018826e81ef01c7f"
31
+ "dependencies": {
32
+ "@quenty/loader": "^3.1.1"
33
+ },
34
+ "gitHead": "6eb398e9fb81191f0815885f807ab0cb3bb9bad1"
32
35
  }
@@ -0,0 +1,96 @@
1
+ ---
2
+ -- @classmod LinearValue
3
+ -- @author Quenty
4
+
5
+ local LinearValue = {}
6
+ LinearValue.ClassName = "LinearValue"
7
+ LinearValue.__index = LinearValue
8
+
9
+ function LinearValue.new(constructor, values)
10
+ return setmetatable({
11
+ _constructor = constructor;
12
+ _values = values;
13
+ }, LinearValue)
14
+ end
15
+
16
+ function LinearValue.isLinear(value)
17
+ return type(value) == "table" and getmetatable(value) == LinearValue
18
+ end
19
+
20
+ function LinearValue:ToBaseValue()
21
+ return self._constructor(unpack(self._values))
22
+ end
23
+
24
+ local function operation(func)
25
+ return function(a, b)
26
+ if LinearValue.isLinear(a) and LinearValue.isLinear(b) then
27
+ assert(a._constructor == b._constructor, "a is not the same type of linearValue as b")
28
+
29
+ local values = {}
30
+ for i=1, #a._values do
31
+ values[i] = func(a._values[i], b._values[i])
32
+ end
33
+ return LinearValue.new(a._constructor, values)
34
+ elseif LinearValue.isLinear(a) then
35
+ if type(b) == "number" then
36
+ local values = {}
37
+ for i=1, #a._values do
38
+ values[i] = func(a._values[i], b)
39
+ end
40
+ return LinearValue.new(a._constructor, values)
41
+ else
42
+ error("Bad type (b)")
43
+ end
44
+ elseif LinearValue.isLinear(b) then
45
+ if type(a) == "number" then
46
+ local values = {}
47
+ for i=1, #b._values do
48
+ values[i] = func(a, b._values[i])
49
+ end
50
+ return LinearValue.new(b._constructor, values)
51
+ else
52
+ error("Bad type (a)")
53
+ end
54
+ else
55
+ error("Neither value is a linearValue")
56
+ end
57
+ end
58
+ end
59
+
60
+ function LinearValue:GetMagnitude()
61
+ local dot = 0
62
+ for i=1, #self._values do
63
+ local value = self._values[i]
64
+ dot = dot + value*value
65
+ end
66
+ return math.sqrt(dot)
67
+ end
68
+
69
+ function LinearValue:__index(key)
70
+ if LinearValue[key] then
71
+ return LinearValue[key]
72
+ elseif key == "magnitude" then
73
+ return self:GetMagnitude()
74
+ else
75
+ return nil
76
+ end
77
+ end
78
+
79
+ LinearValue.__add = operation(function(a, b)
80
+ return a + b
81
+ end)
82
+
83
+ LinearValue.__sub = operation(function(a, b)
84
+ return a - b
85
+ end)
86
+
87
+ LinearValue.__mul = operation(function(a, b)
88
+ return a * b
89
+ end)
90
+
91
+ LinearValue.__div = operation(function(a, b)
92
+ return a / b
93
+ end)
94
+
95
+
96
+ return LinearValue
@@ -4,6 +4,9 @@
4
4
 
5
5
  local EPSILON = 1e-6
6
6
 
7
+ local require = require(script.Parent.loader).load(script)
8
+ local LinearValue = require("LinearValue")
9
+
7
10
  local SpringUtils = {}
8
11
 
9
12
  function SpringUtils.animating(spring, epsilon)
@@ -16,7 +19,7 @@ function SpringUtils.animating(spring, epsilon)
16
19
  if type(target) == "number" then
17
20
  animating = math.abs(spring.Position - spring.Target) > epsilon
18
21
  or math.abs(spring.Velocity) > epsilon
19
- elseif typeof(target) == "Vector3" then
22
+ elseif typeof(target) == "Vector3" or LinearValue.isLinear(target) then
20
23
  animating = (spring.Position - spring.Target).magnitude > epsilon
21
24
  or spring.Velocity.magnitude > epsilon
22
25
  else
@@ -40,4 +43,20 @@ function SpringUtils.getVelocityAdjustment(velocity, dampen, speed)
40
43
  return velocity*(2*dampen/speed)
41
44
  end
42
45
 
46
+ function SpringUtils.toLinearIfNeeded(value)
47
+ if typeof(value) == "Color3" then
48
+ return LinearValue.new(Color3.new, {value.r, value.g, value.b})
49
+ else
50
+ return value
51
+ end
52
+ end
53
+
54
+ function SpringUtils.fromLinearIfNeeded(value)
55
+ if LinearValue.isLinear(value) then
56
+ return value:ToBaseValue()
57
+ else
58
+ return value
59
+ end
60
+ end
61
+
43
62
  return SpringUtils
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }