-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_parser.go
156 lines (147 loc) · 6.55 KB
/
input_parser.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
package main
import (
"math"
"github.com/nsf/termbox-go"
"github.com/mwindels/go-rogue/geom"
)
const (
commandKey uint = iota
commandMouse //separate commands for move, attack, and keys?
)
type inputCommand struct {
commandType uint
key termbox.Key
x int
y int
}
func initKeyCommand(keyIn termbox.Key) inputCommand {
return inputCommand{commandType: commandKey, key: keyIn, x: 0, y: 0}
}
func initMouseCommand(mouseIn termbox.Key, xIn, yIn int) inputCommand {
return inputCommand{commandType: commandMouse, key: mouseIn, x: xIn, y: yIn}
}
//The calculations made in this function are all dependent on how the renderer draws borders and the environment.
func mapClickToEnvironment(click termbox.Event, env *environment, state uint) (bool, int, int) {
termWidth, termHeight := termbox.Size() //Possible that these may change between when the event was polled and when this call was made.
for i := len(stateOverlays[int(state)].canvases) - 1; i >= 0; i-- {
canvas := stateOverlays[int(state)].canvases[i]
border := geom.ScaleRectangle(canvas.border, float64(termWidth - 1), float64(termHeight - 1))
if int(border.UpperLeft().X) <= click.MouseX && click.MouseX <= int(border.UpperRight().X) && int(border.UpperLeft().Y) <= click.MouseY && click.MouseY <= int(border.LowerLeft().Y) {
if int(border.UpperLeft().X) < click.MouseX && click.MouseX < int(border.UpperRight().X) && int(border.UpperLeft().Y) < click.MouseY && click.MouseY < int(border.LowerLeft().Y) || (canvas.attributes & borderless) != 0 {
if canvas.variableContents == displayEnvironment {
env.mutex.RLock()
xLoc := env.player.x + (click.MouseX - int(math.Ceil(border.UpperLeft().X + border.UpperRight().X / 2)))
yLoc := env.player.y + (click.MouseY - int(math.Ceil(border.UpperLeft().Y + border.LowerLeft().Y / 2)))
env.mutex.RUnlock()
return true, xLoc, yLoc
}
}
if (canvas.attributes & opaque) != 0 {
break
}
}
}
return false, 0, 0
}
func runInputParser(envRcv <-chan *environment, envRqst chan<- bool, plyrBuf chan<- inputCommand, stRcv <-chan stateDescriptor, stRqst chan<- stateRequest, stMdfy chan<- stateDescriptor) {
envRqst <- true
env := <-envRcv
mouseLeft, mouseRight := false, false
for {
event := termbox.PollEvent()
if event.Type == termbox.EventKey {
func() {
defer func() {recover()}() //this could maybe print an informative message to a log somewhere
if event.Ch == 0 && event.Key == termbox.KeyEsc {
setState(stMdfy, stRcv, stateExit)
}
switch state := getState(stRqst, stRcv); state {
case stateMainMenu:
if event.Ch == 0 {
if event.Key == termbox.KeyArrowUp {
setSubState(stMdfy, stRcv, state, subStateInitialValues[state][int(stateMainMenuSelectorIndex)] + (len(selectorMap[variableContentsKey{state: state, subStateIndex: stateMainMenuSelectorIndex, displayMode: displayMainMenu}]) + getSubState(stRqst, stRcv, state, stateMainMenuSelectorIndex) - 1) % len(selectorMap[variableContentsKey{state: state, subStateIndex: stateMainMenuSelectorIndex, displayMode: displayMainMenu}]), stateMainMenuSelectorIndex)
}else if event.Key == termbox.KeyArrowDown {
setSubState(stMdfy, stRcv, state, subStateInitialValues[state][int(stateMainMenuSelectorIndex)] + (getSubState(stRqst, stRcv, state, stateMainMenuSelectorIndex) + 1) % len(selectorMap[variableContentsKey{state: state, subStateIndex: stateMainMenuSelectorIndex, displayMode: displayMainMenu}]), stateMainMenuSelectorIndex)
}else if event.Key == termbox.KeyEnter {
switch getSubState(stRqst, stRcv, state, stateMainMenuSelectorIndex) {
case 0:
setState(stMdfy, stRcv, stateNewGame)
case 1:
//options
case 2:
setState(stMdfy, stRcv, stateExit)
}
}
}
case stateNewGame:
if event.Ch == 0 {
if event.Key == termbox.KeyEnter {
setState(stMdfy, stRcv, stateRunningGame)
env.paused.Open()
}
}
case stateRunningGame:
if event.Ch == 0 {
switch event.Key {
case termbox.KeyArrowLeft:
plyrBuf <- initKeyCommand(termbox.KeyArrowLeft)
case termbox.KeyArrowUp:
plyrBuf <- initKeyCommand(termbox.KeyArrowUp)
case termbox.KeyArrowRight:
plyrBuf <- initKeyCommand(termbox.KeyArrowRight)
case termbox.KeyArrowDown:
plyrBuf <- initKeyCommand(termbox.KeyArrowDown)
case termbox.KeySpace:
env.paused.Close()
setState(stMdfy, stRcv, statePausedGame)
}
}
case statePausedGame:
if event.Ch == 0 {
if event.Key == termbox.KeyArrowUp {
setSubState(stMdfy, stRcv, state, subStateInitialValues[state][int(statePausedGameSelectorIndex)] + (len(selectorMap[variableContentsKey{state: state, subStateIndex: statePausedGameSelectorIndex, displayMode: displayPause}]) + getSubState(stRqst, stRcv, state, statePausedGameSelectorIndex) - 1) % len(selectorMap[variableContentsKey{state: state, subStateIndex: statePausedGameSelectorIndex, displayMode: displayPause}]), statePausedGameSelectorIndex)
}else if event.Key == termbox.KeyArrowDown {
setSubState(stMdfy, stRcv, state, subStateInitialValues[state][int(statePausedGameSelectorIndex)] + (getSubState(stRqst, stRcv, state, statePausedGameSelectorIndex) + 1) % len(selectorMap[variableContentsKey{state: state, subStateIndex: statePausedGameSelectorIndex, displayMode: displayPause}]), statePausedGameSelectorIndex)
}else if event.Key == termbox.KeyEnter {
switch getSubState(stRqst, stRcv, state, statePausedGameSelectorIndex) {
case 0:
setState(stMdfy, stRcv, stateRunningGame)
env.paused.Open()
case 1:
setState(stMdfy, stRcv, stateMainMenu)
}
}
}
case stateGameOver:
if event.Ch == 0 {
if event.Key == termbox.KeyEnter {
switch getSubState(stRqst, stRcv, state, stateGameOverSelectorIndex) {
case 0:
setState(stMdfy, stRcv, stateMainMenu)
}
}
}
}
}()
}else if event.Type == termbox.EventMouse {
if event.Key == termbox.MouseLeft {
mouseLeft, mouseRight = true, false
}else if event.Key == termbox.MouseRight {
mouseLeft, mouseRight = false, true
}else{
if event.Key == termbox.MouseRelease {
if state := getState(stRqst, stRcv); state == stateRunningGame {
if valid, x, y := mapClickToEnvironment(event, env, state); valid {
if mouseLeft {
plyrBuf <- initMouseCommand(termbox.MouseLeft, x, y)
}else if mouseRight {
plyrBuf <- initMouseCommand(termbox.MouseRight, x, y)
}
}
}
}
mouseLeft, mouseRight = false, false
}
}
}
}