2023-09-16 02:21:24 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma once
|
|
|
|
#include "feely_pona_unity.h"
|
|
|
|
#endif
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
Dqn_f32 const PHYSICS_STEP = 1 / 60.f;
|
2023-09-24 07:01:21 +00:00
|
|
|
struct FP_GlobalAnimations
|
|
|
|
{
|
2023-09-24 08:05:28 +00:00
|
|
|
Dqn_String8 terry_walk_idle = DQN_STRING8("terry_walk_idle");
|
|
|
|
Dqn_String8 terry_walk_up = DQN_STRING8("terry_walk_up");
|
|
|
|
Dqn_String8 terry_walk_down = DQN_STRING8("terry_walk_down");
|
|
|
|
Dqn_String8 terry_walk_left = DQN_STRING8("terry_walk_left");
|
|
|
|
Dqn_String8 terry_walk_right = DQN_STRING8("terry_walk_right");
|
|
|
|
Dqn_String8 terry_attack_up = DQN_STRING8("terry_attack_up");
|
|
|
|
Dqn_String8 terry_attack_side = DQN_STRING8("terry_attack_side");
|
|
|
|
Dqn_String8 terry_attack_down = DQN_STRING8("terry_attack_down");
|
|
|
|
Dqn_String8 terry_merchant = DQN_STRING8("terry_merchant");
|
|
|
|
Dqn_String8 smoochie_walk_up = DQN_STRING8("smoochie_walk_up");
|
|
|
|
Dqn_String8 smoochie_walk_down = DQN_STRING8("smoochie_walk_down");
|
|
|
|
Dqn_String8 smoochie_walk_left = DQN_STRING8("smoochie_walk_left");
|
|
|
|
Dqn_String8 smoochie_walk_right = DQN_STRING8("smoochie_walk_right");
|
|
|
|
Dqn_String8 smoochie_attack_down = DQN_STRING8("smoochie_attack");
|
|
|
|
Dqn_String8 smoochie_attack_side = DQN_STRING8("smoochie_attack_side");
|
|
|
|
Dqn_String8 smoochie_attack_heart = DQN_STRING8("smoochie_attack_heart");
|
|
|
|
Dqn_String8 smoochie_death = DQN_STRING8("smoochie_death");
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
g_anim_names;
|
|
|
|
|
2023-09-24 05:44:52 +00:00
|
|
|
TELY_AssetSpriteSheet FP_LoadSpriteSheetFromSpec(TELY_Platform *platform, TELY_Assets *assets, Dqn_Arena *arena, Dqn_String8 sheet_name)
|
2023-09-24 04:20:27 +00:00
|
|
|
{
|
2023-09-24 05:44:52 +00:00
|
|
|
TELY_AssetSpriteSheet result = {};
|
|
|
|
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(arena);
|
|
|
|
result.sprite_size = Dqn_V2I_InitNx2(185, 170);
|
|
|
|
result.type = TELY_AssetSpriteSheetType_Rects;
|
2023-09-24 04:20:27 +00:00
|
|
|
|
|
|
|
// NOTE: Load the sprite meta file =========================================================
|
|
|
|
Dqn_String8 sprite_spec_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s.txt", DQN_STRING_FMT(assets->textures_dir), DQN_STRING_FMT(sheet_name));
|
|
|
|
Dqn_String8 sprite_spec_buffer = platform->func_load_file(scratch.arena, sprite_spec_path);
|
|
|
|
Dqn_String8SplitAllocResult lines = Dqn_String8_SplitAlloc(scratch.allocator, sprite_spec_buffer, DQN_STRING8("\n"));
|
|
|
|
Dqn_usize sprite_rect_index = 0;
|
|
|
|
Dqn_usize sprite_anim_index = 0;
|
|
|
|
|
|
|
|
DQN_FOR_UINDEX(line_index, lines.size) {
|
|
|
|
Dqn_String8 line = lines.data[line_index];
|
|
|
|
Dqn_String8SplitAllocResult line_splits = Dqn_String8_SplitAlloc(scratch.allocator, line, DQN_STRING8(";"));
|
|
|
|
|
|
|
|
if (line_index == 0) {
|
|
|
|
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @file lines");
|
|
|
|
DQN_ASSERT(Dqn_String8_StartsWith(line_splits.data[0], DQN_STRING8("@file"), Dqn_String8EqCase_Sensitive));
|
|
|
|
|
|
|
|
// NOTE: Sprite sheet path
|
|
|
|
Dqn_String8 sprite_sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s", DQN_STRING_FMT(assets->textures_dir), DQN_STRING_FMT(line_splits.data[1]));
|
2023-09-24 05:44:52 +00:00
|
|
|
result.tex_handle = platform->func_load_texture(assets, sheet_name, sprite_sheet_path);
|
2023-09-24 04:20:27 +00:00
|
|
|
DQN_ASSERTF(Dqn_Fs_Exists(sprite_sheet_path), "Required file does not exist '%.*s'", DQN_STRING_FMT(sprite_sheet_path));
|
|
|
|
|
|
|
|
// NOTE: Total sprite frame count
|
|
|
|
Dqn_String8ToU64Result total_frame_count = Dqn_String8_ToU64(line_splits.data[2], 0);
|
|
|
|
DQN_ASSERT(total_frame_count.success);
|
2023-09-24 05:44:52 +00:00
|
|
|
result.rects = Dqn_Slice_Alloc<Dqn_Rect>(arena, total_frame_count.value, Dqn_ZeroMem_No);
|
2023-09-24 04:20:27 +00:00
|
|
|
|
|
|
|
// NOTE: Total animation count
|
|
|
|
Dqn_String8ToU64Result total_anim_count = Dqn_String8_ToU64(line_splits.data[3], 0);
|
|
|
|
DQN_ASSERT(total_anim_count.success);
|
|
|
|
result.anims = Dqn_Slice_Alloc<TELY_AssetSpriteAnimation>(arena, total_anim_count.value, Dqn_ZeroMem_No);
|
|
|
|
|
|
|
|
// TODO: Sprite size?
|
|
|
|
|
|
|
|
// TODO: Texture name?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Dqn_String8_StartsWith(line, DQN_STRING8("@anim"))) {
|
|
|
|
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @anim lines");
|
|
|
|
Dqn_String8 anim_name = line_splits.data[1];
|
|
|
|
Dqn_String8ToU64Result frames_per_second = Dqn_String8_ToU64(line_splits.data[2], 0);
|
|
|
|
Dqn_String8ToU64Result frame_count = Dqn_String8_ToU64(line_splits.data[3], 0);
|
|
|
|
|
|
|
|
DQN_ASSERT(anim_name.size);
|
|
|
|
DQN_ASSERT(frame_count.success);
|
|
|
|
DQN_ASSERT(frames_per_second.success);
|
|
|
|
|
|
|
|
Dqn_Allocator allocator = Dqn_Arena_Allocator(arena);
|
|
|
|
TELY_AssetSpriteAnimation *anim = result.anims.data + sprite_anim_index++;
|
|
|
|
anim->label = Dqn_String8_Copy(allocator, anim_name);
|
|
|
|
anim->index = DQN_CAST(uint16_t)sprite_rect_index;
|
|
|
|
anim->count = DQN_CAST(uint16_t)frame_count.value;
|
2023-09-24 13:08:30 +00:00
|
|
|
anim->ms_per_frame = DQN_CAST(uint32_t)(1000.f / frames_per_second.value);
|
|
|
|
DQN_ASSERT(anim->ms_per_frame != 0);
|
2023-09-24 04:20:27 +00:00
|
|
|
} else {
|
|
|
|
DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for sprite frame lines");
|
|
|
|
Dqn_String8ToU64Result x = Dqn_String8_ToU64(line_splits.data[0], 0);
|
|
|
|
Dqn_String8ToU64Result y = Dqn_String8_ToU64(line_splits.data[1], 0);
|
|
|
|
Dqn_String8ToU64Result w = Dqn_String8_ToU64(line_splits.data[2], 0);
|
|
|
|
Dqn_String8ToU64Result h = Dqn_String8_ToU64(line_splits.data[3], 0);
|
|
|
|
|
|
|
|
DQN_ASSERT(x.success);
|
|
|
|
DQN_ASSERT(y.success);
|
|
|
|
DQN_ASSERT(w.success);
|
|
|
|
DQN_ASSERT(h.success);
|
|
|
|
|
2023-09-24 05:44:52 +00:00
|
|
|
result.rects.data[sprite_rect_index++] =
|
2023-09-24 04:20:27 +00:00
|
|
|
Dqn_Rect_InitNx4(DQN_CAST(Dqn_f32) x.value,
|
|
|
|
DQN_CAST(Dqn_f32) y.value,
|
|
|
|
DQN_CAST(Dqn_f32) w.value,
|
|
|
|
DQN_CAST(Dqn_f32) h.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:44:52 +00:00
|
|
|
DQN_ASSERT(result.rects.size == sprite_rect_index);
|
2023-09-24 04:20:27 +00:00
|
|
|
DQN_ASSERT(result.anims.size == sprite_anim_index);
|
2023-09-24 11:54:08 +00:00
|
|
|
|
2023-09-24 04:20:27 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
extern "C" __declspec(dllexport)
|
|
|
|
void TELY_DLL_Reload(void *user_data)
|
|
|
|
{
|
|
|
|
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
|
|
|
|
Dqn_Library_SetPointer(platform->core.dqn_lib);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport)
|
|
|
|
void TELY_DLL_Init(void *user_data)
|
|
|
|
{
|
|
|
|
TELY_Platform *platform = DQN_CAST(TELY_Platform *)user_data;
|
|
|
|
TELY_DLL_Reload(user_data);
|
|
|
|
|
2023-09-24 08:05:28 +00:00
|
|
|
FP_UnitTests(platform);
|
2023-09-16 02:21:24 +00:00
|
|
|
|
|
|
|
// NOTE: TELY Game =============================================================================
|
|
|
|
TELY_Assets *assets = &platform->assets;
|
2023-09-17 10:24:07 +00:00
|
|
|
FP_Game *game = Dqn_Arena_New(&platform->arena, FP_Game, Dqn_ZeroMem_Yes);
|
2023-09-17 10:53:13 +00:00
|
|
|
game->chunk_pool = &platform->chunk_pool;
|
2023-09-25 14:07:39 +00:00
|
|
|
game->meters_to_pixels = 65.416f;
|
|
|
|
|
2023-09-24 08:05:28 +00:00
|
|
|
platform->user_data = game;
|
2023-09-16 02:21:24 +00:00
|
|
|
{
|
2023-09-17 10:24:07 +00:00
|
|
|
TELY_AssetSpriteSheet *sheet = &game->hero_sprite_sheet;
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
|
|
|
|
Dqn_String8 sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/adventurer-v1.5-sheet.png", DQN_STRING_FMT(assets->textures_dir));
|
|
|
|
sheet->tex_handle = platform->func_load_texture(assets, DQN_STRING8("Hero"), sheet_path);
|
|
|
|
sheet->sprite_count = 109;
|
|
|
|
sheet->sprites_per_row = 7;
|
|
|
|
sheet->sprite_size = Dqn_V2I_InitNx2(50, 37);
|
|
|
|
|
|
|
|
TELY_AssetSpriteAnimation hero_anims[] = {
|
2023-09-24 13:08:30 +00:00
|
|
|
{DQN_STRING8("Everything"), /*index*/ 0, /*count*/ sheet->sprite_count, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Idle"), /*index*/ 0, /*count*/ 3, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 4.f)},
|
|
|
|
{DQN_STRING8("Run"), /*index*/ 8, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 8.f)},
|
|
|
|
{DQN_STRING8("Jump"), /*index*/ 14, /*count*/ 10, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Floor slide"), /*index*/ 24, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Unknown"), /*index*/ 29, /*count*/ 9, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Attack A"), /*index*/ 42, /*count*/ 7, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Attack B"), /*index*/ 49, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 8.f)},
|
|
|
|
{DQN_STRING8("Attack C"), /*index*/ 53, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Hurt A"), /*index*/ 59, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Hurt B"), /*index*/ 64, /*count*/ 5, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Unsheath sword"), /*index*/ 69, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Sheath sword"), /*index*/ 73, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Air drift"), /*index*/ 77, /*count*/ 2, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Air drop"), /*index*/ 79, /*count*/ 2, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Ladder climb"), /*index*/ 81, /*count*/ 4, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Chi push"), /*index*/ 85, /*count*/ 8, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Leap slice A"), /*index*/ 93, /*count*/ 7, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Leap slice B"), /*index*/ 100, /*count*/ 3, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
|
|
|
{DQN_STRING8("Leap slice C"), /*index*/ 103, /*count*/ 6, /*ms_per_frame*/ DQN_CAST(uint32_t)(1000.f / 12.f)},
|
2023-09-16 02:21:24 +00:00
|
|
|
};
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
game->hero_sprite_anims = Dqn_Slice_Alloc<TELY_AssetSpriteAnimation>(&platform->arena, DQN_ARRAY_UCOUNT(hero_anims), Dqn_ZeroMem_No);
|
|
|
|
DQN_MEMCPY(game->hero_sprite_anims.data, &hero_anims, sizeof(hero_anims[0]) * DQN_ARRAY_UCOUNT(hero_anims));
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 04:20:27 +00:00
|
|
|
// NOTE: Load sprite sheets ====================================================================
|
2023-09-23 11:21:08 +00:00
|
|
|
{
|
2023-09-24 11:54:08 +00:00
|
|
|
game->terry_sprite_sheet = FP_LoadSpriteSheetFromSpec(platform, assets, &platform->arena, DQN_STRING8("terry_resized_25%"));
|
|
|
|
game->terry_merchant_sprite_sheet = FP_LoadSpriteSheetFromSpec(platform, assets, &platform->arena, DQN_STRING8("terry_merchant_resized_25%"));
|
|
|
|
game->smoochie_sprite_sheet = FP_LoadSpriteSheetFromSpec(platform, assets, &platform->arena, DQN_STRING8("smoochie_resized_25%"));
|
2023-09-23 11:21:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
game->entities = Dqn_VArray_Init<FP_GameEntity>(&platform->arena, 1024 * 8);
|
2023-09-18 11:46:42 +00:00
|
|
|
game->root_entity = Dqn_VArray_Make(&game->entities, Dqn_ZeroMem_No);
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
|
|
|
|
|
2023-09-16 07:32:25 +00:00
|
|
|
// NOTE: Hero
|
2023-09-16 02:21:24 +00:00
|
|
|
{
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_GameEntityHandle terry = FP_Entity_CreateTerry(game, Dqn_V2_InitNx2(1334, 396), "Terry");
|
|
|
|
game->clicked_entity = terry;
|
|
|
|
game->player = terry;
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateMerchant(game, Dqn_V2_InitNx2(1000, 124), "Merchant");
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-23 05:44:37 +00:00
|
|
|
game->tile_size = 37;
|
|
|
|
Dqn_V2I max_tile = platform->core.window_size / game->tile_size;
|
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
// NOTE: Wall ==================================================================================
|
2023-09-24 07:01:21 +00:00
|
|
|
if (0) {
|
2023-09-23 05:44:37 +00:00
|
|
|
Dqn_V2I vert_wall_tile_size = Dqn_V2I_InitNx2(1, 12);
|
|
|
|
Dqn_V2I right_wall_tile_pos = Dqn_V2I_InitNx2(max_tile.x - vert_wall_tile_size.x - 0, (max_tile.y / 2.f) - (vert_wall_tile_size.y * .5f));
|
|
|
|
|
|
|
|
Dqn_V2I left_wall_top_tile = Dqn_V2I_InitNx2(max_tile.x - vert_wall_tile_size.x - 12, (max_tile.y / 2.f) - (vert_wall_tile_size.y * .5f));
|
|
|
|
Dqn_V2I left_wall_bottom_tile = Dqn_V2I_InitNx2(left_wall_top_tile.x, left_wall_top_tile.y + vert_wall_tile_size.y);
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Dqn_V2I const vert_wall_part_tile_size = Dqn_V2I_InitNx2(1, (vert_wall_tile_size.y / 2) - 2);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base left-top wall"), left_wall_top_tile, vert_wall_part_tile_size);
|
2023-09-16 09:06:16 +00:00
|
|
|
|
2023-09-23 05:44:37 +00:00
|
|
|
Dqn_V2I bottom_part_tile = Dqn_V2I_InitNx2(left_wall_top_tile.x, left_wall_top_tile.y + vert_wall_tile_size.y - vert_wall_part_tile_size.y);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base left-bottom wall"), bottom_part_tile, vert_wall_part_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_GameEntityHandle right_wall = FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base right wall"), right_wall_tile_pos, vert_wall_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
Dqn_Rect right_wall_box = FP_Game_CalcEntityWorldHitBox(game, right_wall);
|
|
|
|
Dqn_V2I right_wall_bottom_tile = FP_Game_WorldPosToTilePos(game, right_wall_box.pos + Dqn_V2_InitNx2(0, right_wall_box.size.y));
|
|
|
|
Dqn_V2I right_wall_top_left_tile = FP_Game_WorldPosToTilePos(game, right_wall_box.pos);
|
|
|
|
|
|
|
|
{
|
|
|
|
Dqn_V2I hori_wall_tile_size = Dqn_V2I_InitNx2((right_wall_tile_pos.x - left_wall_top_tile.x - 1) / 2 - 1, 1);
|
|
|
|
{
|
|
|
|
Dqn_V2I bottom_left_wall_tile_pos = Dqn_V2I_InitNx2(left_wall_bottom_tile.x + 1, left_wall_bottom_tile.y - 1);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base bottom-left wall"), bottom_left_wall_tile_pos, hori_wall_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
|
|
|
|
Dqn_V2I bottom_right_wall_tile_pos = Dqn_V2I_InitNx2(right_wall_bottom_tile.x - hori_wall_tile_size.x, right_wall_bottom_tile.y - 1);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base bottom-right wall"), bottom_right_wall_tile_pos, hori_wall_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Dqn_V2I top_left_wall_tile_pos = Dqn_V2I_InitNx2(left_wall_top_tile.x + 1, left_wall_top_tile.y);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base top-left wall"), top_left_wall_tile_pos, hori_wall_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
|
|
|
|
Dqn_V2I top_right_wall_tile_pos = Dqn_V2I_InitNx2(right_wall_top_left_tile.x - hori_wall_tile_size.x, right_wall_top_left_tile.y);
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Entity_CreateWallAtTile(game, DQN_STRING8("Base top-right wall"), top_right_wall_tile_pos, hori_wall_tile_size);
|
2023-09-23 05:44:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-16 09:06:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 04:20:27 +00:00
|
|
|
// NOTE: Mob spawner ===========================================================================
|
|
|
|
{
|
2023-09-25 14:07:39 +00:00
|
|
|
Dqn_V2 mob_spawner_pos = FP_Game_MetersToPixelsNx2(game, 2.f, 5.f);
|
|
|
|
FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, mob_spawner_pos, 16 /*spawn_cap*/, "Mob spawner");
|
2023-09-25 12:13:32 +00:00
|
|
|
FP_Game_PushParentEntity(game, mob_spawner);
|
2023-09-25 14:07:39 +00:00
|
|
|
FP_Entity_CreateWaypointF(game, FP_Game_MetersToPixelsNx2(game, 2.f, 4.f), "Waypoint A");
|
|
|
|
FP_Entity_CreateWaypointF(game, FP_Game_MetersToPixelsNx2(game, 14.f, 8.f), "Waypoint B");
|
2023-09-24 09:11:44 +00:00
|
|
|
FP_Game_PopParentEntity(game);
|
2023-09-23 06:42:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 02:33:40 +00:00
|
|
|
uint16_t font_size = 18;
|
2023-09-16 02:21:24 +00:00
|
|
|
game->camera.scale = Dqn_V2_InitNx1(1);
|
2023-09-17 10:24:07 +00:00
|
|
|
game->inter_regular_font = platform->func_load_font(assets, DQN_STRING8("Inter (Regular)"), DQN_STRING8("Data/Inter-Regular.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->test_audio = platform->func_load_audio(assets, DQN_STRING8("Test Audio"), DQN_STRING8("Data/Audio/Purrple Cat - Moonwinds.qoa"));
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
void FP_EntityActionStateMachine(FP_Game *game, TELY_PlatformInput *input, FP_GameEntity *entity, Dqn_V2 dir_vector)
|
2023-09-24 07:01:21 +00:00
|
|
|
{
|
2023-09-24 12:00:08 +00:00
|
|
|
FP_GameEntityAction *action = &entity->action;
|
2023-09-24 13:08:30 +00:00
|
|
|
bool const we_are_clicked_entity = entity->handle == game->clicked_entity;
|
|
|
|
bool const entity_has_velocity = entity->velocity.x || entity->velocity.y;
|
|
|
|
bool const entering_new_state = action->state != action->next_state;
|
|
|
|
bool const action_has_finished = !entering_new_state && game->clock_ms >= action->end_at_clock_ms;
|
|
|
|
action->state = action->next_state;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
|
|
|
switch (entity->type) {
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityType_Nil: {
|
|
|
|
} break;
|
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
case FP_EntityType_Terry: {
|
2023-09-24 11:54:08 +00:00
|
|
|
FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state;
|
|
|
|
TELY_AssetSpriteSheet *sheet = &game->terry_sprite_sheet;
|
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
switch (*state) {
|
|
|
|
case FP_EntityTerryState_Nil: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_EntityTerryState_Idle: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_walk_idle, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (we_are_clicked_entity) {
|
2023-09-24 12:00:08 +00:00
|
|
|
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
|
|
|
|
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackUp;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Left: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Right: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Down: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackDown;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} else if (dir_vector.x || dir_vector.y) {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Run;
|
2023-09-24 08:05:28 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryState_AttackUp: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_attack_up, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action_has_finished) {
|
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryState_AttackDown: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_attack_down, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
2023-09-24 13:08:30 +00:00
|
|
|
|
|
|
|
if (action_has_finished)
|
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryState_AttackSide: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetFlip flip = entity->direction == FP_GameDirection_Right ? TELY_AssetFlip_X : TELY_AssetFlip_No;
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_attack_side, flip);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (action_has_finished)
|
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryState_Run: {
|
|
|
|
Dqn_String8 desired_action_name = {};
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: desired_action_name = g_anim_names.terry_walk_up; break;
|
|
|
|
case FP_GameDirection_Down: desired_action_name = g_anim_names.terry_walk_down; break;
|
|
|
|
case FP_GameDirection_Left: desired_action_name = g_anim_names.terry_walk_left; break;
|
|
|
|
case FP_GameDirection_Right: desired_action_name = g_anim_names.terry_walk_right; break;
|
|
|
|
}
|
2023-09-24 13:08:30 +00:00
|
|
|
|
|
|
|
if (entering_new_state || action->sprite.anim->label != desired_action_name) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, desired_action_name, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
|
|
|
|
if (we_are_clicked_entity) {
|
|
|
|
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
|
|
|
|
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackUp;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Left: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Right: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Down: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_AttackDown;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} else if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_LeftShift) ||
|
|
|
|
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_A)) {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Dash;
|
2023-09-24 08:05:28 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
if (!entity_has_velocity /*&& !has_collision*/) {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryState_Dash: {
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_walk_right, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
Dqn_V2 dash_dir = {};
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: dash_dir.y = -1.f; break;
|
|
|
|
case FP_GameDirection_Down: dash_dir.y = +1.f; break;
|
|
|
|
case FP_GameDirection_Left: dash_dir.x = -1.f; break;
|
|
|
|
case FP_GameDirection_Right: dash_dir.x = +1.f; break;
|
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
Dqn_V2 dash_acceleration = dash_dir * 400'000'000.f;
|
2023-09-24 14:43:22 +00:00
|
|
|
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(PHYSICS_STEP);
|
2023-09-24 12:00:08 +00:00
|
|
|
entity->velocity = (dash_acceleration * t) + entity->velocity * 2.0f;
|
2023-09-24 13:08:30 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (action_has_finished) {
|
2023-09-24 12:00:08 +00:00
|
|
|
if (entity_has_velocity) {
|
|
|
|
// TODO(doyle): Not sure if this branch triggers properly.
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Run;
|
2023-09-24 12:00:08 +00:00
|
|
|
} else {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
if (*state == FP_EntityTerryState_AttackUp ||
|
|
|
|
*state == FP_EntityTerryState_AttackDown ||
|
|
|
|
*state == FP_EntityTerryState_AttackSide) {
|
2023-09-24 07:01:21 +00:00
|
|
|
entity->attack_box_size = entity->local_hit_box_size;
|
|
|
|
|
|
|
|
// NOTE: Position the attack box
|
|
|
|
if (entity->direction == FP_GameDirection_Left) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
|
|
|
|
entity->local_hit_box_offset.y);
|
2023-09-24 22:37:26 +00:00
|
|
|
} else if (entity->direction == FP_GameDirection_Right) {
|
2023-09-25 11:39:28 +00:00
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
|
2023-09-24 07:01:21 +00:00
|
|
|
entity->local_hit_box_offset.y);
|
2023-09-24 22:37:26 +00:00
|
|
|
} else if (entity->direction == FP_GameDirection_Up) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
|
|
|
|
entity->local_hit_box_offset.y - entity->attack_box_size.h);
|
|
|
|
} else if (entity->direction == FP_GameDirection_Down) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
|
|
|
|
entity->local_hit_box_offset.y + entity->attack_box_size.h);
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entity->attack_box_size = {};
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
case FP_EntityType_Smoochie: {
|
2023-09-24 11:54:08 +00:00
|
|
|
FP_EntitySmoochieState *state = DQN_CAST(FP_EntitySmoochieState *) & action->state;
|
|
|
|
TELY_AssetSpriteSheet *sheet = &game->smoochie_sprite_sheet;
|
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
switch (*state) {
|
|
|
|
case FP_EntitySmoochieState_Nil: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_EntitySmoochieState_Idle: {
|
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.smoochie_walk_down, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (we_are_clicked_entity) {
|
2023-09-24 12:00:08 +00:00
|
|
|
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
|
|
|
|
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
|
|
|
|
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: /*FALLTHRU*/
|
|
|
|
case FP_GameDirection_Right: /*FALLTHRU*/
|
|
|
|
case FP_GameDirection_Left: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Down: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_AttackDown;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} else if (dir_vector.x || dir_vector.y) {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_Run;
|
2023-09-24 08:05:28 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_EntitySmoochieState_AttackDown: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.smoochie_attack_down, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action_has_finished) {
|
|
|
|
action->next_state = FP_EntitySmoochieState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_EntitySmoochieState_AttackSide: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetFlip flip = entity->direction == FP_GameDirection_Right ? TELY_AssetFlip_X : TELY_AssetFlip_No;
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.smoochie_attack_side, flip);
|
|
|
|
uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
2023-09-24 13:08:30 +00:00
|
|
|
|
|
|
|
if (action_has_finished)
|
|
|
|
action->next_state = FP_EntitySmoochieState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntitySmoochieState_AttackHeart: {
|
|
|
|
} break;
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntitySmoochieState_Run: {
|
|
|
|
Dqn_String8 desired_action_name = {};
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: desired_action_name = g_anim_names.smoochie_walk_up; break;
|
|
|
|
case FP_GameDirection_Down: desired_action_name = g_anim_names.smoochie_walk_down; break;
|
|
|
|
case FP_GameDirection_Left: desired_action_name = g_anim_names.smoochie_walk_left; break;
|
|
|
|
case FP_GameDirection_Right: desired_action_name = g_anim_names.smoochie_walk_right; break;
|
|
|
|
}
|
2023-09-24 13:08:30 +00:00
|
|
|
|
|
|
|
if (entering_new_state || action->sprite.anim->label != desired_action_name) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, desired_action_name, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
|
|
|
}
|
2023-09-24 12:00:08 +00:00
|
|
|
|
|
|
|
if (we_are_clicked_entity) {
|
|
|
|
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_J) ||
|
|
|
|
TELY_Platform_InputGamepadButtonCodeIsPressed(input, 0, TELY_PlatformInputGamepadButtonCode_X)) {
|
|
|
|
|
|
|
|
switch (entity->direction) {
|
|
|
|
case FP_GameDirection_Up: /*FALLTHRU*/
|
|
|
|
case FP_GameDirection_Right: /*FALLTHRU*/
|
|
|
|
case FP_GameDirection_Left: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_AttackSide;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case FP_GameDirection_Down: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_AttackDown;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
2023-09-24 08:05:28 +00:00
|
|
|
}
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
if (!entity_has_velocity /*&& !has_collision*/) {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntitySmoochieState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
if (*state == FP_EntitySmoochieState_AttackSide || *state == FP_EntitySmoochieState_AttackDown) {
|
2023-09-24 07:01:21 +00:00
|
|
|
entity->attack_box_size = entity->local_hit_box_size;
|
|
|
|
// NOTE: Position the attack box
|
|
|
|
if (entity->direction == FP_GameDirection_Left) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x - entity->attack_box_size.w,
|
|
|
|
entity->local_hit_box_offset.y);
|
2023-09-25 11:39:28 +00:00
|
|
|
} else if (entity->direction == FP_GameDirection_Right) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x + entity->attack_box_size.w,
|
2023-09-24 07:01:21 +00:00
|
|
|
entity->local_hit_box_offset.y);
|
2023-09-25 11:39:28 +00:00
|
|
|
} else if (entity->direction == FP_GameDirection_Up) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
|
|
|
|
entity->local_hit_box_offset.y - entity->attack_box_size.h);
|
|
|
|
} else if (entity->direction == FP_GameDirection_Down) {
|
|
|
|
entity->attack_box_offset = Dqn_V2_InitNx2(entity->local_hit_box_offset.x,
|
|
|
|
entity->local_hit_box_offset.y + entity->attack_box_size.h);
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entity->attack_box_size = {};
|
|
|
|
}
|
|
|
|
} break;
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
case FP_EntityType_Merchant: {
|
|
|
|
FP_EntityTerryMerchantState *state = DQN_CAST(FP_EntityTerryMerchantState *)&action->state;
|
2023-09-24 11:54:08 +00:00
|
|
|
TELY_AssetSpriteSheet *sheet = &game->terry_merchant_sprite_sheet;
|
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
switch (*state) {
|
|
|
|
case FP_EntityTerryMerchantState_Nil: {
|
2023-09-24 13:08:30 +00:00
|
|
|
action->next_state = FP_EntityTerryMerchantState_Idle;
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 08:05:28 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
case FP_EntityTerryMerchantState_Idle: {
|
2023-09-24 13:08:30 +00:00
|
|
|
if (entering_new_state) {
|
|
|
|
TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.terry_merchant, TELY_AssetFlip_No);
|
|
|
|
uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER;
|
|
|
|
FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite);
|
2023-09-24 12:00:08 +00:00
|
|
|
}
|
2023-09-24 13:08:30 +00:00
|
|
|
|
2023-09-24 12:00:08 +00:00
|
|
|
} break;
|
2023-09-24 08:05:28 +00:00
|
|
|
}
|
|
|
|
} break;
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input)
|
2023-09-16 07:32:25 +00:00
|
|
|
{
|
2023-09-23 06:42:22 +00:00
|
|
|
Dqn_Profiler_ZoneScopeWithIndex("FP_Update", FP_ProfileZone_FPUpdate);
|
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
if (TELY_Platform_InputKeyIsReleased(input->mouse_left))
|
|
|
|
game->clicked_entity = game->prev_active_entity;
|
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
game->clock_ms = DQN_CAST(uint64_t)(platform->input.timer_s * 1000.f);
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_V2 dir_vector = {};
|
2023-09-23 11:09:07 +00:00
|
|
|
|
|
|
|
// NOTE: Keyboard movement input
|
2023-09-16 02:21:24 +00:00
|
|
|
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_W))
|
|
|
|
dir_vector.y = -1.f;
|
|
|
|
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_A))
|
|
|
|
dir_vector.x = -1.f;
|
|
|
|
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_S))
|
|
|
|
dir_vector.y = +1.f;
|
|
|
|
if (TELY_Platform_InputScanCodeIsDown(input, TELY_PlatformInputScanCode_D))
|
|
|
|
dir_vector.x = +1.f;
|
|
|
|
|
2023-09-23 11:09:07 +00:00
|
|
|
// NOTE: Gamepad movement input
|
2023-09-24 05:33:02 +00:00
|
|
|
// NOTE: button_codes 0 should be the first gamepad connected, we can
|
|
|
|
// get this working with other gamepads later
|
|
|
|
uint32_t gamepad = 0;
|
|
|
|
if (input->button_codes[gamepad]) {
|
|
|
|
dir_vector.x += input->left_stick[gamepad].x;
|
|
|
|
dir_vector.y += input->left_stick[gamepad].y;
|
2023-09-23 11:09:07 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 07:18:32 +00:00
|
|
|
if (dir_vector.x && dir_vector.y) {
|
|
|
|
dir_vector.x *= 0.7071067811865475244f;
|
|
|
|
dir_vector.y *= 0.7071067811865475244f;
|
|
|
|
}
|
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
if (game->clicked_entity.id) {
|
|
|
|
if (TELY_Platform_InputScanCodeIsPressed(input, TELY_PlatformInputScanCode_Delete))
|
2023-09-17 10:24:07 +00:00
|
|
|
FP_Game_DeleteEntity(game, game->clicked_entity);
|
2023-09-16 02:21:24 +00:00
|
|
|
} else {
|
|
|
|
game->camera.world_pos += dir_vector * 5.f;
|
|
|
|
}
|
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
Dqn_ProfilerZone update_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: Entity loop"), FP_ProfileZone_FPUpdate_EntityLoop);
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *entity = it.entity;
|
2023-09-24 14:43:22 +00:00
|
|
|
entity->alive_time_s += PHYSICS_STEP;
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-24 05:33:02 +00:00
|
|
|
// NOTE: Move entity by keyboard and gamepad ===============================================
|
2023-09-25 14:07:39 +00:00
|
|
|
Dqn_V2 acceleration_meters_per_s = {};
|
2023-09-24 07:01:21 +00:00
|
|
|
if (game->clicked_entity == entity->handle) {
|
|
|
|
if (entity->flags & (FP_GameEntityFlag_MoveByKeyboard | FP_GameEntityFlag_MoveByGamepad)) {
|
|
|
|
bool move_entity = false;
|
|
|
|
switch (entity->type) {
|
2023-09-24 08:16:14 +00:00
|
|
|
case FP_EntityType_Terry: {
|
2023-09-24 14:43:22 +00:00
|
|
|
auto *state = DQN_CAST(FP_EntityTerryState *)&entity->action.state;
|
|
|
|
move_entity = *state == FP_EntityTerryState_Run || *state == FP_EntityTerryState_Idle;
|
2023-09-24 07:01:21 +00:00
|
|
|
} break;
|
|
|
|
|
2023-09-24 08:16:14 +00:00
|
|
|
case FP_EntityType_Smoochie: {
|
2023-09-24 14:43:22 +00:00
|
|
|
auto *state = DQN_CAST(FP_EntitySmoochieState *)&entity->action.state;
|
|
|
|
move_entity = *state == FP_EntitySmoochieState_Run || *state == FP_EntitySmoochieState_Idle;
|
2023-09-24 07:01:21 +00:00
|
|
|
} break;
|
2023-09-24 09:11:44 +00:00
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
case FP_EntityType_Merchant: move_entity = true; break;
|
|
|
|
case FP_EntityType_Nil: move_entity = true; break;
|
2023-09-24 07:01:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 09:11:44 +00:00
|
|
|
if (move_entity) {
|
2023-09-25 14:07:39 +00:00
|
|
|
acceleration_meters_per_s = dir_vector * 8;
|
2023-09-24 09:11:44 +00:00
|
|
|
if (dir_vector.x)
|
|
|
|
entity->direction = dir_vector.x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left;
|
|
|
|
else if (dir_vector.y)
|
|
|
|
entity->direction = dir_vector.y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up;
|
|
|
|
}
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 13:06:39 +00:00
|
|
|
// NOTE: Determine AI movement =============================================================
|
2023-09-25 14:07:39 +00:00
|
|
|
if (acceleration_meters_per_s.x == 0 && acceleration_meters_per_s.y == 0) {
|
2023-09-25 13:06:39 +00:00
|
|
|
Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
|
|
|
|
|
|
|
|
if (entity->flags & FP_GameEntityFlag_AggrosWhenNearTerry) {
|
|
|
|
Dqn_f32 closest_terry_dist = DQN_F32_MAX;
|
|
|
|
Dqn_V2 closest_terry_pos = Dqn_V2_InitNx1(DQN_F32_MAX);
|
|
|
|
FP_GameEntity *closest_terry = nullptr;
|
|
|
|
for (FP_GameEntityIterator terry_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &terry_it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *terry = terry_it.entity;
|
|
|
|
if (terry->type != FP_EntityType_Terry)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dqn_V2 terry_pos = FP_Game_CalcEntityWorldPos(game, terry->handle);
|
|
|
|
Dqn_f32 dist_to_terry = Dqn_V2_LengthSq_V2x2(terry_pos, entity_pos);
|
|
|
|
if (dist_to_terry < closest_terry_dist) {
|
|
|
|
closest_terry_pos = terry_pos;
|
|
|
|
closest_terry_dist = dist_to_terry;
|
|
|
|
closest_terry = terry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 14:07:39 +00:00
|
|
|
if (closest_terry_dist < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game, 5.f))) {
|
2023-09-25 13:06:39 +00:00
|
|
|
FP_SentinelListLink<FP_GameEntityHandle> *first_waypoint = FP_SentinelList_Front(&entity->waypoints);
|
|
|
|
if (first_waypoint->data != closest_terry->handle) {
|
|
|
|
FP_SentinelListLink<FP_GameEntityHandle> *link = FP_SentinelList_MakeBefore(&entity->waypoints, first_waypoint, game->chunk_pool);
|
|
|
|
link->data = closest_terry->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
while (entity->waypoints.size) {
|
|
|
|
FP_SentinelListLink<FP_GameEntityHandle> *waypoint_link = entity->waypoints.sentinel->next;
|
|
|
|
FP_GameEntity *waypoint = FP_Game_GetEntity(game, waypoint_link->data);
|
|
|
|
if (FP_Game_IsNilEntity(waypoint)) {
|
|
|
|
FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->chunk_pool);
|
|
|
|
continue;
|
2023-09-16 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
// NOTE: We found a waypoint that is valid to move towards
|
|
|
|
Dqn_V2 waypoint_pos = FP_Game_CalcEntityWorldPos(game, waypoint->handle);
|
|
|
|
Dqn_V2 entity_to_waypoint = waypoint_pos - entity_pos;
|
2023-09-23 06:42:22 +00:00
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
// NOTE: Check if we've arrived at the waypoint
|
|
|
|
Dqn_f32 dist_to_waypoint_sq = Dqn_V2_LengthSq(entity_to_waypoint);
|
|
|
|
Dqn_f32 arrival_threshold = DQN_MIN(DQN_SQUARED(entity->local_hit_box_size.x * .5f), 10.f);
|
|
|
|
if (dist_to_waypoint_sq < arrival_threshold) {
|
|
|
|
FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->chunk_pool);
|
|
|
|
continue;
|
2023-09-23 06:42:22 +00:00
|
|
|
}
|
2023-09-24 14:43:22 +00:00
|
|
|
|
|
|
|
// NOTE: We haven't arrived yet, calculate an acceleration vector to the waypoint
|
|
|
|
Dqn_V2 entity_to_waypoint_norm = Dqn_V2_Normalise(entity_to_waypoint);
|
2023-09-25 14:07:39 +00:00
|
|
|
acceleration_meters_per_s = entity_to_waypoint_norm * 4.f;
|
2023-09-24 14:43:22 +00:00
|
|
|
break;
|
2023-09-16 14:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 07:32:25 +00:00
|
|
|
// NOTE: Core equations of motion ==========================================================
|
2023-09-24 06:24:51 +00:00
|
|
|
bool has_collision = false;
|
2023-09-16 07:32:25 +00:00
|
|
|
{
|
|
|
|
// f"(t) = a
|
|
|
|
// f'(t) = at + v
|
|
|
|
// f (t) = 0.5f*a(t^2) + vt + p
|
2023-09-24 14:43:22 +00:00
|
|
|
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(PHYSICS_STEP);
|
2023-09-16 07:32:25 +00:00
|
|
|
Dqn_f32 t_squared = DQN_SQUARED(t);
|
2023-09-24 06:24:51 +00:00
|
|
|
|
2023-09-25 14:07:39 +00:00
|
|
|
Dqn_f32 velocity_falloff_coefficient = 0.82f;
|
|
|
|
Dqn_f32 acceleration_feel_good_factor = 15'000.f;
|
|
|
|
Dqn_V2 acceleration = FP_Game_MetersToPixelsV2(game, acceleration_meters_per_s) * acceleration_feel_good_factor;
|
|
|
|
entity->velocity = (acceleration * t) + entity->velocity * velocity_falloff_coefficient;
|
2023-09-24 06:24:51 +00:00
|
|
|
|
|
|
|
// NOTE: Zero out velocity with epsilon
|
|
|
|
if (DQN_ABS(entity->velocity.x) < 5.f)
|
|
|
|
entity->velocity.x = 0.f;
|
|
|
|
if (DQN_ABS(entity->velocity.y) < 5.f)
|
|
|
|
entity->velocity.y = 0.f;
|
|
|
|
|
2023-09-25 14:07:39 +00:00
|
|
|
Dqn_V2 delta_pos = (acceleration * 0.5f * t_squared) + (entity->velocity * t);
|
2023-09-17 11:55:59 +00:00
|
|
|
Dqn_Rect entity_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_V2 entity_pos = Dqn_Rect_Center(entity_world_hit_box);
|
|
|
|
Dqn_V2 entity_new_pos = entity_pos + delta_pos;
|
2023-09-16 09:06:16 +00:00
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator collider_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &collider_it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *collider = collider_it.entity;
|
2023-09-16 09:06:16 +00:00
|
|
|
if (collider->handle == entity->handle)
|
|
|
|
continue;
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
// TODO(doyle): Calculate the list of collidables at the start of the frame
|
|
|
|
if ((collider->flags & FP_GameEntityFlag_NonTraversable) == 0)
|
|
|
|
continue;
|
|
|
|
|
2023-09-17 11:55:59 +00:00
|
|
|
// NOTE: Sweep collider with half the radius of the source entity
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_Rect collider_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, collider->handle);
|
|
|
|
Dqn_Rect swept_collider_world_hit_box = collider_world_hit_box;
|
|
|
|
swept_collider_world_hit_box.pos -= (entity_world_hit_box.size * .5f);
|
|
|
|
swept_collider_world_hit_box.size += entity_world_hit_box.size;
|
2023-09-17 11:55:59 +00:00
|
|
|
|
2023-09-17 12:22:04 +00:00
|
|
|
if (!Dqn_Rect_ContainsPoint(swept_collider_world_hit_box, entity_new_pos))
|
2023-09-17 11:55:59 +00:00
|
|
|
continue;
|
|
|
|
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_f32 collider_left_wall_x = swept_collider_world_hit_box.pos.x;
|
|
|
|
Dqn_f32 collider_right_wall_x = swept_collider_world_hit_box.pos.x + swept_collider_world_hit_box.size.w;
|
|
|
|
Dqn_f32 collider_top_wall_y = swept_collider_world_hit_box.pos.y;
|
|
|
|
Dqn_f32 collider_bottom_wall_y = swept_collider_world_hit_box.pos.y + swept_collider_world_hit_box.size.h;
|
|
|
|
|
|
|
|
Dqn_V2 o = entity_pos;
|
|
|
|
Dqn_V2 d = delta_pos;
|
|
|
|
|
2023-09-17 11:55:59 +00:00
|
|
|
// NOTE: Solve collision by determining the 't' value at which
|
|
|
|
// we hit one of the walls of the collider and move the entity
|
|
|
|
// at exactly that point.
|
|
|
|
// O + td = x
|
|
|
|
// td = x - O
|
|
|
|
// t = (x - O) / d
|
|
|
|
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_f32 const SENTINEL_T = 999.f;
|
|
|
|
Dqn_f32 earliest_t = SENTINEL_T;
|
2023-09-17 11:55:59 +00:00
|
|
|
if (d.x != 0.f) {
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_f32 left_t = (collider_left_wall_x - o.x) / d.x;
|
|
|
|
Dqn_f32 right_t = (collider_right_wall_x - o.x) / d.x;
|
|
|
|
if (left_t >= 0.f && left_t <= 1.f)
|
2023-09-17 11:55:59 +00:00
|
|
|
earliest_t = DQN_MIN(earliest_t, left_t);
|
2023-09-17 12:22:04 +00:00
|
|
|
if (right_t >= 0.f && right_t <= 1.f)
|
2023-09-17 11:55:59 +00:00
|
|
|
earliest_t = DQN_MIN(earliest_t, right_t);
|
2023-09-16 09:06:16 +00:00
|
|
|
}
|
2023-09-17 11:55:59 +00:00
|
|
|
|
|
|
|
if (d.y != 0.f) {
|
2023-09-17 12:22:04 +00:00
|
|
|
Dqn_f32 top_t = (collider_top_wall_y - o.y) / d.y;
|
|
|
|
Dqn_f32 bottom_t = (collider_bottom_wall_y - o.y) / d.y;
|
|
|
|
if (top_t >= 0.f && top_t <= 1.f)
|
2023-09-17 11:55:59 +00:00
|
|
|
earliest_t = DQN_MIN(earliest_t, top_t);
|
2023-09-17 12:22:04 +00:00
|
|
|
if (bottom_t >= 0.f && bottom_t <= 1.f)
|
2023-09-17 11:55:59 +00:00
|
|
|
earliest_t = DQN_MIN(earliest_t, bottom_t);
|
|
|
|
}
|
|
|
|
|
2023-09-17 12:22:04 +00:00
|
|
|
if (earliest_t != SENTINEL_T) {
|
|
|
|
Dqn_V2 pos_just_before_collide = entity_pos + (d * earliest_t);
|
|
|
|
Dqn_V2 new_delta_p = pos_just_before_collide - entity_pos;
|
|
|
|
entity->local_pos += new_delta_p;
|
|
|
|
has_collision = true;
|
|
|
|
}
|
2023-09-17 11:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_collision) {
|
|
|
|
entity->local_pos += delta_pos;
|
2023-09-16 09:06:16 +00:00
|
|
|
}
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
2023-09-26 12:34:42 +00:00
|
|
|
|
|
|
|
// NOTE: Attack collisions =================================================================
|
|
|
|
{
|
|
|
|
// NOTE: Check if there's an active attack box
|
|
|
|
if (entity->attack_box_size.x != 0 && entity->attack_box_size.y != 0) {
|
|
|
|
Dqn_Rect entity_attack_box = {};
|
|
|
|
// NOTE: Convert from centre origin to top-left origin
|
|
|
|
entity_attack_box.pos = entity->local_pos + entity->attack_box_offset - (entity->attack_box_size * 0.5f);
|
|
|
|
entity_attack_box.size = entity->attack_box_size;
|
|
|
|
|
|
|
|
for (FP_GameEntityIterator target_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &target_it, game->root_entity);) {
|
|
|
|
FP_GameEntity *target = target_it.entity;
|
|
|
|
if (target->handle == entity->handle)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO(josh): This check should be updated based on an entity attackable flag
|
|
|
|
if (target->flags & FP_GameEntityFlag_Attackable) {
|
|
|
|
Dqn_Rect target_world_hit_box = FP_Game_CalcEntityWorldHitBox(game, target->handle);
|
|
|
|
|
|
|
|
if (Dqn_Rect_Intersects(entity_attack_box, target_world_hit_box)) {
|
|
|
|
target->hp -= 1;
|
|
|
|
|
|
|
|
if (target->hp <= 0) {
|
|
|
|
FP_Game_DeleteEntity(game, target->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
// NOTE: Move entity by mouse ==============================================================
|
2023-09-23 02:33:59 +00:00
|
|
|
if (game->active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) {
|
|
|
|
if (entity->flags & FP_GameEntityFlag_MoveByMouse) {
|
2023-09-16 07:32:25 +00:00
|
|
|
entity->velocity = {};
|
2023-09-16 02:21:24 +00:00
|
|
|
entity->local_pos += input->mouse_p_delta;
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:33:59 +00:00
|
|
|
if (entity->flags & FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox) {
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_Rect children_bbox = {};
|
|
|
|
|
|
|
|
// TODO(doyle): Is the hit box supposed to include the containing
|
|
|
|
// entity itself? Not sure
|
2023-09-17 10:24:07 +00:00
|
|
|
children_bbox.pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator child_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &child_it, entity);) {
|
|
|
|
FP_GameEntity *child = child_it.entity;
|
2023-09-16 02:21:24 +00:00
|
|
|
DQN_ASSERT(child != entity);
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_Rect bbox = FP_Game_CalcEntityWorldBoundingBox(game, child->handle);
|
2023-09-16 02:21:24 +00:00
|
|
|
children_bbox = Dqn_Rect_Union(children_bbox, bbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dqn_Rect padded_bbox = Dqn_Rect_Expand(children_bbox, 16.f);
|
|
|
|
entity->local_hit_box_offset = padded_bbox.pos - entity->local_pos + (padded_bbox.size * .5f);
|
|
|
|
entity->local_hit_box_size = padded_bbox.size;
|
|
|
|
}
|
|
|
|
|
2023-09-17 14:12:58 +00:00
|
|
|
// NOTE: Handle input on entity ============================================================
|
2023-09-24 11:54:08 +00:00
|
|
|
FP_EntityActionStateMachine(game, input, entity, dir_vector);
|
2023-09-23 06:42:22 +00:00
|
|
|
|
|
|
|
// NOTE: Mob spawner =======================================================================
|
2023-09-24 04:20:27 +00:00
|
|
|
if (entity->flags & FP_GameEntityFlag_MobSpawner) {
|
2023-09-24 09:11:44 +00:00
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
// NOTE: Flush any spawn entities that are dead
|
|
|
|
for (FP_SentinelListLink<FP_GameEntityHandle> *link = nullptr; FP_SentinelList_Iterate<FP_GameEntityHandle>(&entity->spawn_list, &link); ) {
|
|
|
|
FP_GameEntity *spawned_entity = FP_Game_GetEntity(game, link->data);
|
|
|
|
if (FP_Game_IsNilEntity(spawned_entity)) // NOTE: Entity is dead remove it from the linked list
|
|
|
|
link = FP_SentinelList_Erase(&entity->spawn_list, link, game->chunk_pool);
|
2023-09-24 09:11:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
if (entity->spawn_list.size < entity->spawn_cap) { // NOTE: Spawn new entities
|
2023-09-24 04:20:27 +00:00
|
|
|
if (input->timer_s >= entity->next_spawn_timestamp_s) {
|
|
|
|
entity->next_spawn_timestamp_s = DQN_CAST(uint64_t)(input->timer_s + 5.f);
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
|
|
|
|
FP_SentinelListLink<FP_GameEntityHandle> *link = FP_SentinelList_Make(&entity->spawn_list, game->chunk_pool);
|
2023-09-25 12:13:32 +00:00
|
|
|
link->data = FP_Entity_CreateSmoochie(game, entity_world_pos, "Smoochie");
|
2023-09-24 09:11:44 +00:00
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
// NOTE: Setup the mob with waypoints
|
2023-09-25 13:06:39 +00:00
|
|
|
FP_GameEntity *mob = FP_Game_GetEntity(game, link->data);
|
|
|
|
mob->waypoints = FP_SentinelList_Init<FP_GameEntityHandle>(game->chunk_pool);
|
|
|
|
mob->flags |= FP_GameEntityFlag_AggrosWhenNearTerry;
|
2023-09-24 09:11:44 +00:00
|
|
|
|
|
|
|
for (FP_GameEntity *waypoint_entity = entity->first_child; waypoint_entity; waypoint_entity = waypoint_entity->next) {
|
|
|
|
if ((waypoint_entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// NOTE: Add the waypoint
|
2023-09-24 14:43:22 +00:00
|
|
|
FP_SentinelListLink<FP_GameEntityHandle> *waypoint = FP_SentinelList_Make(&mob->waypoints, game->chunk_pool);
|
|
|
|
waypoint->data = waypoint_entity->handle;
|
2023-09-24 09:11:44 +00:00
|
|
|
}
|
2023-09-24 04:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 06:42:22 +00:00
|
|
|
}
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
2023-09-23 06:42:22 +00:00
|
|
|
Dqn_Profiler_EndZone(update_zone);
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-16 09:06:16 +00:00
|
|
|
// NOTE: Do attacks ============================================================================
|
2023-09-23 06:42:22 +00:00
|
|
|
auto attack_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STRING8("FP_Update: Attacks"), FP_ProfileZone_FPUpdate_Attacks);
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator attacker_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &attacker_it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *attacker = attacker_it.entity;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
|
|
|
// NOTE: Resolve attack boxes
|
2023-09-18 13:35:29 +00:00
|
|
|
if (!Dqn_V2_Area(attacker->attack_box_size))
|
|
|
|
continue;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-18 13:35:29 +00:00
|
|
|
Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle);
|
|
|
|
Dqn_V2 attacker_world_pos = FP_Game_CalcEntityWorldPos(game, attacker->handle);
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-18 13:35:29 +00:00
|
|
|
for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *defender = defender_it.entity;
|
|
|
|
if (defender->handle == attacker->handle)
|
|
|
|
continue;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-18 13:35:29 +00:00
|
|
|
Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle);
|
|
|
|
if (!Dqn_Rect_Intersects(attacker_box, defender_box))
|
|
|
|
continue;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-18 13:35:29 +00:00
|
|
|
Dqn_V2 defender_world_pos = Dqn_Rect_Center(defender_box);
|
|
|
|
Dqn_V2 attack_dir_vector = {};
|
|
|
|
if (attacker_world_pos.x < defender_world_pos.x)
|
|
|
|
attack_dir_vector.x = 1.f;
|
|
|
|
else
|
|
|
|
attack_dir_vector.x = -1.f;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-21 07:11:07 +00:00
|
|
|
Dqn_V2 acceleration = attack_dir_vector * 500'000.f;
|
2023-09-24 14:43:22 +00:00
|
|
|
Dqn_f32 t = DQN_CAST(Dqn_f32)DQN_SQUARED(PHYSICS_STEP);
|
2023-09-18 13:35:29 +00:00
|
|
|
Dqn_f32 t_squared = DQN_SQUARED(t);
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-18 13:35:29 +00:00
|
|
|
Dqn_V2 delta_p = (acceleration * 0.5f * t_squared) + (defender->velocity * t);
|
|
|
|
defender->velocity = (acceleration * t) + defender->velocity * 2.0f;
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 06:42:22 +00:00
|
|
|
Dqn_Profiler_EndZone(attack_zone);
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:33:59 +00:00
|
|
|
void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer)
|
2023-09-16 07:32:25 +00:00
|
|
|
{
|
2023-09-23 06:42:22 +00:00
|
|
|
Dqn_Profiler_ZoneScopeWithIndex("FP_Render", FP_ProfileZone_FPRender);
|
2023-09-16 07:32:25 +00:00
|
|
|
TELY_PlatformInput *input = &platform->input;
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
|
2023-09-16 07:32:25 +00:00
|
|
|
Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p);
|
|
|
|
|
2023-09-16 09:06:16 +00:00
|
|
|
// NOTE: Draw tiles ============================================================================
|
2023-09-23 02:33:59 +00:00
|
|
|
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);
|
2023-09-16 09:06:16 +00:00
|
|
|
|
|
|
|
for (Dqn_usize x = 0; x < tile_count_x; x++) {
|
2023-09-23 02:33:59 +00:00
|
|
|
Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * game->tile_size, 0);
|
2023-09-16 09:06:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Dqn_usize y = 0; y < tile_count_y; y++) {
|
2023-09-23 02:33:59 +00:00
|
|
|
Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * game->tile_size);
|
2023-09-16 09:06:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Draw entities =========================================================================
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *entity = it.entity;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
// NOTE: Render shapes in entity ===========================================================
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
|
|
|
|
for (FP_GameShape const &shape_ : entity->shapes) {
|
|
|
|
FP_GameShape const *shape = &shape_;
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_V2 local_to_world_p1 = world_pos + shape->p1;
|
|
|
|
Dqn_V2 local_to_world_p2 = world_pos + shape->p2;
|
|
|
|
switch (shape->type) {
|
2023-09-17 10:24:07 +00:00
|
|
|
case FP_GameShapeType_None: {
|
2023-09-16 02:21:24 +00:00
|
|
|
} break;
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
case FP_GameShapeType_Circle: {
|
2023-09-16 02:21:24 +00:00
|
|
|
TELY_Render_CircleColourV4(renderer, local_to_world_p1, shape->circle_radius, shape->render_mode, shape->colour);
|
|
|
|
} break;
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
case FP_GameShapeType_Rect: {
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_Rect rect = Dqn_Rect_InitV2x2(local_to_world_p1, local_to_world_p2 - local_to_world_p1);
|
|
|
|
rect.pos -= rect.size * .5f;
|
|
|
|
TELY_Render_RectColourV4(renderer, rect, shape->render_mode, shape->colour);
|
|
|
|
} break;
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
case FP_GameShapeType_Line: {
|
2023-09-16 02:21:24 +00:00
|
|
|
TELY_Render_LineColourV4(renderer, local_to_world_p1, local_to_world_p2, shape->colour, shape->line_thickness);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Render entity sprites =============================================================
|
2023-09-24 11:54:08 +00:00
|
|
|
if (entity->action.sprite.anim) {
|
2023-09-24 13:08:30 +00:00
|
|
|
FP_GameEntityAction const *action = &entity->action;
|
|
|
|
TELY_AssetAnimatedSprite const sprite = action->sprite;
|
|
|
|
|
|
|
|
uint64_t elapsed_ms = game->clock_ms - action->started_at_clock_ms;
|
|
|
|
uint16_t anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite.anim->ms_per_frame) % sprite.anim->count;
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
Dqn_usize sprite_index = sprite.anim->index + anim_frame;
|
2023-09-24 01:34:04 +00:00
|
|
|
Dqn_Rect src_rect = {};
|
2023-09-24 13:08:30 +00:00
|
|
|
switch (sprite.sheet->type) {
|
2023-09-23 11:21:08 +00:00
|
|
|
case TELY_AssetSpriteSheetType_Uniform: {
|
2023-09-24 13:08:30 +00:00
|
|
|
Dqn_usize sprite_sheet_row = sprite_index / sprite.sheet->sprites_per_row;
|
|
|
|
Dqn_usize sprite_sheet_column = sprite_index % sprite.sheet->sprites_per_row;
|
|
|
|
src_rect.pos.x = DQN_CAST(Dqn_f32)(sprite_sheet_column * sprite.sheet->sprite_size.w);
|
|
|
|
src_rect.pos.y = DQN_CAST(Dqn_f32)(sprite_sheet_row * sprite.sheet->sprite_size.y);
|
|
|
|
src_rect.size.w = DQN_CAST(Dqn_f32)sprite.sheet->sprite_size.w;
|
|
|
|
src_rect.size.h = DQN_CAST(Dqn_f32)sprite.sheet->sprite_size.h;
|
2023-09-23 11:21:08 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case TELY_AssetSpriteSheetType_Rects: {
|
2023-09-24 13:08:30 +00:00
|
|
|
DQN_ASSERT(sprite_index < sprite.sheet->rects.size);
|
|
|
|
src_rect = sprite.sheet->rects.data[sprite_index];
|
2023-09-23 11:21:08 +00:00
|
|
|
} break;
|
|
|
|
}
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-25 14:07:39 +00:00
|
|
|
Dqn_f32 sprite_in_meters = src_rect.size.y * FP_Game_PixelsToMeters(game);
|
|
|
|
Dqn_f32 size_scale = entity->sprite_height.meters / sprite_in_meters;
|
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_Rect dest_rect = {};
|
2023-09-25 14:07:39 +00:00
|
|
|
dest_rect.size = src_rect.size * size_scale;
|
2023-09-16 02:46:28 +00:00
|
|
|
dest_rect.pos = world_pos - (dest_rect.size * .5f);
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (sprite.flip & TELY_AssetFlip_X)
|
2023-09-16 02:21:24 +00:00
|
|
|
dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally
|
|
|
|
|
2023-09-24 13:08:30 +00:00
|
|
|
if (sprite.flip & TELY_AssetFlip_Y)
|
|
|
|
dest_rect.size.h *= -1.f; // NOTE: Flip the texture vertically
|
|
|
|
|
|
|
|
TELY_Render_TextureColourV4(renderer, sprite.sheet->tex_handle, src_rect, dest_rect, TELY_COLOUR_WHITE_V4);
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 14:43:22 +00:00
|
|
|
if (entity->flags & FP_GameEntityFlag_MobSpawner) {
|
|
|
|
Dqn_V2 start = world_pos;
|
|
|
|
for (FP_GameEntity *waypoint_entity = entity->first_child; waypoint_entity; waypoint_entity = waypoint_entity->next) {
|
|
|
|
if ((waypoint_entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dqn_V2 end = FP_Game_CalcEntityWorldPos(game, waypoint_entity->handle);
|
|
|
|
TELY_Render_LineColourV4(renderer, start, end, TELY_COLOUR_BLUE_CADET_V4, 2.f);
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 07:32:25 +00:00
|
|
|
// NOTE: Render attack box =================================================================
|
|
|
|
{
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_Rect attack_box = FP_Game_CalcEntityAttackWorldHitBox(game, entity->handle);
|
2023-09-16 07:32:25 +00:00
|
|
|
TELY_Render_RectColourV4(renderer, attack_box, TELY_RenderShapeMode_Line, TELY_COLOUR_RED_TOMATO_V4);
|
|
|
|
}
|
|
|
|
|
2023-09-16 02:21:24 +00:00
|
|
|
// NOTE: Render world position =============================================================
|
|
|
|
TELY_Render_CircleColourV4(renderer, world_pos, 4.f, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4);
|
|
|
|
|
|
|
|
// NOTE: Render hot/active entity ==========================================================
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
|
2023-09-16 02:21:24 +00:00
|
|
|
if (game->clicked_entity == entity->handle) {
|
|
|
|
TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
|
2023-09-24 14:43:22 +00:00
|
|
|
|
|
|
|
// NOTE: Draw the waypoints that the entity is moving along
|
|
|
|
if (entity->waypoints.size) {
|
|
|
|
Dqn_V2 start = world_pos;
|
|
|
|
for (FP_SentinelListLink<FP_GameEntityHandle> *link = nullptr; FP_SentinelList_Iterate(&entity->waypoints, &link); ) {
|
|
|
|
FP_GameEntity *waypoint = FP_Game_GetEntity(game, link->data);
|
|
|
|
if (FP_Game_IsNilEntity(waypoint))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Dqn_V2 end = FP_Game_CalcEntityWorldPos(game, waypoint->handle);
|
|
|
|
TELY_Render_LineColourV4(renderer, start, end, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4, 2.f);
|
|
|
|
start = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:33:59 +00:00
|
|
|
} else if (game->hot_entity == entity->handle || (entity->flags & FP_GameEntityFlag_DrawHitBox)) {
|
2023-09-16 02:21:24 +00:00
|
|
|
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);
|
2023-09-16 09:06:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 11:55:59 +00:00
|
|
|
if (game->hot_entity == entity->handle) {
|
2023-09-16 09:06:16 +00:00
|
|
|
if (entity->name.size) {
|
2023-09-23 02:33:59 +00:00
|
|
|
Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / game->tile_size, world_pos.y / game->tile_size);
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle);
|
2023-09-16 02:21:24 +00:00
|
|
|
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
|
|
|
|
Dqn_String8 label = Dqn_String8_InitF(scratch.allocator,
|
2023-09-16 09:06:16 +00:00
|
|
|
"%.*s (%.1f, %.1f) (%I32d, %I32d)",
|
2023-09-16 02:21:24 +00:00
|
|
|
DQN_STRING_FMT(entity->name),
|
|
|
|
entity_world_pos.x,
|
2023-09-16 09:06:16 +00:00
|
|
|
entity_world_pos.y,
|
|
|
|
player_tile.x,
|
|
|
|
player_tile.y);
|
2023-09-16 02:21:24 +00:00
|
|
|
TELY_Render_Text(renderer, world_mouse_p, Dqn_V2_InitNx2(0.f, 1), label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" __declspec(dllexport)
|
|
|
|
void TELY_DLL_FrameUpdate(void *user_data)
|
|
|
|
{
|
|
|
|
TELY_Platform *platform = DQN_CAST(TELY_Platform *) user_data;
|
|
|
|
TELY_PlatformInput *input = &platform->input;
|
|
|
|
TELY_Assets *assets = &platform->assets;
|
|
|
|
TELY_Renderer *renderer = &platform->renderer;
|
2023-09-17 10:24:07 +00:00
|
|
|
FP_Game *game = DQN_CAST(FP_Game *) platform->user_data;
|
2023-09-20 13:35:38 +00:00
|
|
|
TELY_RFui *rfui = &game->rfui;
|
2023-09-16 07:32:25 +00:00
|
|
|
|
|
|
|
TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb);
|
2023-09-17 10:24:07 +00:00
|
|
|
TELY_Render_PushFont(renderer, game->jetbrains_mono_font);
|
2023-09-16 07:32:25 +00:00
|
|
|
|
2023-09-20 13:35:38 +00:00
|
|
|
TELY_RFui_FrameSetup(rfui, &platform->frame_arena);
|
|
|
|
TELY_RFui_PushFont(rfui, game->jetbrains_mono_font);
|
|
|
|
TELY_RFui_PushLabelColourV4(rfui, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4);
|
2023-09-16 07:32:25 +00:00
|
|
|
|
|
|
|
// =============================================================================================
|
|
|
|
|
|
|
|
game->prev_clicked_entity = game->clicked_entity;
|
|
|
|
game->prev_hot_entity = game->hot_entity;
|
|
|
|
game->prev_active_entity = game->active_entity;
|
|
|
|
game->hot_entity = {};
|
|
|
|
game->active_entity = {};
|
|
|
|
Dqn_FArray_Clear(&game->parent_entity_stack);
|
|
|
|
Dqn_FArray_Add(&game->parent_entity_stack, game->root_entity->handle);
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_M2x3 model_view = FP_Game_CameraModelViewM2x3(game->camera, platform);
|
2023-09-17 12:38:06 +00:00
|
|
|
TELY_Render_PushTransform(renderer, model_view);
|
2023-09-16 07:32:25 +00:00
|
|
|
Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(model_view, input->mouse_p);
|
|
|
|
|
|
|
|
// =============================================================================================
|
|
|
|
|
|
|
|
TELY_Audio *audio = &platform->audio;
|
|
|
|
if (audio->playback_size == 0) {
|
2023-09-17 10:24:07 +00:00
|
|
|
TELY_Audio_Play(audio, game->test_audio, 1.f /*volume*/);
|
2023-09-16 07:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// =============================================================================================
|
|
|
|
|
|
|
|
if (TELY_Platform_InputKeyWasDown(input->mouse_left) && TELY_Platform_InputKeyIsDown(input->mouse_left)) {
|
|
|
|
if (game->prev_active_entity.id)
|
|
|
|
game->active_entity = game->prev_active_entity;
|
|
|
|
} else {
|
2023-09-17 10:24:07 +00:00
|
|
|
for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->root_entity); ) {
|
|
|
|
FP_GameEntity *entity = it.entity;
|
2023-09-16 07:32:25 +00:00
|
|
|
if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0)
|
|
|
|
continue;
|
|
|
|
|
2023-09-23 02:33:59 +00:00
|
|
|
if ((entity->flags & FP_GameEntityFlag_Clickable) == 0)
|
2023-09-16 07:32:25 +00:00
|
|
|
continue;
|
|
|
|
|
2023-09-17 10:24:07 +00:00
|
|
|
Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle);
|
2023-09-16 07:32:25 +00:00
|
|
|
if (!Dqn_Rect_ContainsPoint(world_hit_box, world_mouse_p))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
game->hot_entity = entity->handle;
|
|
|
|
if (TELY_Platform_InputKeyIsPressed(input->mouse_left)) {
|
|
|
|
game->active_entity = entity->handle;
|
|
|
|
game->clicked_entity = entity->handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-16 02:21:24 +00:00
|
|
|
|
2023-09-23 11:21:08 +00:00
|
|
|
for (game->delta_s_accumulator += DQN_CAST(Dqn_f32)input->delta_s;
|
2023-09-23 07:26:18 +00:00
|
|
|
game->delta_s_accumulator > PHYSICS_STEP;
|
|
|
|
game->delta_s_accumulator -= PHYSICS_STEP) {
|
2023-09-24 14:43:22 +00:00
|
|
|
FP_Update(platform, game, input);
|
2023-09-23 07:26:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 02:33:59 +00:00
|
|
|
FP_Render(game, platform, renderer);
|
2023-09-23 06:42:22 +00:00
|
|
|
|
|
|
|
// NOTE: UI ====================================================================================
|
2023-09-20 13:35:38 +00:00
|
|
|
{
|
|
|
|
TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity());
|
|
|
|
DQN_DEFER { TELY_Render_PopTransform(renderer); };
|
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
// NOTE: Info bar ==========================================================================
|
|
|
|
{
|
|
|
|
TELY_RFuiResult info_bar = TELY_RFui_Row(rfui, DQN_STRING8("Info Bar"));
|
|
|
|
info_bar.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute;
|
|
|
|
info_bar.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f;
|
|
|
|
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute;
|
|
|
|
info_bar.widget->semantic_position[TELY_RFuiAxis_Y].value = 10.f;
|
|
|
|
|
|
|
|
TELY_RFui_PushParent(rfui, info_bar.widget);
|
|
|
|
DQN_DEFER { TELY_RFui_PopParent(rfui); };
|
|
|
|
|
|
|
|
Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr);
|
|
|
|
TELY_RFui_TextF(rfui, "TELY");
|
|
|
|
if (Dqn_String8_IsValid(platform->core.os_name)) {
|
|
|
|
TELY_RFui_TextF(rfui, " | %.*s", DQN_STRING_FMT(platform->core.os_name));
|
|
|
|
}
|
2023-09-20 13:35:38 +00:00
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
TELY_RFui_TextF(rfui,
|
|
|
|
" | %dx%d %.1fHz | TSC %.1f GHz",
|
|
|
|
platform->core.display.size.w,
|
|
|
|
platform->core.display.size.h,
|
|
|
|
platform->core.display.refresh_rate,
|
|
|
|
platform->core.tsc_per_second / 1'000'000'000.0);
|
|
|
|
|
|
|
|
if (platform->core.ram_mb)
|
|
|
|
TELY_RFui_TextF(rfui, " | RAM %.1fGB", platform->core.ram_mb / 1024.0);
|
|
|
|
|
|
|
|
TELY_RFui_TextF(rfui,
|
|
|
|
" | Work %04.1fms/f (%04.1f%%) | %05.1f FPS | Frame %'I64u | Timer %.1fs",
|
|
|
|
input->work_ms,
|
|
|
|
input->work_ms * 100.0 / input->delta_ms,
|
|
|
|
1000.0 / input->delta_ms,
|
|
|
|
input->frame_counter,
|
|
|
|
input->timer_s);
|
2023-09-20 13:35:38 +00:00
|
|
|
}
|
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
// NOTE: Profiler
|
|
|
|
{
|
|
|
|
TELY_RFuiResult profiler_layout = TELY_RFui_Column(rfui, DQN_STRING8("Profiler Bar"));
|
|
|
|
profiler_layout.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute;
|
|
|
|
profiler_layout.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f;
|
|
|
|
profiler_layout.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute;
|
|
|
|
profiler_layout.widget->semantic_position[TELY_RFuiAxis_Y].value = TELY_Asset_GetFont(assets, TELY_RFui_ActiveFont(rfui))->pixel_height * 1.5f;
|
|
|
|
|
|
|
|
TELY_RFui_PushParent(rfui, profiler_layout.widget);
|
|
|
|
DQN_DEFER { TELY_RFui_PopParent(rfui); };
|
|
|
|
|
|
|
|
Dqn_ProfilerAnchor *anchors = Dqn_Profiler_AnchorBuffer(Dqn_ProfilerAnchorBuffer_Back);
|
|
|
|
for (size_t anchor_index = 1; anchor_index < DQN_PROFILER_ANCHOR_BUFFER_SIZE; anchor_index++) {
|
|
|
|
Dqn_ProfilerAnchor const *anchor = anchors + anchor_index;
|
|
|
|
if (!anchor->hit_count)
|
|
|
|
continue;
|
2023-09-20 13:35:38 +00:00
|
|
|
|
2023-09-23 06:42:22 +00:00
|
|
|
uint64_t tsc_exclusive = anchor->tsc_exclusive;
|
|
|
|
uint64_t tsc_inclusive = anchor->tsc_inclusive;
|
|
|
|
Dqn_f64 tsc_exclusive_milliseconds = tsc_exclusive * 1000 / DQN_CAST(Dqn_f64)platform->core.tsc_per_second;
|
|
|
|
if (tsc_exclusive == tsc_inclusive) {
|
|
|
|
TELY_RFui_TextF(rfui,
|
|
|
|
"%.*s[%u]: %.1fms",
|
|
|
|
DQN_STRING_FMT(anchor->name),
|
|
|
|
anchor->hit_count,
|
|
|
|
tsc_exclusive_milliseconds);
|
|
|
|
} else {
|
|
|
|
Dqn_f64 tsc_inclusive_milliseconds = tsc_inclusive * 1000 / DQN_CAST(Dqn_f64)platform->core.tsc_per_second;
|
|
|
|
TELY_RFui_TextF(rfui,
|
|
|
|
"%.*s[%u]: %.1f/%.1fms",
|
|
|
|
DQN_STRING_FMT(anchor->name),
|
|
|
|
anchor->hit_count,
|
|
|
|
tsc_exclusive_milliseconds,
|
|
|
|
tsc_inclusive_milliseconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 13:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TELY_RFui_Flush(rfui, renderer, input, assets);
|
2023-09-24 05:33:02 +00:00
|
|
|
//TELY_Audio_MixPlaybackSamples(audio, assets);
|
2023-09-16 02:21:24 +00:00
|
|
|
}
|