-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.go
92 lines (78 loc) · 2.16 KB
/
game.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
package main
import (
"time"
"math/rand"
"github.com/nsf/termbox-go"
)
type game struct {
state uint
subStates []int
envSnd chan *environment
envRqst chan bool
plyrBuf chan inputCommand
stSnd chan stateDescriptor
stRqst chan stateRequest
stMdfy chan stateDescriptor
}
func initGame() game {
g := game{
state: stateMainMenu,
subStates: make([]int, int(totalSubStates[stateMainMenu]), int(totalSubStates[stateMainMenu])),
envSnd: make(chan *environment),
envRqst: make(chan bool),
plyrBuf: make(chan inputCommand, 1028),
stSnd: make(chan stateDescriptor),
stRqst: make(chan stateRequest),
stMdfy: make(chan stateDescriptor),
}
copy(g.subStates, subStateInitialValues[stateMainMenu])
return g
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
rand.Seed(time.Now().UTC().UnixNano())
g := initGame()
go runEnvController(g.envSnd, g.envRqst)
go runRenderer(g.envSnd, g.envRqst, g.stSnd, g.stRqst)
go runInputParser(g.envSnd, g.envRqst, g.plyrBuf, g.stSnd, g.stRqst, g.stMdfy)
go runPlayer(g.envSnd, g.envRqst, g.plyrBuf, g.stSnd, g.stMdfy, initEntity('@', termbox.ColorDefault, 100, 0, 0))
for g.state != stateExit {
select{
case req := <-g.stRqst:
if req.reqType == stateType {
g.stSnd <- initStateDesc(g.state)
break
}else if req.reqType == subStateType {
if req.subStateIndex < uint(len(g.subStates)) {
g.stSnd <- initSubStateDesc(g.state, g.subStates[req.subStateIndex], req.subStateIndex)
break
}
}
g.stSnd <- initErrorDesc()
case mod := <-g.stMdfy:
if mod.state < totalStates {
if mod.descType == stateType {
g.state = mod.state
g.subStates = make([]int, int(totalSubStates[g.state]), int(totalSubStates[g.state]))
copy(g.subStates, subStateInitialValues[g.state])
g.stSnd <- mod
break
}else if mod.descType == subStateType {
if mod.state == g.state {
if mod.subStateIndex < uint(len(g.subStates)) {
g.subStates[mod.subStateIndex] = mod.subState
g.stSnd <- mod
break
}
}
}
}
g.stSnd <- initErrorDesc()
}
}
}