-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDieInstance.ttslua
executable file
·141 lines (114 loc) · 4.87 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require('ge_tts.License')
local Class = require('ge_tts.Class')
local EventManager = require('ge_tts.EventManager')
local Instance = require('ge_tts.Instance')
local Object = require('ge_tts.Object')
local Vector3 = require('ge_tts.Vector3')
---@class ge_tts__DieInstance : ge_tts__Instance
---@class ge_tts__static_DieInstance : ge_tts__static_Instance
---@overload fun(savedState: ge_tts__Instance_SavedState): ge_tts__DieInstance
---@overload fun(object: tts__Object): ge_tts__DieInstance
---@overload fun(guid: string, container: tts__Container): ge_tts__DieInstance
---@overload fun(objectOrSavedState: tts__Object | ge_tts__Instance_SavedState): ge_tts__DieInstance
---@overload fun(objectOrGuidOrSavedState: tts__Object | string | ge_tts__Instance_SavedState, nilOrContainer: nil | tts__Container): ge_tts__DieInstance
local DieInstance = {}
DieInstance.TYPE = 'Die'
setmetatable(DieInstance, {
---@param class self
---@param objectOrGuidOrSavedState tts__Object | string | ge_tts__Instance_SavedState
---@param nilOrContainer nil | tts__Container
__call = function(class, objectOrGuidOrSavedState, nilOrContainer)
local self = --[[---@type ge_tts__DieInstance]] Class.parentConstructor(self, Instance)(
objectOrGuidOrSavedState,
nilOrContainer
)
self.getObject().registerCollisions()
---@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
---@param isRandomizing boolean
local onObjectUpdating = function(object, playerColor, isRandomizing)
if monitoredDice[object] or object.type ~= Object.Type.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 = isRandomizing
monitoredDice[object] = true
local onRollDetected = function()
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)
end
if isRolling then
Wait.frames(function()
onRollDetected()
end)
else
local minRandomizeYVelocity = 1.5 * (math.abs(Physics.getGravity().y) ^ 0.5)
Wait.condition(function()
if isRolling then
onRollDetected()
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 or object.resting then
return true
end
isRolling = not object.isSmoothMoving() and object.held_by_color == nil and (
(object.getRotationValue() ~= initialRotationValue and Vector3.lengthSquared(object.getAngularVelocity()) > MIN_ROLL_ANGULAR_VELOCITY_SQUARED)
or object.getVelocity().y > minRandomizeYVelocity
)
return isRolling
end, 20)
end
end
end
---@param playerColor nil | tts__PlayerColor
---@param object tts__Object
local function onObjectDrop(playerColor, object)
onObjectUpdating(object, playerColor, false)
end
---@param registeredObject tts__Object
local function onObjectCollisionExit(registeredObject)
onObjectUpdating(registeredObject, nil, false)
end
---@param object tts__Object
---@param playerColor tts__PlayerColor
local function onObjectRandomize(object, playerColor)
onObjectUpdating(object, playerColor, true)
end
EventManager.addHandler('onObjectRandomize', onObjectRandomize)
EventManager.addHandler('onObjectDrop', onObjectDrop)
EventManager.addHandler('onObjectCollisionExit', onObjectCollisionExit)
return DieInstance