From 38e3ab041c52a38c9b3312c689a3ece436d03373 Mon Sep 17 00:00:00 2001 From: orangerot Date: Tue, 14 Oct 2025 17:47:34 +0200 Subject: [PATCH] feat: pick up bricks from hand --- domino.h | 2 +- game.c | 50 +++++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/domino.h b/domino.h index b08c332..e473776 100644 --- a/domino.h +++ b/domino.h @@ -4,7 +4,7 @@ #define DOMINO_H struct eye { - int x,y,val; + size_t x,y,val; }; struct brick { diff --git a/game.c b/game.c index 7c788fe..752b32b 100644 --- a/game.c +++ b/game.c @@ -12,13 +12,15 @@ #define CLAMP(x,a,b) (MIN(MAX(x,a),b)) size_t mouse_x = 0, mouse_y = 0; -int eyes_front = 0, eyes_back = 0; struct bricks bricks = {0}; struct brick hand[5]; size_t hand_count = 0; +struct brick active = {0}; +int has_active = 0; + void key_callback(int key, int scancode, int action, int mods) { if (action != GLFW_PRESS) return; switch (key) { @@ -35,15 +37,31 @@ void cursor_position_callback(int xpos, int ypos) { } void mouse_button_callback(int button, int action, int mods) { - if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { - printf("click!\n"); - if (++eyes_back >= NUM_DOMINO_X - 1) { - eyes_back = 0; - eyes_front = (eyes_front+1)%NUM_DOMINO_Y; + for (size_t i = 0; i < hand_count; i++) { + struct brick *b = &hand[i]; + if (!has_active) { + if (b->front.x <= mouse_x && mouse_x <= b->front.x + DOMINO_WIDTH && + b->front.y <= mouse_y && mouse_y <= b->front.y + DOMINO_HEIGHT + ) { + has_active = 1; + active = *b; + active.front.x -= mouse_x; + active.front.y -= mouse_y; + } + } else { + hand[i - 1] = hand[i]; + } } - printf("%d %d\n", eyes_front, eyes_back); + if (has_active) hand_count--; } + + printf("click!\n"); + if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { + has_active = 0; + hand[hand_count++] = active; + } + } void init() { @@ -64,12 +82,12 @@ void init() { hand_count = 3; hand[0] = (struct brick) { - .front = {.x = 12, .y = 5, .val = 2}, - .back = {.x = 13, .y = 5, .val = 5}, + .front = {.x = 12, .y = 5, .val = 0}, + .back = {.x = 13, .y = 5, .val = 3}, }; hand[1] = (struct brick) { - .front = {.x = 12, .y = 5, .val = 2}, - .back = {.x = 13, .y = 5, .val = 5}, + .front = {.x = 12, .y = 5, .val = 4}, + .back = {.x = 13, .y = 5, .val = 1}, }; hand[2] = (struct brick) { .front = {.x = 12, .y = 5, .val = 2}, @@ -98,11 +116,17 @@ void render(struct image canvas) { draw(canvas, domino[b->back.val][b->front.val], b->front.x * EYE_SIZE, b->front.y * EYE_SIZE); } + // hand for (size_t i = 0; i < hand_count; i++) { struct brick *b = &hand[i]; - draw(canvas, domino[b->back.val][b->front.val], (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4), canvas.height - DOMINO_WIDTH); + b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4); + b->front.y = canvas.height - DOMINO_WIDTH; + draw(canvas, domino[b->back.val][b->front.val], b->front.x, b->front.y); } - draw(canvas,domino[eyes_front][eyes_back], CLAMP(mouse_x, 0, canvas.width), CLAMP(mouse_y, 0, canvas.height)); + // active + if (has_active) { + draw(canvas,domino[active.back.val][active.front.val], CLAMP(mouse_x + active.front.x, 0, canvas.width), CLAMP(mouse_y + active.front.y, 0, canvas.height)); + } }