-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequencer.go
73 lines (57 loc) · 1.34 KB
/
sequencer.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package spin
import (
"time"
"github.com/drop-target-pinball/coroutine"
)
type Sequencer struct {
seq *coroutine.Sequencer
env *ScriptEnv
}
func NewSequencer(env *ScriptEnv) *Sequencer {
return &Sequencer{
seq: coroutine.NewSequencer(),
env: env,
}
}
func (s *Sequencer) Defer(act Action) {
s.seq.Defer(func() { s.env.Do(act) })
}
func (s *Sequencer) Do(act Action) {
s.seq.Do(func() { s.env.Do(act) })
switch a := act.(type) {
case PlaySpeech:
s.seq.Cancel(func() { s.env.Do(StopSpeech{ID: a.ID}) })
case PlaySound:
s.seq.Cancel(func() { s.env.Do(StopSound{ID: a.ID}) })
}
}
func (s *Sequencer) DoFunc(fn func()) {
s.seq.Do(fn)
}
func (s *Sequencer) DoRun(fn func() bool) {
s.seq.DoRun(fn)
}
func (s *Sequencer) DoScript(fn func(*ScriptEnv)) {
s.seq.Do(func() { fn(s.env) })
}
func (s *Sequencer) Loop() {
s.seq.Loop()
}
func (s *Sequencer) LoopN(n int) {
s.seq.LoopN(n)
}
func (s *Sequencer) Post(evt Event) {
s.seq.Do(func() { s.env.Post(evt) })
}
func (s *Sequencer) Sleep(ms int) {
s.seq.Sleep(time.Duration(ms) * time.Millisecond)
}
func (s *Sequencer) WaitFor(events ...coroutine.Event) {
s.seq.WaitFor(events...)
}
func (s *Sequencer) WaitForUntil(ms int, events ...coroutine.Event) {
s.seq.WaitForUntil(time.Duration(ms)*time.Millisecond, events...)
}
func (s *Sequencer) Run() bool {
return s.seq.Run(s.env.co)
}