stom66

Tts Lua | Dice text tool

Oct 8th, 2023
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | Source Code | 0 0
  1. function onLoad()
  2.     -- Set some default values for the dice behaviour
  3.     lifespan     = 3           
  4.     spin_speed   = 3           
  5.     rise_speed   = 3
  6.     grow_speed   = 3
  7.     font_size    = 3
  8.     flash_max    = true
  9.     sound_max    = true
  10.     sound_min    = false
  11.     log_chat     = true
  12.     parent_guid  = "card01"
  13.     roll_active  = false
  14.  
  15.     --get the table of "rotational values", which can be set with the F8 Gizmo Tool
  16.     rv = self.getRotationValues() or false
  17. end
  18.  
  19. -- Have the onDrop and onRandomise functions trigger our effect
  20. function onDrop(c) trigger(c) end
  21. function onRandomize(c) trigger(c) end
  22.  
  23. -- Trigger effect - this is the main function which is called when dropped or rolled
  24. function trigger(c)
  25.  
  26.     --it's possible to trigger the effect multiple times, before the dice stops rolling, so we check and then set a flag to prevent multiple triggers
  27.     if roll_active then return false end
  28.     roll_active = true
  29.  
  30.     --wait until the dice stops rolling before activating the trigger
  31.     Wait.condition(function()
  32.         --main body of the function to be run when the dice stops rolling
  33.  
  34.         --disable flag
  35.         roll_active = false
  36.  
  37.         --get the value of the current rotation and assign is to var s
  38.         local s = self.getRotationValue() or false
  39.  
  40.         --check we have a valid rotation value to report, if not we exit
  41.         if not s or not rv then
  42.             log("Dice "..self.guid.." does not have a valid rotation value set! Unable to show roll value.")
  43.             return false
  44.         end
  45.  
  46.         --get current position of dice
  47.         local pos    = self.getPosition() + Vector({0, 1+(font_size/5), 0})
  48.  
  49.         --spawn a TextTool object
  50.         local obj    = spawnObject({
  51.             type     = "3DText",
  52.             position = pos,
  53.             sound    = true,
  54.         })
  55.  
  56.         --customise the textTool ovject with the relevant style/values
  57.         obj.TextTool.setValue(tostring(s))
  58.         obj.TextTool.setFontColor(self.getColorTint())
  59.         obj.TextTool.setFontSize(font_size*24)
  60.  
  61.         --wait a single frame before continuing, to allow the TextTool object to spawn
  62.         Wait.frames(function()
  63.             --make it non-interactable
  64.             obj.interactable = false
  65.             obj.auto_raise   = false
  66.  
  67.             --raise TextTool into the air (function is below)
  68.             rise(obj, pos)
  69.  
  70.             --spin TextTool (function is below)
  71.             spin(obj, {0,spin_speed*18,0})
  72.  
  73.             --grow TextTool (or shrink, if given a negative value at the top) (function is below)
  74.             grow(obj, font_size*24)
  75.  
  76.             --print value of s to log
  77.             log("Score is "..s)
  78.  
  79.             --print to chat, if enabled
  80.             if log_chat then
  81.                 local name = c
  82.                 if self.getName() and self.getName() ~= "" then
  83.                     name = name.." | "..self.getName()
  84.                 end
  85.                 printToAll("["..name.."] "..Player[c].steam_name.." rolled a "..s, c)
  86.             end
  87.  
  88.             --if s is max value, trigger sound or flash
  89.             if (sound_max or flash_max) and s==rv[#rv].value then
  90.                 --flash object, if enabled
  91.                 if flash_max then
  92.                     flash(self)
  93.                     flash(obj)
  94.                 end
  95.                 --play sound, if enabled and parent containing sound object is enabled
  96.                 if sound_max and getObjectFromGUID(parent_guid) then
  97.                     getObjectFromGUID(parent_guid).AssetBundle.playTriggerEffect(0)
  98.                 end
  99.             end
  100.  
  101.             -- if s is min value, trigger fail sound
  102.             if s == 1 and sound_min and getObjectFromGUID(parent_guid) then
  103.                 getObjectFromGUID(parent_guid).AssetBundle.playTriggerEffect(1)
  104.             end
  105.  
  106.             --after the length of lifetime has passed, destroy the TextTool object
  107.             Wait.time(function()
  108.                 obj.destruct()
  109.             end, lifespan)
  110.         end, 1)
  111.     end,
  112.     --condition to check for the wait function
  113.     function() return self.resting end,
  114.     --time to wait for the wait function
  115.     30,
  116.     --function to call if wait time is exceeded
  117.     function()
  118.         log("Timeout exceeded waiting for dice to come to a stop")
  119.         roll_active = false
  120.     end)
  121. end
  122.  
  123. -- below are various utility functions, used by the main function above. they are mostly self-explanatory
  124. function rise(obj, pos)
  125.     if not getObjectFromGUID(obj.guid) then return false end
  126.     obj.setPosition(pos)
  127.     pos[2] = pos[2] + (rise_speed/100)
  128.     Wait.frames(function() rise(obj, pos) end, 1)
  129. end
  130.  
  131. function spin(obj, rot)
  132.     if not getObjectFromGUID(obj.guid) then return false end
  133.     obj.setRotationSmooth(rot, false, true)
  134.     rot[2] = rot[2] + spin_speed*5
  135.     Wait.time(function() spin(obj, rot) end, 0.5)
  136. end
  137.  
  138. function grow(obj, font_size)
  139.     if not getObjectFromGUID(obj.guid) then return false end
  140.     obj.TextTool.setFontSize(font_size)
  141.     Wait.time(function() grow(obj, font_size*((grow_speed+100)/100)) end, 0.1)
  142. end
  143.  
  144. function flash(obj, i)
  145.     if not getObjectFromGUID(obj.guid) or (i and i > 20) then return false end
  146.     local c = i or 1
  147.     local col = self.getColorTint()
  148.     if c % 2 == 0 then
  149.         col = randomColor()
  150.     end
  151.     if obj.tag == "3D Text" then
  152.         obj.TextTool.setFontColor(col)
  153.     else
  154.         obj.highlightOn(col, 0.1)
  155.     end
  156.     Wait.time(function() flash(obj, c+1) end, 0.2)
  157. end
  158.  
  159. function randomColor()
  160.     local r = math.random
  161.     return {r(255)/255, r(255)/255, r(255)/255}
  162. end
Advertisement
Add Comment
Please, Sign In to add comment