Advertisement
Kijan

FW Dice

May 21st, 2020
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.61 KB | None | 0 0
  1. function onLoad()
  2.    lifespan = 10
  3.    spin_speed = 3
  4.    rise_speed = 3
  5.    grow_speed = 3
  6.    font_size = 3.4
  7.    flash_max = true
  8.    sound_max = false
  9.    sound_min = false
  10.    log_chat = true
  11.    parent_guid = "card01"
  12.    roll_active = false
  13.     firedByDiceRoller = false
  14.    rv = self.getRotationValues() or false
  15.     playerColorToDiceColor = {
  16.         Red = {r = 219, g = 26, b = 24},
  17.         Purple = {r = 160, g = 32, b = 240},
  18.         Pink = {r = 245, g = 112, b = 206},
  19.         Yellow = {r = 234, g = 180, b = 0},
  20.         Green = {r = 49, g = 179, b = 43},
  21.         Blue = {r = 31, g = 136, b = 255},
  22.         Orange = {r = 244, g = 100, b = 29}
  23.     }
  24. end
  25.  
  26. function onRandomize(a)
  27.     trigger(a)
  28. end
  29.  
  30. function onUpdate()
  31.     if not firedByDiceRoller then return end
  32.    if not self.resting and not roll_active then
  33.         trigger(firedByDiceRoller)
  34.    end
  35. end
  36.  
  37. function trigger(a)
  38.    if roll_active then
  39.       return false
  40.    end
  41.     local selfColor = self.getColorTint()
  42.     local playerColor = playerColorToDiceColor[a]
  43.  
  44.     if playerColor == nil then
  45.         return false
  46.     end
  47.  
  48.     local dif = {
  49.         r = math.abs(selfColor.r - (playerColor.r / 255)),
  50.         g = math.abs(selfColor.g - (playerColor.g / 255)),
  51.         b = math.abs(selfColor.b - (playerColor.b / 255))
  52.     }
  53.     if dif.r > 0.01 or dif.g > 0.01 or dif.b > 0.01 then
  54.         return false
  55.     end
  56.  
  57.    roll_active = true
  58.    Wait.condition(
  59.       function()
  60.          roll_active = false
  61.             firedByDiceRoller = false
  62.          local b = self.getRotationValue() or false
  63.          if not b or not rv then
  64.             log("Dice "..self.guid.." does not have a valid rotation value set! Unable to show roll value.")
  65.             return false
  66.          end
  67.          local c = self.getPosition() + Vector({0, 1 + font_size / 5, 0})
  68.          local d = spawnObject({type = "3DText", position = c, sound = true})
  69.          d.TextTool.setValue(tostring(b))
  70.          d.TextTool.setFontColor(self.getColorTint())
  71.          d.TextTool.setFontSize(font_size * 24)
  72.          Wait.frames(
  73.             function()
  74.                d.interactable = false
  75.                d.auto_raise = false
  76.                rise(d, c)
  77.                spin(d, {0, spin_speed * 18, 0})
  78.                grow(d, font_size * 24)
  79.                log("Score is "..b)
  80.                if log_chat then
  81.                   local e = a
  82.                   if self.getName() and self.getName() ~= "" then
  83.                      e = e.." | "..self.getName()
  84.                   end
  85.                   printToAll("["..e.."] "..Player[a].steam_name.." rolled a "..b, a)
  86.                end
  87.                if(sound_max or flash_max)and b == rv[#rv].value then
  88.                   if flash_max then
  89.                      flash(self)
  90.                      flash(d)
  91.                   end
  92.                   if sound_max and getObjectFromGUID(parent_guid) then
  93.                      getObjectFromGUID(parent_guid).AssetBundle.playTriggerEffect(0)
  94.                   end
  95.                end
  96.                if b == 1 and sound_min and getObjectFromGUID(parent_guid) then
  97.                   getObjectFromGUID(parent_guid).AssetBundle.playTriggerEffect(1)
  98.                end
  99.                Wait.time(
  100.                   function()
  101.                      d.destruct()
  102.                   end,
  103.                lifespan)
  104.             end,
  105.          1)
  106.       end,
  107.       function()
  108.          return self.resting
  109.       end,
  110.       30,
  111.       function()
  112.          log("Timeout exceeded waiting for dice to come to a stop")
  113.          roll_active = false
  114.             firedByDiceRoller = false
  115.       end
  116.    )
  117. end
  118.  
  119. function rise(d, c)
  120.    if not getObjectFromGUID(d.guid) then
  121.       return false
  122.    end
  123.    d.setPosition(c)c[2] = c[2] + rise_speed / 100
  124.    Wait.frames(function()rise(d, c)
  125.    end, 1)
  126. end
  127.  
  128. function spin(d, f)
  129.    if not getObjectFromGUID(d.guid)then
  130.       return false
  131.    end
  132.    d.setRotationSmooth(f, false, true)f[2] = f[2] + spin_speed * 5
  133.    Wait.time(function()
  134.       spin(d, f)
  135.    end, 0.5)
  136. end
  137.  
  138. function grow(d, font_size)
  139.    if not getObjectFromGUID(d.guid)then
  140.       return false
  141.    end
  142.    d.TextTool.setFontSize(font_size)
  143.    Wait.time(function()
  144.       grow(d, font_size * (grow_speed + 100) / 100)
  145.    end, 0.1)
  146. end
  147.  
  148. function flash(d, g)
  149.    if not getObjectFromGUID(d.guid)or g and g > 20 then
  150.       return false
  151.    end
  152.    local a = g or 1
  153.    local h = self.getColorTint()
  154.    if a%2 == 0 then
  155.       h = randomColor()
  156.    end
  157.    if d.tag == "3D Text" then
  158.       d.TextTool.setFontColor(h)
  159.    else
  160.       d.highlightOn(h, 0.1)
  161.    end
  162.    Wait.time(function()
  163.       flash(d, a + 1)
  164.    end, 0.2)
  165. end
  166.  
  167. function randomColor()
  168.    local i = math.random
  169.    return{i(255) / 255, i(255) / 255, i(255) / 255}
  170.  
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement