forked from eugene-kirzhanov/flipper-zero-2048-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_controller_handler.c
141 lines (130 loc) · 3.97 KB
/
game_controller_handler.c
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
#define __game_controller_c
#include "game_controller.h"
#include <input/input.h>
void ui_state_menu_handle(
GameController* gamectrl,
InputEvent input,
GameControllerInputHandlerResult* out) {
if(input.type != InputTypePress) return;
switch(input.key) {
case InputKeyUp:
gamectrl->selected_menu_item--;
if(gamectrl->selected_menu_item < 0) {
gamectrl->selected_menu_item = MENU_ITEMS_COUNT - 1;
}
out->is_handled = true;
break;
case InputKeyDown:
gamectrl->selected_menu_item++;
if(gamectrl->selected_menu_item >= MENU_ITEMS_COUNT) {
gamectrl->selected_menu_item = 0;
}
out->is_handled = true;
break;
case InputKeyOk:
if(gamectrl->selected_menu_item == 0) {
// Continue
game_controller_close_menu(gamectrl);
out->is_handled = true;
} else if(gamectrl->selected_menu_item == 1) {
// new game
game_state_send(&gamectrl->state, GameReset);
game_controller_save_state(gamectrl);
game_controller_close_menu(gamectrl);
out->is_handled = true;
}
break;
case InputKeyBack:
game_controller_close_menu(gamectrl);
out->is_handled = true;
break;
default:
break;
}
return;
}
void ui_state_in_progress_handle(
GameController* gamectrl,
InputEvent input,
GameControllerInputHandlerResult* out) {
switch(input.key) {
case InputKeyLeft:
if(input.type != InputTypePress) break;
game_state_send(&gamectrl->state, GameMoveLeft);
out->is_handled = true;
break;
case InputKeyRight:
if(input.type != InputTypePress) break;
game_state_send(&gamectrl->state, GameMoveRight);
out->is_handled = true;
break;
case InputKeyUp:
if(input.type != InputTypePress) break;
game_state_send(&gamectrl->state, GameMoveUp);
out->is_handled = true;
break;
case InputKeyDown:
if(input.type != InputTypePress) break;
game_state_send(&gamectrl->state, GameMoveDown);
out->is_handled = true;
break;
case InputKeyOk:
if(input.type != InputTypePress) break;
game_controller_show_menu(gamectrl);
out->is_handled = true;
break;
case InputKeyBack:
if(input.type != InputTypeShort) break;
game_state_send(&gamectrl->state, GameMoveUndo);
out->is_handled = true;
break;
default:
break;
}
if(gamectrl->state.is_over) {
gamectrl->ui_state = UIStateGameOver;
}
return;
}
void ui_state_game_over_handle(
GameController* gamectrl,
InputEvent input,
GameControllerInputHandlerResult* out) {
switch(input.key) {
case InputKeyOk:
if(input.type != InputTypePress) break;
game_state_send(&gamectrl->state, GameReset);
game_controller_save_state(gamectrl);
gamectrl->ui_state = UIStateInProgress;
out->is_handled = true;
break;
case InputKeyBack:
if(input.type != InputTypeShort) break;
game_state_send(&gamectrl->state, GameMoveUndo);
gamectrl->ui_state = UIStateInProgress;
out->is_handled = true;
default:
break;
}
}
void game_controller_handle_input(
GameController* gamectrl,
InputEvent input,
GameControllerInputHandlerResult* out) {
if(input.key == InputKeyBack && input.type == InputTypeLong) {
game_controller_save_state(gamectrl);
out->is_handled = true;
out->should_exit = true;
return;
}
out->is_handled = false;
out->should_exit = false;
switch(gamectrl->ui_state) {
case UIStateMenu:
return ui_state_menu_handle(gamectrl, input, out);
case UIStateInProgress:
return ui_state_in_progress_handle(gamectrl, input, out);
case UIStateGameOver:
return ui_state_game_over_handle(gamectrl, input, out);
}
}