/
sceneLoop.go
151 lines (126 loc) · 3.3 KB
/
sceneLoop.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
package oak
import (
"image"
"github.com/oakmound/oak/v2/collision"
"github.com/oakmound/oak/v2/dlog"
"github.com/oakmound/oak/v2/event"
"github.com/oakmound/oak/v2/mouse"
"github.com/oakmound/oak/v2/render"
"github.com/oakmound/oak/v2/scene"
"github.com/oakmound/oak/v2/timing"
)
var (
loadingScene = scene.Scene{
Start: func(prevScene string, data interface{}) {
dlog.Info("Loading Scene Init")
},
Loop: func() bool {
select {
case <-startupLoadCh:
dlog.Info("Load Complete")
return false
default:
return true
}
},
End: func() (string, *scene.Result) {
return firstScene, nil
},
}
)
var firstScene string
func sceneLoop(first string, trackingInputs bool, debugConsoleDisabled bool) {
var prevScene string
result := new(scene.Result)
dlog.Info("First Scene Start")
drawCh <- true
drawCh <- true
dlog.Verb("Draw Channel Activated")
firstScene = first
SceneMap.CurrentScene = "loading"
for {
ViewPos = image.Point{0, 0}
updateScreen(0, 0)
useViewBounds = false
dlog.Info("Scene Start", SceneMap.CurrentScene)
scen, ok := SceneMap.GetCurrent()
if !ok {
dlog.Error("Unknown scene", SceneMap.CurrentScene)
panic("Unknown scene " + SceneMap.CurrentScene)
}
if trackingInputs {
trackInputChanges()
}
go func() {
dlog.Info("Starting scene in goroutine", SceneMap.CurrentScene)
scen.Start(prevScene, result.NextSceneInput)
transitionCh <- true
}()
sceneTransition(result)
// Post transition, begin loading animation
dlog.Info("Starting load animation")
drawCh <- true
dlog.Info("Getting Transition Signal")
<-transitionCh
dlog.Info("Resume Drawing")
// Send a signal to resume (or begin) drawing
drawCh <- true
dlog.Info("Looping Scene")
cont := true
dlog.ErrorCheck(logicHandler.UpdateLoop(FrameRate, sceneCh))
for cont {
select {
case <-sceneCh:
cont = scen.Loop()
case <-skipSceneCh:
cont = false
}
}
dlog.Info("Scene End", SceneMap.CurrentScene)
// We don't want enterFrames going off between scenes
dlog.ErrorCheck(logicHandler.Stop())
prevScene = SceneMap.CurrentScene
// Send a signal to stop drawing
drawCh <- true
// Reset any ongoing delays
delayLabel:
for {
select {
case timing.ClearDelayCh <- true:
default:
break delayLabel
}
}
dlog.Verb("Resetting Engine")
// Reset transient portions of the engine
// We start by clearing the event bus to
// remove most ongoing code
logicHandler.Reset()
// We follow by clearing collision areas
// because otherwise collision function calls
// on non-entities (i.e. particles) can still
// be triggered and attempt to access an entity
dlog.Verb("Event Bus Reset")
collision.Clear()
mouse.Clear()
event.ResetEntities()
render.ResetDrawStack()
render.PreDraw()
dlog.Verb("Engine Reset")
// Todo: Add in customizable loading scene between regular scenes,
// In addition to the existing customizable loading renderable?
SceneMap.CurrentScene, result = scen.End()
// For convenience, we allow the user to return nil
// but it gets translated to an empty result
if result == nil {
result = new(scene.Result)
}
if !debugConsoleDisabled && !debugResetInProgress {
debugResetInProgress = true
go func() {
debugResetCh <- true
debugResetInProgress = false
}()
}
}
}