-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
178 lines (151 loc) · 3.11 KB
/
script.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package gamebuilder
import (
"errors"
"sync"
)
// Script describe the application/game scenes and the current use
type Script interface {
Close() error
SetLoader(s Scene) error
AddScene(index string, s Scene) error
LoadScene(index string) error
CloseScene(index string) error
Scene(index string) (Scene, error)
SceneIndex() string
}
type script struct {
sync.Mutex
loader Scene
scenes map[string]Scene
scene string
running bool
}
// NewScript instance
func NewScript() Script {
return &script{scenes: make(map[string]Scene)}
}
// Close free all scenes resources
func (s *script) Close() error {
var err error
s.scene = ""
if s.loader != nil && s.loader.IsInit() {
if err = s.loader.Close(); err != nil {
return err
}
}
for _, sn := range s.scenes {
if err = sn.Close(); err != nil {
return err
}
}
return nil
}
// SetLoader build a loader scene to use it in transition
func (s *script) SetLoader(sn Scene) error {
var err error
if s.running {
return errors.New(errorScriptModifier)
}
if s.loader != nil {
if err = s.loader.Close(); err != nil {
return err
}
}
s.loader = sn
return nil
}
// AddScene ajoute the scene in the gamebuilder script. The scene should be called by the index
func (s *script) AddScene(index string, sn Scene) error {
var err error
if s.running {
return errors.New(errorScriptModifier)
}
if old, ok := s.scenes[index]; ok {
if err = old.Close(); err != nil {
return err
}
}
s.scenes[index] = sn
sn.SetSwitcher(s.LoadScene)
sn.SetCloser(s.CloseScene)
return nil
}
// LoadScene by index reference
func (s *script) LoadScene(index string) error {
var err error
var sn Scene
if !s.running {
s.running = true
}
ok := false
if sn, ok = s.scenes[index]; !ok {
return errors.New(errorSceneNotFound)
}
s.Lock()
defer s.Unlock()
// start loader if configured
if s.loader != nil {
if !s.loader.IsInit() {
if err = s.loader.Build(); err != nil {
return err
}
if err = s.loader.Init(); err != nil {
return err
}
}
if err = s.loader.Run(); err != nil {
return err
}
}
// stop the previous scene
if s.scene != "" {
s.scenes[s.scene].Stop()
}
// setup the new scene
if !sn.IsInit() {
if err = sn.Build(); err != nil {
return err
}
if err = sn.Init(); err != nil {
return err
}
}
// start the new scene
s.scene = index
if err = sn.Run(); err != nil {
return err
}
// stop the loader if configured
if s.loader != nil {
s.loader.Stop()
}
return nil
}
// CloseScene by index reference
func (s *script) CloseScene(index string) error {
var err error
var sn Scene
ok := false
if sn, ok = s.scenes[index]; !ok {
return errors.New(errorSceneNotFound)
}
s.Lock()
defer s.Unlock()
if err = sn.Close(); err != nil {
return err
}
return nil
}
// SceneIndex getter return the current scene loaded
func (s *script) SceneIndex() string {
return s.scene
}
// Scene getter return current scene by index reference
func (s *script) Scene(index string) (Scene, error) {
var sn Scene
ok := false
if sn, ok = s.scenes[index]; !ok {
return nil, errors.New(errorSceneNotFound)
}
return sn, nil
}