Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AddCSLuaFile()
- ENT.Type = "anim"
- ENT.AutomaticFrameAdvance = true
- ENT.Model = "models/wheel.mdl"
- ENT.Segments = 32
- ENT.StartFrame = 356
- ENT.FrameCount = 360
- -- Ordered table of colors
- ENT.Colors = {
- "red", -- Red is the first segment in rotation
- "black"
- }
- function ENT:Initialize()
- self:SetModel(self.Model)
- local uFrameCount = self.FrameCount
- if (uFrameCount < 1) then
- error(string.format("expected ENT.FrameCount >= 1, got %d", uFrameCount))
- end
- local uStartFrame = self.StartFrame
- if (uStartFrame < 0 or uStartFrame > uFrameCount) then
- error(string.format("expected ENT.StartFrame [0, %u), got %d", uFrameCount, uStartFrame))
- end
- local uColors = #self.Colors
- if (uColors == 0) then
- error("expected #ENT.Colors >= 1, got 0")
- end
- -- Cache these since they're constant
- -- How many frames is each segment touching the needle
- self.m_nFramesPerSegment = uFrameCount / self.Segments
- -- How many frames between StartFrame and FrameCount
- self.m_uFrameDelta = uFrameCount - uStartFrame
- -- Number of colors
- self.m_uColors = uColors
- end
- function ENT:StartRotation()
- -- Do maths here for determining how fast/how long your wheel will be travelling
- -- Use ENT.StartFrame to start the wheel in the exact center
- -- Call self:StopRotation() when the spinning is done
- end
- function ENT:StopRotation()
- -- Call this when the rotation has finished
- local uStartFrame = self.StartFrame
- local uFrameCount = self.FrameCount
- -- Cycle is [0, 1] where 0 is frame 0 (not FrameStart), and 1 is FrameCount - 1
- -- Convert this to frame numbers so the value can be put in a proper range
- local uFrame = math.Remap(self:GetCycle(), 0, 1, 0, uFrameCount - 1)
- -- Now adjust the frame to be relative to FrameStart
- -- Frames start at 0 so the last frame is FrameCount - 1
- if (uFrame >= uStartFrame) then
- -- If the frame is greater than StartFrame,
- -- these are the starting frames
- uFrame = uFrame - uStartFrame
- else
- -- If the frame is less than StartFrame,
- -- the value has cycled back around and needs to be raised
- -- with the distance between StartFrame and FrameCount
- uFrame = uFrame + self.m_uFrameDelta
- end
- -- Map the localised frame value to a percentage [0, 1)
- local uColor = self:GetWinningColor(math.Remap(uFrame, 0, uFrameCount, 0, 1))
- -- Do whatever you need to with winning here
- -- uColor is an index in the Colors table
- -- Example
- print(string.format("Landed on color %s!", self.Colors[uColor]))
- end
- -- Takes a number [0, 1)
- function ENT:GetWinningColor(uCycle)
- -- Calculates the color at whatever frame the wheel has stopped rotating on
- -- Remap the value one more time into the range [0, SegmentCount) to get the
- -- segment the wheel stopped on, then put it in range of the repeating colors
- return math.floor(math.Remap(uCycle, 0, 1, 0, self.Segments)) % self.m_uColors + 1
- end
- function ENT:Think()
- self:NextThink(CurTime())
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement