forked from Benjamin-Dobell/ge_tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropZone.ttslua
232 lines (187 loc) · 8.61 KB
/
DropZone.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local TableUtils = require('ge_tts.TableUtils')
local Vector3 = require('ge_tts.Vector3')
local Zone = require('ge_tts.Zone')
local MAX_DROP_VELOCITY_SQUARED = 5 * 5 -- 5 in/sec
---@class ge_tts__DropZone : ge_tts__Zone
---@shape ge_tts__DropZone_SavedState : ge_tts__Zone_SavedState
---@field occupantScale number
---@field rotationEnabled boolean
---@field rotationAxis number
---@field facing ge_tts__DropZone_Facing
---@field dropOffset tts__CharVectorShape
---@class ge_tts__static_DropZone : ge_tts__static_Zone
---@overload fun(position: tts__VectorShape, rotation: tts__VectorShape, scale: tts__VectorShape): ge_tts__DropZone
---@overload fun(position: tts__VectorShape, rotation: tts__VectorShape, scale: tts__VectorShape, occupantScale: nil | number): ge_tts__DropZone
---@overload fun(trigger: tts__ScriptingTrigger): ge_tts__DropZone
---@overload fun(trigger: tts__ScriptingTrigger, occupantScale: nil | number): ge_tts__DropZone
---@overload fun(savedState: ge_tts__DropZone_SavedState): ge_tts__DropZone
---@overload fun(positionTriggerOrSavedState: tts__VectorShape | tts__ScriptingTrigger | ge_tts__DropZone_SavedState, nilOrRotationOrOccupantScale: nil | number | tts__VectorShape, nilOrScale: nil | tts__VectorShape, nilOrOccupantScale: nil | number): ge_tts__DropZone
local DropZone = {}
DropZone.TYPE = 'DropZone'
DropZone.Facing = {
UP = 1,
DOWN = 2,
DROPPED = 3,
}
DropZone.RotationAxis = {
X = 1,
Y = 2,
Z = 4,
All = 7
}
---@alias ge_tts__DropZone_Facing 1 | 2 | 3
setmetatable(DropZone, TableUtils.merge(getmetatable(Zone), {
---@param positionTriggerOrSavedState tts__VectorShape | tts__ScriptingTrigger | ge_tts__DropZone_SavedState
---@param nilOrRotationOrOccupantScale nil | number | tts__VectorShape
---@param nilOrScale nil | tts__VectorShape
---@param nilOrOccupantScale nil | number @Optional - occupant's desired X-axis scale. When scaling is applied it is applied to all dimensions i.e. aspect ratio is preserved. `nil` means dropped objects will not have their scale altered.
__call = function(_, positionTriggerOrSavedState, nilOrRotationOrOccupantScale, nilOrScale, nilOrOccupantScale)
local triggerProvided = type(positionTriggerOrSavedState) == 'userdata'
local self = --[[---@type ge_tts__DropZone]] Zone(
positionTriggerOrSavedState,
not triggerProvided and --[[---@type nil | tts__VectorShape]] nilOrRotationOrOccupantScale or nil,
nilOrScale
)
---@type nil | number
local occupantScale
---@type boolean
local rotationEnabled = true
---@type number
local rotationAxis = DropZone.RotationAxis.All
---@type ge_tts__DropZone_Facing
local facing = DropZone.Facing.UP
---@type ge_tts__Vector3
local dropOffset = Vector3()
---@return nil | number @occupant's desired X-axis scale
function self.getOccupantScale()
return occupantScale
end
---@return number
function self.getRotationAxis()
return rotationAxis
end
---@param axis number
function self.setRotationAxis(axis)
rotationAxis = axis
self.invalidateSavedState()
end
---@return boolean
function self.getRotationEnabled()
return rotationEnabled
end
---@param enabled boolean
function self.setRotationEnabled(enabled)
rotationEnabled = enabled
self.invalidateSavedState()
end
---@return ge_tts__DropZone_Facing
function self.getFacing()
return facing
end
---@param face ge_tts__DropZone_Facing
function self.setFacing(face)
facing = face
self.invalidateSavedState()
end
---@return ge_tts__Vector3
function self.getDropOffset()
return Vector3(dropOffset)
end
---@param offset tts__VectorShape
function self.setDropOffset(offset)
dropOffset = Vector3(offset)
self.invalidateSavedState()
end
function self.getType()
return DropZone.TYPE
end
--- Called when a player attempts to drop an object within this zone. A drop zone will
--- ignore objects that are dropped whilst moving at a high velocity, as it's assumed the
--- player is trying to throw the object, not drop it in this zone.
---@param colorName tts__PlayerColor @Color of the TTS player that dropped the TTS object.
---@param object tts__Object
---@return ge_tts__Zone_FilterResult
function self.filterObject(colorName, object)
-- TODO: Once released, use object.getSmoothMoveTargetPosition() API. For now, we assume if the object is
-- smooth moving that it's explicitly doing so into the zone i.e. we shouldn't ignore it.
if object.isSmoothMoving() or Vector3.lengthSquared(object.getVelocity()) < MAX_DROP_VELOCITY_SQUARED then
return Zone.FilterResult.ACCEPT
else
return Zone.FilterResult.IGNORE
end
end
local superOnDrop = self.onDrop
--- Called when a TTS object is dropped within this DropZone.
---@param colorName nil | tts__PlayerColor @Color of the TTS player that dropped the TTS object.
---@param object tts__Object @The object that was dropped.
function self.onDrop(colorName, object)
superOnDrop(colorName, object)
local objectRotation = object.getRotation()
local position = (self.getObject()).positionToWorld(dropOffset)
object.setPositionSmooth({ position.x, math.max(position.y, object.getPosition().y), position.z })
if rotationEnabled then
local rotation = self.getRotation()
if bit32.band(rotationAxis, DropZone.RotationAxis.X) ~= 0 then
objectRotation.x = rotation.x
end
if bit32.band(rotationAxis, DropZone.RotationAxis.Y) ~= 0 then
objectRotation.y = rotation.y
end
if bit32.band(rotationAxis, DropZone.RotationAxis.Z) ~= 0 then
if facing == DropZone.Facing.DROPPED then
objectRotation.z = (objectRotation.z + 360) % 360
if objectRotation.z >= 270 then
objectRotation.z = 360
elseif objectRotation.z <= 90 then
objectRotation.z = 0
else
objectRotation.z = 180
end
else
objectRotation.z = facing == DropZone.Facing.UP and 0 or 180
end
end
object.setRotationSmooth(objectRotation)
end
if occupantScale then
object.scale((--[[---@not nil]] occupantScale) / object.getScale()[1])
end
end
local superInsertOccupyingObject = self.insertOccupyingObject
--- Used programmatically when `object` should be made a direct occupant, but not dropped by a player.
---@param object tts__Object @The object that was dropped.
function self.insertOccupyingObject(object)
if occupantScale then
object.scale((--[[---@not nil]] occupantScale) / object.getScale()[1])
end
superInsertOccupyingObject(object)
end
---@type fun(): ge_tts__Zone_SavedState
local superSave = self.save
---@return ge_tts__DropZone_SavedState
function self.save()
return --[[---@type ge_tts__DropZone_SavedState]] TableUtils.merge(superSave(), {
occupantScale = occupantScale,
rotationEnabled = rotationEnabled,
rotationAxis = rotationAxis,
facing = facing,
dropOffset = dropOffset.toData()
})
end
if DropZone.isSavedState(positionTriggerOrSavedState) then
local data = --[[---@type ge_tts__DropZone_SavedState]] positionTriggerOrSavedState
occupantScale = data.occupantScale
rotationEnabled = data.rotationEnabled
rotationAxis = data.rotationAxis
facing = data.facing
dropOffset = Vector3(data.dropOffset)
elseif triggerProvided then
occupantScale = --[[---@type nil | number]] nilOrRotationOrOccupantScale
else
occupantScale = nilOrOccupantScale
end
return self
end,
__index = Zone,
}))
return DropZone