-
Notifications
You must be signed in to change notification settings - Fork 2
/
timing.go
74 lines (64 loc) · 1.75 KB
/
timing.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
74
package skill
import (
"fmt"
"time"
)
// Timing represents when a skill effect should be triggered. It allows both
// concrete time.Duration values that specify the amount of time after skill
// execution starts, and virtualised "points" in the skill execution like "end"
// or "when the blow lands".
type Timing struct {
point *TimingPoint
sched time.Duration
}
//go:generate stringer -type=TimingPoint
type TimingPoint int
const (
// AttackApexTimingPoint applies to spells too!
AttackApexTimingPoint TimingPoint = iota
EndTimingPoint
)
func TimingPointFromString(s string) *TimingPoint {
for i := 0; i <= int(EndTimingPoint); i++ {
c := TimingPoint(i)
if c.String() == s {
return &c
}
}
return nil
}
// NewTiming constructs a Timing for an effect that should be triggered the
// specified Duration after the skill execution starts.
func NewTiming(at time.Duration) Timing {
return Timing{
sched: at,
}
}
// NewTimingFromPoint constructs a Timing from a TimingPoint.
func NewTimingFromPoint(point TimingPoint) Timing {
return Timing{
point: &point,
}
}
// when to trigger an effect. Returns either a virtual TimingPoint, or a
// concrete time as a time.Duration. Use a realiser provided by
// NewTimingRealiser to resolve either case to a concrete time.Duration.
func (t Timing) when() interface{} {
if t.point != nil {
return *t.point
}
return t.sched
}
// NewTimingRealiser creates a function to realise when to trigger an effect.
func NewTimingRealiser(m map[TimingPoint]time.Duration) func(Timing) time.Duration {
return func(t Timing) time.Duration {
switch v := t.when().(type) {
case TimingPoint:
return m[v]
case time.Duration:
return v
default:
panic(fmt.Sprintf("misconfigured skill.Timing or code error, %T", v))
}
}
}