Code:
shuffledAmount = 0
frameCounter = 0
diceDropTable = {}
diceSmoothMoving = {}
dieSlotCoords = {
{-4,1.29,-16},
{-2,1.29,-16},
{0,1.29,-16},
{2,1.29,-16},
{4,1.29,-16}
}
openDieSlots = {false, false, false, false, false}
numObjsPlayerPickedUp = 0
function onLoad() end
function onObjectRandomize(_, str)
if shuffledAmount < 1 then
shuffledAmount = countTable(Player[str].getHoldingObjects())
end
end
function onUpdate()
frameCounter = frameCounter + 1
if frameCounter == 4 then checkResting(); end
if frameCounter == 6 then checkSmoothMoving(); end
if frameCounter > 7 then frameCounter = 0; end
end
function checkSmoothMoving()
local o, vPos, vRot, newObject, passKey
local next = next
if next(diceSmoothMoving) ~= nil then
for key, sGuid in pairs(diceSmoothMoving) do
o = getObjectFromGUID(sGuid)
if o.isSmoothMoving() == false then
for j = 1,5 do
if openDieSlots[j] == sGuid then passKey = j; break; end
end
vPos = o.getPosition(); vRot = o.getRotation()
destroyObject(o)
table.remove(diceSmoothMoving, key)
newObject = spawnObject({ type = "Die_6", position = {x=vPos.x, y=vPos.y, z=vPos.z}, rotation = {x=vRot.x,y=vRot.y,z=vRot.z},
sound = false, callback = "assignName", callback_owner = Global,
params = { name = "Example Dice", iKey = passKey }
})
end
end
end
end
function checkResting()
local oDice
local next = next
if next(diceDropTable) ~= nil then
for key, sGuid in pairs(diceDropTable) do
oDice = getObjectFromGUID(sGuid)
if oDice.resting == true then
for i = 1,5 do
if openDieSlots[i] == false then
openDieSlots[i] = sGuid
oDice.rotate({x=0, y=(360 - oDice.getRotation().y), z=0})
oDice.setPositionSmooth(dieSlotCoords[i],false,false)
break
end
end
table.insert(diceSmoothMoving, sGuid)
table.remove(diceDropTable, key)
end
end
end
end
function onObjectDrop(player_color, dropped_object)
if shuffledAmount > 0 and dropped_object.getName() == "Example Dice" then
table.insert(diceDropTable, dropped_object.guid)
end
shuffledAmount = shuffledAmount - 1
end
function onObjectPickUp(p, o)
for i = 1,5 do
if openDieSlots[i] == o.guid then openDieSlots[i] = false; break; end
end
end
function assignName(o, t)
o.setName(t.name); openDieSlots[t.iKey] = o.guid;
end
function countTable(t)
local c = 0; for _ in pairs(t) do c = c + 1; end return c
end