forked from Benjamin-Dobell/ge_tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDieInstance.ttslua
109 lines (88 loc) · 3.96 KB
/
DieInstance.ttslua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
local EventManager = require('ge_tts.EventManager')
local Instance = require('ge_tts.Instance')
local Object = require('ge_tts.Object')
local ObjectUtils = require('ge_tts.ObjectUtils')
local Vector3 = require('ge_tts.Vector3')
---@class ge_tts__DieInstance : ge_tts__Instance
---@class ge_tts__static_DieInstance
---@overload fun(savedState: ge_tts__Instance_SavedState): ge_tts__Instance
---@overload fun(object: tts__Object): ge_tts__Instance
---@overload fun(guid: string, container: tts__Container): ge_tts__Instance
---@overload fun(objectOrSavedState: tts__Object | ge_tts__Instance_SavedState): ge_tts__Instance
---@overload fun(objectOrGuidOrSavedState: tts__Object | string | ge_tts__Instance_SavedState, nilOrContainer: nil | tts__Container): ge_tts__Instance
---@field TYPE string
local DieInstance = {}
DieInstance.TYPE = 'Die'
setmetatable(DieInstance, {
---@param objectOrGuidOrSavedState tts__Object | string | ge_tts__Instance_SavedState
---@param nilOrContainer nil | tts__Container
__call = function(_, objectOrGuidOrSavedState, nilOrContainer)
local self = --[[---@type ge_tts__DieInstance]] Instance(objectOrGuidOrSavedState, nilOrContainer)
ObjectUtils.getSpawnedObject(self.getObject(), function(object)
object.registerCollisions()
end)
---@return string
function self.getType()
return DieInstance.TYPE
end
---@param value number | string
---@param playerColor nil | tts__PlayerColor
function self.onRolled(value, playerColor)
end
---@param value number | string
---@param playerColor nil | tts__PlayerColor
function self.onRotationValueUpdated(value, playerColor)
end
return self
end,
__index = Instance,
})
local MIN_ROLL_ANGULAR_VELOCITY_SQUARED = 2 * math.pi * math.pi
---@type table<tts__Object, true>
local monitoredDice = {}
---@param object tts__Object
---@param playerColor nil | tts__PlayerColor
local onObjectUpdating = function(object, playerColor)
if monitoredDice[object] or object.tag ~= Object.Tag.Die then
return
end
local instance = Instance.getOneInstance(object)
if instance and (--[[---@type ge_tts__DieInstance]] instance).onRolled ~= nil then
local dieInstance = (--[[---@type ge_tts__DieInstance]] instance)
local initialRotationValue = object.getRotationValue()
local isRolling = false
monitoredDice[object] = true
Wait.condition(function()
if isRolling then
Wait.condition(function()
monitoredDice[object] = nil
if object ~= nil then
local value = object.getRotationValue()
dieInstance.onRolled(value, playerColor)
dieInstance.onRotationValueUpdated(value, playerColor)
end
end, function()
return object == nil or object.resting
end)
else
if object ~= nil then
local value = object.getRotationValue()
if value ~= initialRotationValue then
dieInstance.onRotationValueUpdated(value, playerColor)
end
end
monitoredDice[object] = nil
end
end, function()
if object == nil then
return true
end
isRolling = object.getRotationValue() ~= initialRotationValue and Vector3.lengthSquared(object.getAngularVelocity()) > MIN_ROLL_ANGULAR_VELOCITY_SQUARED
return isRolling or object.resting
end, 20)
end
end
EventManager.addHandler('onObjectRandomize', onObjectUpdating)
EventManager.addHandler('onObjectDrop', function(playerColor, object) onObjectUpdating(object, playerColor) end)
EventManager.addHandler('onObjectCollisionExit', function(registeredObject) onObjectUpdating(registeredObject, nil) end)
return DieInstance