-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
62 lines (53 loc) · 1.48 KB
/
scene.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
package cinematic
import (
"github.com/df-mc/dragonfly/server/player"
"github.com/unickorn/cinematic/act"
"sort"
"time"
)
// Scene is a collection of acts that are performed in sequence.
type Scene struct {
// Name is the name of the scene.
Name string `json:"name"`
// Actions is a map of actions with their timestamps in milliseconds.
Actions map[int]act.Act `json:"actions"`
}
var emptyScene = Scene{}
// NewScene creates a new scene with the given name.
func NewScene(name string) Scene {
return Scene{
Name: name,
Actions: map[int]act.Act{},
}
}
// WithActions returns a new scene with the given actions.
func (s Scene) WithActions(actions map[int]act.Act) Scene {
s.Actions = actions
return s
}
// AddAction adds an action to the scene at the given timestamp.
func (s Scene) AddAction(timestamp int, a act.Act) Scene {
s.Actions[timestamp] = a
return s
}
// RemoveAction removes the action at the given timestamp from the scene.
func (s Scene) RemoveAction(timestamp int) Scene {
delete(s.Actions, timestamp)
return s
}
// Play plays the scene for the given player.
func (s Scene) Play(p *player.Player) {
// sort actions by timestamp
var timestamps []int
for timestamp := range s.Actions {
timestamps = append(timestamps, timestamp)
}
sort.Ints(timestamps)
lastTimestamp := 0
// play actions
for _, timestamp := range timestamps {
time.Sleep(time.Duration(timestamp-lastTimestamp) * time.Millisecond)
go s.Actions[timestamp].Do(p)
lastTimestamp = timestamp
}
}