fp: Move game stuff into game files

This commit is contained in:
doyle 2023-09-23 12:33:59 +10:00
parent f9f3cec9cc
commit cc202cefac
4 changed files with 142 additions and 146 deletions

View File

@ -484,9 +484,9 @@ void TELY_DLL_Init(void *user_data)
hero->sprite_sheet = &game->hero_sprite_sheet; hero->sprite_sheet = &game->hero_sprite_sheet;
hero->sprite_anims = game->hero_sprite_anims; hero->sprite_anims = game->hero_sprite_anims;
hero->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size); hero->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size);
hero->flags |= FP_EntityFlag_Clickable; hero->flags |= FP_GameEntityFlag_Clickable;
hero->flags |= FP_EntityFlag_MoveByKeyboard; hero->flags |= FP_GameEntityFlag_MoveByKeyboard;
hero->flags |= FP_EntityFlag_MoveByMouse; hero->flags |= FP_GameEntityFlag_MoveByMouse;
game->clicked_entity = hero->handle; game->clicked_entity = hero->handle;
} }
@ -498,9 +498,9 @@ void TELY_DLL_Init(void *user_data)
enemy->sprite_sheet = &game->hero_sprite_sheet; enemy->sprite_sheet = &game->hero_sprite_sheet;
enemy->sprite_anims = game->hero_sprite_anims; enemy->sprite_anims = game->hero_sprite_anims;
enemy->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size); enemy->local_hit_box_size = Dqn_V2_InitV2I(game->hero_sprite_sheet.sprite_size);
enemy->flags |= FP_EntityFlag_Clickable; enemy->flags |= FP_GameEntityFlag_Clickable;
enemy->flags |= FP_EntityFlag_MoveByKeyboard; enemy->flags |= FP_GameEntityFlag_MoveByKeyboard;
enemy->flags |= FP_EntityFlag_MoveByMouse; enemy->flags |= FP_GameEntityFlag_MoveByMouse;
} }
// NOTE: Wall // NOTE: Wall
@ -508,9 +508,9 @@ void TELY_DLL_Init(void *user_data)
FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "V. Wall"); FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "V. Wall");
entity->local_pos = Dqn_V2_InitNx2(100.f, 300.f); entity->local_pos = Dqn_V2_InitNx2(100.f, 300.f);
entity->local_hit_box_size = Dqn_V2_InitNx2(100.f, 300.f); entity->local_hit_box_size = Dqn_V2_InitNx2(100.f, 300.f);
entity->flags |= FP_EntityFlag_Clickable; entity->flags |= FP_GameEntityFlag_Clickable;
entity->flags |= FP_EntityFlag_MoveByKeyboard; entity->flags |= FP_GameEntityFlag_MoveByKeyboard;
entity->flags |= FP_EntityFlag_MoveByMouse; entity->flags |= FP_GameEntityFlag_MoveByMouse;
FP_GameShape *wall = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes); FP_GameShape *wall = Dqn_FArray_Make(&entity->shapes, Dqn_ZeroMem_Yes);
wall->type = FP_GameShapeType_Rect; wall->type = FP_GameShapeType_Rect;
@ -524,115 +524,10 @@ void TELY_DLL_Init(void *user_data)
game->inter_italic_font = platform->func_load_font(assets, DQN_STRING8("Inter (Italic)"), DQN_STRING8("Data/Inter-Italic.otf"), font_size); game->inter_italic_font = platform->func_load_font(assets, DQN_STRING8("Inter (Italic)"), DQN_STRING8("Data/Inter-Italic.otf"), font_size);
game->jetbrains_mono_font = platform->func_load_font(assets, DQN_STRING8("JetBrains Mono NL (Regular)"), DQN_STRING8("Data/JetBrainsMonoNL-Regular.ttf"), font_size); game->jetbrains_mono_font = platform->func_load_font(assets, DQN_STRING8("JetBrains Mono NL (Regular)"), DQN_STRING8("Data/JetBrainsMonoNL-Regular.ttf"), font_size);
game->test_audio = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa")); game->test_audio = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa"));
game->tile_size = 37;
} }
struct AStarNode void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, TELY_PlatformInput *input)
{
Dqn_usize cost;
Dqn_usize heuristic;
Dqn_V2I came_from;
};
Dqn_f32 const FP_TILE_SIZE = 37.f;
Dqn_Slice<Dqn_V2I> AStarPathFind(Dqn_Arena *arena, TELY_Platform *platform, Dqn_V2I src_tile, Dqn_V2I dest_tile)
{
Dqn_DSMap<AStarNode> astar_info = Dqn_DSMap_Init<AStarNode>(128);
DQN_DEFER { Dqn_DSMap_Deinit(&astar_info); };
Dqn_FArray<Dqn_V2I, 128> frontier = {};
Dqn_FArray_Add(&frontier, src_tile);
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / FP_TILE_SIZE);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / FP_TILE_SIZE);
// NOTE: Initialise the starting cost
uint64_t src_tile_u64 = (DQN_CAST(uint64_t)src_tile.y << 32) | (DQN_CAST(uint64_t)src_tile.x << 0);
Dqn_DSMap_MakeKeyU64(&astar_info, src_tile_u64);
while (frontier.size) {
Dqn_V2I curr_tile = Dqn_FArray_PopFront(&frontier, 1);
if (curr_tile == dest_tile)
break;
Dqn_FArray<Dqn_V2I, 4> neighbours = {};
{
Dqn_V2I left = Dqn_V2I_InitNx2(curr_tile.x - 1, curr_tile.y);
Dqn_V2I right = Dqn_V2I_InitNx2(curr_tile.x + 1, curr_tile.y);
Dqn_V2I top = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y - 1);
Dqn_V2I bottom = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y + 1);
if (left.x >= 0)
Dqn_FArray_Add(&neighbours, left);
if (right.x <= tile_count_x)
Dqn_FArray_Add(&neighbours, right);
if (top.y >= 0)
Dqn_FArray_Add(&neighbours, top);
if (bottom.y <= tile_count_y)
Dqn_FArray_Add(&neighbours, bottom);
}
uint64_t const curr_tile_u64 = (DQN_CAST(uint64_t)curr_tile.y << 32) | (DQN_CAST(uint64_t)curr_tile.x << 0);
Dqn_usize const curr_cost = Dqn_DSMap_FindKeyU64(&astar_info, curr_tile_u64).value->cost;
for (Dqn_V2I next_tile : neighbours) {
Dqn_usize new_cost = curr_cost + 1;
uint64_t next_tile_u64 = (DQN_CAST(uint64_t)next_tile.y << 32) | (DQN_CAST(uint64_t)next_tile.x << 0);
Dqn_DSMapResult<AStarNode> next_cost_result = Dqn_DSMap_MakeKeyU64(&astar_info, next_tile_u64);
if (next_cost_result.found && new_cost >= next_cost_result.value->cost)
continue;
Dqn_usize manhattan_dist = DQN_ABS(dest_tile.x - next_tile.x) + DQN_ABS(dest_tile.y - next_tile.y);
next_cost_result.value->cost = new_cost;
next_cost_result.value->came_from = curr_tile;
next_cost_result.value->heuristic = new_cost + manhattan_dist;
struct AStarBinarySearchContext
{
Dqn_DSMap<AStarNode> *astar_info;
Dqn_f32 heuristic;
};
// TODO(doyle): Find the insert location into the frontier
bool inserted = false;
DQN_FOR_UINDEX(index, frontier.size) {
Dqn_V2I frontier_tile = frontier.data[index];
uint64_t frontier_tile_u64 = DQN_CAST(uint64_t)frontier_tile.y << 32 | DQN_CAST(uint64_t)frontier_tile.x << 0;
Dqn_usize frontier_heuristic = Dqn_DSMap_FindKeyU64(&astar_info, frontier_tile_u64).value->heuristic;
if (next_cost_result.value->heuristic >= frontier_heuristic)
continue;
Dqn_FArray_Insert(&frontier, index, next_tile);
inserted = true;
break;
}
if (inserted)
continue;
Dqn_FArray_Add(&frontier, next_tile);
}
}
Dqn_usize slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; slice_size++) {
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
Dqn_Slice<Dqn_V2I> result = Dqn_Slice_Alloc<Dqn_V2I>(arena, slice_size, Dqn_ZeroMem_No);
slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; ) {
result.data[slice_size++] = it;
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
DQN_ASSERT(result.size == slice_size);
return result;
}
void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *renderer, TELY_PlatformInput *input)
{ {
if (TELY_Platform_InputKeyIsReleased(input->mouse_left)) if (TELY_Platform_InputKeyIsReleased(input->mouse_left))
game->clicked_entity = game->prev_active_entity; game->clicked_entity = game->prev_active_entity;
@ -667,7 +562,7 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
Dqn_V2 acceleration = {}; Dqn_V2 acceleration = {};
if (game->clicked_entity == entity->handle && if (game->clicked_entity == entity->handle &&
(entity->action.state == FP_GameEntityState_Run || entity->action.state == FP_GameEntityState_Idle)) { (entity->action.state == FP_GameEntityState_Run || entity->action.state == FP_GameEntityState_Idle)) {
if (entity->flags & FP_EntityFlag_MoveByKeyboard) { if (entity->flags & FP_GameEntityFlag_MoveByKeyboard) {
acceleration = dir_vector * 10000000.f; acceleration = dir_vector * 10000000.f;
if (dir_vector.x) if (dir_vector.x)
entity->facing_left = dir_vector.x < 0.f; entity->facing_left = dir_vector.x < 0.f;
@ -675,7 +570,7 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
} }
// NOTE: Stalk entity ====================================================================== // NOTE: Stalk entity ======================================================================
#if 0 #if 1
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
if (entity->name == DQN_STRING8("Enemy")) { if (entity->name == DQN_STRING8("Enemy")) {
if (entity->handle != game->clicked_entity && entity->stalk_entity != game->clicked_entity) { if (entity->handle != game->clicked_entity && entity->stalk_entity != game->clicked_entity) {
@ -685,7 +580,7 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
FP_GameEntity *stalk_entity = FP_Game_GetEntity(game, entity->stalk_entity); FP_GameEntity *stalk_entity = FP_Game_GetEntity(game, entity->stalk_entity);
if (stalk_entity) { if (stalk_entity) {
Dqn_V2 stalk_world_pos = FP_Game_CalcEntityWorldPos(game, stalk_entity->handle); Dqn_V2 stalk_world_pos = FP_Game_CalcEntityWorldPos(game, stalk_entity->handle);
Dqn_V2I stalk_tile = Dqn_V2I_InitNx2(stalk_world_pos.x / FP_TILE_SIZE, stalk_world_pos.y / FP_TILE_SIZE); Dqn_V2I stalk_tile = Dqn_V2I_InitNx2(stalk_world_pos.x / game->tile_size, stalk_world_pos.y / game->tile_size);
if (entity->stalk_entity_last_known_tile != stalk_tile) { if (entity->stalk_entity_last_known_tile != stalk_tile) {
entity->stalk_entity_last_known_tile = stalk_tile; entity->stalk_entity_last_known_tile = stalk_tile;
@ -698,8 +593,8 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
entity->waypoints->next = entity->waypoints; entity->waypoints->next = entity->waypoints;
entity->waypoints->prev = entity->waypoints; entity->waypoints->prev = entity->waypoints;
Dqn_V2I entity_tile = Dqn_V2I_InitNx2(entity_world_pos.x / FP_TILE_SIZE, entity_world_pos.y / FP_TILE_SIZE); Dqn_V2I entity_tile = Dqn_V2I_InitNx2(entity_world_pos.x / game->tile_size, entity_world_pos.y / game->tile_size);
Dqn_Slice<Dqn_V2I> path_find = AStarPathFind(&platform->arena, platform, entity_tile, stalk_tile); Dqn_Slice<Dqn_V2I> path_find = FP_Game_AStarPathFind(game, &platform->frame_arena, platform, entity_tile, stalk_tile);
for (Dqn_usize index = path_find.size - 1; index < path_find.size; index--) { for (Dqn_usize index = path_find.size - 1; index < path_find.size; index--) {
FP_GameWaypoint *waypoint = TELY_ChunkPool_New(game->chunk_pool, FP_GameWaypoint); FP_GameWaypoint *waypoint = TELY_ChunkPool_New(game->chunk_pool, FP_GameWaypoint);
@ -713,15 +608,16 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
} }
} }
// NOTE: Render the waypoints
for (FP_GameWaypoint *waypoint = entity->waypoints->next; waypoint != entity->waypoints; waypoint = waypoint->next) { for (FP_GameWaypoint *waypoint = entity->waypoints->next; waypoint != entity->waypoints; waypoint = waypoint->next) {
Dqn_V2 circle_pos = Dqn_V2_InitNx2(waypoint->pos.x * FP_TILE_SIZE + FP_TILE_SIZE * .5f, waypoint->pos.y * FP_TILE_SIZE + FP_TILE_SIZE * .5f); Dqn_V2 circle_pos = Dqn_V2_InitNx2(waypoint->pos.x * game->tile_size + game->tile_size * .5f, waypoint->pos.y * game->tile_size + game->tile_size * .5f);
TELY_Render_CircleColourV4(renderer, circle_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_MAGENTA_V4); TELY_Render_CircleColourV4(renderer, circle_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_MAGENTA_V4);
} }
if (entity->waypoints->next != entity->waypoints) { if (entity->waypoints->next != entity->waypoints) {
FP_GameWaypoint *waypoint = entity->waypoints->next; FP_GameWaypoint *waypoint = entity->waypoints->next;
Dqn_V2I target_tile = entity->waypoints->next->pos; Dqn_V2I target_tile = entity->waypoints->next->pos;
Dqn_V2 target_pos = Dqn_V2_InitNx2(target_tile.x * FP_TILE_SIZE + FP_TILE_SIZE *.5f, target_tile.y * FP_TILE_SIZE + FP_TILE_SIZE * .5f); Dqn_V2 target_pos = Dqn_V2_InitNx2(target_tile.x * game->tile_size + game->tile_size *.5f, target_tile.y * game->tile_size + game->tile_size * .5f);
Dqn_V2 entity_to_target_pos = target_pos - entity_world_pos; Dqn_V2 entity_to_target_pos = target_pos - entity_world_pos;
if (Dqn_V2_LengthSq(entity_to_target_pos) < DQN_SQUARED(entity->local_hit_box_size.x * .5f)) { if (Dqn_V2_LengthSq(entity_to_target_pos) < DQN_SQUARED(entity->local_hit_box_size.x * .5f)) {
@ -814,14 +710,14 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
} }
// NOTE: Move entity by mouse ============================================================== // NOTE: Move entity by mouse ==============================================================
if (game->active_entity == entity->handle && entity->flags & FP_EntityFlag_MoveByMouse) { if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
if (entity->flags & FP_EntityFlag_MoveByMouse) { if (entity->flags & FP_GameEntityFlag_MoveByMouse) {
entity->velocity = {}; entity->velocity = {};
entity->local_pos += input->mouse_p_delta; entity->local_pos += input->mouse_p_delta;
} }
} }
if (entity->flags & FP_EntityFlag_DeriveHitBoxFromChildrenBoundingBox) { if (entity->flags & FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox) {
Dqn_Rect children_bbox = {}; Dqn_Rect children_bbox = {};
// TODO(doyle): Is the hit box supposed to include the containing // TODO(doyle): Is the hit box supposed to include the containing
@ -1008,24 +904,24 @@ void FP_GameUpdate(TELY_Platform *platform, FP_Game *game, TELY_Renderer *render
} }
} }
void FP_GameRender(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer) void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer)
{ {
TELY_PlatformInput *input = &platform->input; TELY_PlatformInput *input = &platform->input;
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform); Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p); Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p);
// NOTE: Draw tiles ============================================================================ // NOTE: Draw tiles ============================================================================
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / FP_TILE_SIZE); Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / game->tile_size);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / FP_TILE_SIZE); Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / game->tile_size);
for (Dqn_usize x = 0; x < tile_count_x; x++) { for (Dqn_usize x = 0; x < tile_count_x; x++) {
Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * FP_TILE_SIZE, 0); Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * game->tile_size, 0);
Dqn_V2 end = Dqn_V2_InitNx2(start.x, platform->core.window_size.h); Dqn_V2 end = Dqn_V2_InitNx2(start.x, platform->core.window_size.h);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f); TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
} }
for (Dqn_usize y = 0; y < tile_count_y; y++) { for (Dqn_usize y = 0; y < tile_count_y; y++) {
Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * FP_TILE_SIZE); Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * game->tile_size);
Dqn_V2 end = Dqn_V2_InitNx2(platform->core.window_size.w, start.y); Dqn_V2 end = Dqn_V2_InitNx2(platform->core.window_size.w, start.y);
TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f); TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f);
} }
@ -1102,14 +998,14 @@ void FP_GameRender(FP_Game *game, TELY_Platform *platform, TELY_Renderer *render
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
if (game->clicked_entity == entity->handle) { if (game->clicked_entity == entity->handle) {
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4); TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
} else if (game->hot_entity == entity->handle || (entity->flags & FP_EntityFlag_DrawHitBox)) { } else if (game->hot_entity == entity->handle || (entity->flags & FP_GameEntityFlag_DrawHitBox)) {
Dqn_V4 hot_colour = game->hot_entity == entity->handle ? TELY_COLOUR_RED_TOMATO_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_YELLOW_SANDY_V4, .5f); Dqn_V4 hot_colour = game->hot_entity == entity->handle ? TELY_COLOUR_RED_TOMATO_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_YELLOW_SANDY_V4, .5f);
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, hot_colour); TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, hot_colour);
} }
if (game->hot_entity == entity->handle) { if (game->hot_entity == entity->handle) {
if (entity->name.size) { if (entity->name.size) {
Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / FP_TILE_SIZE, world_pos.y / FP_TILE_SIZE); Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / game->tile_size, world_pos.y / game->tile_size);
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
Dqn_String8 label = Dqn_String8_InitF(scratch.allocator, Dqn_String8 label = Dqn_String8_InitF(scratch.allocator,
@ -1174,7 +1070,7 @@ void TELY_DLL_FrameUpdate(void *user_data)
if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0) if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0)
continue; continue;
if ((entity->flags & FP_EntityFlag_Clickable) == 0) if ((entity->flags & FP_GameEntityFlag_Clickable) == 0)
continue; continue;
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
@ -1189,8 +1085,8 @@ void TELY_DLL_FrameUpdate(void *user_data)
} }
} }
FP_GameUpdate(platform, game, renderer, input); FP_Update(platform, game, renderer, input);
FP_GameRender(game, platform, renderer); FP_Render(game, platform, renderer);
{ {
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
DQN_DEFER { TELY_Render_PopTransform(renderer); }; DQN_DEFER { TELY_Render_PopTransform(renderer); };

View File

@ -1,6 +1,6 @@
#if defined(__clang__) #if defined(__clang__)
#pragma once #pragma once
#include "playground_unity.h" #include "feely_pona_unity.h"
#endif #endif
static bool operator==(FP_GameEntityHandle const &lhs, FP_GameEntityHandle const &rhs) static bool operator==(FP_GameEntityHandle const &lhs, FP_GameEntityHandle const &rhs)
@ -449,3 +449,96 @@ static bool FP_Game_EntityActionHasFailed(FP_GameEntityAction const *action)
bool result = action ? action->flags & FP_GameEntityActionFlag_Failed : true; bool result = action ? action->flags & FP_GameEntityActionFlag_Failed : true;
return result; return result;
} }
static Dqn_Slice<Dqn_V2I> FP_Game_AStarPathFind(FP_Game *game, Dqn_Arena *arena, TELY_Platform *platform, Dqn_V2I src_tile, Dqn_V2I dest_tile)
{
Dqn_DSMap<FP_GameAStarNode> astar_info = Dqn_DSMap_Init<FP_GameAStarNode>(128);
DQN_DEFER { Dqn_DSMap_Deinit(&astar_info); };
Dqn_FArray<Dqn_V2I, 128> frontier = {};
Dqn_FArray_Add(&frontier, src_tile);
Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(platform->core.window_size.w / game->tile_size);
Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(platform->core.window_size.h / game->tile_size);
// NOTE: Initialise the starting cost
uint64_t src_tile_u64 = (DQN_CAST(uint64_t)src_tile.y << 32) | (DQN_CAST(uint64_t)src_tile.x << 0);
Dqn_DSMap_MakeKeyU64(&astar_info, src_tile_u64);
while (frontier.size) {
Dqn_V2I curr_tile = Dqn_FArray_PopFront(&frontier, 1);
if (curr_tile == dest_tile)
break;
Dqn_FArray<Dqn_V2I, 4> neighbours = {};
{
Dqn_V2I left = Dqn_V2I_InitNx2(curr_tile.x - 1, curr_tile.y);
Dqn_V2I right = Dqn_V2I_InitNx2(curr_tile.x + 1, curr_tile.y);
Dqn_V2I top = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y - 1);
Dqn_V2I bottom = Dqn_V2I_InitNx2(curr_tile.x, curr_tile.y + 1);
if (left.x >= 0)
Dqn_FArray_Add(&neighbours, left);
if (right.x <= tile_count_x)
Dqn_FArray_Add(&neighbours, right);
if (top.y >= 0)
Dqn_FArray_Add(&neighbours, top);
if (bottom.y <= tile_count_y)
Dqn_FArray_Add(&neighbours, bottom);
}
uint64_t const curr_tile_u64 = (DQN_CAST(uint64_t)curr_tile.y << 32) | (DQN_CAST(uint64_t)curr_tile.x << 0);
Dqn_usize const curr_cost = Dqn_DSMap_FindKeyU64(&astar_info, curr_tile_u64).value->cost;
for (Dqn_V2I next_tile : neighbours) {
Dqn_usize new_cost = curr_cost + 1;
uint64_t next_tile_u64 = (DQN_CAST(uint64_t)next_tile.y << 32) | (DQN_CAST(uint64_t)next_tile.x << 0);
Dqn_DSMapResult<FP_GameAStarNode> next_cost_result = Dqn_DSMap_MakeKeyU64(&astar_info, next_tile_u64);
if (next_cost_result.found && new_cost >= next_cost_result.value->cost)
continue;
Dqn_usize manhattan_dist = DQN_ABS(dest_tile.x - next_tile.x) + DQN_ABS(dest_tile.y - next_tile.y);
next_cost_result.value->cost = new_cost;
next_cost_result.value->came_from = curr_tile;
next_cost_result.value->heuristic = new_cost + manhattan_dist;
// TODO(doyle): Find the insert location into the frontier
bool inserted = false;
DQN_FOR_UINDEX(index, frontier.size) {
Dqn_V2I frontier_tile = frontier.data[index];
uint64_t frontier_tile_u64 = DQN_CAST(uint64_t)frontier_tile.y << 32 | DQN_CAST(uint64_t)frontier_tile.x << 0;
Dqn_usize frontier_heuristic = Dqn_DSMap_FindKeyU64(&astar_info, frontier_tile_u64).value->heuristic;
if (next_cost_result.value->heuristic >= frontier_heuristic)
continue;
Dqn_FArray_Insert(&frontier, index, next_tile);
inserted = true;
break;
}
if (inserted)
continue;
Dqn_FArray_Add(&frontier, next_tile);
}
}
Dqn_usize slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; slice_size++) {
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
Dqn_Slice<Dqn_V2I> result = Dqn_Slice_Alloc<Dqn_V2I>(arena, slice_size, Dqn_ZeroMem_No);
slice_size = 0;
for (Dqn_V2I it = dest_tile; it != src_tile; ) {
result.data[slice_size++] = it;
uint64_t key_u64 = (DQN_CAST(uint64_t)it.y << 32) | (DQN_CAST(uint64_t)it.x << 0);
it = Dqn_DSMap_FindKeyU64(&astar_info, key_u64).value->came_from;
}
DQN_ASSERT(result.size == slice_size);
return result;
}

