-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.odin
97 lines (82 loc) · 3.39 KB
/
input.odin
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
package snake
import rl "vendor:raylib"
input_keys_pressed_down : [4]rl.KeyboardKey
input_num_keys_pressed_down : i32 = 0
input_key_active_index : i32 : 0
input_keys_pressed_down_add_as_active :: proc(key: rl.KeyboardKey) {
if input_num_keys_pressed_down == 0 {
input_keys_pressed_down[input_key_active_index] = key
input_num_keys_pressed_down += 1
}
else {
// if key is first element, do nothing
if input_keys_pressed_down[0] == key {
return
}
input_keys_pressed_down_remove_if_exists(key)
// shift all elements in array to right
for i := input_num_keys_pressed_down - 1; i >= 0; i -= 1 {
input_keys_pressed_down[i + 1] = input_keys_pressed_down[i]
}
// place key
input_keys_pressed_down[0] = key
input_num_keys_pressed_down += 1
}
}
input_keys_pressed_down_remove_if_exists :: proc(key: rl.KeyboardKey) {
for i in 0..<input_num_keys_pressed_down {
if input_keys_pressed_down[i] == key {
for j in i..<input_num_keys_pressed_down {
input_keys_pressed_down[j] = input_keys_pressed_down[j + 1]
}
input_num_keys_pressed_down -= 1
}
}
}
// input procedures
input :: proc() {
// modify array of keys pressed down based on current keyboard state
if rl.IsKeyPressed(rl.KeyboardKey.UP) && snake_movement_direction != Direction.DOWN && snake_movement_direction != Direction.UP
{
input_keys_pressed_down_add_as_active(rl.KeyboardKey.UP)
}
else if rl.IsKeyReleased(rl.KeyboardKey.UP) {
input_keys_pressed_down_remove_if_exists(rl.KeyboardKey.UP)
}
if rl.IsKeyPressed(rl.KeyboardKey.DOWN) && snake_movement_direction != Direction.UP && snake_movement_direction != Direction.DOWN
{
input_keys_pressed_down_add_as_active(rl.KeyboardKey.DOWN)
}
else if rl.IsKeyReleased(rl.KeyboardKey.DOWN) {
input_keys_pressed_down_remove_if_exists(rl.KeyboardKey.DOWN)
}
if rl.IsKeyPressed(rl.KeyboardKey.LEFT) && snake_movement_direction != Direction.RIGHT && snake_movement_direction != Direction.LEFT
{
input_keys_pressed_down_add_as_active(rl.KeyboardKey.LEFT)
}
else if rl.IsKeyReleased(rl.KeyboardKey.LEFT) {
input_keys_pressed_down_remove_if_exists(rl.KeyboardKey.LEFT)
}
if rl.IsKeyPressed(rl.KeyboardKey.RIGHT) && snake_movement_direction != Direction.LEFT && snake_movement_direction != Direction.RIGHT
{
input_keys_pressed_down_add_as_active(rl.KeyboardKey.RIGHT)
}
else if rl.IsKeyReleased(rl.KeyboardKey.RIGHT) {
input_keys_pressed_down_remove_if_exists(rl.KeyboardKey.RIGHT)
}
// set direction of snake based on most recent key pressed down
if input_num_keys_pressed_down > 0 {
if input_keys_pressed_down[input_key_active_index] == rl.KeyboardKey.UP {
snake_movement_direction = Direction.UP
}
else if input_keys_pressed_down[input_key_active_index] == rl.KeyboardKey.DOWN {
snake_movement_direction = Direction.DOWN
}
else if input_keys_pressed_down[input_key_active_index] == rl.KeyboardKey.LEFT {
snake_movement_direction = Direction.LEFT
}
else if input_keys_pressed_down[input_key_active_index] == rl.KeyboardKey.RIGHT {
snake_movement_direction = Direction.RIGHT
}
}
}