feat: pick up bricks from hand

This commit is contained in:
orangerot 2025-10-14 17:47:34 +02:00
parent 9efddf8a1a
commit 38e3ab041c
2 changed files with 38 additions and 14 deletions

View file

@ -4,7 +4,7 @@
#define DOMINO_H
struct eye {
int x,y,val;
size_t x,y,val;
};
struct brick {

50
game.c
View file

@ -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));
}
}