Advertisement
code_gs

Color wheel example

Jun 17th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. AddCSLuaFile()
  2. ENT.Type = "anim"
  3. ENT.AutomaticFrameAdvance = true
  4.  
  5. ENT.Model = "models/wheel.mdl"
  6. ENT.Segments = 32
  7. ENT.StartFrame = 356
  8. ENT.FrameCount = 360
  9.  
  10. -- Ordered table of colors
  11. ENT.Colors = {
  12.     "red", -- Red is the first segment in rotation
  13.     "black"
  14. }
  15.  
  16. function ENT:Initialize()
  17.     self:SetModel(self.Model)
  18.    
  19.     local uFrameCount = self.FrameCount
  20.    
  21.     if (uFrameCount < 1) then
  22.         error(string.format("expected ENT.FrameCount >= 1, got %d", uFrameCount))
  23.     end
  24.    
  25.     local uStartFrame = self.StartFrame
  26.    
  27.     if (uStartFrame < 0 or uStartFrame > uFrameCount) then
  28.         error(string.format("expected ENT.StartFrame [0, %u), got %d", uFrameCount, uStartFrame))
  29.     end
  30.    
  31.     local uColors = #self.Colors
  32.    
  33.     if (uColors == 0) then
  34.         error("expected #ENT.Colors >= 1, got 0")
  35.     end
  36.    
  37.     -- Cache these since they're constant
  38.    
  39.     -- How many frames is each segment touching the needle
  40.     self.m_nFramesPerSegment = uFrameCount / self.Segments
  41.    
  42.     -- How many frames between StartFrame and FrameCount
  43.     self.m_uFrameDelta = uFrameCount - uStartFrame
  44.    
  45.     -- Number of colors
  46.     self.m_uColors = uColors
  47. end
  48.  
  49. function ENT:StartRotation()
  50.     -- Do maths here for determining how fast/how long your wheel will be travelling
  51.     -- Use ENT.StartFrame to start the wheel in the exact center
  52.     -- Call self:StopRotation() when the spinning is done
  53. end
  54.  
  55. function ENT:StopRotation()
  56.     -- Call this when the rotation has finished
  57.    
  58.     local uStartFrame = self.StartFrame
  59.     local uFrameCount = self.FrameCount
  60.    
  61.     -- Cycle is [0, 1] where 0 is frame 0 (not FrameStart), and 1 is FrameCount - 1
  62.     -- Convert this to frame numbers so the value can be put in a proper range
  63.     local uFrame = math.Remap(self:GetCycle(), 0, 1, 0, uFrameCount - 1)
  64.    
  65.     -- Now adjust the frame to be relative to FrameStart
  66.     -- Frames start at 0 so the last frame is FrameCount - 1
  67.     if (uFrame >= uStartFrame) then
  68.         -- If the frame is greater than StartFrame,
  69.         -- these are the starting frames
  70.         uFrame = uFrame - uStartFrame
  71.     else
  72.         -- If the frame is less than StartFrame,
  73.         -- the value has cycled back around and needs to be raised
  74.         -- with the distance between StartFrame and FrameCount
  75.         uFrame = uFrame + self.m_uFrameDelta
  76.     end
  77.    
  78.     -- Map the localised frame value to a percentage [0, 1)
  79.     local uColor = self:GetWinningColor(math.Remap(uFrame, 0, uFrameCount, 0, 1))
  80.    
  81.     -- Do whatever you need to with winning here
  82.     -- uColor is an index in the Colors table
  83.    
  84.     -- Example
  85.     print(string.format("Landed on color %s!", self.Colors[uColor]))
  86. end
  87.  
  88. -- Takes a number [0, 1)
  89. function ENT:GetWinningColor(uCycle)
  90.     -- Calculates the color at whatever frame the wheel has stopped rotating on
  91.     -- Remap the value one more time into the range [0, SegmentCount) to get the
  92.     -- segment the wheel stopped on, then put it in range of the repeating colors
  93.     return math.floor(math.Remap(uCycle, 0, 1, 0, self.Segments)) % self.m_uColors + 1
  94. end
  95.  
  96. function ENT:Think()
  97.     self:NextThink(CurTime())
  98.    
  99.     return true
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement