Advertisement
Phil_N

TabletopDieRegister

Apr 9th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. dieCount = 0
  2. dice = {}
  3. results = {}
  4.  
  5. --This detects when a die6 is placed on the notecard and registers it.
  6. --    A die can only be registered once.
  7.  
  8. function onCollisionEnter(collision_info)
  9.     l_colObj = collision_info.collision_object
  10.    
  11.     if string.find(l_colObj.name, "Die_6") == nil then
  12.         print("Can only register 6-sided dice")
  13.         return
  14.     end
  15.     if dieCount > 0 then
  16.         for i = 1, dieCount do
  17.             if l_colObj.guid == dice[i].guid then
  18.                 print("This die is already registered!")
  19.                 return
  20.             end
  21.         end
  22.     end
  23.    
  24.     dieCount = dieCount + 1
  25.  
  26.     extendResults()
  27.     dice[dieCount] = l_colObj
  28.     infiltrateDie(l_colObj)
  29.  
  30.     --l_colObj.colEnter = l_colObj.onCollisionEnter
  31.     --print("dannach")
  32.     --function l_colObj:onCollisionEnter (collision_info)
  33.     --    print("Collsion")
  34.     --    l_colObj.colEnter(collision_info)
  35.     --end
  36.    
  37.     print("New die registered, " .. (dieCount) .. " dice registered")
  38.  
  39.  
  40. end
  41.  
  42. function onLoad()
  43.  
  44.     refreshDescription()
  45. end
  46.  
  47. function onDiceRolled(dice)
  48.     dice.setVar("rolledRecently", true)
  49. end
  50.  
  51. function infiltrateDie(die)
  52.     stuffToAdd = "getObjectFromGUID(\"" .. self.getGUID() .. "\").call(\"onDiceRolled\", self)"
  53.     die_script = die.getLuaScript()
  54.     posi = string.find(die_script, "function onRandomize")
  55.  
  56.     if(posi != nil) then
  57.         print(string.sub(die_script, posi, posi + 20))
  58.     else
  59.         die_script = die_script .. "\nfunction onRandomize() " .. stuffToAdd .. " end"
  60.          
  61.         die.setLuaScript(die_script)
  62.         die.reload() --only call once!
  63.     end
  64.  
  65. end
  66.  
  67. function update()
  68.     if dieCount == 0 then
  69.         return -- Returns when no die has been registered yet
  70.     end
  71.     if(dice[1].getVar("rolledRecently")) then
  72.         if(dice[1].resting) then
  73.  
  74.             dice[1].setVar("rolledRecently", false)
  75.             sum = 0
  76.  
  77.             for i = 1, dieCount do
  78.                 sum = sum + dice[i].getValue()
  79.             end
  80.  
  81.             results[sum] = results[sum] + 1
  82.  
  83.             refreshDescription()
  84.         end
  85.     end
  86. end
  87.  
  88. function extendResults()
  89.     for i = ((dieCount-1)*6)+1, (6*dieCount) do
  90.         results[i] = 0
  91.     end
  92. end
  93.  
  94. function refreshDescription()
  95.     if dieCount == 0 then
  96.         return
  97.     end
  98.     descr = ""
  99.     for i = 1, (6*dieCount) do
  100.         descr = descr .. i .. ": " .. results[i] .. "\n"
  101.     end
  102.     self.setDescription(descr)
  103. end
  104.  
  105. function createBaseXml()
  106.     height = 90 + (dieCount*40)
  107.     xmlTable = {
  108.        
  109.     }
  110.        
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement