#include #include #include #include #include #include #include "game.h" #include "domino.h" #include "draw.h" #include "assets/white_and_blue_dominoes.h" #include "assets/red_and_peach_dominoes.h" #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) #define CLAMP(x,a,b) (MIN(MAX(x,a),b)) const uint32_t glyph_heart = 0b00010101111111111011100010000000; const uint32_t glyph_flag = 0b00011000111001100010000100011100; const uint32_t glyph_monster = 0b00111011111101011111111111101010; int mouse_x = 0, mouse_y = 0; struct bricks bricks = {0}; struct brick hand[5]; size_t hand_count = 0; struct brick active = {0}; bool has_active = 0; struct bricks preview = {0}; int camera_x = 0; int camera_y = 0; bool is_dragging = 0; int goal_x = 5; int goal_y = 5; bool is_goal_reached = 0; struct enemy { int x, y, attack, health; }; struct enemy enemies[20] = {0}; size_t enemy_count = 0; void key_callback(int key, int scancode, int action, int mods) { (void) scancode; (void) mods; if (action != GLFW_PRESS) return; switch (key) { case GLFW_KEY_R: if (active.front.vertical) { struct eye tmp = active.front; active.front.val = active.back.val; active.back.val = tmp.val; } active.front.vertical = !active.front.vertical; brick_previews(active, bricks, &preview); printf("rotate\n"); break; case GLFW_KEY_ENTER: break; case GLFW_KEY_BACKSPACE: break; } } void cursor_position_callback(int xpos, int ypos) { if (is_dragging) { camera_x += xpos - mouse_x; camera_y += ypos - mouse_y; } mouse_x = xpos; mouse_y = ypos; } void mouse_button_callback(int button, int action, int mods) { (void) mods; printf("click!\n"); if (button == GLFW_MOUSE_BUTTON_RIGHT) { is_dragging = (action == GLFW_PRESS); } if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { // pick up brick from hand for (size_t i = 0; i < hand_count; i++) { struct brick *b = &hand[i]; if (has_active) { hand[i - 1] = hand[i]; continue; } 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; } } if (has_active) { hand_count--; brick_previews(active, bricks, &preview); } } if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { if (has_active) { has_active = 0; // get preview with minimal distance from active brick size_t min_dist = -1; for (size_t i = 0; i < preview.count; i++) { int active_x = active.front.x + mouse_x + DOMINO_WIDTH / 2; int active_y = active.front.y + mouse_y + DOMINO_HEIGHT / 2; int preview_x = camera_x + preview.items.brick[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2; int preview_y = camera_y + preview.items.brick[i].front.y * EYE_SIZE + DOMINO_HEIGHT / 2; preview.items.brick[i].front.val = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y); if (min_dist == (size_t)-1) min_dist = i; if (preview.items.brick[i].front.val < preview.items.brick[min_dist].front.val) min_dist = i; } if (preview.items.brick[min_dist].front.val < EYE_SIZE * EYE_SIZE && preview.count) { preview.items.brick[min_dist].front.val = active.front.val; preview.items.brick[min_dist].front.vertical = active.front.vertical; preview.items.brick[min_dist].back.val = active.back.val; bricks_append(&bricks, preview.items.brick[min_dist]); hand[hand_count++] = (struct brick) { .front = {.val = rand() % 6}, .back = {.val = rand() % 6}, }; if ((preview.items.brick[min_dist].front.x == goal_x && preview.items.brick[min_dist].front.y == goal_y) || (preview.items.brick[min_dist].back.x == goal_x && preview.items.brick[min_dist].back.y == goal_y)) { is_goal_reached = 1; } for (size_t i = 0; i < bricks.count; i++) brick_print(bricks.items.brick[i]); } else { hand[hand_count++] = active; } printf("dist: %d\n", preview.items.brick[min_dist].front.val); preview.count = 0; } } } void draw_enemy(struct image canvas, int attack, int health, size_t xpos, size_t ypos) { char a[] = {(attack + '0'), 0}; char h[] = {(health + '0'), 0}; draw_glyph(canvas, glyph_monster, camera_x + xpos * EYE_SIZE + 2, camera_y + ypos * EYE_SIZE + 3, (struct color) {255, 0, 0, 255}); draw_text(canvas, a, camera_x + xpos * EYE_SIZE + 8, camera_y + ypos * EYE_SIZE, (struct color) {255, 0, 0, 255}); draw_text(canvas, h, camera_x + xpos * EYE_SIZE + 8, camera_y + ypos * EYE_SIZE + 6, (struct color) {255, 0, 0, 255}); } void init(struct image canvas) { camera_x = canvas.width / 2; camera_y = canvas.height / 2; bricks_append( &bricks, (struct brick) { .front = {.x = 0, .y = 0, .val = rand() % 6}, .back = {.x = 1, .y = 0, .val = rand() % 6}, } ); for (size_t i = 0; i < 5; i++) { hand[hand_count++] = (struct brick) { .front = {.val = rand() % 6}, .back = {.val = rand() % 6}, }; } } void render(struct image canvas) { for (size_t i = 0; i < canvas.bufsize; i++) canvas.buf[i] = 0; // domino playground for (size_t i = 0; i < bricks.count; i++) { struct brick *b = &bricks.items.brick[i]; draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], camera_x + b->front.x * EYE_SIZE, camera_y + b->front.y * EYE_SIZE, b->front.vertical); } // preview for (size_t i = 0; i < preview.count; i++) { draw_image(canvas, white_and_blue_dominoes[active.back.val][active.front.val], camera_x + preview.items.brick[i].front.x * EYE_SIZE, camera_y + preview.items.brick[i].front.y * EYE_SIZE, active.front.vertical); } draw_glyph(canvas, glyph_flag, camera_x + goal_x * EYE_SIZE + 3, camera_y + goal_y * EYE_SIZE + 3, (struct color) {255, 255, 0, 255}); draw_enemy(canvas, 6, 3, 0, 3); // hand for (size_t i = 0; i < hand_count; i++) { struct brick *b = &hand[i]; b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4); b->front.y = canvas.height - DOMINO_WIDTH; draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y, b->front.vertical); } // active if (has_active) { draw_image(canvas, red_and_peach_dominoes[active.back.val][active.front.val], mouse_x + active.front.x, mouse_y + active.front.y, active.front.vertical); } // character // char title[] = "domino dungeon"; char inventory[] = "Inventory"; char game_over[] = "You reached the goal!"; // print(canvas, title, (canvas.width - (sizeof(title)-1) * 5) / 2, 5, (struct color) {255, 255, 255, 255}); draw_text(canvas, inventory, (canvas.width - (sizeof(inventory)-1) * 5) / 2, canvas.height - DOMINO_WIDTH - 10, (struct color) {255, 255, 255, 255}); draw_text(canvas, "Drag Dominos from your inventory onto the chain to", 3, 2, (struct color) {255, 255, 255, 255}); draw_text(canvas, "reach the goal. Press [R] to rotate", 3, 9, (struct color) {255, 255, 255, 255}); draw_glyph(canvas, glyph_heart, 3, 20, (struct color) {255, 0, 0, 255}); draw_text(canvas, "10", 10, 20, (struct color) {255, 255, 255, 255}); if (is_goal_reached) { draw_text(canvas, game_over, (canvas.width - (sizeof(game_over)-1) * 5) / 2, 20, (struct color) {255, 255, 255, 255}); } }