View File

@ -3,13 +3,13 @@
#include "feely_pona_unity.h" #include "feely_pona_unity.h"
#endif #endif
enum FP_EntityFlag enum FP_GameEntityFlag
{ {
FP_EntityFlag_Clickable = 1 << 0, FP_GameEntityFlag_Clickable = 1 << 0,
FP_EntityFlag_MoveByKeyboard = 1 << 1, FP_GameEntityFlag_MoveByKeyboard = 1 << 1,
FP_EntityFlag_MoveByMouse = 1 << 2, FP_GameEntityFlag_MoveByMouse = 1 << 2,
FP_EntityFlag_DrawHitBox = 1 << 3, FP_GameEntityFlag_DrawHitBox = 1 << 3,
FP_EntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 4, FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox = 1 << 4,
}; };
enum FP_GameShapeType enum FP_GameShapeType
@ -131,6 +131,7 @@ struct FP_GameCamera
struct FP_Game struct FP_Game
{ {
uint16_t tile_size;
TELY_ChunkPool *chunk_pool; TELY_ChunkPool *chunk_pool;
TELY_AssetFontHandle inter_regular_font; TELY_AssetFontHandle inter_regular_font;
TELY_AssetFontHandle inter_italic_font; TELY_AssetFontHandle inter_italic_font;
@ -151,3 +152,10 @@ struct FP_Game
FP_GameCamera camera; FP_GameCamera camera;
TELY_RFui rfui; TELY_RFui rfui;
}; };
struct FP_GameAStarNode
{
Dqn_usize cost;
Dqn_usize heuristic;
Dqn_V2I came_from;
};

View File

@ -61,8 +61,7 @@ DQN_MSVC_WARNING_DISABLE(4505) // warning C4505: unreferenced function with inte
#include "External/tely/tely_rfui.cpp" #include "External/tely/tely_rfui.cpp"
// NOTE: feely_pona ================================================================================ // NOTE: feely_pona ================================================================================
#include "feely_pona_game.h"
#include "feely_pona.h"
#include "feely_pona_game.cpp" #include "feely_pona_game.cpp"
#include "feely_pona.cpp" #include "feely_pona.cpp"