-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
121 lines (99 loc) · 2.29 KB
/
main.go
File metadata and controls
121 lines (99 loc) · 2.29 KB
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
package main
import (
"fmt"
"platformer/events"
"time"
"platformer/stages"
"net/http"
_ "net/http/pprof"
"github.com/shinomontaz/pixel"
"github.com/shinomontaz/pixel/pixelgl"
)
var (
win *pixelgl.Window
title string = "platformer"
currBounds pixel.Rect // current viewport
isquit bool
isdebug bool
currStage stages.Stager
stgs map[int]stages.Stager
loadingStage stages.Stager
)
func init() {
// load video mode and sound volumes
initRuntime()
}
func run() {
cfg := pixelgl.WindowConfig{
Title: title,
Bounds: currBounds,
VSync: true,
}
var err error
win, err = pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
win.SetSmooth(true)
initScreen(win)
initSound()
stgs = make(map[int]stages.Stager, 0)
loadingStage = stages.NewLoading(inform, assetloader)
stgs[stages.LOADING] = loadingStage
stgs[stages.MENU] = stages.NewMenu(inform, assetloader, win, currBounds) // main menu
stgs[stages.GAME] = stages.NewGame(inform, assetloader, win, currBounds)
currStage = stgs[stages.LOADING]
currStage.SetUp(stages.WithJob(stgs[stages.MENU].Init), stages.WithNext(events.STAGEVENT_DONE, stages.MENU))
currStage.Init()
currStage.Start()
if startConfig.TestFlag {
go func() {
http.ListenAndServe("localhost:5000", nil)
}()
}
last := time.Now()
for !win.Closed() && !isquit {
dt := time.Since(last).Seconds()
last = time.Now()
currStage.Run(win, dt)
win.Update()
}
}
func main() {
pixelgl.Run(run)
}
func inform(e int) {
switch e {
case events.STAGEVENT_DONE:
fmt.Println("event done")
next, ok := currStage.GetNext(events.STAGEVENT_DONE)
if ok {
setStage(next)
}
case events.STAGEVENT_NEXT:
fmt.Println("event next")
next, ok := currStage.GetNext(events.STAGEVENT_NEXT)
if ok {
setStage(next)
}
case events.STAGEVENT_QUIT:
fmt.Println("event quit")
next, ok := currStage.GetNext(events.STAGEVENT_QUIT)
if ok {
setStage(next)
} else {
isquit = true
}
case events.GAMEVENT_INITSCREEN:
initScreen(win)
case events.GAMEVENT_UPDATEVOLUME:
initSound()
case events.STAGEVENT_NOTREADY:
loadingStage.SetUp(stages.WithJob(currStage.Init), stages.WithNext(events.STAGEVENT_DONE, currStage.GetID()))
setStage(loadingStage.GetID())
}
}
func setStage(id int) {
currStage = stgs[id]
currStage.Start()
}