File size: 927 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
--[[

  IntervalFunction.lua - Interval functions that run every x seconds or ticks

  Examples:

    IntervalFunction.new(5, function()
      -- run no more often than once every 5 seconds
      -- chill interval to check on states that don't
      -- need to feel so snappy
    end),

    IntervalFunction.new(-15, function ()
      -- run every 15 ticks, ~0.5 seconds
      -- maybe a good interval for updating some states
      -- in a way that feels responsive, like selections
    end)

]]--

IntervalFunction = Polo {
  new = function(interval, f)
    return {
      interval = interval,
      f = f,
      last = 0
    }
  end
}

function IntervalFunction:react(time)
  if self.interval >= 0 then
    if time - self.last >= self.interval then
      self.f()
      self.last = time
    end
  else
    self.last = self.last - 1

    if self.last < self.interval then
      self.f()
      self.last = 0
    end
  end
end