eb8834324e
Game state struct ends the variable mess in main.c
48 lines
1.2 KiB
C
48 lines
1.2 KiB
C
#include "ui.h"
|
|
#include "game.h"
|
|
#include <math.h>
|
|
#include <raylib.h>
|
|
#include <stdbool.h>
|
|
|
|
static bool is_dragging;
|
|
static Vector2 drag_start_pos;
|
|
|
|
void handle_mouse_input(GameState_t *game_state) {
|
|
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
|
|
drag_start_pos = GetMousePosition();
|
|
if (drag_start_pos.x >= 500 && drag_start_pos.x <= 775 && drag_start_pos.y >= 455 && drag_start_pos.y <= 479) {
|
|
reset_board(game_state);
|
|
return;
|
|
}
|
|
|
|
is_dragging = true;
|
|
} else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON) && is_dragging) {
|
|
Vector2 current_pos = GetMousePosition();
|
|
|
|
float dx = current_pos.x - drag_start_pos.x;
|
|
float dy = current_pos.y - drag_start_pos.y;
|
|
|
|
float drag_distance = sqrt(dx * dx + dy * dy);
|
|
|
|
// We don't want to register single clicks as a move
|
|
if (drag_distance >= 30) {
|
|
if (fabs(dx) > fabs(dy)) {
|
|
if (dx > 0) {
|
|
move(game_state, RIGHT);
|
|
} else {
|
|
move(game_state, LEFT);
|
|
}
|
|
} else {
|
|
if (dy > 0) {
|
|
move(game_state, DOWN);
|
|
} else {
|
|
move(game_state, UP);
|
|
}
|
|
}
|
|
spawn_tile(game_state);
|
|
}
|
|
|
|
is_dragging = false;
|
|
}
|
|
}
|