domino-dungeon/game.c

325 lines
11 KiB
C
Raw Normal View History

2025-10-13 21:18:09 +02:00
#include <GLFW/glfw3.h>
2025-10-14 14:48:54 +02:00
#include <stddef.h>
2025-10-14 06:17:32 +02:00
#include <stdint.h>
#include <stdio.h>
2025-10-15 10:40:39 +02:00
#include <stdbool.h>
2025-10-15 15:50:18 +02:00
#include <stdlib.h>
2025-10-13 21:18:09 +02:00
#include "game.h"
2025-10-14 14:48:54 +02:00
#include "domino.h"
2025-10-18 17:32:37 +02:00
#include "draw.h"
2025-10-14 21:39:22 +02:00
#include "assets/white_and_blue_dominoes.h"
#include "assets/red_and_peach_dominoes.h"
2025-10-13 21:18:09 +02:00
2025-10-14 04:11:36 +02:00
#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))
2025-10-21 14:57:37 +02:00
#define VEC2_EQ(a,b) ((a).x == (b).x && (a).y == (b).y)
2025-10-13 21:18:09 +02:00
2025-10-18 17:32:37 +02:00
const uint32_t glyph_heart = 0b00010101111111111011100010000000;
const uint32_t glyph_flag = 0b00011000111001100010000100011100;
const uint32_t glyph_monster = 0b00111011111101011111111111101010;
2025-10-21 14:57:37 +02:00
const uint32_t glyph_sword = 0b00000010001110110011001101010000;
const uint32_t glyph_shield = 0b00111111000110001100010101000100;
2025-10-18 17:32:37 +02:00
2025-10-21 14:57:37 +02:00
struct vec2 {int x,y;};
struct vec2 mouse = {0,0};
2025-10-14 04:11:36 +02:00
2025-10-14 14:48:54 +02:00
struct bricks bricks = {0};
2025-10-14 16:32:45 +02:00
struct brick hand[5];
size_t hand_count = 0;
2025-10-14 17:47:34 +02:00
struct brick active = {0};
2025-10-15 10:40:39 +02:00
bool has_active = 0;
2025-10-14 17:47:34 +02:00
2025-10-18 17:32:37 +02:00
struct bricks preview = {0};
2025-10-15 07:18:13 +02:00
2025-10-21 14:57:37 +02:00
struct vec2 camera = {0,0};
2025-10-15 14:08:17 +02:00
bool is_dragging = 0;
2025-10-21 14:57:37 +02:00
struct vec2 goal = {5,5};
2025-10-15 15:50:18 +02:00
bool is_goal_reached = 0;
2025-10-15 17:01:30 +02:00
struct enemy {
int x, y, attack, health;
2025-10-21 14:57:37 +02:00
bool is_reachable;
2025-10-15 17:01:30 +02:00
};
struct enemy enemies[20] = {0};
size_t enemy_count = 0;
2025-11-10 19:27:11 +01:00
size_t player_health = 10;
char player_health_s[4] = " ";
2025-10-15 10:40:39 +02:00
2025-10-14 04:11:36 +02:00
void key_callback(int key, int scancode, int action, int mods) {
2025-10-15 10:40:39 +02:00
(void) scancode;
(void) mods;
2025-10-13 21:18:09 +02:00
if (action != GLFW_PRESS) return;
switch (key) {
2025-10-15 10:40:39 +02:00
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;
2025-10-18 17:32:37 +02:00
brick_previews(active, bricks, &preview);
2025-10-15 10:40:39 +02:00
printf("rotate\n");
break;
2025-10-13 21:18:09 +02:00
case GLFW_KEY_ENTER:
break;
case GLFW_KEY_BACKSPACE:
break;
}
}
2025-10-14 04:11:36 +02:00
void cursor_position_callback(int xpos, int ypos) {
2025-10-15 14:08:17 +02:00
if (is_dragging) {
2025-10-21 14:57:37 +02:00
camera.x += xpos - mouse.x;
camera.y += ypos - mouse.y;
2025-10-15 14:08:17 +02:00
}
2025-10-21 14:57:37 +02:00
mouse.x = xpos;
mouse.y = ypos;
}
2025-11-10 19:27:11 +01:00
void enemy_closest_get(const struct enemy *enemies, const size_t enemy_count, size_t *closest_i, size_t *min_dist) {
*closest_i = -1;
*min_dist = (size_t)-1;
for (size_t i = 0; i < enemy_count; i++) {
if (!enemies[i].is_reachable) continue;
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 + enemies[i].x * EYE_SIZE + EYE_SIZE / 2;
int preview_y = camera.y + enemies[i].y * EYE_SIZE + EYE_SIZE / 2;
size_t dist = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
if (*closest_i == (size_t)-1) *closest_i = i;
if (dist < *min_dist) {
*closest_i = i;
*min_dist = dist;
}
}
}
void brick_preview_closest_get(const struct bricks preview, struct brick *closest, size_t *min_dist) {
2025-10-21 14:57:37 +02:00
size_t min_dist_i = -1;
*min_dist = (size_t)-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;
size_t dist = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
if (min_dist_i == (size_t)-1) min_dist_i = i;
if (dist < *min_dist) {
min_dist_i = i;
*min_dist = dist;
}
}
*closest = preview.items.brick[min_dist_i];
2025-10-14 04:11:36 +02:00
}
void mouse_button_callback(int button, int action, int mods) {
2025-10-15 10:40:39 +02:00
(void) mods;
2025-10-18 17:32:37 +02:00
printf("click!\n");
2025-10-15 10:40:39 +02:00
2025-10-15 14:08:17 +02:00
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
is_dragging = (action == GLFW_PRESS);
}
2025-10-14 06:17:32 +02:00
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
2025-10-15 07:18:13 +02:00
// pick up brick from hand
2025-10-14 17:47:34 +02:00
for (size_t i = 0; i < hand_count; i++) {
struct brick *b = &hand[i];
2025-10-18 17:32:37 +02:00
if (has_active) {
2025-10-14 17:47:34 +02:00
hand[i - 1] = hand[i];
2025-10-18 17:32:37 +02:00
continue;
}
2025-10-21 14:57:37 +02:00
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
2025-10-18 17:32:37 +02:00
) {
has_active = 1;
active = *b;
2025-10-21 14:57:37 +02:00
active.front.x -= mouse.x;
active.front.y -= mouse.y;
2025-10-14 17:47:34 +02:00
}
2025-10-14 06:17:32 +02:00
}
2025-10-15 07:18:13 +02:00
if (has_active) {
hand_count--;
2025-10-18 17:32:37 +02:00
brick_previews(active, bricks, &preview);
2025-10-15 07:18:13 +02:00
}
2025-10-14 06:17:32 +02:00
}
2025-10-14 17:47:34 +02:00
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
2025-10-14 21:39:22 +02:00
if (has_active) {
has_active = 0;
2025-10-15 07:18:13 +02:00
2025-10-18 17:32:37 +02:00
// get preview with minimal distance from active brick
2025-11-10 19:27:11 +01:00
size_t brick_preview_min_dist = -1;
struct brick brick_preview_closest = {0};
brick_preview_closest_get(preview, &brick_preview_closest, &brick_preview_min_dist);
size_t enemy_min_dist = -1;
size_t enemy_closest_i = -1;
if (active.front.vertical) enemy_closest_get(enemies, enemy_count, &enemy_closest_i, &enemy_min_dist);
if (enemy_min_dist < brick_preview_min_dist && enemy_min_dist < EYE_SIZE * EYE_SIZE && enemy_count) {
enemies[enemy_closest_i].health -= active.front.val + 1;
if (enemies[enemy_closest_i].health <= 0) {
enemies[enemy_closest_i].health = 0;
for (size_t i = enemy_closest_i + 1; i < enemy_count; i++) {
enemies[i - 1] = enemies[i];
}
enemy_count--;
enemies[enemy_count++] = (struct enemy) {
.attack = rand() % 6 + 1,
.health = rand() % 6 + 1,
.x = bricks.items.brick[bricks.count - 1].front.x + rand() % 8 - 4,
.y = bricks.items.brick[bricks.count - 1].front.y + rand() % 8 - 4,
.is_reachable = 0,
};
for (size_t i = 0; i < bricks.count * 2; i++) {
if (VEC2_EQ(bricks.items.eye[i], enemies[enemy_count - 1])) {
enemies[enemy_count - 1].is_reachable = 1;
break;
}
}
for (size_t i = 0; i < 3; i++) {
hand[hand_count = MIN(hand_count + 1, 4)] = (struct brick) {
.front = {.val = rand() % 6},
.back = {.val = rand() % 6},
};
}
} else {
player_health -= enemies[enemy_closest_i].attack - (active.back.val + 1);
sprintf(player_health_s, "%zu", player_health);
}
} else if (brick_preview_min_dist < EYE_SIZE * EYE_SIZE && preview.count) {
bricks_append(&bricks, brick_preview_closest);
2025-10-21 14:57:37 +02:00
for (size_t i = 0; i < enemy_count; i++) {
2025-11-10 19:27:11 +01:00
enemies[i].is_reachable |= VEC2_EQ(brick_preview_closest.front, enemies[i]) || VEC2_EQ(brick_preview_closest.back, enemies[i]);
2025-10-21 14:57:37 +02:00
}
2025-11-10 19:27:11 +01:00
// hand[hand_count++] = (struct brick) {
// .front = {.val = rand() % 6},
// .back = {.val = rand() % 6},
// };
is_goal_reached |= VEC2_EQ(brick_preview_closest.front, goal) || VEC2_EQ(brick_preview_closest.back, goal);
2025-10-18 17:32:37 +02:00
for (size_t i = 0; i < bricks.count; i++) brick_print(bricks.items.brick[i]);
2025-10-15 07:18:13 +02:00
} else {
hand[hand_count++] = active;
}
2025-11-10 19:27:11 +01:00
printf("dist: %d\n", brick_preview_closest.front.val);
2025-10-18 17:32:37 +02:00
preview.count = 0;
2025-10-14 21:39:22 +02:00
}
2025-10-14 17:47:34 +02:00
}
2025-10-14 04:11:36 +02:00
}
2025-10-21 14:57:37 +02:00
void draw_enemy(struct image canvas, struct enemy enemy, struct color color) {
char attack[] = {(enemy.attack + '0'), 0};
char health[] = {(enemy.health + '0'), 0};
int x = camera.x + enemy.x * EYE_SIZE;
int y = camera.y + enemy.y * EYE_SIZE;
draw_glyph(canvas, glyph_monster, x + 2, y + 3, color);
draw_text(canvas , attack , x + 8, y + 0, color);
draw_text(canvas , health , x + 8, y + 6, color);
2025-10-18 17:32:37 +02:00
}
2025-10-15 15:11:47 +02:00
void init(struct image canvas) {
2025-10-21 14:57:37 +02:00
camera.x = canvas.width / 2;
camera.y = canvas.height / 2;
2025-10-15 15:11:47 +02:00
2025-11-10 19:27:11 +01:00
sprintf(player_health_s, "%zu", player_health);
2025-10-14 14:48:54 +02:00
bricks_append(
&bricks,
(struct brick) {
2025-10-15 15:50:18 +02:00
.front = {.x = 0, .y = 0, .val = rand() % 6},
.back = {.x = 1, .y = 0, .val = rand() % 6},
2025-10-14 14:48:54 +02:00
}
);
2025-10-14 16:32:45 +02:00
2025-10-15 15:50:18 +02:00
for (size_t i = 0; i < 5; i++) {
hand[hand_count++] = (struct brick) {
.front = {.val = rand() % 6},
2025-10-15 16:07:56 +02:00
.back = {.val = rand() % 6},
2025-10-15 15:50:18 +02:00
};
}
2025-10-21 14:57:37 +02:00
enemies[enemy_count++] = (struct enemy) {
.x = 0,
.y = 3,
.attack = 6,
.health = 2,
.is_reachable = 0
};
2025-10-14 16:32:45 +02:00
}
void render(struct image canvas) {
2025-11-10 19:27:11 +01:00
for (size_t i = 0; i < canvas.bufsize; i++) canvas.color[i] = (struct color) {0x12, 0x0b, 0x10, 0xFF};
2025-10-14 14:48:54 +02:00
2025-10-14 16:32:45 +02:00
// domino playground
2025-10-14 14:48:54 +02:00
for (size_t i = 0; i < bricks.count; i++) {
struct brick *b = &bricks.items.brick[i];
2025-10-21 14:57:37 +02:00
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);
2025-10-14 21:39:22 +02:00
}
2025-10-15 14:08:17 +02:00
// preview
2025-10-18 17:32:37 +02:00
for (size_t i = 0; i < preview.count; i++) {
2025-10-21 14:57:37 +02:00
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);
2025-10-14 14:48:54 +02:00
}
2025-10-14 06:17:32 +02:00
2025-10-21 14:57:37 +02:00
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});
2025-10-15 16:25:55 +02:00
2025-10-21 14:57:37 +02:00
for (size_t i = 0; i < enemy_count; i++) {
2025-11-10 19:27:11 +01:00
// struct color color = {100, 0, 0, 255};
struct color color = {0xFF, 0x00, 0x55, 0xFF};
if (enemies[i].is_reachable) {
2025-10-21 14:57:37 +02:00
color = (struct color) {255, 0, 0, 255};
2025-11-10 19:27:11 +01:00
if (has_active) {
color = (struct color) {0, 0, 255, 255};
}
2025-10-21 14:57:37 +02:00
}
draw_enemy(canvas, enemies[i], color);
}
2025-10-15 17:01:30 +02:00
2025-10-14 17:47:34 +02:00
// hand
2025-10-14 16:32:45 +02:00
for (size_t i = 0; i < hand_count; i++) {
struct brick *b = &hand[i];
2025-10-14 17:47:34 +02:00
b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4);
b->front.y = canvas.height - DOMINO_WIDTH;
2025-10-18 17:32:37 +02:00
draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y, b->front.vertical);
2025-10-14 06:17:32 +02:00
}
2025-10-14 16:32:45 +02:00
2025-10-14 17:47:34 +02:00
// active
if (has_active) {
2025-10-21 14:57:37 +02:00
int x = mouse.x + active.front.x;
int y = mouse.y + active.front.y;
draw_image(canvas, red_and_peach_dominoes[active.back.val][active.front.val], x, y, active.front.vertical);
if (active.front.vertical) {
draw_glyph(canvas, glyph_sword, x - 5, y, (struct color) {0, 0, 255, 255});
draw_glyph(canvas, glyph_shield, x - 5, y + EYE_SIZE, (struct color) {0, 255, 0, 255});
}
2025-10-14 17:47:34 +02:00
}
2025-10-14 22:09:59 +02:00
// character
2025-10-15 15:50:18 +02:00
// char title[] = "domino dungeon";
2025-10-15 15:11:47 +02:00
char inventory[] = "Inventory";
2025-10-15 15:50:18 +02:00
char game_over[] = "You reached the goal!";
2025-10-15 15:11:47 +02:00
// print(canvas, title, (canvas.width - (sizeof(title)-1) * 5) / 2, 5, (struct color) {255, 255, 255, 255});
2025-10-18 17:32:37 +02:00
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});
2025-11-10 19:27:11 +01:00
draw_text(canvas, player_health_s, 10, 20, (struct color) {255, 255, 255, 255});
2025-10-15 17:01:30 +02:00
2025-10-15 15:50:18 +02:00
if (is_goal_reached) {
2025-10-18 17:32:37 +02:00
draw_text(canvas, game_over, (canvas.width - (sizeof(game_over)-1) * 5) / 2, 20, (struct color) {255, 255, 255, 255});
2025-10-15 15:50:18 +02:00
}
2025-10-13 21:18:09 +02:00
}
2025-10-14 16:32:45 +02:00