2020-09-14 00:00:00 +00:00
|
|
|
#include "./controller.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "core/locommon/input.h"
|
|
|
|
#include "core/locommon/position.h"
|
2020-10-09 00:00:00 +00:00
|
|
|
#include "core/locommon/ticker.h"
|
2020-09-14 00:00:00 +00:00
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
#define DODGE_PRESS_MAX_DURATION_ 300
|
|
|
|
|
|
|
|
void loplayer_controller_initialize(
|
|
|
|
loplayer_controller_t* controller, const locommon_ticker_t* ticker) {
|
2020-09-14 00:00:00 +00:00
|
|
|
assert(controller != NULL);
|
2020-10-09 00:00:00 +00:00
|
|
|
assert(ticker != NULL);
|
2020-09-14 00:00:00 +00:00
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
*controller = (typeof(*controller)) {
|
|
|
|
.ticker = ticker,
|
|
|
|
};
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loplayer_controller_deinitialize(loplayer_controller_t* controller) {
|
|
|
|
assert(controller != NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
void loplayer_controller_handle_input(
|
2020-09-14 00:00:00 +00:00
|
|
|
loplayer_controller_t* controller,
|
|
|
|
const locommon_input_t* input,
|
|
|
|
const locommon_position_t* cursor) {
|
|
|
|
assert(controller != NULL);
|
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
if (locommon_position_valid(cursor)) {
|
|
|
|
controller->cursor = *cursor;
|
|
|
|
}
|
2020-09-14 00:00:00 +00:00
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_NONE;
|
|
|
|
if (input == NULL) return;
|
2020-09-14 00:00:00 +00:00
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_LEFT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_WALK_LEFT;
|
|
|
|
}
|
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_RIGHT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_WALK_RIGHT;
|
|
|
|
}
|
2020-09-14 00:00:00 +00:00
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_DODGE) {
|
|
|
|
if (!controller->sprint) {
|
|
|
|
controller->sprint = true;
|
|
|
|
controller->last_sprint_start = controller->ticker->time;
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_LEFT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_SPRINT_LEFT;
|
|
|
|
} else if (input->buttons & LOCOMMON_INPUT_BUTTON_RIGHT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_SPRINT_RIGHT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (controller->sprint) {
|
|
|
|
controller->sprint = false;
|
|
|
|
|
|
|
|
assert(controller->ticker->time >= controller->last_sprint_start);
|
|
|
|
const size_t t =
|
|
|
|
controller->ticker->time - controller->last_sprint_start;
|
|
|
|
if (t < DODGE_PRESS_MAX_DURATION_) {
|
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_LEFT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_DODGE_LEFT;
|
|
|
|
} else if (input->buttons & LOCOMMON_INPUT_BUTTON_RIGHT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_DODGE_RIGHT;
|
|
|
|
} else {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_DODGE_FORWARD;
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_JUMP) {
|
|
|
|
if (!controller->jump) {
|
|
|
|
controller->jump = true;
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_JUMP;
|
|
|
|
}
|
2020-09-14 00:00:00 +00:00
|
|
|
} else {
|
2020-10-09 00:00:00 +00:00
|
|
|
controller->jump = false;
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_GUARD) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_GUARD;
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
2020-10-09 00:00:00 +00:00
|
|
|
if (input->buttons & LOCOMMON_INPUT_BUTTON_SHOOT) {
|
|
|
|
controller->state = LOPLAYER_CONTROLLER_STATE_SHOOT;
|
2020-09-14 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|