Repository navigation
Expand file tree
/
Copy pathinput_processor.cpp
More file actions
207 lines (180 loc) · 6.94 KB
/
Copy pathinput_processor.cpp
File metadata and controls
207 lines (180 loc) · 6.94 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "input_processor.h"
#include <iostream>
/*************************************************************************************************
* *
* Initialize / Destroy Function *
* *
*************************************************************************************************/
bool InputProcessor::state[InputAction::IA_NUM] = {false};
InputProcessor::InputProcessor()
{
for (int i = 0; i < InputAction::IA_NUM; i++)
{
filter[i] = true;
commands[i] = 0;
}
al_init_user_event_source(&InputMaster);
}
InputProcessor::~InputProcessor()
{
al_destroy_user_event_source(&InputMaster);
}
void InputProcessor::SetCommand(InputAction define, int key) noexcept
{
/* Keys[0] is a unregistered key, means disabled */
if (key > -1 && key < ALLEGRO_KEY_MAX)
commands[define] = key;
}
/*************************************************************************************************
* *
* Update Function *
* *
*************************************************************************************************/
/* make command and buffer it to event queue */
void InputProcessor::Update(const bool keys[])
{
/* is gamer make pause or resume? */
if (check_button_just_pressed(InputAction::IA_PAUSE, keys))
/* just pause and no need to check buttons */
GameCon->Esc();
else
/* true check happens */
check_buttons(keys);
/* Save state for this turn */
for (int i = 0; i < InputAction::IA_NUM; i++)
state[i] = keys[commands[i]];
}
void InputProcessor::Flush() noexcept
{
for (int i = 0; i < InputAction::IA_NUM; i++)
filter[i] = true;
}
inline bool InputProcessor::check_button_just_pressed(
InputAction button, const bool keys[]) const noexcept
{
if (keys[commands[button]] && !state[button])
{
#ifdef _DEBUG
std::cout << "Button pressed: " << al_keycode_to_name(commands[button]) << "\n";
#endif
return true;
}
return false;
}
inline bool InputProcessor::check_button_just_released(
InputAction button, const bool keys[]) const noexcept
{
if (!keys[commands[button]] && state[button])
{
#ifdef _DEBUG
std::cout << "Button released: " << al_keycode_to_name(commands[button]) << "\n";
#endif
return true;
}
return false;
}
/*************************************************************************************************
* *
* Different Type Input Processor *
* *
*************************************************************************************************/
/* STG buttons check seqence */
void STGInput::check_buttons(const bool keys[])
{
ALLEGRO_EVENT event;
// event.type = static_cast<ALLEGRO_EVENT_TYPE>(GameEventType::INPUT_COMMAND);
/* 4-direction move */
if (keys[commands[InputAction::IA_MOVE_UP]] &&
filter[InputAction::IA_MOVE_UP])
{
event.user.data1 = STGCharCommand::SCC_UP;
al_emit_user_event(&InputMaster, &event, nullptr);
filter[InputAction::IA_MOVE_UP] = false;
}
if (keys[commands[InputAction::IA_MOVE_DOWN]] &&
filter[InputAction::IA_MOVE_DOWN])
{
event.user.data1 = STGCharCommand::SCC_DOWN;
al_emit_user_event(&InputMaster, &event, nullptr);
filter[InputAction::IA_MOVE_DOWN] = false;
}
if (keys[commands[InputAction::IA_MOVE_LEFT]] &&
filter[InputAction::IA_MOVE_LEFT])
{
event.user.data1 = STGCharCommand::SCC_LEFT;
al_emit_user_event(&InputMaster, &event, nullptr);
filter[InputAction::IA_MOVE_LEFT] = false;
}
if (keys[commands[InputAction::IA_MOVE_RIGHT]] &&
filter[InputAction::IA_MOVE_RIGHT])
{
event.user.data1 = STGCharCommand::SCC_RIGHT;
al_emit_user_event(&InputMaster, &event, nullptr);
filter[InputAction::IA_MOVE_RIGHT] = false;
}
/* action fire or cease */
if (check_button_just_pressed(InputAction::IA_ACTION, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_FIRE;
al_emit_user_event(&InputMaster, &event, nullptr);
}
else if (check_button_just_released(InputAction::IA_ACTION, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_CEASE;
al_emit_user_event(&InputMaster, &event, nullptr);
}
/* shift */
if (check_button_just_pressed(InputAction::IA_SHIFT, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_CHANGE;
al_emit_user_event(&InputMaster, &event, nullptr);
}
/* skill */
if (check_button_just_pressed(InputAction::IA_CANCEL, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_SYNC;
al_emit_user_event(&InputMaster, &event, nullptr);
}
/* bomb fire or cease */
if (check_button_just_pressed(InputAction::IA_SPECIAL, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_FORCE_SYNC_REQUEST;
al_emit_user_event(&InputMaster, &event, nullptr);
}
else if (check_button_just_released(InputAction::IA_SPECIAL, keys))
{
event.user.data1 = STGCharCommand::SCC_STG_FORCE_SYNC_RESPONE;
al_emit_user_event(&InputMaster, &event, nullptr);
}
}
/* menu input just check press down */
void MenuInput::check_buttons(const bool keys[])
{
ALLEGRO_EVENT event;
// event.type = static_cast<ALLEGRO_EVENT_TYPE>(GameEventType::INPUT_COMMAND);
if (check_button_just_pressed(InputAction::IA_MOVE_UP, keys))
{
event.user.data1 = InputAction::IA_MOVE_UP;
al_emit_user_event(&InputMaster, &event, nullptr);
}
if (check_button_just_pressed(InputAction::IA_MOVE_DOWN, keys))
{
event.user.data1 = InputAction::IA_MOVE_DOWN;
al_emit_user_event(&InputMaster, &event, nullptr);
}
if (check_button_just_pressed(InputAction::IA_MOVE_LEFT, keys))
{
event.user.data1 = InputAction::IA_MOVE_LEFT;
al_emit_user_event(&InputMaster, &event, nullptr);
}
if (check_button_just_pressed(InputAction::IA_MOVE_RIGHT, keys))
{
event.user.data1 = InputAction::IA_MOVE_RIGHT;
al_emit_user_event(&InputMaster, &event, nullptr);
}
if (check_button_just_pressed(InputAction::IA_ACTION, keys))
{
event.user.data1 = InputAction::IA_ACTION;
al_emit_user_event(&InputMaster, &event, nullptr);
}
}