-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
95 lines (68 loc) · 2.01 KB
/
input.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
package main
import "github.com/hajimehoshi/ebiten"
type InputHandler struct {
inputDown bool
}
//the player cannot reverse direction,
//which we handle here. we basically see if x,y is
//the inverse of the current direction, in which case,
//we just return
//note that if the input is the same as last, it still
//sets it, but this has no effect
//x is the "leftright axis" , right is 1, left is -1
//this might be a "game controller" function,
//but we will keep it here for now because i think
//its the only function that will be really processed
//in said controller
func (game *GameState) HandleMovement(x int) {
//find the intermediate direction by flipping the
//current one
newDirection := game.snake.direction.Reverse()
//if we are going right, or clockwise, we check
//to see if y is non-null in the reverse, if it is,
//we multiply the value by -1 because it has crossed
//an axes
if newDirection.y != 0 && x == 1 {
newDirection = newDirection.Multiply(-1)
}
//if we are going left, we check the x value,
//and also multiply by -1 if it is non null,
//because we have crossed the x axis
if newDirection.x != 0 && x == -1 {
newDirection = newDirection.Multiply(-1)
}
game.snake.direction = newDirection
return
}
//process the input from Game, its understood that
//this will alter the state of game and input
func (input *InputHandler) ProcessInput(game *GameState) error {
switch {
case ebiten.IsKeyPressed(ebiten.KeyLeft):
if !input.inputDown {
game.HandleMovement(1)
}
input.inputDown = true
case ebiten.IsKeyPressed(ebiten.KeyRight):
if !input.inputDown {
game.HandleMovement(-1)
}
input.inputDown = true
default:
input.inputDown = false
}
return nil
}
func (input *InputHandler) WaitForOption(game *GameState) {
keyPressed := false
switch {
case ebiten.IsKeyPressed(ebiten.KeyY) || ebiten.IsKeyPressed(ebiten.KeyEnter):
game.snake.advanced = true
keyPressed = true
case ebiten.IsKeyPressed(ebiten.KeyN):
keyPressed = true
}
if keyPressed {
game.Start()
}
}