#if defined(_CLANGD) #pragma once #include "feely_pona_unity.h" #endif Dqn_V2 const FP_TARGET_VIEWPORT_SIZE = Dqn_V2_InitNx2(1824, 1046); static FP_ParticleDescriptor FP_DefaultFloatUpParticleDescriptor(Dqn_Str8 anim_name, Dqn_usize duration_ms) { FP_ParticleDescriptor result = {}; result.anim_name = anim_name; result.velocity.y = -16.f; result.velocity_variance.y = (result.velocity.y * .5f); result.velocity_variance.x = DQN_ABS(result.velocity.y); result.colour_begin = TELY_COLOUR_WHITE_V4; result.colour_end = TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.f); result.duration_ms = duration_ms; return result; } static void FP_EmitParticle(FP_Game *game, FP_ParticleDescriptor descriptor, Dqn_usize count) { DQN_FOR_UINDEX (index, count) { uint32_t particle_index = game->play.particle_next_index++ & (DQN_ARRAY_UCOUNT(game->play.particles) - 1); FP_Particle *particle = game->play.particles + particle_index; if (particle->alive) continue; particle->anim_name = Dqn_Str8_Copy(Dqn_Arena_Allocator(game->frame_arena), descriptor.anim_name); particle->alive = true; particle->pos = descriptor.pos; particle->velocity.x = descriptor.velocity.x + (descriptor.velocity_variance.x * (Dqn_PCG32_NextF32(&game->play.rng) - 0.5f)); particle->velocity.y = descriptor.velocity.y + (descriptor.velocity_variance.y * (Dqn_PCG32_NextF32(&game->play.rng) - 0.5f)); particle->colour_begin = descriptor.colour_begin; particle->colour_end = descriptor.colour_end; particle->start_ms = game->play.clock_ms; particle->end_ms = game->play.clock_ms + descriptor.duration_ms; } } static TELY_AssetSpriteSheet FP_LoadSpriteSheetFromSpec(TELY_OS *os, TELY_Assets *assets, Dqn_Arena *arena, Dqn_Str8 sheet_name) { TELY_AssetSpriteSheet result = {}; Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(arena); result.sprite_size = Dqn_V2I_InitNx2(185, 170); result.type = TELY_AssetSpriteSheetType_Rects; // NOTE: Load the sprite meta file ========================================================= Dqn_Str8 sprite_spec_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s.txt", DQN_STR_FMT(assets->textures_dir), DQN_STR_FMT(sheet_name)); Dqn_Str8 sprite_spec_buffer = os->funcs.load_file(scratch.arena, sprite_spec_path); Dqn_Str8SplitAllocResult lines = Dqn_Str8_SplitAlloc(scratch.allocator, sprite_spec_buffer, DQN_STR8("\n")); Dqn_usize sprite_rect_index = 0; Dqn_usize sprite_anim_index = 0; DQN_FOR_UINDEX(line_index, lines.size) { Dqn_Str8 line = lines.data[line_index]; Dqn_Str8SplitAllocResult line_splits = Dqn_Str8_SplitAlloc(scratch.allocator, line, DQN_STR8(";")); if (line_index == 0) { DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @file lines"); DQN_ASSERT(Dqn_Str8_StartsWith(line_splits.data[0], DQN_STR8("@file"), Dqn_Str8EqCase_Sensitive)); // NOTE: Sprite sheet path Dqn_Str8 sprite_sheet_path = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/%.*s", DQN_STR_FMT(assets->textures_dir), DQN_STR_FMT(line_splits.data[1])); result.tex_handle = os->funcs.load_texture(assets, sheet_name, sprite_sheet_path); DQN_ASSERTF(Dqn_Fs_Exists(sprite_sheet_path), "Required file does not exist '%.*s'", DQN_STR_FMT(sprite_sheet_path)); // NOTE: Total sprite frame count Dqn_Str8ToU64Result total_frame_count = Dqn_Str8_ToU64(line_splits.data[2], 0); DQN_ASSERT(total_frame_count.success); result.rects = Dqn_Slice_Alloc(arena, total_frame_count.value, Dqn_ZeroMem_No); // NOTE: Total animation count Dqn_Str8ToU64Result total_anim_count = Dqn_Str8_ToU64(line_splits.data[3], 0); DQN_ASSERT(total_anim_count.success); result.anims = Dqn_Slice_Alloc(arena, total_anim_count.value, Dqn_ZeroMem_No); // TODO: Sprite size? // TODO: Texture name? continue; } if (Dqn_Str8_StartsWith(line, DQN_STR8("@anim"))) { DQN_ASSERTF(line_splits.size == 4, "Expected 4 splits for @anim lines"); Dqn_Str8 anim_name = line_splits.data[1]; Dqn_Str8ToU64Result frames_per_second = Dqn_Str8_ToU64(line_splits.data[2], 0); Dqn_Str8ToU64Result frame_count = Dqn_Str8_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_Str8_Copy(allocator, anim_name); anim->index = DQN_CAST(uint16_t)sprite_rect_index; anim->count = DQN_CAST(uint16_t)frame_count.value; anim->ms_per_frame = DQN_CAST(uint32_t)(1000.f / frames_per_second.value); DQN_ASSERT(anim->ms_per_frame != 0); } else { DQN_ASSERTF(line_splits.size == 5, "Expected 5 splits for sprite frame lines"); Dqn_Str8ToU64Result x = Dqn_Str8_ToU64(line_splits.data[0], 0); Dqn_Str8ToU64Result y = Dqn_Str8_ToU64(line_splits.data[1], 0); Dqn_Str8ToU64Result w = Dqn_Str8_ToU64(line_splits.data[2], 0); Dqn_Str8ToU64Result h = Dqn_Str8_ToU64(line_splits.data[3], 0); DQN_ASSERT(x.success); DQN_ASSERT(y.success); DQN_ASSERT(w.success); DQN_ASSERT(h.success); result.rects.data[sprite_rect_index++] = 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); } } DQN_ASSERT(result.rects.size == sprite_rect_index); DQN_ASSERT(result.anims.size == sprite_anim_index); return result; } static void FP_SetDefaultGamepadBindings(FP_GameControls *controls) { // NOTE: Note up/down/left/right uses analog sticks, non-negotiable. controls->attack.gamepad_key = TELY_InputGamepadKey_X; controls->range_attack.gamepad_key = TELY_InputGamepadKey_Y; controls->build_mode.gamepad_key = TELY_InputGamepadKey_L3; controls->strafe.gamepad_key = TELY_InputGamepadKey_B; controls->dash.gamepad_key = TELY_InputGamepadKey_A; controls->buy_building.gamepad_key = TELY_InputGamepadKey_LeftBumper; controls->buy_upgrade.gamepad_key = TELY_InputGamepadKey_RightBumper; controls->move_building_ui_cursor_left.gamepad_key = TELY_InputGamepadKey_DLeft; controls->move_building_ui_cursor_right.gamepad_key = TELY_InputGamepadKey_DRight; } static FP_ListenForNewPlayerResult FP_ListenForNewPlayer(TELY_Input *input, FP_Game *game, bool tutorial_is_allowed) { FP_ListenForNewPlayerResult result = {}; if (game->play.players.size == 2) return result; FP_GamePlay *play = &game->play; bool keyboard_already_allocated = false; uint32_t gamepad_index = 0; if (play->players.size) { FP_GameEntityHandle first_player_handle = play->players.data[0]; FP_GameEntity *player = FP_Game_GetEntity(game, first_player_handle); if (player->controls.mode == FP_GameControlMode_Gamepad) { gamepad_index = 1; } else { // NOTE: We only allow one player to use the keyboard, everyone // else must use a gamepad. keyboard_already_allocated = true; } } bool keyboard_pressed = !keyboard_already_allocated && TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_B); bool gamepad_pressed = TELY_Input_GamepadKeyIsPressed(input, gamepad_index, TELY_InputGamepadKey_Start); if (tutorial_is_allowed) { if (!keyboard_already_allocated && TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_T)) { keyboard_pressed = true; result.tutorial_requested = true; } if (TELY_Input_GamepadKeyIsPressed(input, gamepad_index, TELY_InputGamepadKey_Select)) { gamepad_pressed = true; result.tutorial_requested = true; } } if (keyboard_pressed || gamepad_pressed) { FP_GameEntityHandle terry_handle = {}; if (play->players.size) { FP_GameEntityHandle player = play->players.data[play->players.size - 1]; Dqn_V2 player_pos = FP_Game_CalcEntityWorldPos(game, player); terry_handle = FP_Entity_CreatePerry(game, player_pos, "Perry"); game->play.perry_joined = FP_GamePerryJoins_Enters; } else { terry_handle = FP_Entity_CreateTerry(game, Dqn_V2_InitNx2(1380, 11), "Perry"); } Dqn_FArray_Add(&play->players, terry_handle); FP_GameEntity *terry = FP_Game_GetEntity(game, terry_handle); FP_GameControls *controls = &terry->controls; if (keyboard_pressed) { controls->mode = FP_GameControlMode_Keyboard; controls->up.scan_key = TELY_InputScanKey_W; controls->down.scan_key = TELY_InputScanKey_S; controls->left.scan_key = TELY_InputScanKey_A; controls->right.scan_key = TELY_InputScanKey_D; controls->attack.scan_key = TELY_InputScanKey_J; controls->range_attack.scan_key = TELY_InputScanKey_K; controls->build_mode.scan_key = TELY_InputScanKey_H; controls->strafe.scan_key = TELY_InputScanKey_L; controls->dash.scan_key = TELY_InputScanKey_N; controls->buy_building.scan_key = TELY_InputScanKey_U; controls->buy_upgrade.scan_key = TELY_InputScanKey_I; controls->move_building_ui_cursor_left.scan_key = TELY_InputScanKey_Q; controls->move_building_ui_cursor_right.scan_key = TELY_InputScanKey_E; } else { controls->mode = FP_GameControlMode_Gamepad; controls->gamepad_index = gamepad_index; FP_SetDefaultGamepadBindings(controls); } result.yes = true; } return result; } uint64_t const FP_COOLDOWN_WAVE_TIME_MS = 30'000; static void FP_PlayReset(FP_Game *game, TELY_OS *os) { FP_GamePlay *play = &game->play; if (game->play.root_entity) FP_Game_DeleteEntity(game, game->play.root_entity->handle); Dqn_VArray shallow_entities_copy = play->entities; DQN_MEMSET(play, 0, sizeof(*play)); play->chunk_pool = &os->chunk_pool; play->meters_to_pixels = 65.416f; play->entities = shallow_entities_copy; if (play->entities.data) { Dqn_VArray_Clear(&play->entities, Dqn_ZeroMem_Yes); } else { play->entities = Dqn_VArray_Init(&play->arena, 1024 * 8); } play->root_entity = Dqn_VArray_Make(&play->entities, Dqn_ZeroMem_No); Dqn_FArray_Add(&play->parent_entity_stack, play->root_entity->handle); Dqn_PCG32_Seed(&play->rng, os->core.epoch_time); // NOTE: Seed the shuffled list with indexes DQN_FOR_UINDEX (index, DQN_ARRAY_UCOUNT(play->monkey_spawn_shuffled_list)) { play->monkey_spawn_shuffled_list[index] = DQN_CAST(uint8_t)index; } // NOTE: Fisher yates shuffle the list DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(6293) // Ill-formed for loop (potential wrapping index) for (Dqn_usize index = DQN_ARRAY_UCOUNT(play->monkey_spawn_shuffled_list) - 1; index < DQN_ARRAY_UCOUNT(play->monkey_spawn_shuffled_list); index--) { DQN_MSVC_WARNING_POP uint32_t swap_index = Dqn_PCG32_Range(&play->rng, 0, DQN_CAST(uint32_t)index + 1); DQN_SWAP(play->monkey_spawn_shuffled_list[swap_index], play->monkey_spawn_shuffled_list[index]); } // NOTE: Map =================================================================================== { TELY_AssetSpriteAnimation *sprite_anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.map); Dqn_Rect sprite_rect = game->atlas_sprite_sheet.rects.data[sprite_anim->index]; FP_GameEntity *entity = FP_Game_MakeEntityPointerF(game, "Map"); entity->type = FP_EntityType_Map; FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, entity->type, 0 /*state*/, FP_GameDirection_Down); entity->sprite_height = render_data.height; entity->local_pos = {}; Dqn_f32 size_scale = FP_Entity_CalcSpriteScaleForDesiredHeight(game, entity->sprite_height, sprite_rect); Dqn_V2 sprite_rect_scaled = sprite_rect.size * size_scale; entity->local_hit_box_size = sprite_rect_scaled; FP_Game_EntityActionReset(game, entity->handle, FP_GAME_ENTITY_ACTION_INFINITE_TIMER, render_data.sprite); play->map = entity; } // NOTE: Map walls ============================================================================= FP_GameEntity const *map = play->map; Dqn_Rect const map_hit_box = FP_Game_CalcEntityWorldHitBox(game, map->handle); { Dqn_f32 wall_thickness = FP_Game_MetersToPixelsNx1(game->play, 1.f); Dqn_f32 half_wall_thickness = wall_thickness * .5f; FP_Entity_CreateWallAtPos(game, DQN_STR8("Left Wall"), Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.f, 0.5f)) - Dqn_V2_InitNx2(half_wall_thickness, 0.f), Dqn_V2_InitNx2(wall_thickness, map_hit_box.size.h)); FP_Entity_CreateWallAtPos(game, DQN_STR8("Right Wall"), Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(1.f, 0.5f)) + Dqn_V2_InitNx2(half_wall_thickness, 0.f), Dqn_V2_InitNx2(wall_thickness, map_hit_box.size.h)); FP_Entity_CreateWallAtPos(game, DQN_STR8("Top Wall"), Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.5f, 0.f)) - Dqn_V2_InitNx2(0.f, half_wall_thickness), Dqn_V2_InitNx2(map_hit_box.size.w, wall_thickness)); FP_Entity_CreateWallAtPos(game, DQN_STR8("Bottom Wall"), Dqn_Rect_InterpolatedPoint(map_hit_box, Dqn_V2_InitNx2(0.5f, 1.f)) + Dqn_V2_InitNx2(0.f, half_wall_thickness), Dqn_V2_InitNx2(map_hit_box.size.w, wall_thickness)); } // NOTE: Map building zones { { FP_Entity_CreatePermittedBuildZone(game, Dqn_V2_InitNx2(0.f, -1206), Dqn_V2_InitNx2(map_hit_box.size.w, 335), "Building Zone"); } { FP_Entity_CreatePermittedBuildZone(game, Dqn_V2_InitNx2(-839.9, -460), Dqn_V2_InitNx2(2991.3, 670), "Building Zone"); } { FP_Entity_CreatePermittedBuildZone(game, Dqn_V2_InitNx2(-839.9, 460), Dqn_V2_InitNx2(2991.3, 670), "Building Zone"); } { FP_Entity_CreatePermittedBuildZone(game, Dqn_V2_InitNx2(0.f, 1200), Dqn_V2_InitNx2(map_hit_box.size.w, 335), "Building Zone"); } } Dqn_V2 base_mid_p = Dqn_V2_InitNx2(1580, 0.f); { Dqn_V2 mid_lane_mob_spawner_pos = Dqn_V2_InitNx2(play->map->local_hit_box_size.w * -0.5f + 128.f, 0.f); Dqn_V2 bottom_lane_mob_spawner_pos = Dqn_V2_InitNx2(mid_lane_mob_spawner_pos.x, mid_lane_mob_spawner_pos.y + 932.f); Dqn_V2 top_lane_mob_spawner_pos = Dqn_V2_InitNx2(mid_lane_mob_spawner_pos.x, mid_lane_mob_spawner_pos.y - 915.f); Dqn_usize spawn_cap = 16; // NOTE: Top lane spawner =================================================================== { FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, top_lane_mob_spawner_pos, spawn_cap, "Mob spawner"); FP_Game_PushParentEntity(game, mob_spawner); FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-top_lane_mob_spawner_pos.x + base_mid_p.x, 0.f), "Waypoint"); FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-top_lane_mob_spawner_pos.x + base_mid_p.x, +915.f), "Waypoint"); FP_Game_PopParentEntity(game); Dqn_FArray_Add(&play->mob_spawners, mob_spawner); } // NOTE: Mid lane mob spawner ================================================================== { FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, mid_lane_mob_spawner_pos, spawn_cap, "Mob spawner"); FP_Game_PushParentEntity(game, mob_spawner); FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-mid_lane_mob_spawner_pos.x + base_mid_p.x, base_mid_p.y), "Waypoint"); FP_Game_PopParentEntity(game); Dqn_FArray_Add(&play->mob_spawners, mob_spawner); } // NOTE: Bottom lane spawner =================================================================== { FP_GameEntityHandle mob_spawner = FP_Entity_CreateMobSpawner(game, bottom_lane_mob_spawner_pos, spawn_cap, "Mob spawner"); FP_Game_PushParentEntity(game, mob_spawner); FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-bottom_lane_mob_spawner_pos.x + base_mid_p.x, 0.f), "Waypoint"); FP_Entity_CreateWaypointF(game, Dqn_V2_InitNx2(-bottom_lane_mob_spawner_pos.x + base_mid_p.x, -932.f), "Waypoint"); FP_Game_PopParentEntity(game); Dqn_FArray_Add(&play->mob_spawners, mob_spawner); } } { Dqn_V2 base_top_left_pos = Dqn_V2_InitNx2(1018, -335); Dqn_V2 base_bottom_right_pos = Dqn_V2_InitNx2(2050, +351); Dqn_V2 base_top_left = base_top_left_pos; Dqn_V2 base_top_right = Dqn_V2_InitNx2(base_bottom_right_pos.x, base_top_left_pos.y); Dqn_V2 base_bottom_left = Dqn_V2_InitNx2(base_top_left_pos.x, base_bottom_right_pos.y); Dqn_V2 base_bottom_right = Dqn_V2_InitNx2(base_bottom_right_pos.x, base_bottom_right_pos.y); play->merchant_terry = FP_Entity_CreateMerchantTerry(game, base_top_left, "Merchant"); play->merchant_graveyard = FP_Entity_CreateMerchantGraveyard(game, base_bottom_left, "Graveyard"); play->merchant_gym = FP_Entity_CreateMerchantGym(game, base_bottom_right, "Gym"); play->merchant_phone_company = FP_Entity_CreateMerchantPhoneCompany(game, base_top_right, "PhoneCompany"); } #if 0 FP_Entity_CreateClubTerry(game, Dqn_V2_InitNx2(+500, -191), "Club Terry"); FP_Entity_CreateKennelTerry(game, Dqn_V2_InitNx2(-300, -191), "Kennel Terry"); FP_Entity_CreateChurchTerry(game, Dqn_V2_InitNx2(-800, -191), "Church Terry"); FP_Entity_CreateAirportTerry(game, Dqn_V2_InitNx2(-1200, -191), "Airport Terry"); #endif // NOTE: Heart game->play.heart = FP_Entity_CreateHeart(game, base_mid_p, "Heart"); play->tile_size = 37; // NOTE: Create billboads ====================================================================== FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2( 511, 451), FP_EntityBillboardState_Dash, "Dash Billboard"); FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2( 511, -451), FP_EntityBillboardState_Attack, "Attack Billboard"); FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2(2047, -720), FP_EntityBillboardState_RangeAttack, "Range Attack Billboard"); FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2(-936, -500), FP_EntityBillboardState_Monkey, "Monkey Billboard"); FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2(1898, 771), FP_EntityBillboardState_Strafe, "Strafe Billboard"); play->billboard_build = FP_Entity_CreateBillboard(game, Dqn_V2_InitNx2(1068, 619), FP_EntityBillboardState_Build, "Build Billboard"); // NOTE: Camera ================================================================================ play->camera.world_pos = {}; play->camera.scale = Dqn_V2_InitNx1(1); play->camera.size = FP_TARGET_VIEWPORT_SIZE; } TELY_OS_DLL_FUNCTION void TELY_OS_DLLReload(TELY_OS *os) { Dqn_Library_SetPointer(os->core.dqn_lib); g_tely = os->funcs; os->funcs.set_window_title(DQN_STR8("Terry Cherry")); } TELY_OS_DLL_FUNCTION void TELY_OS_DLLInit(TELY_OS *os) { TELY_OS_DLLReload(os); FP_UnitTests(os); // NOTE: TELY Game ============================================================================= TELY_Assets *assets = &os->assets; assets->chunk_pool = &os->chunk_pool; FP_Game *game = Dqn_Arena_New(&os->arena, FP_Game, Dqn_ZeroMem_Yes); Dqn_f32 font_scalar = FP_TARGET_VIEWPORT_SIZE.w / DQN_CAST(Dqn_f32)os->core.window_size.x; game->font_size = DQN_CAST(uint16_t)(18 * font_scalar); game->large_font_size = DQN_CAST(uint16_t)(game->font_size * 5.f); game->large_talkco_font_size = DQN_CAST(uint16_t)(game->font_size * 1.5f); game->xlarge_talkco_font_size = DQN_CAST(uint16_t)(game->font_size * 2.f); game->inter_regular_font = TELY_Asset_LoadFont(assets, DQN_STR8("Inter (Regular)"), DQN_STR8("Data/Fonts/Inter-Regular.otf")); game->inter_italic_font = TELY_Asset_LoadFont(assets, DQN_STR8("Inter (Italic)"), DQN_STR8("Data/Fonts/Inter-Italic.otf")); game->jetbrains_mono_font = TELY_Asset_LoadFont(assets, DQN_STR8("JetBrains Mono NL (Regular)"), DQN_STR8("Data/Fonts/JetBrainsMonoNL-Regular.ttf")); game->talkco_font = TELY_Asset_LoadFont(assets, DQN_STR8("Talkco"), DQN_STR8("Data/Fonts/Talkco.otf")); game->audio[FP_GameAudio_TerryHit] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Terry Hit"), DQN_STR8("Data/Audio/terry_hit.ogg")); game->audio[FP_GameAudio_Ching] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Ching"), DQN_STR8("Data/Audio/ching.ogg")); game->audio[FP_GameAudio_Church] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Church"), DQN_STR8("Data/Audio/church.ogg")); game->audio[FP_GameAudio_Club] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Club"), DQN_STR8("Data/Audio/club_terry.ogg")); game->audio[FP_GameAudio_Dog] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Dog"), DQN_STR8("Data/Audio/dog.ogg")); game->audio[FP_GameAudio_MerchantGhost] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Ghost"), DQN_STR8("Data/Audio/merchant_ghost.ogg")); game->audio[FP_GameAudio_MerchantGym] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Gym"), DQN_STR8("Data/Audio/merchant_gym.ogg")); game->audio[FP_GameAudio_MerchantPhone] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Phone"), DQN_STR8("Data/Audio/merchant_tech.ogg")); game->audio[FP_GameAudio_MerchantTerry] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Door"), DQN_STR8("Data/Audio/merchant_terry.ogg")); game->audio[FP_GameAudio_Message] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Message"), DQN_STR8("Data/Audio/message.ogg")); game->audio[FP_GameAudio_Monkey] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Monkey"), DQN_STR8("Data/Audio/monkey.ogg")); game->audio[FP_GameAudio_Plane] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Plane"), DQN_STR8("Data/Audio/airport.ogg")); game->audio[FP_GameAudio_PortalDestroy] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Portal Destroy"), DQN_STR8("Data/Audio/portal_destroy.ogg")); game->audio[FP_GameAudio_Smooch] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Smooch"), DQN_STR8("Data/Audio/smooch.ogg")); game->audio[FP_GameAudio_Woosh] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Woosh"), DQN_STR8("Data/Audio/woosh.ogg")); game->audio[FP_GameAudio_GameStart] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Game Start"), DQN_STR8("Data/Audio/game_start.ogg")); game->audio[FP_GameAudio_PerryStart] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Perry Start"), DQN_STR8("Data/Audio/perry_start.ogg")); game->audio[FP_GameAudio_Ambience1] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Ambience one"), DQN_STR8("Data/Audio/ambience_1.ogg")); game->audio[FP_GameAudio_Ambience2] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Ambience two"), DQN_STR8("Data/Audio/ambience_2.ogg")); game->audio[FP_GameAudio_Music1] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Music one"), DQN_STR8("Data/Audio/music_1.ogg")); game->audio[FP_GameAudio_Music2] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Music two"), DQN_STR8("Data/Audio/music_2.ogg")); game->audio[FP_GameAudio_RelaxMusic] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Relax Music"), DQN_STR8("Data/Audio/relax.wav")); game->audio[FP_GameAudio_StressMusic] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Stress Music"), DQN_STR8("Data/Audio/stress.wav")); game->audio[FP_GameAudio_Relax01Music] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Relax_01 Music"), DQN_STR8("Data/Audio/relax_01.wav")); game->audio[FP_GameAudio_Stress01Music] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Stress_01 Music"), DQN_STR8("Data/Audio/stress_01.wav")); game->audio[FP_GameAudio_Relax02Music] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Relax_02 Music"), DQN_STR8("Data/Audio/relax_02.wav")); game->audio[FP_GameAudio_Stress03Music] = TELY_Asset_LoadAudio(assets, os, DQN_STR8("Stress_03 Music"), DQN_STR8("Data/Audio/stress_03.wav")); // NOTE: Load sprite sheets ==================================================================== os->user_data = game; game->atlas_sprite_sheet = FP_LoadSpriteSheetFromSpec(os, assets, &os->arena, DQN_STR8("atlas")); FP_PlayReset(game, os); } struct FP_GetClosestPortalMonkeyResult { FP_GameEntityHandle entity; Dqn_f32 dist; }; FP_GetClosestPortalMonkeyResult FP_GetClosestPortalMonkey(FP_Game *game, FP_GameEntityHandle handle) { // NOTE: Check if we are nearby a monkey and picking it up FP_GetClosestPortalMonkeyResult result = {}; result.dist = DQN_F32_MAX; for (FP_GameEntityHandle portal_monkey_handle : game->play.portal_monkeys) { FP_GameEntity *portal_monkey = FP_Game_GetEntity(game, portal_monkey_handle); if (FP_Game_IsNilEntity(portal_monkey)) continue; Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, handle); Dqn_V2 portal_monkey_pos = FP_Game_CalcEntityWorldPos(game, portal_monkey_handle); Dqn_f32 dist_squared = Dqn_V2_LengthSq_V2x2(entity_pos, portal_monkey_pos); if (dist_squared < result.dist) { result.dist = dist_squared; result.entity = portal_monkey_handle; } } return result; } static void FP_AppendMobSpawnerWaypoints(FP_Game *game, FP_GameEntityHandle src_handle, FP_GameEntityHandle dest_handle) { FP_GameEntity *src = FP_Game_GetEntity(game, src_handle); FP_GameEntity *dest = FP_Game_GetEntity(game, dest_handle); if (FP_Game_IsNilEntity(src) || FP_Game_IsNilEntity(dest)) return; Dqn_f32 one_meter = FP_Game_MetersToPixelsNx1(game->play, 1.f); for (FP_GameEntity *waypoint_entity = src->first_child; waypoint_entity; waypoint_entity = waypoint_entity->next) { if ((waypoint_entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) == 0) continue; // NOTE: Add the waypoint FP_SentinelListLink *waypoint = FP_SentinelList_Make(&dest->waypoints, game->play.chunk_pool); waypoint->data.entity = waypoint_entity->handle; waypoint->data.arrive = FP_GameWaypointArrive_WhenWithinEntitySize; waypoint->data.value = 1.5f; uint32_t min_vary = DQN_CAST(uint32_t)(one_meter * .5f); uint32_t max_vary = DQN_CAST(uint32_t)(one_meter * 2.f); waypoint->data.offset += Dqn_V2_InitNx2(DQN_CAST(Dqn_f32) Dqn_PCG32_Range(&game->play.rng, min_vary, max_vary), DQN_CAST(Dqn_f32) Dqn_PCG32_Range(&game->play.rng, min_vary, max_vary)); if (Dqn_PCG32_NextF32(&game->play.rng) >= .5f) waypoint->data.offset.x *= -1; if (Dqn_PCG32_NextF32(&game->play.rng) >= .5f) waypoint->data.offset.y *= -1; } } static void FP_EntityActionStateMachine(FP_Game *game, TELY_Audio *audio, TELY_Input *input, FP_GameEntity *entity, Dqn_V2 *acceleration_meters_per_s) { TELY_AssetSpriteSheet *sheet = &game->atlas_sprite_sheet; FP_GameEntityAction *action = &entity->action; bool const we_are_clicked_entity = entity->handle == game->play.clicked_entity; bool const entity_has_velocity = entity->velocity.x || entity->velocity.y; bool const entering_new_state = entity->alive_time_s == 0.f || action->state != action->next_state; bool const action_has_finished = !entering_new_state && game->play.clock_ms >= action->end_at_clock_ms; FP_GameControls const *controls = &entity->controls; action->state = action->next_state; FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, entity->type, entity->action.state, entity->direction); switch (entity->type) { case FP_EntityType_Perry: /*FALLTHRU*/ case FP_EntityType_Terry: { FP_EntityTerryState *state = DQN_CAST(FP_EntityTerryState *) & action->state; { FP_GameEntity *portal_monkey = FP_Game_GetEntity(game, entity->carried_monkey); if (!FP_Game_IsNilEntity(portal_monkey)) { Dqn_Rect hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); Dqn_f32 trig_t = DQN_CAST(Dqn_f32)game->play.clock_ms / 1000.f * 2.f; Dqn_V2 offset = Dqn_V2_InitNx2(DQN_COSF(trig_t), DQN_SINF(trig_t)) * 18.f; portal_monkey->local_pos = Dqn_Rect_InterpolatedPoint(hit_box, Dqn_V2_InitNx2(0.5f, -0.75f)) + offset; } } if (entity->hp <= 0 && *state != FP_EntityTerryState_DeadGhost) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_DeadGhost); break; } switch (*state) { case FP_EntityTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (entity->in_game_menu != FP_GameInGameMenu_Build) { bool picked_up_monkey_this_frame = false; if (FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { // NOTE: Check if we are nearby a monkey and picking it up FP_GetClosestPortalMonkeyResult closest_monkey = FP_GetClosestPortalMonkey(game, entity->handle); if (closest_monkey.dist < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 2.f))) { if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { entity->carried_monkey = closest_monkey.entity; picked_up_monkey_this_frame = true; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Monkey], 1.f); } } } if (FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack); } else if (FP_Game_KeyBindIsPressed(input, controls, controls->range_attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_RangeAttack); } } else { if (!picked_up_monkey_this_frame && FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_GameEntity *portal_monkey = FP_Game_GetEntity(game, entity->carried_monkey); portal_monkey->local_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); entity->carried_monkey = {}; } } } if (action->next_state == action->state && (acceleration_meters_per_s->x || acceleration_meters_per_s->y)) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Run); } } break; case FP_EntityTerryState_Attack: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; entity->attack_direction = entity->direction; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (action_has_finished) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle); } } break; case FP_EntityTerryState_RangeAttack: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->attack_direction = entity->direction; entity->terry_mobile_data_plan -= FP_TERRY_MOBILE_DATA_PER_RANGE_ATTACK; } if (action_has_finished) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle); } } break; case FP_EntityTerryState_Run: { if (entering_new_state || action->sprite.anim->label != render_data.anim_name) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } bool picked_up_monkey_this_frame = false; if (entity->in_game_menu != FP_GameInGameMenu_Build) { if (FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { // NOTE: Check if we are nearby a monkey and picking it up FP_GetClosestPortalMonkeyResult closest_monkey = FP_GetClosestPortalMonkey(game, entity->handle); if (closest_monkey.dist < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 2.f))) { if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { entity->carried_monkey = closest_monkey.entity; picked_up_monkey_this_frame = true; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Monkey], 1.f); } } } if (FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack); } else if (FP_Game_KeyBindIsPressed(input, controls, controls->range_attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_RangeAttack); } } } if (FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { if (FP_Game_KeyBindIsPressed(input, controls, controls->dash)) FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Dash); } else { if (!picked_up_monkey_this_frame && FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_GameEntity *portal_monkey = FP_Game_GetEntity(game, entity->carried_monkey); portal_monkey->local_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); entity->carried_monkey = {}; } } if (!entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle); } } break; case FP_EntityTerryState_Dash: { if (entering_new_state) { uint64_t duration_ms = 250; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); *acceleration_meters_per_s *= 35.f; entity->stamina -= FP_TERRY_DASH_STAMINA_COST; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Woosh], 1.f); } entity->action.sprite_alpha = Dqn_PCG32_NextF32(&game->play.rng); if (action_has_finished) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Run); entity->action.sprite_alpha = 1.f; } } break; case FP_EntityTerryState_DeadGhost: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->action.sprite_play_once = true; entity->faction = FP_GameEntityFaction_Nil; } Dqn_f32 hp_t = entity->hp / DQN_CAST(Dqn_f32)entity->hp_cap; if (hp_t >= 0.9f) entity->action.sprite_alpha = Dqn_PCG32_NextF32(&game->play.rng); if (entity->hp >= entity->hp_cap) { entity->faction = FP_GameEntityFaction_Friendly; entity->action.sprite_alpha = 1.f; entity->action.sprite_play_once = false; FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Idle); } } break; } } break; case FP_EntityType_Smoochie: { FP_EntitySmoochieState *state = DQN_CAST(FP_EntitySmoochieState *) & action->state; switch (*state) { case FP_EntitySmoochieState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack); } else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Run); } if (entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Run); } } break; case FP_EntitySmoochieState_Attack: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->attack_direction = entity->direction; // NOTE: Deal with this further down with the gameplay attack code. TELY_Audio_Play(audio, game->audio[FP_GameAudio_Smooch], 1.f); } // NOTE: Check if the heart animation is playing bool has_heart_flourish = false; DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) { FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index; if (sprite->asset.anim->label == g_anim_names.smoochie_attack_heart) { has_heart_flourish = true; break; } } // NOTE: If we don't have the anim playing make one if (!has_heart_flourish) { FP_GameRenderSprite *cosmetic_sprite = Dqn_FArray_Make(&entity->extra_cosmetic_anims, Dqn_ZeroMem_Yes); if (cosmetic_sprite) { cosmetic_sprite->asset = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.smoochie_attack_heart, TELY_AssetFlip_No); cosmetic_sprite->started_at_clock_ms = game->play.clock_ms; cosmetic_sprite->height.meters = entity->sprite_height.meters * .35f; cosmetic_sprite->loop = true; uint32_t max_rng_dist_x = DQN_CAST(uint32_t)(FP_Game_MetersToPixelsNx1(game->play, entity->sprite_height.meters) * 1.f); uint32_t rng_x = Dqn_PCG32_Range(&game->play.rng, DQN_CAST(uint32_t)0, max_rng_dist_x); cosmetic_sprite->offset.x = rng_x - (max_rng_dist_x * .5f); uint32_t max_rng_dist_y = DQN_CAST(uint32_t)(FP_Game_MetersToPixelsNx1(game->play, entity->sprite_height.meters) * .25f); uint32_t rng_y = Dqn_PCG32_Range(&game->play.rng, DQN_CAST(uint32_t)0, max_rng_dist_y); cosmetic_sprite->offset.y = -DQN_CAST(Dqn_f32)rng_y; } } if (action_has_finished) { // NOTE: Ensure the heart animation terminates by removing the loop DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) { FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index; if (sprite->asset.anim->label == g_anim_names.smoochie_attack_heart) sprite->loop = false; } // NOTE: Transition out of the action FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle); } } break; case FP_EntitySmoochieState_HurtSide: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (action_has_finished) FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle); } break; case FP_EntitySmoochieState_Death: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->local_hit_box_size = {}; } if (action_has_finished) FP_Game_DeleteEntity(game, entity->handle); } break; case FP_EntitySmoochieState_Run: { if (entering_new_state || action->sprite.anim->label != render_data.anim_name) { TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, render_data.anim_name, render_data.flip); uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite); } if (we_are_clicked_entity) { if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack); } } if (!entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Idle); } } break; } if (entity->is_dying && *state != FP_EntitySmoochieState_Death) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Death); } } break; case FP_EntityType_Clinger: { FP_EntityClingerState *state = DQN_CAST(FP_EntityClingerState *)&action->state; switch (*state) { case FP_EntityClingerState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack); } else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) { FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Run); } if (entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Run); } } break; case FP_EntityClingerState_Attack: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; entity->attack_direction = entity->direction; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (action_has_finished) FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Idle); } break; case FP_EntityClingerState_Death: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->local_hit_box_size = {}; } if (action_has_finished) FP_Game_DeleteEntity(game, entity->handle); } break; case FP_EntityClingerState_Run: { if (entering_new_state || action->sprite.anim->label != render_data.anim_name) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack); if (!entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Idle); } } break; } if (entity->is_dying && *state != FP_EntityClingerState_Death) { FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Death); } } break; case FP_EntityType_MerchantTerry: { FP_EntityMerchantTerryState *state = DQN_CAST(FP_EntityMerchantTerryState *)&action->state; switch (*state) { case FP_EntityMerchantTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_MerchantPhoneCompany: { FP_EntityMerchantPhoneCompanyState *state = DQN_CAST(FP_EntityMerchantPhoneCompanyState *)&action->state; switch (*state) { case FP_EntityMerchantPhoneCompanyState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_MerchantGym: { FP_EntityMerchantGymState *state = DQN_CAST(FP_EntityMerchantGymState *)&action->state; switch (*state) { case FP_EntityMerchantGymState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_MerchantGraveyard: { FP_EntityMerchantGraveyardState *state = DQN_CAST(FP_EntityMerchantGraveyardState *)&action->state; switch (*state) { case FP_EntityMerchantGraveyardState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_ClubTerry: { FP_EntityClubTerryState *state = DQN_CAST(FP_EntityClubTerryState *)&action->state; switch (*state) { case FP_EntityClubTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; case FP_EntityClubTerryState_PartyTime: { if (entering_new_state) { uint64_t duration_ms = 5000; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (action_has_finished) { if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) { FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron); patron->flags &= ~FP_GameEntityFlag_OccupiedInBuilding; patron->base_acceleration_per_s.meters *= .5f; patron->is_drunk = true; } entity->building_patron = {}; FP_Game_EntityTransitionState(game, entity, FP_EntityClubTerryState_Idle); } } break; } } break; case FP_EntityType_Map: { FP_EntityMapState *state = DQN_CAST(FP_EntityMapState *) & action->state; switch (*state) { case FP_EntityMapState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_Heart: { FP_EntityHeartState *state = DQN_CAST(FP_EntityHeartState *) & action->state; switch (*state) { case FP_EntityHeartState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_AirportTerry: { FP_EntityAirportTerryState *state = DQN_CAST(FP_EntityAirportTerryState *)&action->state; switch (*state) { case FP_EntityAirportTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; case FP_EntityAirportTerryState_FlyPassenger: { if (entering_new_state) { uint64_t duration_ms = 5000; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Plane], 1.f); } if (action_has_finished) { Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); FP_GameEntityHandle plane_handle = FP_Entity_CreateAirportTerryPlane(game, world_pos, "Aiport Terry Plane"); FP_GameEntity *plane = FP_Game_GetEntity(game, plane_handle); // NOTE: Transfer the entity to the plane plane->building_patron = entity->building_patron; entity->building_patron = {}; // NOTE: Add a waypoint for the plane to the mob spawn FP_GameEntityHandle mob_spawner = {}; Dqn_V2 mob_spawner_pos = {}; { uint32_t mob_spawner_index = Dqn_PCG32_Range(&game->play.rng, 0, DQN_CAST(uint32_t)game->play.mob_spawners.size); mob_spawner = game->play.mob_spawners.data[mob_spawner_index]; mob_spawner_pos = FP_Game_CalcEntityWorldPos(game, mob_spawner); plane->waypoints = FP_SentinelList_Init(game->play.chunk_pool); FP_SentinelListLink *link = FP_SentinelList_MakeBefore(&plane->waypoints, FP_SentinelList_Front(&plane->waypoints), game->play.chunk_pool); FP_GameWaypoint *waypoint = &link->data; waypoint->entity = mob_spawner; waypoint->type = FP_GameWaypointType_ClosestSide; } // NOTE: Update the mob's waypoints to the mob spawner waypoints FP_GameEntity *patron = FP_Game_GetEntity(game, plane->building_patron); FP_SentinelList_Clear(&patron->waypoints, game->play.chunk_pool); FP_AppendMobSpawnerWaypoints(game, entity->handle, plane->building_patron); FP_Game_EntityTransitionState(game, entity, FP_EntityAirportTerryState_Idle); } } break; } } break; case FP_EntityType_AirportTerryPlane: { FP_EntityAirportTerryPlaneState *state = DQN_CAST(FP_EntityAirportTerryPlaneState *)&action->state; switch (*state) { case FP_EntityAirportTerryPlaneState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) FP_Game_EntityTransitionState(game, entity, FP_EntityAirportTerryPlaneState_FlyPassenger); } break; case FP_EntityAirportTerryPlaneState_FlyPassenger: { if (entity->waypoints.size == 0) { FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron); patron->local_pos = entity->local_pos; patron->flags &= ~(FP_GameEntityFlag_OccupiedInBuilding); FP_Game_DeleteEntity(game, entity->handle); return; } } break; } } break; case FP_EntityType_Catfish: { FP_EntityCatfishState *state = DQN_CAST(FP_EntityCatfishState *) & action->state; switch (*state) { case FP_EntityCatfishState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack); } else if (acceleration_meters_per_s->x || acceleration_meters_per_s->y) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Run); } if (entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Run); } } break; case FP_EntityCatfishState_Attack: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->attack_direction = entity->direction; } if (action_has_finished) FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Idle); } break; case FP_EntityCatfishState_Death: { if (entering_new_state) { uint64_t duration_ms = render_data.sprite.anim->count * render_data.sprite.anim->ms_per_frame; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->local_hit_box_size = {}; } if (action_has_finished) FP_Game_DeleteEntity(game, entity->handle); } break; case FP_EntityCatfishState_Run: { if (entering_new_state || action->sprite.anim->label != render_data.anim_name) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (FP_Game_KeyBindIsPressed(input, controls, controls->attack)) FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack); if (!entity_has_velocity) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Idle); } } break; } if (entity->is_dying && *state != FP_EntityCatfishState_Death) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Death); } } break; case FP_EntityType_ChurchTerry: { FP_EntityChurchTerryState *state = DQN_CAST(FP_EntityChurchTerryState *)&action->state; switch (*state) { case FP_EntityChurchTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; case FP_EntityChurchTerryState_ConvertPatron: { if (entering_new_state) { uint64_t duration_ms = 5000; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (action_has_finished) { if (!FP_Game_IsNilEntityHandle(game, entity->building_patron)) { FP_GameEntity *patron = FP_Game_GetEntity(game, entity->building_patron); patron->flags &= ~(FP_GameEntityFlag_OccupiedInBuilding | FP_GameEntityFlag_RespondsToBuildings); patron->faction = FP_GameEntityFaction_Friendly; patron->converted_faction = true; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Church], 1.f); } entity->building_patron = {}; FP_Game_EntityTransitionState(game, entity, FP_EntityChurchTerryState_Idle); } } break; } } break; case FP_EntityType_KennelTerry: { FP_EntityKennelTerryState *state = DQN_CAST(FP_EntityKennelTerryState *)&action->state; switch (*state) { case FP_EntityKennelTerryState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } } break; } } break; case FP_EntityType_Nil: break; case FP_EntityType_Count: DQN_INVALID_CODE_PATH; break; case FP_EntityType_PhoneMessageProjectile: break; case FP_EntityType_MobSpawner: { FP_EntityMobSpawnerState *state = DQN_CAST(FP_EntityMobSpawnerState *)&action->state; switch (*state) { case FP_EntityMobSpawnerState_Idle: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (!FP_Game_IsNilEntityHandle(game, entity->carried_monkey)) { FP_Game_EntityTransitionState(game, entity, FP_EntityMobSpawnerState_Shutdown); } } break; case FP_EntityMobSpawnerState_Shutdown: { if (entering_new_state) { uint64_t duration_ms = FP_GAME_ENTITY_ACTION_INFINITE_TIMER; FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); entity->action.sprite_play_once = true; TELY_Audio_Play(audio, game->audio[FP_GameAudio_PortalDestroy], 1.f); FP_Game_DeleteEntity(game, entity->carried_monkey); } } break; } } break; case FP_EntityType_PortalMonkey: break; case FP_EntityType_Billboard: break; } switch (entity->type) { case FP_EntityType_Nil: case FP_EntityType_AirportTerry: case FP_EntityType_ChurchTerry: case FP_EntityType_ClubTerry: case FP_EntityType_Heart: case FP_EntityType_KennelTerry: case FP_EntityType_Map: case FP_EntityType_MerchantGraveyard: case FP_EntityType_MerchantGym: case FP_EntityType_MerchantPhoneCompany: case FP_EntityType_MerchantTerry: case FP_EntityType_PhoneMessageProjectile: case FP_EntityType_Count: break; case FP_EntityType_Terry: /*FALLTHRU*/ case FP_EntityType_Perry: /*FALLTHRU*/ case FP_EntityType_Catfish: /*FALLTHRU*/ case FP_EntityType_Clinger: /*FALLTHRU*/ case FP_EntityType_Smoochie: { bool is_attacking = false; bool is_range_attack = false; if (entity->type == FP_EntityType_Catfish) { is_attacking = entity->action.state == FP_EntityCatfishState_Attack; } else if (entity->type == FP_EntityType_Clinger) { is_attacking = entity->action.state == FP_EntityClingerState_Attack; } else if (entity->type == FP_EntityType_Smoochie) { is_attacking = entity->action.state == FP_EntitySmoochieState_Attack; } else { DQN_ASSERT(entity->type == FP_EntityType_Terry || entity->type == FP_EntityType_Perry); is_range_attack = entity->action.state == FP_EntityTerryState_RangeAttack; is_attacking = is_range_attack || entity->action.state == FP_EntityTerryState_Attack; } if (!is_attacking) { entity->attack_processed = false; entity->attack_box_size = {}; break; } // NOTE: Position the attack box uint64_t duration_ms = action->sprite.anim->count * action->sprite.anim->ms_per_frame; uint64_t midpoint_clock_ms = action->started_at_clock_ms + (duration_ms / 2); DQN_ASSERT(duration_ms >= FP_GAME_PHYSICS_STEP); DQN_ASSERT(action->sprite.anim); // NOTE: Adding an attack_processed bool to make sure things only fire once if (!entity->attack_processed && game->play.clock_ms >= midpoint_clock_ms) { // NOTE: Position the attack box if (is_range_attack) { Dqn_V2 dir_vector = {}; switch (entity->attack_direction) { case FP_GameDirection_Left: dir_vector.x = -1.f; break; case FP_GameDirection_Right: dir_vector.x = +1.f; break; case FP_GameDirection_Up: dir_vector.y = -1.f; break; case FP_GameDirection_Down: dir_vector.y = +1.f; break; case FP_GameDirection_Count: break; } Dqn_V2 projectile_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); Dqn_V2 projectile_acceleration = FP_Game_MetersToPixelsV2(game->play, dir_vector * 0.25f); FP_Entity_CreatePhoneMessageProjectile(game, entity->handle, projectile_pos, projectile_acceleration, "Phone Message Projectile"); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Message], 1.f /*volume*/); } else { Dqn_FArray attack_boxes = FP_Game_CalcEntityMeleeAttackBoxes(game, entity->handle); entity->attack_box_size = attack_boxes.data[entity->attack_direction].size; entity->attack_box_offset = attack_boxes.data[entity->attack_direction].pos - FP_Game_CalcEntityWorldPos(game, entity->handle); TELY_Audio_Play(audio, game->audio[FP_GameAudio_TerryHit], 1.f); } entity->attack_processed = true; } else { entity->attack_box_size = {}; } } break; case FP_EntityType_AirportTerryPlane: break; case FP_EntityType_MobSpawner: case FP_EntityType_PortalMonkey: break; case FP_EntityType_Billboard: break; } } static void FP_Update(TELY_OS *os, FP_Game *game, TELY_Input *input, TELY_Audio *audio) { Dqn_Profiler_ZoneScopeWithIndex("FP_Update", FP_ProfileZone_FPUpdate); if (game->play.state == FP_GameState_Pause) return; game->play.update_counter++; game->play.clock_ms += DQN_CAST(uint64_t)(os->input.delta_ms); Dqn_ProfilerZone update_zone = Dqn_Profiler_BeginZoneWithIndex(DQN_STR8("FP_Update: Entity loop"), FP_ProfileZone_FPUpdate_EntityLoop); if (game->play.state == FP_GameState_Play && game->play.perry_joined != FP_GamePerryJoins_Enters) { DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(4127) // Conditional expression is constant 'FP_DEVELOPER_MODE' if (FP_DEVELOPER_MODE && TELY_Input_KeyIsReleased(input->mouse_keys[TELY_InputMouseKey_Left])) game->play.clicked_entity = game->play.prev_active_entity; if (TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_Escape)) { game->play.state = FP_GameState_Pause; return; } if (FP_DEVELOPER_MODE && game->play.clicked_entity.id) { if (TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_Delete)) FP_Game_DeleteEntity(game, game->play.clicked_entity); } DQN_MSVC_WARNING_POP // NOTE: Handle input ========================================================================== for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->play.root_entity); ) { FP_GameEntity *entity = it.entity; entity->alive_time_s += FP_GAME_PHYSICS_STEP; if (entity->flags & FP_GameEntityFlag_OccupiedInBuilding) continue; // NOTE: Calc direction vector from input FP_GameControls const *controls = &entity->controls; Dqn_V2 dir_vector = {}; { if (FP_Game_KeyBindIsDown(input, controls, controls->up)) dir_vector.y = -1.f; if (FP_Game_KeyBindIsDown(input, controls, controls->left)) dir_vector.x = -1.f; if (FP_Game_KeyBindIsDown(input, controls, controls->down)) dir_vector.y = +1.f; if (FP_Game_KeyBindIsDown(input, controls, controls->right)) dir_vector.x = +1.f; // NOTE: Gamepad movement input if (controls->mode == FP_GameControlMode_Gamepad) { TELY_InputGamepad *gamepad = input->gamepads + controls->gamepad_index; dir_vector += gamepad->left_stick; } // TODO(doyle): Some bug, diagonal is still faster if (dir_vector.x && dir_vector.y) { dir_vector.x *= 0.7071067811865475244f; dir_vector.y *= 0.7071067811865475244f; } } // NOTE: Move entity by keyboard and gamepad =============================================== Dqn_V2 acceleration_meters_per_s = entity->constant_acceleration_per_s; if (entity->flags & (FP_GameEntityFlag_MoveByKeyboard | FP_GameEntityFlag_MoveByGamepad)) { bool move_entity = true; switch (entity->type) { case FP_EntityType_Perry: /*FALLTHRU*/ case FP_EntityType_Terry: { auto *state = DQN_CAST(FP_EntityTerryState *)&entity->action.state; move_entity = *state == FP_EntityTerryState_Run || *state == FP_EntityTerryState_Idle; if (*state == FP_EntityTerryState_DeadGhost) { FP_GameEntityAction const *action = &entity->action; uint64_t const elapsed_ms = game->play.clock_ms - action->started_at_clock_ms; uint16_t const raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / action->sprite.anim->ms_per_frame); if (raw_anim_frame >= action->sprite.anim->count) { move_entity = true; } } } break; case FP_EntityType_Smoochie: { auto *state = DQN_CAST(FP_EntitySmoochieState *)&entity->action.state; move_entity = *state == FP_EntitySmoochieState_Run || *state == FP_EntitySmoochieState_Idle; } break; case FP_EntityType_Clinger: { auto *state = DQN_CAST(FP_EntityClingerState *)&entity->action.state; move_entity = *state == FP_EntityClingerState_Run || *state == FP_EntityClingerState_Idle; } break; case FP_EntityType_Nil: break; case FP_EntityType_ClubTerry: break; case FP_EntityType_Count: break; case FP_EntityType_Map: break; case FP_EntityType_MerchantTerry: break; case FP_EntityType_MerchantGraveyard: break; case FP_EntityType_MerchantPhoneCompany: break; case FP_EntityType_Heart: break; case FP_EntityType_MerchantGym: break; case FP_EntityType_AirportTerry: break; case FP_EntityType_Catfish: break; case FP_EntityType_ChurchTerry: break; case FP_EntityType_KennelTerry: break; case FP_EntityType_PhoneMessageProjectile: break; case FP_EntityType_AirportTerryPlane: break; case FP_EntityType_MobSpawner: break; case FP_EntityType_PortalMonkey: break; case FP_EntityType_Billboard: break; } if (move_entity) { acceleration_meters_per_s += dir_vector * entity->base_acceleration_per_s.meters; if (dir_vector.x) entity->direction = entity->velocity.x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left; if (dir_vector.y) entity->direction = entity->velocity.y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up; } } else { if (entity->velocity.x) entity->direction = entity->velocity.x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left; else if (entity->velocity.y) entity->direction = entity->velocity.y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up; } // NOTE: Determine AI movement ============================================================= Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); if (entity->flags & FP_GameEntityFlag_Aggros && entity->faction != FP_GameEntityFaction_Nil) { FP_GameFindClosestEntityResult closest_defender = {}; closest_defender.dist_squared = DQN_F32_MAX; closest_defender.pos = Dqn_V2_InitNx1(DQN_F32_MAX); FP_GameEntityFaction enemy_faction = entity->faction == FP_GameEntityFaction_Friendly ? FP_GameEntityFaction_Foe : FP_GameEntityFaction_Friendly; for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->play.root_entity); ) { FP_GameEntity *it_entity = defender_it.entity; Dqn_V2 pos = FP_Game_CalcEntityWorldPos(game, it_entity->handle); Dqn_f32 dist = Dqn_V2_LengthSq_V2x2(pos, entity_pos); if (it_entity->faction != enemy_faction) continue; if (dist < closest_defender.dist_squared) { closest_defender.pos = pos; closest_defender.dist_squared = dist; closest_defender.entity = it_entity->handle; } } Dqn_f32 aggro_dist_threshold = FP_Game_MetersToPixelsNx1(game->play, 4.f); Dqn_f32 dist_to_defender = DQN_SQRTF(closest_defender.dist_squared); if (dist_to_defender > (aggro_dist_threshold * 1.5f)) { for (FP_SentinelListLink *link = nullptr; FP_SentinelList_Iterate(&entity->waypoints, &link); ) { FP_GameEntity *maybe_terry = FP_Game_GetEntity(game, link->data.entity); if (maybe_terry->type == FP_EntityType_Terry || maybe_terry->type == FP_EntityType_Perry) { link = FP_SentinelList_Erase(&entity->waypoints, link, game->play.chunk_pool); } } } else if (dist_to_defender < aggro_dist_threshold) { bool has_waypoint_to_defender = false; for (FP_SentinelListLink *link = nullptr; !has_waypoint_to_defender && FP_SentinelList_Iterate(&entity->waypoints, &link); ) { has_waypoint_to_defender = link->data.entity == closest_defender.entity; } if (!has_waypoint_to_defender) { FP_GameEntity *defender = FP_Game_GetEntity(game, closest_defender.entity); FP_SentinelListLink *link = FP_SentinelList_MakeBefore(&entity->waypoints, FP_SentinelList_Front(&entity->waypoints), game->play.chunk_pool); FP_GameWaypoint *waypoint = &link->data; waypoint->entity = defender->handle; waypoint->type = FP_GameWaypointType_ClosestSide; } } } // NOTE: Make waypoint to building ===================================================== // We can queue up to ente a building if we respond to the building AND we aren't // already queuing up in a building already. if (entity->flags & FP_GameEntityFlag_RespondsToBuildings && FP_Game_IsNilEntityHandle(game, entity->queued_at_building)) { FP_GameFindClosestEntityResult closest_building = {}; closest_building.dist_squared = DQN_F32_MAX; closest_building.pos = Dqn_V2_InitNx1(DQN_F32_MAX); for (FP_GameEntityIterator building_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &building_it, game->play.root_entity); ) { FP_GameEntity *it_entity = building_it.entity; if (it_entity->type != FP_EntityType_ClubTerry && it_entity->type != FP_EntityType_AirportTerry && it_entity->type != FP_EntityType_ChurchTerry) continue; // NOTE: Already converted, we cannot attend church again if (entity->converted_faction && it_entity->type == FP_EntityType_ChurchTerry) continue; // NOTE: Already drunk, we are not allowed to enter the nightclub again if (entity->is_drunk && it_entity->type == FP_EntityType_ClubTerry) continue; // NOTE: The queue to enter the building is completely full skip if (it_entity->building_queue.size == Dqn_FArray_Max(&it_entity->building_queue)) continue; // NOTE: Entity is already in the building queue, skip if (Dqn_FArray_Find(&it_entity->building_queue, entity->handle).data) continue; bool already_visited_building = false; for (FP_SentinelListLink *link_it = {}; !already_visited_building && FP_SentinelList_Iterate(&entity->buildings_visited, &link_it); ) { FP_GameEntityHandle visit_item = link_it->data; already_visited_building = visit_item == it_entity->handle; } if (already_visited_building) continue; Dqn_V2 pos = FP_Game_CalcEntityWorldPos(game, it_entity->handle); Dqn_f32 dist = Dqn_V2_LengthSq_V2x2(pos, entity_pos); if (dist < closest_building.dist_squared) { closest_building.pos = pos; closest_building.dist_squared = dist; closest_building.entity = it_entity->handle; } } if (!FP_Game_IsNilEntityHandle(game, closest_building.entity) && closest_building.dist_squared < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 5.f))) { bool has_waypoint_to_building = false; for (FP_SentinelListLink *link = nullptr; !has_waypoint_to_building && FP_SentinelList_Iterate(&entity->waypoints, &link); ) { has_waypoint_to_building = link->data.entity == closest_building.entity; } if (!has_waypoint_to_building) { FP_SentinelListLink *link = FP_SentinelList_MakeBefore(&entity->waypoints, FP_SentinelList_Front(&entity->waypoints), game->play.chunk_pool); FP_GameWaypoint *waypoint = &link->data; waypoint->entity = closest_building.entity; waypoint->type = FP_GameWaypointType_Queue; // NOTE: Add the entity to the building queue FP_GameEntity *building = FP_Game_GetEntity(game, closest_building.entity); Dqn_FArray_Add(&building->building_queue, entity->handle); // NOTE: Remember the building we are queued at entity->queued_at_building = building->handle; } } } // NOTE: Building queue ================================================================ for (Dqn_usize index = 0; index < entity->building_queue.size; index++) { FP_GameEntityHandle queue_entity_handle = entity->building_queue.data[index]; // NOTE: Delete dead entities if (FP_Game_IsNilEntityHandle(game, queue_entity_handle)) { index = Dqn_FArray_EraseRange(&entity->building_queue, index, 1 /*count*/, Dqn_ArrayErase_Stable).it_index; continue; } // NOTE: Delete far away entities FP_GameEntity *queue_entity = FP_Game_GetEntity(game, queue_entity_handle); Dqn_V2 queue_entity_p = FP_Game_CalcEntityWorldPos(game, queue_entity_handle); Dqn_V2 building_p = FP_Game_CalcEntityWorldPos(game, entity->handle); Dqn_f32 dist_sq = Dqn_V2_LengthSq_V2x2(queue_entity_p, building_p); Dqn_f32 threshold_sq = DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 10.f)); if (dist_sq >= threshold_sq || queue_entity->converted_faction) { // NOTE: Remove the entity from the building queue_entity->queued_at_building = {}; // NOTE: Remove it from the queue index = Dqn_FArray_EraseRange(&entity->building_queue, index, 1 /*count*/, Dqn_ArrayErase_Stable).it_index; // NOTE: Make sure the entity doesnt' try and revisit FP_SentinelList_Add(&queue_entity->buildings_visited, game->play.chunk_pool, entity->handle); // NOTE: Remove the waypoint from the entity if (queue_entity->waypoints.size) { for (FP_SentinelListLink *link = nullptr; FP_SentinelList_Iterate(&queue_entity->waypoints, &link); ) { if (link->data.entity == entity->handle) { FP_SentinelList_Erase(&queue_entity->waypoints, link, game->play.chunk_pool); } } } } } // NOTE: Handle waypoints ============================================================== while (entity->waypoints.size) { FP_SentinelListLink *waypoint_link = entity->waypoints.sentinel->next; FP_GameWaypoint const *waypoint = &waypoint_link->data; FP_GameEntity *waypoint_entity = FP_Game_GetEntity(game, waypoint_link->data.entity); if (FP_Game_IsNilEntity(waypoint_entity)) { FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->play.chunk_pool); continue; } // NOTE: We found a waypoint that is valid to move towards Dqn_V2 target_pos = FP_Game_CalcWaypointWorldPos(game, entity->handle, waypoint); Dqn_V2 entity_to_waypoint = target_pos - entity_pos; // NOTE: Check if we've arrived at the waypoint Dqn_f32 dist_to_waypoint_sq = Dqn_V2_LengthSq(entity_to_waypoint); // NOTE: Calculate the approaching direction FP_GameDirection approach_dir = FP_GameDirection_Up; { Dqn_V2 dir_vectors[FP_GameDirection_Count] = {}; dir_vectors[FP_GameDirection_Up] = Dqn_V2_InitNx2(+0, -1); dir_vectors[FP_GameDirection_Down] = Dqn_V2_InitNx2(+0, +1); dir_vectors[FP_GameDirection_Left] = Dqn_V2_InitNx2(-1, +0); dir_vectors[FP_GameDirection_Right] = Dqn_V2_InitNx2(+1, +0); Dqn_V2 target_entity_pos = FP_Game_CalcEntityWorldPos(game, waypoint_entity->handle); Dqn_V2 entity_to_target = target_entity_pos - entity_pos; Dqn_V2 entity_to_target_norm = Dqn_V2_Normalise(entity_to_target); Dqn_f32 approach_dir_scalar_projection_onto_entity_to_waypoint_vector = -1.1f; DQN_FOR_UINDEX (dir_index, FP_GameDirection_Count) { Dqn_V2 attack_dir = dir_vectors[dir_index]; Dqn_f32 scalar_projection = Dqn_V2_Dot(attack_dir, entity_to_target_norm); if (scalar_projection > approach_dir_scalar_projection_onto_entity_to_waypoint_vector) { approach_dir = DQN_CAST(FP_GameDirection)dir_index; approach_dir_scalar_projection_onto_entity_to_waypoint_vector = scalar_projection; } } } Dqn_f32 arrival_threshold = {}; switch (waypoint->arrive) { case FP_GameWaypointArrive_Default: { if (approach_dir == FP_GameDirection_Up || approach_dir == FP_GameDirection_Down) arrival_threshold = 10.f; else arrival_threshold = 10.f; } break; case FP_GameWaypointArrive_WhenWithinEntitySize: { arrival_threshold = DQN_MAX(waypoint_entity->local_hit_box_size.w, waypoint_entity->local_hit_box_size.h) * waypoint_link->data.value; } break; } // NOTE: We haven't arrived yet, calculate an acceleration vector to the waypoint if (dist_to_waypoint_sq > DQN_SQUARED(arrival_threshold)) { Dqn_V2 entity_to_waypoint_norm = Dqn_V2_Normalise(entity_to_waypoint); acceleration_meters_per_s = entity_to_waypoint_norm * (entity->base_acceleration_per_s.meters * 0.25f); if (entity->type == FP_EntityType_Smoochie) { if (waypoint_entity->type == FP_EntityType_Terry || waypoint_entity->type == FP_EntityType_Perry) { if (dist_to_waypoint_sq < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 2.f)) && !entity->smoochie_has_teleported) { if (entity->smoochie_teleport_timestamp == 0) { entity->smoochie_teleport_timestamp = game->play.clock_ms + DQN_CAST(Dqn_usize)(Dqn_PCG32_NextF32(&game->play.rng) * 5000); } if (game->play.clock_ms < entity->smoochie_teleport_timestamp) { Dqn_usize time_to_teleport_ms = entity->smoochie_teleport_timestamp - game->play.clock_ms; if (time_to_teleport_ms < 2000) entity->action.sprite_alpha = Dqn_PCG32_NextF32(&game->play.rng); } else if (game->play.clock_ms >= entity->smoochie_teleport_timestamp) { Dqn_Rect waypoint_rect = FP_Game_CalcEntityWorldHitBox(game, waypoint_entity->handle); entity->smoochie_has_teleported = true; entity->action.sprite_alpha = 1.f; Dqn_FArray teleport_pos = {}; if (waypoint_entity->direction != FP_GameDirection_Up) { Dqn_FArray_Add(&teleport_pos, Dqn_Rect_InterpolatedPoint(waypoint_rect, Dqn_V2_InitNx2(0.5f, -1.2f))); } if (waypoint_entity->direction != FP_GameDirection_Down) { Dqn_FArray_Add(&teleport_pos, Dqn_Rect_InterpolatedPoint(waypoint_rect, Dqn_V2_InitNx2(0.5f, +1.2f))); } if (waypoint_entity->direction != FP_GameDirection_Left) { Dqn_FArray_Add(&teleport_pos, Dqn_Rect_InterpolatedPoint(waypoint_rect, Dqn_V2_InitNx2(-1.2f, +0.5f))); } if (waypoint_entity->direction != FP_GameDirection_Right) { Dqn_FArray_Add(&teleport_pos, Dqn_Rect_InterpolatedPoint(waypoint_rect, Dqn_V2_InitNx2(+1.2f, +0.5f))); } uint32_t teleport_index = Dqn_PCG32_Range(&game->play.rng, 0, DQN_CAST(uint32_t)teleport_pos.size); entity->local_pos = teleport_pos.data[teleport_index]; } } } } else if (entity->type == FP_EntityType_Clinger) { if (game->play.clock_ms >= entity->clinger_next_dash_timestamp && dist_to_waypoint_sq > (DQN_SQUARED(arrival_threshold * 4.f))) { entity->clinger_next_dash_timestamp = game->play.clock_ms + 2000; acceleration_meters_per_s = entity_to_waypoint_norm * entity->base_acceleration_per_s.meters * 45.f; } } break; } // NOTE: We have arrived at the waypoint bool aggro = false; if (entity->flags & FP_GameEntityFlag_Aggros) { aggro |= entity->faction == FP_GameEntityFaction_Friendly && waypoint_entity->faction & FP_GameEntityFaction_Foe; aggro |= entity->faction == FP_GameEntityFaction_Foe && waypoint_entity->faction & FP_GameEntityFaction_Friendly; } bool building_response = ((entity->flags & FP_GameEntityFlag_RespondsToBuildings) && (waypoint_entity->type == FP_EntityType_ClubTerry)) || (waypoint_entity->type == FP_EntityType_AirportTerry) || (waypoint_entity->type == FP_EntityType_ChurchTerry); if (aggro || building_response) { bool can_attack = !entity->is_dying; // TODO(doyle): State transition needs to check if it's valid to move to making this not necessary if (building_response) { FP_GameEntity *building = waypoint_entity; can_attack = false; DQN_ASSERTF( waypoint->type == FP_GameWaypointType_Queue, "There's nothing stopping us from supporting other " "waypoint types to buildings, but, for this game " "we only ever make mobs queue at the building"); DQN_ASSERTF( building->building_queue.size, "An entity should only be forming a waypoint to " "the building if there was space in the queue and " "they were added the the queue"); if (FP_Game_IsNilEntityHandle(game, building->building_patron)) { if (waypoint_entity->building_queue.data[0] == entity->handle) { // NOTE: This entity is front-in-line in the queue to enter the building, we can enter! building->building_patron = entity->handle; entity->queued_at_building = {}; // NOTE: Remove them from the queue Dqn_FArray_EraseRange(&waypoint_entity->building_queue, 0 /*index*/, 1 /*count*/, Dqn_ArrayErase_Stable); Dqn_Rect building_hit_box = FP_Game_CalcEntityWorldHitBox(game, building->handle); Dqn_V2 exit_pos = Dqn_Rect_InterpolatedPoint(building_hit_box, Dqn_V2_InitNx2(0.5f, 1.1f)); if (building->type == FP_EntityType_ClubTerry) { FP_Game_EntityTransitionState(game, building, FP_EntityClubTerryState_PartyTime); entity->local_pos = exit_pos; // TODO(doyle): Only works when parent world pos is 0,0 } else if (building->type == FP_EntityType_AirportTerry) { FP_Game_EntityTransitionState(game, building, FP_EntityAirportTerryState_FlyPassenger); } else { DQN_ASSERT(building->type == FP_EntityType_ChurchTerry); FP_Game_EntityTransitionState(game, building, FP_EntityChurchTerryState_ConvertPatron); } entity->flags |= FP_GameEntityFlag_OccupiedInBuilding; FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->play.chunk_pool); // NOTE: Add the building to the entity's visit list to prevent them from re-entering FP_SentinelList_Add(&entity->buildings_visited, game->play.chunk_pool, building->handle); } } } if (can_attack) { switch (entity->type) { case FP_EntityType_Perry: /*FALLTHRU*/ case FP_EntityType_Terry: /*FALLTHRU*/ case FP_EntityType_Smoochie: /*FALLTHRU*/ case FP_EntityType_Catfish: /*FALLTHRU*/ case FP_EntityType_Clinger: { // TODO(doyle): We should check if it's valid to enter this new state // from the entity's current state if (entity->type == FP_EntityType_Terry || entity->type == FP_EntityType_Perry) { FP_Game_EntityTransitionState(game, entity, FP_EntityTerryState_Attack); } else if (entity->type == FP_EntityType_Smoochie) { FP_Game_EntityTransitionState(game, entity, FP_EntitySmoochieState_Attack); } else if (entity->type == FP_EntityType_Catfish) { FP_Game_EntityTransitionState(game, entity, FP_EntityCatfishState_Attack); } else { DQN_ASSERT(entity->type == FP_EntityType_Clinger); FP_Game_EntityTransitionState(game, entity, FP_EntityClingerState_Attack); } entity->direction = approach_dir; } break; case FP_EntityType_Nil: break; case FP_EntityType_ClubTerry: break; case FP_EntityType_Map: break; case FP_EntityType_Heart: break; case FP_EntityType_MerchantTerry: break; case FP_EntityType_MerchantGraveyard: break; case FP_EntityType_MerchantGym: break; case FP_EntityType_MerchantPhoneCompany: break; case FP_EntityType_AirportTerry: break; case FP_EntityType_ChurchTerry: break; case FP_EntityType_KennelTerry: break; case FP_EntityType_PhoneMessageProjectile: break; case FP_EntityType_Count: DQN_INVALID_CODE_PATH; break; case FP_EntityType_AirportTerryPlane: case FP_EntityType_MobSpawner: case FP_EntityType_PortalMonkey: break; case FP_EntityType_Billboard: break; } } // NOTE: Aggro makes the entity attack, we will exit here preserving the waypoint in the entity. break; } else { FP_SentinelList_Erase(&entity->waypoints, waypoint_link, game->play.chunk_pool); } } // NOTE: Move entity by mouse ============================================================== if (game->play.active_entity == entity->handle && entity->flags & FP_GameEntityFlag_MoveByMouse) { Dqn_V2 mouse_p_delta = input->mouse_p_delta * (Dqn_V2_One / game->play.camera.scale); entity->velocity = {}; acceleration_meters_per_s = {}; entity->local_pos += mouse_p_delta; } if (entity->flags & FP_GameEntityFlag_CameraTracking) { if (!Dqn_FArray_Find(&game->play.camera_tracking_entity, entity->handle).data) Dqn_FArray_Add(&game->play.camera_tracking_entity, entity->handle); } // NOTE: Building logic ================================================================ for (FP_GameEntityHandle player : game->play.players) { if (player != entity->handle) continue; // NOTE: Toggle menu =============================================================== if (entity->in_game_menu == FP_GameInGameMenu_Nil || entity->in_game_menu == FP_GameInGameMenu_Build) { if (FP_Game_KeyBindIsPressed(input, controls, controls->build_mode)) entity->in_game_menu = DQN_CAST(FP_GameInGameMenu)(DQN_CAST(uint32_t)entity->in_game_menu ^ FP_GameInGameMenu_Build); } // NOTE: Building selector ========================================================= Dqn_usize const last_building_index = DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) - 1; if (FP_Game_KeyBindIsPressed(input, controls, controls->move_building_ui_cursor_left)) { if (entity->build_mode_building_index <= 0) { entity->build_mode_building_index = last_building_index; } else { entity->build_mode_building_index -= 1; } } if (FP_Game_KeyBindIsPressed(input, controls, controls->move_building_ui_cursor_right)) { if (entity->build_mode_building_index >= last_building_index) { entity->build_mode_building_index = 0; } else { entity->build_mode_building_index += 1; } } // NOTE: Builder blueprint ========================================================= FP_GamePlaceableBuilding placeable_building = PLACEABLE_BUILDINGS[entity->build_mode_building_index]; entity->build_mode_can_place_building = false; uint8_t *inventory_count = nullptr; if (placeable_building.type == FP_EntityType_ChurchTerry) inventory_count = &entity->inventory.churchs; else if (placeable_building.type == FP_EntityType_KennelTerry) inventory_count = &entity->inventory.kennels; else if (placeable_building.type == FP_EntityType_ClubTerry) inventory_count = &entity->inventory.clubs; else if (placeable_building.type == FP_EntityType_AirportTerry) inventory_count = &entity->inventory.airports; bool have_building_inventory = inventory_count && (*inventory_count) > 0; if (have_building_inventory && entity->in_game_menu == FP_GameInGameMenu_Build) { Dqn_Rect dest_rect = FP_Game_GetBuildingPlacementRectForEntity(game, placeable_building, entity->handle); Dqn_V2 placement_pos = Dqn_Rect_Center(dest_rect); for (FP_GameEntityIterator zone_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &zone_it, game->play.root_entity); ) { FP_GameEntity *zone = zone_it.entity; bool cant_overlap_build = zone->type == FP_EntityType_KennelTerry || zone->type == FP_EntityType_AirportTerry || zone->type == FP_EntityType_ChurchTerry || zone->type == FP_EntityType_ClubTerry; // NOTE: We also can't overlap with the player incase of 2 players if (game->play.players.size > 1) { if (!cant_overlap_build) { cant_overlap_build = Dqn_FArray_Find(&game->play.players, zone_it.entity->handle).data; } } Dqn_Rect zone_hit_box = FP_Game_CalcEntityWorldHitBox(game, zone->handle); if (cant_overlap_build) { if (Dqn_Rect_Intersects(zone_hit_box, dest_rect)) { entity->build_mode_can_place_building = false; break; } } if ((zone->flags & FP_GameEntityFlag_BuildZone) == 0) continue; zone_hit_box.pos += dest_rect.size * .5f; zone_hit_box.size -= dest_rect.size; zone_hit_box.size = Dqn_V2_Max(zone_hit_box.size, Dqn_V2_Zero); entity->build_mode_can_place_building |= Dqn_Rect_ContainsPoint(zone_hit_box, placement_pos); } if (entity->build_mode_can_place_building && FP_Game_KeyBindIsPressed(input, controls, controls->attack)) { if (placeable_building.type == FP_EntityType_ClubTerry) { FP_Entity_CreateClubTerry(game, placement_pos, "Club Terry"); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Club], 1.f); } else if (placeable_building.type == FP_EntityType_ChurchTerry) { FP_Entity_CreateChurchTerry(game, placement_pos, "Church Terry"); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Church], 1.f); } else if (placeable_building.type == FP_EntityType_AirportTerry) { FP_Entity_CreateAirportTerry(game, placement_pos, "Airport Terry"); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Plane], 1.f); } else { DQN_ASSERT(placeable_building.type == FP_EntityType_KennelTerry); FP_Entity_CreateKennelTerry(game, placement_pos, "Kennel Terry"); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Dog], 1.f); } (*inventory_count)--; } } } if (!FP_Game_IsNilEntityHandle(game, entity->carried_monkey) && entity->type != FP_EntityType_MobSpawner) { FP_GameFindClosestEntityResult closest_portal = FP_Game_FindClosestEntityWithType(game, entity->carried_monkey, game->play.mob_spawners.data, game->play.mob_spawners.size); if (closest_portal.dist_squared < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 1.f))) { FP_GameEntity *portal = FP_Game_GetEntity(game, closest_portal.entity); portal->carried_monkey = entity->carried_monkey; entity->carried_monkey = {}; } acceleration_meters_per_s *= 0.5f; // TODO(doyle): Penalise the player } else { acceleration_meters_per_s *= 1.f; // TODO(doyle): Penalise the player } if (!FP_Game_KeyBindIsDown(input, controls, controls->strafe)) { if (acceleration_meters_per_s.x) entity->direction = acceleration_meters_per_s.x > 0.f ? FP_GameDirection_Right : FP_GameDirection_Left; else if (acceleration_meters_per_s.y) entity->direction = acceleration_meters_per_s.y > 0.f ? FP_GameDirection_Down : FP_GameDirection_Up; } // NOTE: Tick the state machine // NOTE: This can delete the entity! Take caution FP_GameEntityHandle entity_handle = entity->handle; FP_EntityActionStateMachine(game, &os->audio, input, entity, &acceleration_meters_per_s); // NOTE: Limit the entity to within bounds of the camera only in multiplayer =========== bool entity_is_oob_with_camera = false; Dqn_V2 entity_oob_move = {}; if (game->play.players.size > 1) { FP_GameCamera *camera = &game->play.camera; for (FP_GameEntityHandle player : game->play.players) { if (entity->handle != player) continue; Dqn_Rect const camera_view_rect = Dqn_Rect_InitV2x2((camera->size * -.5f) + (camera->world_pos / camera->scale), camera->size); Dqn_Rect const entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity_handle); Dqn_Rect const playable_bounds = Dqn_Rect_ExpandV2(camera_view_rect, -entity_hit_box.size); Dqn_V2 new_entity_pos = entity_pos; new_entity_pos.x = DQN_MAX(new_entity_pos.x, playable_bounds.pos.x); new_entity_pos.y = DQN_MAX(new_entity_pos.y, playable_bounds.pos.y); new_entity_pos.x = DQN_MIN(new_entity_pos.x, playable_bounds.pos.x + playable_bounds.size.x); new_entity_pos.y = DQN_MIN(new_entity_pos.y, playable_bounds.pos.y + playable_bounds.size.y); if (new_entity_pos != entity_pos) { entity_is_oob_with_camera = true; Dqn_V2 delta_pos = new_entity_pos - entity_pos; entity_oob_move = delta_pos; } } } if (entity_is_oob_with_camera) { if (FP_Game_CanEntityMoveToPosition(game, entity_handle, entity_oob_move).yes) entity->local_pos += entity_oob_move; } else { // NOTE: Core equations of motion ================================================== FP_Game_MoveEntity(game, entity_handle, acceleration_meters_per_s); } } // NOTE: Typically, the game starts the cooldown for the next wave after // all enemies are spawned which makes the game fast paced. However, to // give the player a break to reorganise, every 3 waves- we don't start // the wave countdown until all enemies are killed. bool all_enemies_spawned = game->play.enemies_spawned_this_wave >= game->play.enemies_per_wave; bool advance_to_next_wave = false; bool current_wave_is_break_for_player = game->play.current_wave % 1 == 0; bool monkey_spawn = game->play.current_wave % 3 == 0; if (current_wave_is_break_for_player) { Dqn_usize enemy_count = 0; for (FP_GameEntityHandle spawner_handle : game->play.mob_spawners) { FP_GameEntity *spawner = FP_Game_GetEntity(game, spawner_handle); // NOTE: Count all the mobs spawned that are a foe // (e.g. churches that convert mobs don't count). for (FP_SentinelListLink *link = nullptr; FP_SentinelList_Iterate(&spawner->spawn_list, &link); ) { FP_GameEntity *mob = FP_Game_GetEntity(game, link->data); if (mob->faction == FP_GameEntityFaction_Foe) enemy_count++; } } bool all_enemies_killed = enemy_count == 0; advance_to_next_wave = all_enemies_spawned && all_enemies_killed; } else { advance_to_next_wave = all_enemies_spawned; } if (advance_to_next_wave) { // NOTE: If the cooldown timestamp is 0, the wave is complete and we // haven't given the player cooldown yet, so we assign a timestamp for that if (game->play.wave_cooldown_timestamp_ms == 0) { game->play.wave_cooldown_timestamp_ms = game->play.clock_ms + FP_COOLDOWN_WAVE_TIME_MS; TELY_Audio_Fade(audio, game->bg_music1, TELY_AudioEffectFade_Out, 2000 /*fade_duration_ms*/); game->bg_music2 = TELY_Audio_Play(audio, game->audio[FP_GameAudio_Relax02Music], 1.f, false); TELY_Audio_Fade(audio, game->bg_music2, TELY_AudioEffectFade_In, 2000 /*fade_duration_ms*/); } else { // NOTE: Check if cooldown has elapsed, the next wave can start if so if (game->play.clock_ms > game->play.wave_cooldown_timestamp_ms) { game->play.enemies_per_wave = DQN_MAX(5 * DQN_CAST(uint32_t)game->play.mob_spawners.size, DQN_CAST(uint32_t)(game->play.enemies_per_wave * 1.5)); game->play.enemies_spawned_this_wave = 0; // Important! Reset the spawn count game->play.wave_cooldown_timestamp_ms = 0; // Important! We reset the timestamp for the next wave TELY_Audio_Fade(audio, game->bg_music2, TELY_AudioEffectFade_Out, 2000 /*fade_duration_ms*/); game->bg_music1 = TELY_Audio_Play(audio, game->audio[FP_GameAudio_Stress03Music], 1.f, true); TELY_Audio_Fade(audio, game->bg_music1, TELY_AudioEffectFade_In, 2000 /*fade_duration_ms*/); if (monkey_spawn && game->play.current_wave != 0) { // NOTE: We spawn a monkey at these wave intervals; if (game->play.monkey_spawn_count < 3) { DQN_ASSERT(game->play.monkey_spawn_count < DQN_ARRAY_UCOUNT(game->play.monkey_spawn_shuffled_list)); uint8_t spawn_pos_index = game->play.monkey_spawn_shuffled_list[game->play.monkey_spawn_count++]; DQN_ASSERT(spawn_pos_index < DQN_ARRAY_UCOUNT(FP_MONKEY_SPAWN_LOCATIONS)); Dqn_V2 spawn_pos = FP_MONKEY_SPAWN_LOCATIONS[spawn_pos_index]; FP_GameEntityHandle portal_monkey = FP_Entity_CreatePortalMonkey(game, spawn_pos, "Portal Monkey"); Dqn_FArray_Add(&game->play.portal_monkeys, portal_monkey); TELY_Audio_Play(audio, game->audio[FP_GameAudio_Monkey], 1.f); } } game->play.current_wave++; //TELY_Audio_Stop(audio, game->audio[FP_GameAudio_Music2]); } } } } // NOTE: Update entity ========================================================================= for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->play.root_entity); ) { FP_GameEntity *entity = it.entity; if (entity->flags & FP_GameEntityFlag_TTL) { if (game->play.clock_ms >= entity->ttl_end_timestamp) { FP_Game_DeleteEntity(game, entity->handle); continue; } } // NOTE: Recover mobile data entity->terry_mobile_data_plan = DQN_MIN(entity->terry_mobile_data_plan + DQN_CAST(Dqn_usize)(FP_TERRY_MOBILE_DATA_PER_RANGE_ATTACK * .25f * FP_GAME_PHYSICS_STEP), entity->terry_mobile_data_plan_cap); // NOTE: Recover hp & stamina if (game->play.update_counter % 4 == 0) { entity->stamina = DQN_MIN(entity->stamina + 1, entity->stamina_cap); } if (entity->flags & FP_GameEntityFlag_RecoversHP) { if (game->play.update_counter % entity->hp_recover_every_n_ticks == 0) { entity->hp = DQN_MIN(entity->hp + 1, entity->hp_cap); } } entity->trauma01 = DQN_MAX(0.f, entity->trauma01 - 0.05f); Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); if (entity->is_drunk) { if (game->play.clock_ms > entity->drunk_particles_end_ms) { Dqn_usize duration_ms = 2000; FP_ParticleDescriptor particle_desc = FP_DefaultFloatUpParticleDescriptor(g_anim_names.particle_drunk, duration_ms); particle_desc.velocity_variance.x *= 2.f; particle_desc.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.5f, -0.1f)); particle_desc.colour_begin.a = .8f; entity->drunk_particles_end_ms = game->play.clock_ms + duration_ms; FP_EmitParticle(game, particle_desc, 3); } } // NOTE: Derive dynmamic bounding boxes ==================================================== if (entity->flags & FP_GameEntityFlag_DeriveHitBoxFromChildrenBoundingBox) { Dqn_Rect children_bbox = {}; // TODO(doyle): Is the hit box supposed to include the containing // entity itself? Not sure children_bbox.pos = FP_Game_CalcEntityWorldPos(game, entity->handle); for (FP_GameEntityIterator child_it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &child_it, entity);) { FP_GameEntity *child = child_it.entity; DQN_ASSERT(child != entity); Dqn_Rect bbox = FP_Game_CalcEntityWorldBoundingBox(game, child->handle); 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; } // NOTE: Mob spawner ======================================================================= if (entity->type == FP_EntityType_MobSpawner && !game->play.debug_disable_mobs && game->play.state != FP_GameState_Tutorial) { // NOTE: Flush any spawn entities that are dead for (FP_SentinelListLink *link = nullptr; FP_SentinelList_Iterate(&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->play.chunk_pool); } if (entity->action.state != FP_EntityMobSpawnerState_Shutdown && game->play.enemies_spawned_this_wave < game->play.enemies_per_wave && entity->spawn_list.size < entity->spawn_cap) { // NOTE: Spawn new entities if (game->play.clock_ms >= entity->next_spawn_timestamp_s) { Dqn_usize spawn_count = DQN_MIN(game->play.current_wave + 1, 8); for (Dqn_usize spawn_index = 0; spawn_index < spawn_count; spawn_index++) { uint16_t hp_adjustment = DQN_CAST(uint16_t)game->play.current_wave; entity->next_spawn_timestamp_s = DQN_CAST(uint64_t)(game->play.clock_ms + 2.5f); FP_SentinelListLink *link = FP_SentinelList_Make(&entity->spawn_list, game->play.chunk_pool); Dqn_V2 entity_world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); Dqn_Rect entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); Dqn_f32 step_y = (entity_hit_box.size.h / spawn_count) * 1.5f; Dqn_f32 mob_y_offset = (Dqn_PCG32_NextF32(&game->play.rng) * step_y) + (step_y * spawn_index); if (Dqn_PCG32_NextF32(&game->play.rng) >= .5f) mob_y_offset *= -1; Dqn_V2 mob_world_pos = Dqn_V2_InitNx2(entity_world_pos.x, entity_world_pos.y + mob_y_offset); Dqn_f32 mob_choice = Dqn_PCG32_NextF32(&game->play.rng); if (mob_choice <= 0.33f) link->data = FP_Entity_CreateClinger(game, mob_world_pos, "Clinger"); else if (mob_choice <= 0.66f) link->data = FP_Entity_CreateSmoochie(game, mob_world_pos, "Smoochie"); else link->data = FP_Entity_CreateCatfish(game, mob_world_pos, "Catfish"); // NOTE: Setup the mob with waypoints FP_GameEntity *mob = FP_Game_GetEntity(game, link->data); mob->waypoints = FP_SentinelList_Init(game->play.chunk_pool); mob->flags |= FP_GameEntityFlag_Aggros; mob->flags |= FP_GameEntityFlag_RespondsToBuildings; mob->hp_cap *= hp_adjustment*2; mob->hp = mob->hp_cap; FP_AppendMobSpawnerWaypoints(game, entity->handle, mob->handle); game->play.enemies_spawned_this_wave++; } } } } // NOTE: Do attacks ============================================================================ { Dqn_Profiler_ZoneScopeWithIndex("FP_Update: Attacks", FP_ProfileZone_FPUpdate_Attacks); FP_GameEntity *attacker = entity; // NOTE: Resolve attack boxes if (!Dqn_V2_Area(attacker->attack_box_size)) continue; Dqn_Rect attacker_box = FP_Game_CalcEntityAttackWorldHitBox(game, attacker->handle); FP_GameEntityFaction enemy_faction = entity->faction == FP_GameEntityFaction_Friendly ? FP_GameEntityFaction_Foe : FP_GameEntityFaction_Friendly; for (FP_GameEntityIterator defender_it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &defender_it, game->play.root_entity); ) { FP_GameEntity *defender = defender_it.entity; if (defender->handle == attacker->handle) continue; if ((defender->flags & FP_GameEntityFlag_Attackable) == 0) continue; // NOTE: Projectiles can't hurt the owner that spawned it if (attacker->projectile_owner == defender->handle) continue; if (defender->faction != enemy_faction) continue; Dqn_Rect defender_box = FP_Game_CalcEntityWorldHitBox(game, defender->handle); if (!Dqn_Rect_Intersects(attacker_box, defender_box)) continue; // NOTE: Emit hit particles ======================================================== FP_ParticleDescriptor particle_desc = {}; Dqn_usize particle_selector = Dqn_PCG32_Range(&game->play.rng, 0, 3); if (particle_selector == 0) { particle_desc.anim_name = g_anim_names.particle_hit_1; } else if (particle_selector == 1) { particle_desc.anim_name = g_anim_names.particle_hit_2; } else { particle_desc.anim_name = g_anim_names.particle_hit_3; DQN_ASSERT(particle_selector == 2); } particle_desc.pos = Dqn_Rect_InterpolatedPoint(defender_box, Dqn_V2_InitNx2(0.5f, 0.0f)); particle_desc.velocity.y = -16.f; particle_desc.velocity_variance.y = (particle_desc.velocity.y * .5f); particle_desc.velocity_variance.x = DQN_ABS(particle_desc.velocity.y); particle_desc.colour_begin = TELY_COLOUR_WHITE_V4; particle_desc.colour_end = TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.f); particle_desc.duration_ms = 1000; FP_EmitParticle(game, particle_desc, Dqn_PCG32_Range(&game->play.rng, 1, 3)); // NOTE: God mode override ========================================================= if (game->play.heart == defender->handle) { if (game->play.god_mode) continue; } bool god_mode_override = false; bool attacker_is_player = false; if (game->play.god_mode) { for (FP_GameEntityHandle player : game->play.players) { god_mode_override |= player == defender->handle; attacker_is_player |= player == attacker->handle; } } if (god_mode_override) continue; // NOTE: Do hit logic ============================================================== defender->hp = defender->hp >= attacker->base_attack ? defender->hp - attacker->base_attack : 0; defender->hit_on_clock_ms = game->play.clock_ms; defender->trauma01 = 1.f - (defender->hp / DQN_CAST(Dqn_f32)defender->hp_cap); if (defender->hp <= 0) { if (!defender->is_dying) { FP_GameEntity *coin_receiver = FP_Game_GetEntity(game, attacker->projectile_owner); if (FP_Game_IsNilEntity(coin_receiver)) coin_receiver = attacker; coin_receiver->coins += 2; } defender->is_dying = true; } // NOTE: Interrupt hit entity ===================================================== if (attacker_is_player) { switch (defender->type) { case FP_EntityType_Catfish: { FP_Game_EntityTransitionState(game, defender, FP_EntityCatfishState_Idle); defender->last_attack_timestamp = game->play.clock_ms; } break; case FP_EntityType_Terry: { FP_Game_EntityTransitionState(game, defender, FP_EntityTerryState_Idle); defender->last_attack_timestamp = game->play.clock_ms; } break; case FP_EntityType_Perry: { FP_Game_EntityTransitionState(game, defender, FP_EntityTerryState_Idle); defender->last_attack_timestamp = game->play.clock_ms; } break; case FP_EntityType_Clinger: { FP_Game_EntityTransitionState(game, defender, FP_EntityClingerState_Idle); defender->last_attack_timestamp = game->play.clock_ms; } break; case FP_EntityType_Smoochie: { FP_Game_EntityTransitionState(game, defender, FP_EntitySmoochieState_Idle); defender->last_attack_timestamp = game->play.clock_ms; } break; case FP_EntityType_Nil: break; case FP_EntityType_AirportTerry: break; case FP_EntityType_AirportTerryPlane: break; case FP_EntityType_ChurchTerry: break; case FP_EntityType_ClubTerry: break; case FP_EntityType_Heart: break; case FP_EntityType_KennelTerry: break; case FP_EntityType_Map: break; case FP_EntityType_MerchantGraveyard: break; case FP_EntityType_MerchantGym: break; case FP_EntityType_MerchantPhoneCompany: break; case FP_EntityType_MerchantTerry: break; case FP_EntityType_MobSpawner: break; case FP_EntityType_PortalMonkey: break; case FP_EntityType_PhoneMessageProjectile: break; case FP_EntityType_Billboard: break; case FP_EntityType_Count: break; } } // NOTE: Kickback ====================================================================== #if 0 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; Dqn_V2 attack_acceleration_meters_per_s = attack_dir_vector * 60.f; FP_Game_MoveEntity(game, defender->handle, attack_acceleration_meters_per_s); #endif } } } bool all_portals_shutdown = true; for (FP_GameEntityHandle portal_handle : game->play.mob_spawners) { FP_GameEntity *portal = FP_Game_GetEntity(game, portal_handle); all_portals_shutdown &= portal->action.state == FP_EntityMobSpawnerState_Shutdown; } if (all_portals_shutdown) { game->play.state = FP_GameState_WinGame; } // NOTE: Game over check ======================================================================= { FP_GameEntity *heart = FP_Game_GetEntity(game, game->play.heart); if (heart->hp <= 0) game->play.state = FP_GameState_LoseGame; } game->play.global_camera_trauma01 = DQN_MAX(0.f, game->play.global_camera_trauma01 - 0.05f); // NOTE: Update all particles ================================================================== for (FP_Particle &particle_ : game->play.particles) { FP_Particle *particle = &particle_; if (game->play.clock_ms >= particle->end_ms) particle->alive = false; if (!particle->alive) continue; particle->pos += particle->velocity * DQN_CAST(Dqn_f32)input->delta_s; } // NOTE: Camera ================================================================================ FP_GamePlay *play = &game->play; FP_GameCamera *camera = &play->camera; if (game->play.state == FP_GameState_Tutorial) { Dqn_f32 arrival_dist = Dqn_V2_LengthSq(camera->world_pos_target - camera->world_pos); bool camera_arrived = arrival_dist < DQN_SQUARED(5.f); switch (game->play.tutorial_state) { case FP_GameStateTutorial_ShowPlayer: { camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, game->play.players.data[0]) * game->play.camera.scale; if (camera_arrived) { game->play.tutorial_state = DQN_CAST(FP_GameStateTutorial)(DQN_CAST(uint32_t)game->play.tutorial_state + 1); game->play.tutorial_wait_end_time_ms = game->play.clock_ms + 3000; } } break; case FP_GameStateTutorial_ShowPortalOneWait: /*FALLTHRU*/ case FP_GameStateTutorial_ShowPortalTwoWait: /*FALLTHRU*/ case FP_GameStateTutorial_ShowPortalThreeWait: /*FALLTHRU*/ case FP_GameStateTutorial_ShowBillboardBuildWait: /*FALLTHRU*/ case FP_GameStateTutorial_ShowPlayerWait: { if (game->play.clock_ms > game->play.tutorial_wait_end_time_ms) { game->play.tutorial_state = DQN_CAST(FP_GameStateTutorial)(DQN_CAST(uint32_t)game->play.tutorial_state + 1); } } break; case FP_GameStateTutorial_ShowPortalOne: /*FALLTHRU*/ case FP_GameStateTutorial_ShowPortalTwo: /*FALLTHRU*/ case FP_GameStateTutorial_ShowPortalThree: { if (game->play.tutorial_state == FP_GameStateTutorial_ShowPortalOne) { camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, game->play.mob_spawners.data[0]) * game->play.camera.scale; } else if (game->play.tutorial_state == FP_GameStateTutorial_ShowPortalTwo) { camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, game->play.mob_spawners.data[1]) * game->play.camera.scale; } else { DQN_ASSERT(game->play.tutorial_state == FP_GameStateTutorial_ShowPortalThree); camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, game->play.mob_spawners.data[2]) * game->play.camera.scale; } if (camera_arrived) { game->play.tutorial_state = DQN_CAST(FP_GameStateTutorial)(DQN_CAST(uint32_t)game->play.tutorial_state + 1); game->play.tutorial_wait_end_time_ms = game->play.clock_ms + 2000; } } break; case FP_GameStateTutorial_ShowBillboardBuild: { camera->world_pos_target = FP_Game_CalcEntityWorldPos(game, game->play.billboard_build) * game->play.camera.scale; if (camera_arrived) { game->play.tutorial_state = DQN_CAST(FP_GameStateTutorial)(DQN_CAST(uint32_t)game->play.tutorial_state + 1); game->play.tutorial_wait_end_time_ms = game->play.clock_ms + 4000; } } break; case FP_GameStateTutorial_Count: { game->play.state = FP_GameState_Play; } break; } } else { camera->world_pos_target = {}; for (FP_GameEntityHandle camera_entity : game->play.camera_tracking_entity) { Dqn_V2 entity_pos = FP_Game_CalcEntityWorldPos(game, camera_entity) * game->play.camera.scale; camera->world_pos_target += entity_pos; } if (game->play.camera_tracking_entity.size) camera->world_pos_target /= DQN_CAST(Dqn_f32)game->play.camera_tracking_entity.size; } // NOTE: Clamp camera to map bounds ============================================================ { Dqn_V2 window_size = Dqn_V2_InitV2I(os->core.window_size); camera->scale = window_size / camera->size; Dqn_V2 camera_size_screen = camera->size * camera->scale; Dqn_V2 map_world_size = play->map->local_hit_box_size; Dqn_V2 map_screen_size = map_world_size * camera->scale; Dqn_V2 half_map_screen_size = map_screen_size * .5f; camera->world_pos_target.x = DQN_MIN(camera->world_pos_target.x, half_map_screen_size.w - (camera_size_screen.w * .5f)); camera->world_pos_target.x = DQN_MAX(camera->world_pos_target.x, -half_map_screen_size.w + (camera_size_screen.w * .5f)); camera->world_pos_target.y = DQN_MAX(camera->world_pos_target.y, -half_map_screen_size.h + (camera_size_screen.h * .5f)); camera->world_pos_target.y = DQN_MIN(camera->world_pos_target.y, half_map_screen_size.h - (camera_size_screen.h * .5f)); } camera->world_pos += (camera->world_pos_target - camera->world_pos) * DQN_MIN(1.f, (5.f * DQN_CAST(Dqn_f32)input->delta_s)); Dqn_Profiler_EndZone(update_zone); } static Dqn_Str8 FP_ScanKeyToLabel(Dqn_Arena *arena, TELY_InputScanKey scan_key) { Dqn_Str8 result = {}; Dqn_Allocator allocator = Dqn_Arena_Allocator(arena); if (scan_key >= TELY_InputScanKey_A && scan_key <= TELY_InputScanKey_Z) { char scan_key_ch = DQN_CAST(char)('A' + (scan_key - TELY_InputScanKey_A)); result = Dqn_Str8_InitF(allocator, "[%c]", scan_key_ch); } else { if (scan_key == TELY_InputScanKey_Up) { result = Dqn_Str8_InitF(allocator, "[Up]"); } else if (scan_key == TELY_InputScanKey_Down) { result = Dqn_Str8_InitF(allocator, "[Down]"); } else if (scan_key == TELY_InputScanKey_Left) { result = Dqn_Str8_InitF(allocator, "[Left]"); } else if (scan_key == TELY_InputScanKey_Right) { result = Dqn_Str8_InitF(allocator, "[Right]"); } else if (scan_key == TELY_InputScanKey_Semicolon) { result = Dqn_Str8_InitF(allocator, "[;]"); } else if (scan_key == TELY_InputScanKey_Apostrophe) { result = Dqn_Str8_InitF(allocator, "[']"); } else if (scan_key == TELY_InputScanKey_Backslash) { result = Dqn_Str8_InitF(allocator, "[/]"); } } return result; } static void FP_DrawBillboardKeyBindHint(TELY_Renderer *renderer, TELY_Assets *assets, FP_Game *game, Dqn_usize player_index, FP_GameControlMode mode, FP_GameKeyBind key_bind, Dqn_V2 draw_p, bool draw_player_prefix) { Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_Str8 player_prefix = {}; if (draw_player_prefix) player_prefix = Dqn_Str8_InitF(scratch.allocator, "P%zu ", player_index); if (mode == FP_GameControlMode_Gamepad) { Dqn_Str8 tex_name = {}; if (key_bind.gamepad_key == TELY_InputGamepadKey_A) tex_name = g_anim_names.merchant_button_a; else if (key_bind.gamepad_key == TELY_InputGamepadKey_B) tex_name = g_anim_names.merchant_button_b; else if (key_bind.gamepad_key == TELY_InputGamepadKey_X) tex_name = g_anim_names.merchant_button_x; else if (key_bind.gamepad_key == TELY_InputGamepadKey_Y) tex_name = g_anim_names.merchant_button_y; if (tex_name.size) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, tex_name); Dqn_Rect button_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_V2 text_size = TELY_Asset_MeasureText(assets, TELY_Render_ActiveFont(renderer), player_prefix); TELY_Render_Text(renderer, draw_p, Dqn_V2_InitNx2(0, +1.f), player_prefix); Dqn_Rect gamepad_btn_rect = {}; gamepad_btn_rect.size = button_rect.size; gamepad_btn_rect.pos = Dqn_V2_InitNx2(draw_p.x + (text_size.x * 1.25f), draw_p.y - button_rect.size.y); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, button_rect, gamepad_btn_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } } else { Dqn_Str8 key_bind_label = FP_ScanKeyToLabel(scratch.arena, key_bind.scan_key); TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0, +1.f), "%.*s%.*s", DQN_STR_FMT(player_prefix), DQN_STR_FMT(key_bind_label)); } } static void FP_Render(FP_Game *game, TELY_OS *os, TELY_Renderer *renderer, TELY_Audio *audio) { Dqn_Profiler_ZoneScopeWithIndex("FP_Render", FP_ProfileZone_FPRender); TELY_Input *input = &os->input; TELY_RFui *rfui = &game->rfui; TELY_Assets *assets = &os->assets; TELY_Render_ClearColourV3(renderer, TELY_COLOUR_BLACK_MIDNIGHT_V4.rgb); TELY_Render_PushFontSize(renderer, game->jetbrains_mono_font, game->font_size); TELY_RFui_FrameSetup(rfui, &os->frame_arena); TELY_RFui_PushFontSize(rfui, game->jetbrains_mono_font, game->font_size); FP_GameCamera shake_camera = game->play.camera; { Dqn_f32 trauma01 = 0.f; for (FP_GameEntityHandle camera_entity_handle : game->play.camera_tracking_entity) { FP_GameEntity *camera_entity = FP_Game_GetEntity(game, camera_entity_handle); trauma01 = DQN_MAX(trauma01, DQN_SQUARED(camera_entity->trauma01)); } // NOTE: The heart shake is trauma^3 to emphasise the severity of losing heart health FP_GameEntity *heart = FP_Game_GetEntity(game, game->play.heart); trauma01 = DQN_MAX(trauma01, DQN_SQUARED(heart->trauma01) * heart->trauma01*5); trauma01 = DQN_MAX(trauma01, DQN_SQUARED(game->play.global_camera_trauma01)); // NOTE: Calculate camera position based on camera shake Dqn_f32 max_shake_dist = 400.f; Dqn_f32 half_shake_dist = max_shake_dist * .5f; Dqn_V2 shake_offset = {}; shake_offset.x = (Dqn_PCG32_NextF32(&game->play.rng) * max_shake_dist - half_shake_dist) * trauma01; shake_offset.y = (Dqn_PCG32_NextF32(&game->play.rng) * max_shake_dist - half_shake_dist) * trauma01; Dqn_f32 interp_rate = 5.0f * DQN_CAST(Dqn_f32)input->delta_s; shake_camera.world_pos += shake_offset * interp_rate; } FP_GameCameraM2x3 camera_xforms = FP_Game_CameraModelViewM2x3(shake_camera); TELY_Render_PushTransform(renderer, camera_xforms.model_view); Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(camera_xforms.view_model, input->mouse_p); // NOTE: Draw tiles ============================================================================ Dqn_usize tile_count_x = DQN_CAST(Dqn_usize)(os->core.window_size.w / game->play.tile_size); Dqn_usize tile_count_y = DQN_CAST(Dqn_usize)(os->core.window_size.h / game->play.tile_size); for (Dqn_usize x = 0; x < tile_count_x; x++) { Dqn_V2 start = Dqn_V2_InitNx2((x + 1) * game->play.tile_size, 0); Dqn_V2 end = Dqn_V2_InitNx2(start.x, os->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++) { Dqn_V2 start = Dqn_V2_InitNx2(0, (y + 1) * game->play.tile_size); Dqn_V2 end = Dqn_V2_InitNx2(os->core.window_size.w, start.y); TELY_Render_LineColourV4(renderer, start, end, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .25f), 1.f); } TELY_Render_PushColourV4(renderer, TELY_COLOUR_BLACK_V4); // NOTE: Draw entities ========================================================================= bool render_build_mode = false; for (FP_GameEntityHandle player : game->play.players) { FP_GameEntity *entity = FP_Game_GetEntity(game, player); render_build_mode = entity->in_game_menu == FP_GameInGameMenu_Build;; if (render_build_mode) break; } Dqn_V4 const colour_accent_yellow = TELY_Colour_V4InitRGBU32(0xFFE726); for (FP_GameEntityIterator it = {}; FP_Game_DFSPostOrderWalkEntityTree(game, &it, game->play.root_entity); ) { FP_GameEntity *entity = it.entity; if (entity->flags & FP_GameEntityFlag_OccupiedInBuilding) continue; // NOTE: Render shapes in entity =========================================================== Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, entity->handle); for (FP_GameShape const &shape_ : entity->shapes) { FP_GameShape const *shape = &shape_; Dqn_V2 local_to_world_p1 = world_pos + shape->p1; Dqn_V2 local_to_world_p2 = world_pos + shape->p2; switch (shape->type) { case FP_GameShapeType_None: { } break; case FP_GameShapeType_Circle: { TELY_Render_CircleColourV4(renderer, local_to_world_p1, shape->circle_radius, shape->render_mode, shape->colour); } break; case FP_GameShapeType_Rect: { 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; case FP_GameShapeType_Line: { TELY_Render_LineColourV4(renderer, local_to_world_p1, local_to_world_p2, shape->colour, shape->line_thickness); } break; } } if (entity->flags & FP_GameEntityFlag_HasShadow) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.shadow_tight_circle); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect shadow_rect = {}; shadow_rect.size = tex_rect.size * .5f; shadow_rect.pos = Dqn_V2_InitNx2(world_hit_box.pos.x - (shadow_rect.size.w * .5f) + (shadow_rect.size.w * .2f), world_hit_box.pos.y + world_hit_box.size.h); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, shadow_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .8f)); } // NOTE: Render entity sprites ============================================================= if (entity->action.sprite.anim) { FP_GameEntityAction const *action = &entity->action; TELY_AssetAnimatedSprite const sprite = action->sprite; uint64_t const elapsed_ms = game->play.clock_ms - action->started_at_clock_ms; uint16_t const raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite.anim->ms_per_frame); DQN_ASSERTF(sprite.anim->count, "We will modulo by 0 or overflow to UINT64_MAX"); // TODO(doyle): So many ways to create and get sprite data .. its a mess // I want to override per sprite anim height, we currently use the one // in the entity which is not correct. FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, entity->type, action->state, entity->direction); uint16_t anim_frame = 0; if (action->sprite_play_once) anim_frame = DQN_MIN(raw_anim_frame, (sprite.anim->count - 1)); else anim_frame = raw_anim_frame % sprite.anim->count; Dqn_usize sprite_index = sprite.anim->index + anim_frame; Dqn_Rect src_rect = {}; switch (sprite.sheet->type) { case TELY_AssetSpriteSheetType_Uniform: { 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; } break; case TELY_AssetSpriteSheetType_Rects: { DQN_ASSERT(sprite_index < sprite.sheet->rects.size); src_rect = sprite.sheet->rects.data[sprite_index]; } break; } Dqn_f32 sprite_in_meters = FP_Game_PixelsToMetersNx1(game->play, src_rect.size.y); Dqn_f32 size_scale = render_data.height.meters / sprite_in_meters; Dqn_Rect dest_rect = {}; dest_rect.size = src_rect.size * size_scale; dest_rect.pos = world_pos - (dest_rect.size * .5f) + render_data.offset; if (sprite.flip & TELY_AssetFlip_X) dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally if (sprite.flip & TELY_AssetFlip_Y) dest_rect.size.h *= -1.f; // NOTE: Flip the texture vertically Dqn_V4 sprite_colour = TELY_COLOUR_WHITE_V4; if (entity->hit_on_clock_ms) { DQN_ASSERT(game->play.clock_ms >= entity->hit_on_clock_ms); Dqn_usize ms_since_hit = game->play.clock_ms - entity->hit_on_clock_ms; Dqn_usize const HIT_CONFIRM_DURATION_MS = 8 * 16; if (ms_since_hit < HIT_CONFIRM_DURATION_MS) { sprite_colour = TELY_COLOUR_RED_V4; sprite_colour.g = ((1.f / HIT_CONFIRM_DURATION_MS) * ms_since_hit); sprite_colour.b = ((1.f / HIT_CONFIRM_DURATION_MS) * ms_since_hit); } } sprite_colour.a *= entity->action.sprite_alpha; TELY_Render_TextureColourV4(renderer, sprite.sheet->tex_handle, src_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotate radians*/, sprite_colour); } DQN_FOR_UINDEX(anim_index, entity->extra_cosmetic_anims.size) { FP_GameRenderSprite *sprite = entity->extra_cosmetic_anims.data + anim_index; uint64_t elapsed_ms = game->play.clock_ms - sprite->started_at_clock_ms; uint16_t raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite->asset.anim->ms_per_frame); if (raw_anim_frame > sprite->asset.anim->count && !sprite->loop) { anim_index = Dqn_FArray_EraseRange(&entity->extra_cosmetic_anims, anim_index, 1, Dqn_ArrayErase_Unstable).it_index; continue; } uint16_t anim_frame = raw_anim_frame % sprite->asset.anim->count; Dqn_usize sprite_index = sprite->asset.anim->index + anim_frame; Dqn_Rect src_rect = {}; switch (sprite->asset.sheet->type) { case TELY_AssetSpriteSheetType_Uniform: { Dqn_usize sprite_sheet_row = sprite_index / sprite->asset.sheet->sprites_per_row; Dqn_usize sprite_sheet_column = sprite_index % sprite->asset.sheet->sprites_per_row; src_rect.pos.x = DQN_CAST(Dqn_f32)(sprite_sheet_column * sprite->asset.sheet->sprite_size.w); src_rect.pos.y = DQN_CAST(Dqn_f32)(sprite_sheet_row * sprite->asset.sheet->sprite_size.y); src_rect.size.w = DQN_CAST(Dqn_f32)sprite->asset.sheet->sprite_size.w; src_rect.size.h = DQN_CAST(Dqn_f32)sprite->asset.sheet->sprite_size.h; } break; case TELY_AssetSpriteSheetType_Rects: { DQN_ASSERT(sprite_index < sprite->asset.sheet->rects.size); src_rect = sprite->asset.sheet->rects.data[sprite_index]; } break; } Dqn_f32 sprite_in_meters = FP_Game_PixelsToMetersNx1(game->play, src_rect.size.y); Dqn_f32 size_scale = sprite->height.meters / sprite_in_meters; Dqn_Rect dest_rect = {}; dest_rect.size = src_rect.size * size_scale; dest_rect.pos = world_pos - (dest_rect.size * .5f) + sprite->offset; if (sprite->asset.flip & TELY_AssetFlip_X) dest_rect.size.w *= -1.f; // NOTE: Flip the texture horizontally if (sprite->asset.flip & TELY_AssetFlip_Y) dest_rect.size.h *= -1.f; // NOTE: Flip the texture vertically TELY_Render_TextureColourV4(renderer, sprite->asset.sheet->tex_handle, src_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } if (entity->is_drunk) { bool show_martini = entity->handle.id & 1; TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, show_martini ? g_anim_names.particle_drunk_martini : g_anim_names.particle_drunk_bottle); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size; if (show_martini) { dest_rect.size *= 1.5f; dest_rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.0f, 0.0f)) - (Dqn_V2_InitNx2(dest_rect.size.x * .6f, dest_rect.size.y * .7f)); dest_rect.pos.y += (DQN_SINF(DQN_CAST(Dqn_f32)game->play.clock_ms / 1000.f * 2.f) + 1.f / 2.f) * dest_rect.size.y * .005f; } else { dest_rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.0f, 0.0f)) - (Dqn_V2_InitNx2(dest_rect.size.x * .7f, dest_rect.size.y * .9f)); } dest_rect.pos.y += (DQN_SINF(DQN_CAST(Dqn_f32)game->play.clock_ms / 1000.f * 2.f) + 1.f / 2.f) * dest_rect.size.y * .1f; TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } if (entity->converted_faction) { bool show_halo = entity->handle.id & 1; TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, show_halo ? g_anim_names.particle_church_halo : g_anim_names.particle_church_cross); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size; if (show_halo) { dest_rect.size *= .6f; dest_rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.5f, 0.0f)) - (Dqn_V2_InitNx2(dest_rect.size.x * .5f, dest_rect.size.y * 1.2f)); } else { dest_rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.5f, 0.0f)) - (Dqn_V2_InitNx2(dest_rect.size.x * .5f, dest_rect.size.y * .9f)); } TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } if (entity->handle != game->play.heart && entity->hp != entity->hp_cap && entity->hp) { // NOTE: We don't draw health bar for the player either bool current_entity_is_a_player = false; for (FP_GameEntityHandle player : game->play.players) { current_entity_is_a_player |= (entity->handle == player); } if (!current_entity_is_a_player) { Dqn_f32 bar_height = 12.f; Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.f, -0.2f)); Dqn_f32 health_t = entity->hp / DQN_CAST(Dqn_f32)entity->hp_cap; Dqn_Rect health_rect = Dqn_Rect_InitNx4(draw_p.x, draw_p.y, DQN_CAST(Dqn_f32)entity->hp_cap, bar_height); Dqn_Rect curr_health_rect = Dqn_Rect_InitNx4(draw_p.x, draw_p.y, DQN_CAST(Dqn_f32)entity->hp_cap * health_t, bar_height); TELY_Render_RectColourV4(renderer, curr_health_rect, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4(renderer, health_rect, TELY_RenderShapeMode_Line, TELY_COLOUR_BLACK_V4); cmd->thickness = 1.f; } } if (entity->type == FP_EntityType_ClubTerry || entity->type == FP_EntityType_AirportTerry || entity->type == FP_EntityType_ChurchTerry) { FP_GameEntityAction const *action = &entity->action; bool draw_timer = (entity->type == FP_EntityType_ClubTerry && action->state == FP_EntityClubTerryState_PartyTime) || (entity->type == FP_EntityType_AirportTerry && action->state == FP_EntityAirportTerryState_FlyPassenger) || (entity->type == FP_EntityType_ChurchTerry && action->state == FP_EntityChurchTerryState_ConvertPatron); if (draw_timer) { Dqn_f32 duration = action->end_at_clock_ms - DQN_CAST(Dqn_f32)action->started_at_clock_ms; Dqn_f32 elapsed = DQN_CAST(Dqn_f32)(game->play.clock_ms - action->started_at_clock_ms); Dqn_f32 t01 = DQN_MIN(1.f, elapsed / duration); Dqn_Rect rect = {}; rect.pos = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, -0.3f)); rect.size = Dqn_V2_InitNx2(world_hit_box.size.w * t01, 16.f); TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, 1.0f)); } } if (render_build_mode) { if (entity->flags & FP_GameEntityFlag_BuildZone) TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLUE_CADET_V4, 0.5f)); } if (game->play.debug_ui) { // NOTE: Render waypoint entities ====================================================== if (entity->type == FP_EntityType_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; } TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_BLUE_CADET_V4); } // NOTE: Draw the waypoints that the entity is moving along Dqn_V2 start = world_pos; for (FP_SentinelListLink *link = nullptr; FP_SentinelList_Iterate(&entity->waypoints, &link); ) { Dqn_V2 end = FP_Game_CalcWaypointWorldPos(game, entity->handle, &link->data); TELY_Render_LineColourV4(renderer, start, end, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4, 2.f); start = end; } if (entity->flags & FP_GameEntityFlag_MobSpawnerWaypoint) TELY_Render_CircleColourV4(renderer, world_pos, 16.f /*radius*/, TELY_RenderShapeMode_Line, TELY_COLOUR_BLUE_CADET_V4); if (entity->flags & FP_GameEntityFlag_BuildZone) { { Dqn_V2 line_p1 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, 0)); Dqn_V2 line_p2 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(1, 1)); TELY_Render_LineColourV4(renderer, line_p1, line_p2, TELY_COLOUR_BLACK_V4, 2.f); } { Dqn_V2 line_p1 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0, 1)); Dqn_V2 line_p2 = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(1, 0)); TELY_Render_LineColourV4(renderer, line_p1, line_p2, TELY_COLOUR_BLACK_V4, 2.f); } } // NOTE: Render attack box ================================================================= if (!game->play.debug_hide_bounding_rectangles) { Dqn_FArray attack_boxes = FP_Game_CalcEntityMeleeAttackBoxes(game, entity->handle); for (Dqn_Rect box : attack_boxes) TELY_Render_RectColourV4(renderer, box, TELY_RenderShapeMode_Line, TELY_COLOUR_RED_TOMATO_V4); } // 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 ========================================================== if (!game->play.debug_hide_bounding_rectangles) { if (game->play.clicked_entity == entity->handle) { TELY_Render_RectColourV4(renderer, world_hit_box, TELY_RenderShapeMode_Line, TELY_COLOUR_WHITE_PALE_GOLDENROD_V4); } else if (game->play.hot_entity == entity->handle || (entity->flags & FP_GameEntityFlag_DrawHitBox)) { Dqn_V4 hot_colour = game->play.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); } } if (game->play.hot_entity == entity->handle) { if (entity->name.size) { Dqn_V2I player_tile = Dqn_V2I_InitNx2(world_pos.x / game->play.tile_size, world_pos.y / game->play.tile_size); Dqn_f32 line_height = TELY_Render_FontSize(renderer) * os->core.dpi_scale; Dqn_V2 draw_p = world_mouse_p; TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "%.*s", DQN_STR_FMT(entity->name)); draw_p.y += line_height; TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "World Pos: (%.1f, %.1f)", world_pos.x, world_pos.y); draw_p.y += line_height; TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "Hit Box Size: %.1fx%.1f", world_hit_box.size.x, world_hit_box.size.y); draw_p.y += line_height; TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "Tile: %I32dx%I32d", player_tile.x, player_tile.y); draw_p.y += line_height; TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "World Mouse Pos: (%.1f, %.1f)", world_mouse_p.x, world_mouse_p.y); draw_p.y += line_height; Dqn_Str8 faction = {}; switch (entity->faction) { case FP_GameEntityFaction_Nil: faction = DQN_STR8("Nil"); break; case FP_GameEntityFaction_Friendly: faction = DQN_STR8("Friendly"); break; case FP_GameEntityFaction_Foe: faction = DQN_STR8("Foe"); break; } TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx2(0.f, 1), "Faction: %.*s", DQN_STR_FMT(faction)); draw_p.y += line_height; } } } if (entity->type == FP_EntityType_Billboard) { TELY_Render_PushFontSize(renderer, game->talkco_font, game->font_size); DQN_DEFER { TELY_Render_PopFont(renderer); }; DQN_FOR_UINDEX (player_index, game->play.players.size) { FP_GameEntityHandle player_handle = game->play.players.data[player_index]; FP_GameEntity const *player = FP_Game_GetEntity(game, player_handle); FP_GameControls const *controls = &player->controls; FP_EntityBillboardState state = DQN_CAST(FP_EntityBillboardState) entity->action.state; switch (state) { case FP_EntityBillboardState_Attack: { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.6f, 0.2f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, colour_accent_yellow); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->attack, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); } break; case FP_EntityBillboardState_Dash: { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.505f, -0.08f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_Colour_V4InitRGBU32(0xFFE726)); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->dash, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); } break; case FP_EntityBillboardState_Monkey: { } break; case FP_EntityBillboardState_RangeAttack: { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.20f, -0.13f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_Colour_V4InitRGBU32(0x364659)); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->range_attack, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); } break; case FP_EntityBillboardState_Strafe: { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.36f, -0.15f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_Colour_V4InitRGBU32(0xFF68A8)); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->strafe, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); } break; case FP_EntityBillboardState_Build: { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.065f, 0.2f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, colour_accent_yellow); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->build_mode, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.35f, 0.2f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_Colour_V4InitRGBU32(0xFF68A8)); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->move_building_ui_cursor_left, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.44f, 0.2f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_Colour_V4InitRGBU32(0xFF68A8)); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->move_building_ui_cursor_right, draw_p, false /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); draw_p = Dqn_Rect_InterpolatedPoint(world_hit_box, Dqn_V2_InitNx2(0.665f, 0.2f)); draw_p.y += TELY_Render_FontSize(renderer) * player_index * os->core.dpi_scale; TELY_Render_PushColourV4(renderer, TELY_COLOUR_BLACK_V4); FP_DrawBillboardKeyBindHint(renderer, assets, game, player_index, controls->mode, controls->attack, draw_p, true /*draw_player_prefix*/); TELY_Render_PopColourV4(renderer); } break; } } } } // NOTE: Draw perry ============================================================================ if (game->play.perry_joined == FP_GamePerryJoins_Enters) { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; // NOTE: Shake the world =================================================================== Dqn_Rect window_rect = {}; window_rect.size = Dqn_V2_InitV2I(os->core.window_size); Dqn_f32 tex_scalar = {}; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_terry); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_f32 desired_width = window_rect.size.x * .25f; tex_scalar = desired_width / tex_rect.size.w; } game->play.perry_join_bg_alpha += (1.f - game->play.perry_join_bg_alpha) * (DQN_CAST(Dqn_f32)input->delta_s * 8.f); game->play.perry_join_bg_alpha = DQN_MIN(game->play.perry_join_bg_alpha, .8f); TELY_Render_RectColourV4(renderer, window_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLACK_V4, game->play.perry_join_bg_alpha)); if (game->play.perry_join_bg_alpha > 0.5f) { if (!game->play.perry_join_splash_screen_shake_triggered) { game->play.global_camera_trauma01 = 1.f; game->play.perry_join_splash_screen_shake_triggered = true; game->play.perry_join_flash_alpha = 1.f; game->play.perry_join_splash_screen_end_ms = game->play.clock_ms + 2000; TELY_Audio_Play(audio, game->audio[FP_GameAudio_PerryStart], 1.f); } } if (game->play.perry_join_splash_screen_shake_triggered) { game->play.perry_join_flash_alpha -= game->play.perry_join_bg_alpha * (DQN_CAST(Dqn_f32)input->delta_s * 4.f); game->play.perry_join_flash_alpha = DQN_MAX(game->play.perry_join_flash_alpha, .0f); TELY_Render_RectColourV4(renderer, window_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, game->play.perry_join_flash_alpha)); TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_perry_joins_the_fight); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_f32 sin_t = (DQN_SINF(DQN_CAST(Dqn_f32)game->play.clock_ms / 1000.f * 3.f) + 1) / 2.f; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * (tex_scalar * 1.5f) + (tex_rect.size * (0.005f * sin_t)); dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(1, 1)) - dest_rect.size; Dqn_f32 max_shake_dist = 50.f; Dqn_f32 half_shake_dist = max_shake_dist * .5f; Dqn_V2 shake_offset = {}; shake_offset.x = (Dqn_PCG32_NextF32(&game->play.rng) * max_shake_dist - half_shake_dist) * game->play.global_camera_trauma01; shake_offset.y = (Dqn_PCG32_NextF32(&game->play.rng) * max_shake_dist - half_shake_dist) * game->play.global_camera_trauma01; dest_rect.pos += shake_offset; if (game->play.clock_ms > game->play.perry_join_splash_screen_end_ms) { game->play.perry_join_splash_pos_offset.x -= dest_rect.size.x * (12.f * DQN_CAST(Dqn_f32)input->delta_s); dest_rect.pos += game->play.perry_join_splash_pos_offset; if (dest_rect.pos.x < -dest_rect.size.x) game->play.perry_joined = FP_GamePerryJoins_PostEnter; } TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } } // NOTE: Render overlay UI ===================================================================== if (!game->play.debug_hide_hud && (game->play.state == FP_GameState_Pause || game->play.state == FP_GameState_Play || game->play.state == FP_GameState_Tutorial)) { // NOTE: Render the merchant menus for each player ========================================= FP_GamePlay *play = &game->play; FP_GameEntityHandle merchant_handles[] = { play->merchant_terry, play->merchant_graveyard, play->merchant_gym, play->merchant_phone_company, }; // NOTE: Calculate which merchant sound trigger flags to reset ============================= FP_GameEntityHandle merchant_with_more_than_2_menus_open = {}; static bool sound_played_flags[DQN_ARRAY_UCOUNT(merchant_handles)] = {false, false, false, false}; DQN_FOR_UINDEX (merchant_index, DQN_ARRAY_UCOUNT(merchant_handles)) { FP_GameEntityHandle merchant = merchant_handles[merchant_index]; Dqn_V2 merchant_pos = FP_Game_CalcEntityWorldPos(game, merchant); Dqn_usize menu_activation_count = 0; bool all_players_are_far_away_from_merchant_menu_trigger = true; DQN_FOR_UINDEX (player_index, game->play.players.size) { FP_GameEntityHandle player_handle = game->play.players.data[player_index]; Dqn_V2 player_pos = FP_Game_CalcEntityWorldPos(game, player_handle); Dqn_f32 dist_squared = Dqn_V2_LengthSq_V2x2(merchant_pos, player_pos); if (dist_squared < DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 4))) { all_players_are_far_away_from_merchant_menu_trigger = false; menu_activation_count++; } } if (all_players_are_far_away_from_merchant_menu_trigger) sound_played_flags[merchant_index] = false; if (menu_activation_count >= 2) { DQN_ASSERT(merchant_with_more_than_2_menus_open.id == 0); merchant_with_more_than_2_menus_open = merchant; } } Dqn_V2 const player_avatar_base_pos[] = { Dqn_V2_InitNx1(32.f), Dqn_V2_InitNx2(os->core.window_size.x - 320.f, 32.f), }; DQN_ASSERT(game->play.players.size <= DQN_ARRAY_UCOUNT(player_avatar_base_pos)); DQN_ASSERTF(game->play.players.size <= 2, "We hardcode 2 player support"); if (game->play.players.size == 1 && game->play.state != FP_GameState_Tutorial) { // NOTE: We show the Press to join for the remaining 2nd player TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); TELY_Render_PushFontSize(renderer, game->talkco_font, game->font_size); DQN_DEFER { TELY_Render_PopFont(renderer); TELY_Render_PopTransform(renderer); }; FP_GameEntity *first_player = FP_Game_GetEntity(game, game->play.players.data[0]); Dqn_Str8 join_game_key = {}; if (first_player->controls.mode == FP_GameControlMode_Keyboard) join_game_key = DQN_STR8(""); else join_game_key = DQN_STR8(""); Dqn_f32 font_height = TELY_Render_FontSize(renderer) * os->core.dpi_scale; Dqn_V2 base_p = player_avatar_base_pos[game->play.players.size]; TELY_Render_TextF(renderer, base_p, Dqn_V2_Zero, "Press %.*s", DQN_STR_FMT(join_game_key)); base_p.y += font_height; FP_ListenForNewPlayer(input, game, false /*tutorial_is_allowed*/); } // NOTE: Render the player(s) HUD and merchant menu interaction ============================ FP_ParticleDescriptor dollar_buy_particle = FP_DefaultFloatUpParticleDescriptor(g_anim_names.particle_purchase, 2000 /*duration*/); Dqn_Rect first_player_avatar_rect = {}; DQN_FOR_UINDEX (player_index, game->play.players.size) { FP_GameEntityHandle player_handle = game->play.players.data[player_index]; FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); Dqn_V2 player_pos = FP_Game_CalcEntityWorldPos(game, player_handle); { FP_GameInventory *invent = &player->inventory; struct FP_MerchantToMenuMapping { FP_GameEntityHandle merchant; Dqn_V2 *menu_pos; Dqn_Str8 upgrade_icon; Dqn_Str8 menu_anim; Dqn_Str8 building; Dqn_V2 building_offset01; uint8_t *inventory_count; uint32_t *building_base_price; uint32_t *upgrade_base_price; FP_GameAudio audio_type; bool *sound_played; } merchants[] = { {play->merchant_terry, &player->merchant_terry_menu_pos, g_anim_names.icon_attack, g_anim_names.merchant_terry_menu, g_anim_names.club_terry_dark, Dqn_V2_InitNx2(0.015f, +0.04f), &invent->clubs, &invent->clubs_base_price, &invent->health_base_price, FP_GameAudio_MerchantTerry, &sound_played_flags[0]}, {play->merchant_graveyard, &player->merchant_graveyard_menu_pos, g_anim_names.icon_stamina, g_anim_names.merchant_graveyard_menu, g_anim_names.church_terry_dark, Dqn_V2_InitNx2(0.04f, -0.15f), &invent->churchs, &invent->churchs_base_price, &invent->stamina_base_price, FP_GameAudio_MerchantGhost, &sound_played_flags[1]}, {play->merchant_gym, &player->merchant_gym_menu_pos, g_anim_names.icon_health, g_anim_names.merchant_gym_menu, g_anim_names.kennel_terry, Dqn_V2_InitNx2(0, +0), &invent->kennels, &invent->kennels_base_price, &invent->attack_base_price, FP_GameAudio_MerchantGym, &sound_played_flags[2]}, {play->merchant_phone_company, &player->merchant_phone_company_menu_pos, g_anim_names.icon_phone, g_anim_names.merchant_phone_company_menu, g_anim_names.airport_terry, Dqn_V2_InitNx2(0, -0.1f), &invent->airports, &invent->airports_base_price, &invent->mobile_plan_base_price, FP_GameAudio_MerchantPhone, &sound_played_flags[3]}, }; bool activated_merchant = false; for (FP_MerchantToMenuMapping mapping : merchants) { FP_GameEntityHandle merchant_handle = mapping.merchant; Dqn_V2 world_pos = FP_Game_CalcEntityWorldPos(game, merchant_handle); Dqn_f32 dist_squared = Dqn_V2_LengthSq_V2x2(world_pos, player_pos); if (dist_squared > DQN_SQUARED(FP_Game_MetersToPixelsNx1(game->play, 4))) { continue; } // NOTE: Render animated merchant menu ============================= activated_merchant = true; Dqn_Rect merchant_menu_rect = {}; { FP_GameRenderSprite *sprite = &game->play.player_merchant_menu; if (!sprite->asset.anim || sprite->asset.anim->label != mapping.menu_anim) { sprite->asset = TELY_Asset_MakeAnimatedSprite(&game->atlas_sprite_sheet, mapping.menu_anim, TELY_AssetFlip_No); sprite->started_at_clock_ms = game->play.clock_ms; } uint64_t elapsed_ms = game->play.clock_ms - sprite->started_at_clock_ms; uint16_t raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / sprite->asset.anim->ms_per_frame); uint16_t anim_frame = raw_anim_frame % sprite->asset.anim->count; Dqn_usize sprite_index = sprite->asset.anim->index + anim_frame; Dqn_Rect src_rect = sprite->asset.sheet->rects.data[sprite_index]; if (*mapping.menu_pos == Dqn_V2_Zero) *mapping.menu_pos = world_pos; Dqn_Rect top_rect = Dqn_Rect_InitV2x2(world_pos - (src_rect.size * .5f) - Dqn_V2_InitNx2(0.f, src_rect.size.h), src_rect.size); Dqn_Rect bottom_rect = Dqn_Rect_InitV2x2(world_pos - (src_rect.size * .5f) + Dqn_V2_InitNx2(0.f, src_rect.size.h), src_rect.size); // NOTE: Position the merchant menu ======================================== if (merchant_with_more_than_2_menus_open == mapping.merchant) { // NOTE: More than 2 players have activated the same merchant, we just // draw the menus and overlap with the players. DQN_ASSERTF(player_index <= 2, "We only handle 2 players gracefully"); if (player_index == 0) *mapping.menu_pos = top_rect.pos; else *mapping.menu_pos = bottom_rect.pos; } else { // NOTE: Move the merchant menu if we overlap with it so as to not // occlude the player. We *only* do this if the merchant is activated // by 1 player only. If multiple players activate it, we just draw the // menus-as-is. Dqn_V2 target_pos = {}; Dqn_Rect camera_entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, player->handle); if (Dqn_Rect_Intersects(top_rect, camera_entity_hit_box)) { target_pos = bottom_rect.pos; } else { target_pos = top_rect.pos; } // NOTE: Interpolate the menu position *mapping.menu_pos += (target_pos - *mapping.menu_pos) * (24.f * DQN_CAST(Dqn_f32)input->delta_s); } merchant_menu_rect.size = src_rect.size; merchant_menu_rect.pos = *mapping.menu_pos; // NOTE: Bob the merchant menu Dqn_f32 sin_t = DQN_SINF(DQN_CAST(Dqn_f32)game->play.clock_ms / 1000.f * 3.f); merchant_menu_rect.pos.y += sin_t * 4.f; TELY_Render_TextureColourV4(renderer, sprite->asset.sheet->tex_handle, src_rect, merchant_menu_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); if (activated_merchant && !*mapping.sound_played) { TELY_Audio_Play(audio, game->audio[mapping.audio_type], 1.f); *mapping.sound_played = true; } } TELY_Render_PushColourV4(renderer, TELY_COLOUR_BLACK_V4); TELY_Render_PushFontSize(renderer, game->talkco_font, game->large_talkco_font_size); DQN_DEFER { TELY_Render_PopFont(renderer); TELY_Render_PopColourV4(renderer); }; // NOTE: Render the merchant button for buildings ================== uint64_t const buy_duration_ms = 500; Dqn_V4 keybind_btn_shadow_colour = Dqn_V4_InitNx4(0.4f, 0.4f, 0.4f, 1.f); { bool const have_enough_coins = player->coins >= *mapping.building_base_price; FP_GameKeyBind key_bind = player->controls.buy_building; // NOTE: Render the (A) button ================================= Dqn_V2 dollar_text_label_pos = {}; Dqn_V4 tex_mod_colour = have_enough_coins ? TELY_COLOUR_WHITE_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, .5f); { // NOTE: Render the interaction button Dqn_Rect interact_btn_rect = {}; interact_btn_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.345f, 0.41f)); if (player->controls.mode == FP_GameControlMode_Gamepad) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.merchant_button_a); Dqn_Rect button_rect = game->atlas_sprite_sheet.rects.data[anim->index]; interact_btn_rect.size = button_rect.size * 1.5f; Dqn_Rect key_bind_rect = interact_btn_rect; key_bind_rect.pos.y += interact_btn_rect.size.y * .3f; TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, button_rect, key_bind_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, tex_mod_colour); } else { Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); TELY_AssetFontSizeHandle font_size = TELY_Render_ActiveFont(renderer); Dqn_Str8 key_bind_label = FP_ScanKeyToLabel(scratch.arena, key_bind.scan_key); interact_btn_rect.size = TELY_Asset_MeasureText(assets, font_size, key_bind_label); Dqn_Rect key_bind_rect = interact_btn_rect; key_bind_rect.pos.y += interact_btn_rect.size.y * .8f; TELY_Render_RectColourV4(renderer, Dqn_Rect_Expand(key_bind_rect, 2.f), TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(keybind_btn_shadow_colour, tex_mod_colour.a)); TELY_Render_PushColourV4(renderer, TELY_Colour_V4Alpha(colour_accent_yellow, tex_mod_colour.a)); TELY_Render_Text(renderer, Dqn_Rect_InterpolatedPoint(key_bind_rect, Dqn_V2_InitNx1(0.5f)), Dqn_V2_InitNx1(0.5f), key_bind_label); TELY_Render_PopColourV4(renderer); } // NOTE: Render the $ cost dollar_text_label_pos = Dqn_Rect_InterpolatedPoint(interact_btn_rect, Dqn_V2_InitNx2(0.5f, -1.5f)); TELY_Render_TextF(renderer, dollar_text_label_pos, Dqn_V2_InitNx2(0.5, 0.f), "$%u", *mapping.building_base_price); } // NOTE: Buy trigger + animation =========================================== { bool trigger_buy_anim = false; if (have_enough_coins) { if (TELY_Input_ScanKeyIsPressed(input, key_bind.scan_key)) { game->play.player_trigger_purchase_building_timestamp = game->play.clock_ms + buy_duration_ms; } else if (TELY_Input_ScanKeyIsDown(input, key_bind.scan_key)) { trigger_buy_anim = true; if (game->play.clock_ms > game->play.player_trigger_purchase_building_timestamp) game->play.player_trigger_purchase_building_timestamp = game->play.clock_ms; } else if (TELY_Input_ScanKeyIsReleased(input, key_bind.scan_key)) { if (game->play.clock_ms > game->play.player_trigger_purchase_building_timestamp) { if (mapping.inventory_count) { player->coins -= *mapping.building_base_price; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Ching], 1.f); *mapping.building_base_price *= 1; // NOTE: Raise the prices of everything else invent->airports_base_price *= 1; invent->clubs_base_price *= 1; invent->kennels_base_price *= 1; invent->churchs_base_price *= 1; (*mapping.inventory_count)++; dollar_buy_particle.pos = dollar_text_label_pos; FP_EmitParticle(game, dollar_buy_particle, 1); } } else { game->play.player_trigger_purchase_building_timestamp = UINT64_MAX; } } if (trigger_buy_anim) { uint64_t start_buy_time = game->play.player_trigger_purchase_building_timestamp - buy_duration_ms; uint64_t elapsed_time = game->play.clock_ms - start_buy_time; Dqn_f32 buy_t = DQN_MIN(elapsed_time / DQN_CAST(Dqn_f32)buy_duration_ms, 1.f); Dqn_Rect buy_lerp_rect = {}; buy_lerp_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.297f, 0.215f)); buy_lerp_rect.size.w = (merchant_menu_rect.size.w * 0.38f) * buy_t; buy_lerp_rect.size.h = merchant_menu_rect.size.h * .611f; TELY_Render_RectColourV4(renderer, buy_lerp_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLUE_CADET_V4, 0.5f)); } } } // NOTE: Render the merchant shop item building ================ { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, mapping.building); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * 0.35f; dest_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.42f, 0.25f) + mapping.building_offset01); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, tex_mod_colour); } } // NOTE: Render the merchant button for buildings { bool const have_enough_coins = player->coins >= *mapping.upgrade_base_price; FP_GameKeyBind key_bind = player->controls.buy_upgrade; // NOTE: Render the (B) button ================================= Dqn_V4 tex_mod_colour = have_enough_coins ? TELY_COLOUR_WHITE_V4 : TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, .5f); Dqn_V2 dollar_text_label_pos = {}; { Dqn_V2 interp_pos01 = Dqn_V2_InitNx2(0.7f, 0.41f); Dqn_Rect interact_btn_rect = {}; interact_btn_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, interp_pos01); // NOTE: Render the interact button ==================================== if (player->controls.mode == FP_GameControlMode_Gamepad) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.merchant_button_b); Dqn_Rect button_rect = game->atlas_sprite_sheet.rects.data[anim->index]; interact_btn_rect.size = button_rect.size * 1.5f; Dqn_Rect key_bind_rect = interact_btn_rect; key_bind_rect.pos.y += interact_btn_rect.size.y * .3f; TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, button_rect, key_bind_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, tex_mod_colour); } else { Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); TELY_AssetFontSizeHandle font_size = TELY_Render_ActiveFont(renderer); Dqn_Str8 key_bind_label = FP_ScanKeyToLabel(scratch.arena, key_bind.scan_key); interact_btn_rect.size = TELY_Asset_MeasureText(assets, font_size, key_bind_label); Dqn_Rect key_bind_rect = interact_btn_rect; key_bind_rect.pos.y += interact_btn_rect.size.y * .8f; TELY_Render_RectColourV4(renderer, Dqn_Rect_Expand(key_bind_rect, 2.f), TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(keybind_btn_shadow_colour, tex_mod_colour.a)); TELY_Render_PushColourV4(renderer, TELY_Colour_V4Alpha(colour_accent_yellow, tex_mod_colour.a)); TELY_Render_Text(renderer, Dqn_Rect_InterpolatedPoint(key_bind_rect, Dqn_V2_InitNx1(0.5f)), Dqn_V2_InitNx1(0.5f), key_bind_label); TELY_Render_PopColourV4(renderer); } // NOTE: Render the icon =============================================== { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, mapping.upgrade_icon); Dqn_Rect button_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = button_rect.size * .75f; dest_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, interp_pos01 + Dqn_V2_InitNx2(0.135f, 0.15f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, button_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, tex_mod_colour); } // NOTE: Render the $ cost dollar_text_label_pos = Dqn_Rect_InterpolatedPoint(interact_btn_rect, Dqn_V2_InitNx2(1.f, -1.5f)); TELY_Render_TextF(renderer, dollar_text_label_pos, Dqn_V2_InitNx2(0.5, 0.f), "$%u", *mapping.upgrade_base_price); } // NOTE: Buy trigger + animation =========================================== { bool trigger_buy_anim = false; if (have_enough_coins) { if (TELY_Input_ScanKeyIsPressed(input, key_bind.scan_key)) { game->play.player_trigger_purchase_upgrade_timestamp = game->play.clock_ms + buy_duration_ms; } else if (TELY_Input_ScanKeyIsDown(input, key_bind.scan_key)) { trigger_buy_anim = true; if (game->play.clock_ms > game->play.player_trigger_purchase_upgrade_timestamp) game->play.player_trigger_purchase_upgrade_timestamp = game->play.clock_ms; } else if (TELY_Input_ScanKeyIsReleased(input, key_bind.scan_key)) { if (game->play.clock_ms > game->play.player_trigger_purchase_upgrade_timestamp) { player->coins -= *mapping.upgrade_base_price; *mapping.upgrade_base_price *= 1; TELY_Audio_Play(audio, game->audio[FP_GameAudio_Ching], 1.f); dollar_buy_particle.pos = dollar_text_label_pos; FP_EmitParticle(game, dollar_buy_particle, 1); if (mapping.merchant == game->play.merchant_terry) { player->base_attack += DQN_CAST(uint32_t)(FP_DEFAULT_DAMAGE * 1.2f); } else if (mapping.merchant == game->play.merchant_graveyard) { player->stamina_cap += DQN_CAST(uint32_t)(FP_TERRY_DASH_STAMINA_COST * .5f); } else if (mapping.merchant == game->play.merchant_gym) { player->hp_cap += FP_DEFAULT_DAMAGE; player->hp = player->hp_cap; } else if (mapping.merchant == game->play.merchant_phone_company) { player->terry_mobile_data_plan_cap += DQN_KILOBYTES(1); } } else { game->play.player_trigger_purchase_upgrade_timestamp = UINT64_MAX; } } if (trigger_buy_anim) { uint64_t start_buy_time = game->play.player_trigger_purchase_upgrade_timestamp - buy_duration_ms; uint64_t elapsed_time = game->play.clock_ms - start_buy_time; Dqn_f32 buy_t = DQN_MIN(elapsed_time / DQN_CAST(Dqn_f32)buy_duration_ms, 1.f); Dqn_Rect buy_lerp_rect = {}; buy_lerp_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.68f, 0.215f)); buy_lerp_rect.size.w = (merchant_menu_rect.size.w * 0.211f) * buy_t; buy_lerp_rect.size.h = merchant_menu_rect.size.h * .611f; TELY_Render_RectColourV4(renderer, buy_lerp_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_RED_TOMATO_V4, 0.5f)); } } } } } if (activated_merchant) { player->in_game_menu = FP_GameInGameMenu_Merchant; } else { if (player->in_game_menu == FP_GameInGameMenu_Merchant) { player->in_game_menu = FP_GameInGameMenu_Nil; } } } // NOTE: Render player avatar HUD ========================================================== Dqn_Rect player_avatar_rect = {}; player_avatar_rect.pos = player_avatar_base_pos[player_index]; Dqn_V2 next_pos = {}; { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, player->type, 0 /*state, usually 0 is idle*/, FP_GameDirection_Down); player_avatar_rect.size = render_data.render_size; TELY_Render_TextureColourV4(renderer, render_data.sheet->tex_handle, render_data.sheet_rect, player_avatar_rect, Dqn_V2_Zero, 0.f, TELY_COLOUR_WHITE_V4); TELY_Render_PushFontSize(renderer, game->talkco_font, game->font_size); DQN_DEFER { TELY_Render_PopFont(renderer); }; next_pos = Dqn_Rect_InterpolatedPoint(player_avatar_rect, Dqn_V2_InitNx2(1.f, 0)); Dqn_f32 font_height = TELY_Render_FontSize(renderer) * os->core.dpi_scale; // NOTE: Health bar ==================================================== Dqn_f32 bar_height = font_height * .75f; Dqn_Rect health_icon_rect = {}; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.icon_health); Dqn_Rect icon_tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; { health_icon_rect.size = icon_tex_rect.size * .4f; health_icon_rect.pos = Dqn_V2_InitNx2(next_pos.x - health_icon_rect.size.x * .25f, next_pos.y - (health_icon_rect.size.y * .35f)); } Dqn_f32 bar_x = next_pos.x + (health_icon_rect.size.x * .25f); Dqn_f32 health_t = player->hp / DQN_CAST(Dqn_f32)player->hp_cap; Dqn_Rect health_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, DQN_CAST(Dqn_f32)player->hp_cap, bar_height); Dqn_Rect curr_health_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, DQN_CAST(Dqn_f32)player->hp_cap * health_t, bar_height); TELY_Render_RectColourV4(renderer, curr_health_rect, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4( renderer, health_rect, TELY_RenderShapeMode_Line, TELY_COLOUR_BLACK_V4); cmd->thickness = 4.f; // NOTE: Draw the heart icon shadow TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, Dqn_Rect_InitV2x2(health_icon_rect.pos - (cmd->thickness * 2.f), health_icon_rect.size + (cmd->thickness * 4.f)), Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_BLACK_V4); // NOTE: Draw the heart icon TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, health_icon_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } // NOTE: Stamina bar =================================================== next_pos.y += health_icon_rect.size.h * .8f; Dqn_Rect stamina_icon_rect = {}; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.icon_stamina); Dqn_Rect icon_tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; { stamina_icon_rect.size = icon_tex_rect.size * .35f; stamina_icon_rect.pos = Dqn_V2_InitNx2(next_pos.x - stamina_icon_rect.size.x * .25f, next_pos.y - (stamina_icon_rect.size.y * .35f)); } Dqn_f32 bar_x = next_pos.x + (stamina_icon_rect.size.x * .25f); Dqn_f32 stamina_t = player->stamina / DQN_CAST(Dqn_f32)player->stamina_cap; Dqn_Rect stamina_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, DQN_CAST(Dqn_f32)player->stamina_cap, bar_height); Dqn_Rect curr_stamina_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, DQN_CAST(Dqn_f32)player->stamina_cap * stamina_t, bar_height); TELY_Render_RectColourV4( renderer, curr_stamina_rect, TELY_RenderShapeMode_Fill, TELY_COLOUR_YELLOW_SANDY_V4); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4( renderer, stamina_rect, TELY_RenderShapeMode_Line, TELY_COLOUR_BLACK_V4); cmd->thickness = 4.f; // NOTE: Draw the icon shadow TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, Dqn_Rect_InitV2x2(stamina_icon_rect.pos - (cmd->thickness * 2.f), stamina_icon_rect.size + (cmd->thickness * 4.f)), Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_BLACK_V4); // NOTE: Draw the icon TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, stamina_icon_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } // NOTE: Mobile data bar =================================================== next_pos.y += stamina_icon_rect.size.h * .8f; Dqn_Rect phone_icon_rect = {}; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.icon_phone); Dqn_Rect icon_tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; { phone_icon_rect.size = icon_tex_rect.size * .35f; phone_icon_rect.pos = Dqn_V2_InitNx2(next_pos.x - phone_icon_rect.size.x * .25f, next_pos.y - (phone_icon_rect.size.y * .35f)); } Dqn_f32 pixels_per_kb = 15.5f; Dqn_f32 bar_width = DQN_CAST(Dqn_f32)player->terry_mobile_data_plan_cap / DQN_KILOBYTES(1) * pixels_per_kb; Dqn_f32 bar_x = next_pos.x + (phone_icon_rect.size.x * .25f); Dqn_f32 data_plan_t = player->terry_mobile_data_plan / DQN_CAST(Dqn_f32)player->terry_mobile_data_plan_cap; Dqn_Rect data_plan_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, bar_width, bar_height); Dqn_Rect curr_data_plan_rect = Dqn_Rect_InitNx4(bar_x, next_pos.y, bar_width * data_plan_t, bar_height); TELY_Render_RectColourV4( renderer, curr_data_plan_rect, TELY_RenderShapeMode_Fill, TELY_COLOUR_BLUE_CADET_V4); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4( renderer, data_plan_rect, TELY_RenderShapeMode_Line, TELY_COLOUR_BLACK_V4); cmd->thickness = 4.f; // NOTE: Draw the icon shadow TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, Dqn_Rect_InitV2x2(phone_icon_rect.pos - (cmd->thickness * 2.f), phone_icon_rect.size + (cmd->thickness * 4.f)), Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_BLACK_V4); // NOTE: Draw the icon TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, phone_icon_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } next_pos.y += phone_icon_rect.size.h * .8f; Dqn_Rect money_icon_rect = {}; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.icon_money); Dqn_Rect icon_tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; { money_icon_rect.size = icon_tex_rect.size * .35f; money_icon_rect.pos = Dqn_V2_InitNx2(next_pos.x - money_icon_rect.size.x * .25f, next_pos.y - (money_icon_rect.size.y * .15f)); } TELY_Render_TextF(renderer, Dqn_V2_InitNx2(next_pos.x + money_icon_rect.size.x * .75f, money_icon_rect.pos.y), Dqn_V2_InitNx2(0, -0.5), "$%zu", player->coins); // NOTE: Draw the icon shadow Dqn_f32 thickness = 4.f; TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, Dqn_Rect_InitV2x2(money_icon_rect.pos - (thickness * 2.f), money_icon_rect.size + (thickness * 4.f)), Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_BLACK_V4); // NOTE: Draw the icon TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, money_icon_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } next_pos.y += money_icon_rect.size.h; #if 0 next_pos.y += font_height; TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[H] Build Menu"); next_pos.y += font_height; TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[Shift+WASD] Strafe"); next_pos.y += font_height; TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[Ctrl+WASD] Dash"); next_pos.y += font_height; TELY_Render_TextF(renderer, next_pos, Dqn_V2_Zero, "[J|K] Melee/Range"); #endif if (player_index == 0) first_player_avatar_rect = player_avatar_rect; } // NOTE: Render building blueprint ===================================================== if (player->in_game_menu == FP_GameInGameMenu_Build) { FP_GamePlaceableBuilding placeable_building = PLACEABLE_BUILDINGS[player->build_mode_building_index]; FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, placeable_building.type, placeable_building.state, player->direction); Dqn_Rect dest_rect = FP_Game_GetBuildingPlacementRectForEntity(game, placeable_building, player->handle); Dqn_V4 colour = player->build_mode_can_place_building ? TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.5f) : TELY_Colour_V4Alpha(TELY_COLOUR_RED_V4, 0.5f); TELY_Render_RectColourV4(renderer, dest_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLUE_CADET_V4, 0.5f)); TELY_Render_TextureColourV4(renderer, render_data.sheet->tex_handle, render_data.sheet_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, colour); } // NOTE: Render the building selector UI =============================================== { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; player->build_mode_building_index = DQN_CLAMP(player->build_mode_building_index, 0, DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS) - 1); Dqn_f32 building_ui_size = 64.f; Dqn_f32 padding = 10.f; Dqn_f32 start_x = player_avatar_rect.pos.x; DQN_FOR_UINDEX (building_index, DQN_ARRAY_UCOUNT(PLACEABLE_BUILDINGS)) { FP_GamePlaceableBuilding building = PLACEABLE_BUILDINGS[building_index]; FP_EntityRenderData render_data = FP_Entity_GetRenderData(game, building.type, building.state, FP_GameDirection_Down); Dqn_Rect rect = Dqn_Rect_InitNx4(start_x + (building_index * building_ui_size) + (padding * building_index), next_pos.y, building_ui_size, building_ui_size); Dqn_V4 texture_colour = TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, .5f); Dqn_V4 outline_colour = TELY_COLOUR_WHITE_PALE_GOLDENROD_V4; if (player->build_mode_building_index == building_index) { outline_colour = TELY_COLOUR_RED_TOMATO_V4; texture_colour.a = 1.f; } TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, 0.5f)); TELY_Render_TextureColourV4(renderer, render_data.sheet->tex_handle, render_data.sheet_rect, rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, texture_colour); uint32_t building_count = 0; if (building.type == FP_EntityType_ClubTerry) building_count = player->inventory.clubs; else if (building.type == FP_EntityType_AirportTerry) building_count = player->inventory.airports; else if (building.type == FP_EntityType_KennelTerry) building_count = player->inventory.kennels; else if (building.type == FP_EntityType_ChurchTerry) building_count = player->inventory.churchs; TELY_Render_PushFontSize(renderer, game->talkco_font, game->font_size); DQN_DEFER { TELY_Render_PopFont(renderer); }; Dqn_V2 label_p = Dqn_Rect_InterpolatedPoint(rect, Dqn_V2_InitNx2(0.5f, 1.25f)); TELY_Render_TextF(renderer, label_p, Dqn_V2_InitNx2(0.5f, 0.5f), "x %u", building_count); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4(renderer, rect, TELY_RenderShapeMode_Line, outline_colour); cmd->thickness = 2.f; } } } // NOTE: Render the wave =================================================================== { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; TELY_Render_PushFontSize(renderer, game->talkco_font, game->large_talkco_font_size); DQN_DEFER { TELY_Render_PopFont(renderer); }; uint64_t time_until_next_wave_ms = 0; if (game->play.enemies_spawned_this_wave >= game->play.enemies_per_wave) { if (game->play.wave_cooldown_timestamp_ms > game->play.clock_ms) { time_until_next_wave_ms = game->play.wave_cooldown_timestamp_ms - game->play.clock_ms; } } Dqn_f32 mid_x = os->core.window_size.x * .5f; if (time_until_next_wave_ms) { TELY_Render_TextF(renderer, Dqn_V2_InitNx2(mid_x, first_player_avatar_rect.pos.y), Dqn_V2_InitNx1(0.5f), "%.1fs remaining until Wave %u!", (time_until_next_wave_ms / 1000.f), game->play.current_wave + 1); } else { TELY_Render_TextF(renderer, Dqn_V2_InitNx2(mid_x, first_player_avatar_rect.pos.y), Dqn_V2_InitNx1(0.5f), "Wave %u", game->play.current_wave); } } // NOTE: Render the heart health =========================================================== { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; Dqn_f32 font_height = TELY_Render_FontSize(renderer) * os->core.dpi_scale; Dqn_f32 bar_height = font_height * 1.25f; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.heart); FP_GameEntity *heart = FP_Game_GetEntity(game, game->play.heart); Dqn_f32 max_width = os->core.window_size.x * .5f; Dqn_V2 draw_p = Dqn_V2_InitNx2(os->core.window_size.x * .25f, first_player_avatar_rect.pos.y + bar_height); Dqn_f32 health_t = heart->hp / DQN_CAST(Dqn_f32)heart->hp_cap; Dqn_Rect health_rect = Dqn_Rect_InitNx4(draw_p.x, draw_p.y, max_width, bar_height); Dqn_Rect curr_health_rect = Dqn_Rect_InitNx4(draw_p.x, draw_p.y, max_width * health_t, bar_height); TELY_Render_RectColourV4(renderer, curr_health_rect, TELY_RenderShapeMode_Fill, TELY_COLOUR_RED_TOMATO_V4); TELY_RenderCommandRect *cmd = TELY_Render_RectColourV4( renderer, health_rect, TELY_RenderShapeMode_Line, TELY_COLOUR_BLACK_V4); cmd->thickness = 4.f; // NOTE: Draw the heart icon Dqn_Rect icon_tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect heart_icon_rect = {}; heart_icon_rect.size = icon_tex_rect.size * .4f; heart_icon_rect.pos = Dqn_V2_InitNx2(draw_p.x - (heart_icon_rect.size.w * .7f), draw_p.y - (heart_icon_rect.size.y * .4f)); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, icon_tex_rect, heart_icon_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); TELY_Render_PushFontSize(renderer, game->talkco_font, game->large_talkco_font_size); DQN_DEFER { TELY_Render_PopFont(renderer); }; TELY_Render_TextF(renderer, Dqn_Rect_InterpolatedPoint(heart_icon_rect, Dqn_V2_InitNx2(1.f, 0.65f)), Dqn_V2_Zero, "Terry's Heart"); } } } // NOTE: Debug show camera bounds #if 0 FP_GameCamera *camera = &game->play.camera; Dqn_Rect camera_view_rect = Dqn_Rect_InitV2x2((camera->size * -.5f) + (camera->world_pos / camera->scale), camera->size); TELY_Render_RectColourV4(renderer, Dqn_Rect_Expand(camera_view_rect, -5.f), TELY_RenderShapeMode_Line, TELY_COLOUR_RED_V4); #endif // NOTE: Render the other game state modes ===================================================== Dqn_V4 const maroon_colour = TELY_Colour_V4InitRGBAU32(0x301010FF); // NOTE: Maroon if (game->play.state == FP_GameState_Tutorial) { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); TELY_Render_PushFontSize(renderer, game->talkco_font, game->large_talkco_font_size); TELY_Render_PushColourV4(renderer, TELY_COLOUR_WHITE_V4); DQN_DEFER { TELY_Render_PopFont(renderer); TELY_Render_PopTransform(renderer); }; // NOTE: Calculate text bounds struct LineSize { Dqn_Str8 line; Dqn_V2 size; }; Dqn_FArray lines = {}; if (game->play.tutorial_state == FP_GameStateTutorial_ShowPlayer || game->play.tutorial_state == FP_GameStateTutorial_ShowPlayerWait) { LineSize *line_size = Dqn_FArray_Make(&lines, Dqn_ZeroMem_Yes); line_size->line = DQN_STR8("Defend Terry's heart from the oncoming cherries!"); } else if (game->play.tutorial_state == FP_GameStateTutorial_ShowBillboardBuild || game->play.tutorial_state == FP_GameStateTutorial_ShowBillboardBuildWait) { { LineSize *line_size = Dqn_FArray_Make(&lines, Dqn_ZeroMem_Yes); line_size->line = DQN_STR8("Lookout for billboards for tips!"); } { LineSize *line_size = Dqn_FArray_Make(&lines, Dqn_ZeroMem_Yes); line_size->line = DQN_STR8("Build buildings to slow cherries down and afford more time"); } } else { LineSize *line_size = Dqn_FArray_Make(&lines, Dqn_ZeroMem_Yes); line_size->line = DQN_STR8("Defeat the cherries spawning from the portals"); } Dqn_f32 scaled_font_size = TELY_Render_FontSize(renderer) * os->core.dpi_scale; Dqn_V2 text_bounding_size = {}; text_bounding_size.y = lines.size * scaled_font_size; for (LineSize &line_size_ : lines) { LineSize *line_size = &line_size_; line_size->size = TELY_Asset_MeasureText(assets, TELY_Render_ActiveFont(renderer), line_size->line); text_bounding_size.x = DQN_MAX(text_bounding_size.x, line_size->size.x); } // NOTE: Calculate terry avatar variables TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_terry); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_f32 desired_width = os->core.window_size.x * .1f; Dqn_f32 tex_scalar = desired_width / tex_rect.size.w; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * tex_scalar; dest_rect.pos = Dqn_V2_InitV2I(os->core.window_size) * Dqn_V2_InitNx2(0.5f, 0.8f) - (dest_rect.size * .5f) - Dqn_V2_InitNx2(text_bounding_size.x * .5f, 0.f); Dqn_Rect terry_bounding_rect = dest_rect; // NOTE: Draw text { Dqn_V2 draw_p = Dqn_Rect_InterpolatedPoint(terry_bounding_rect, Dqn_V2_InitNx2(1.0f, 0.5f)); Dqn_Rect bounding_rect = Dqn_Rect_InitV2x2(draw_p, text_bounding_size); bounding_rect = Dqn_Rect_ExpandV2(bounding_rect, Dqn_V2_InitNx2(scaled_font_size * 1.5f, scaled_font_size * .1f)); TELY_Render_RectColourV4(renderer, Dqn_Rect_Expand(bounding_rect, 5.f), TELY_RenderShapeMode_Fill, TELY_COLOUR_BLACK_V4); TELY_Render_RectColourV4(renderer, bounding_rect, TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(maroon_colour, 1.f)); for (LineSize &line_size_ : lines) { LineSize *line_size = &line_size_; TELY_Render_TextF(renderer, draw_p, Dqn_V2_Zero, "%.*s", DQN_STR_FMT(line_size->line)); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; } } // NOTE: Draw terry TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } if (game->play.state == FP_GameState_Pause) { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); TELY_Render_PushColourV4(renderer, TELY_COLOUR_WHITE_V4); DQN_DEFER { TELY_Render_PopColourV4(renderer); TELY_Render_PopTransform(renderer); }; TELY_Render_RectColourV4( renderer, Dqn_Rect_InitNx4(0, 0, DQN_CAST(Dqn_f32)os->core.window_size.x, DQN_CAST(Dqn_f32)os->core.window_size.y), TELY_RenderShapeMode_Fill, TELY_Colour_V4Alpha(TELY_COLOUR_BLACK_V4, .8f)); Dqn_V2 draw_p = Dqn_V2_InitV2I(os->core.window_size) * Dqn_V2_InitNx2(0.5f, 0.5f); TELY_Render_PushFontSize(renderer, game->talkco_font, game->xlarge_talkco_font_size); TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx1(0.5f), "Paused"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_PopFont(renderer); TELY_Render_PushFontSize(renderer, game->talkco_font, game->large_talkco_font_size); TELY_Render_TextF(renderer, draw_p, Dqn_V2_InitNx1(0.5f), "Press enter to resume"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_PopFont(renderer); TELY_Render_PopColourV4(renderer); if (TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_Return)) game->play.state = FP_GameState_Play; } // NOTE: Add scanlines into the game for A E S T H E T I C S =================================== if (game->play.state == FP_GameState_Play || game->play.state == FP_GameState_Tutorial) { Dqn_V2 screen_size = Dqn_V2_InitNx2(os->core.window_size.w, os->core.window_size.h); Dqn_f32 scanline_gap = 4.0f; Dqn_f32 scanline_thickness = 3.0f; FP_GameRenderScanlines(renderer, scanline_gap, scanline_thickness, screen_size); } if (game->play.state == FP_GameState_IntroScreen || game->play.state == FP_GameState_WinGame || game->play.state == FP_GameState_LoseGame) { TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; Dqn_V2I inset = os->core.window_size * .05f; Dqn_f32 min_inset = DQN_CAST(Dqn_f32)DQN_MIN(inset.x, inset.y); Dqn_V2 draw_p = Dqn_V2_InitNx2(min_inset, min_inset); TELY_Render_PushColourV4(renderer, TELY_COLOUR_WHITE_V4); Dqn_V4 bg_colour = maroon_colour; TELY_Render_RectColourV4( renderer, Dqn_Rect_InitNx4(0, 0, DQN_CAST(Dqn_f32)os->core.window_size.x, DQN_CAST(Dqn_f32)os->core.window_size.y), TELY_RenderShapeMode_Fill, bg_colour); Dqn_Rect window_rect = Dqn_Rect_InitNx4(0, 0, DQN_CAST(Dqn_f32)os->core.window_size.w, DQN_CAST(Dqn_f32)os->core.window_size.h); if (game->play.state == FP_GameState_IntroScreen || game->play.state == FP_GameState_LoseGame) { Dqn_f32 tex_scalar = 0.f; { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_terry); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_f32 desired_width = window_rect.size.x * .25f; tex_scalar = desired_width / tex_rect.size.w; } if (game->play.state == FP_GameState_IntroScreen) { // NOTE: Draw terry logo =========================================================== { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_terry); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * tex_scalar; dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.5f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } } else { // NOTE: Draw end screen logo ====================================================== TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.end_screen); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * (tex_scalar * 1.5f); dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.5f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } // NOTE: Draw cupid arrows around logo ================================================= { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_arrows); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * tex_scalar; dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.5f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } // NOTE: Draw title text =============================================================== { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_title); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * (tex_scalar * 1.5f); dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.20f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } // NOTE: Draw title subtitle =========================================================== if (game->play.state == FP_GameState_IntroScreen) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, g_anim_names.intro_screen_subtitle); Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size * tex_scalar; dest_rect.pos = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.8f)) - (dest_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, TELY_COLOUR_WHITE_V4); } TELY_Render_PushFontSize(renderer, game->inter_regular_font, game->font_size); Dqn_f32 t = (DQN_SINF(DQN_CAST(Dqn_f32)input->timer_s * 5.f) + 1.f) / 2.f; TELY_Render_PushColourV4(renderer, TELY_Colour_V4Alpha(TELY_COLOUR_WHITE_V4, t)); Dqn_V2 text_p = Dqn_Rect_InterpolatedPoint(window_rect, Dqn_V2_InitNx2(0.5f, 0.925f)); TELY_Render_TextF(renderer, text_p, Dqn_V2_InitNx1(0.5f), "Press or to %s", game->play.state == FP_GameState_IntroScreen ? "start" : "restart"); text_p.y += TELY_Render_ActiveFont(renderer).size * os->core.dpi_scale; TELY_Render_TextF(renderer, text_p, Dqn_V2_InitNx1(0.5f), "Press or for the tutorial"); text_p.y += TELY_Render_ActiveFont(renderer).size * os->core.dpi_scale; TELY_Render_PopColourV4(renderer); TELY_Render_PopFont(renderer); if (game->play.state == FP_GameState_LoseGame) FP_PlayReset(game, os); FP_ListenForNewPlayerResult new_player = FP_ListenForNewPlayer(input, game, true /*tutorial_is_allowed*/); if (new_player.yes) { if (new_player.tutorial_requested) { game->play.state = FP_GameState_Tutorial; TELY_Audio_Play(audio, game->audio[FP_GameAudio_GameStart], 1.f); } else { game->play.state = FP_GameState_Play; TELY_Audio_Play(audio, game->audio[FP_GameAudio_GameStart], 1.f); } } } else { DQN_ASSERT(game->play.state == FP_GameState_WinGame); TELY_Render_PushFontSize(renderer, game->inter_regular_font, game->large_font_size); TELY_Render_TextF(renderer, draw_p, Dqn_V2_Zero, "Terry has been saved"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_TextF(renderer, draw_p, Dqn_V2_Zero, "from his terrible calamity"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_PopFont(renderer); TELY_Render_PushFontSize(renderer, game->inter_regular_font, game->font_size); TELY_Render_TextF(renderer, draw_p, Dqn_V2_Zero, "He lives for yet another day and another love"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_TextF(renderer, draw_p, Dqn_V2_Zero, "Press enter to restart"); draw_p.y += TELY_Render_FontSize(renderer) * os->core.dpi_scale; TELY_Render_PopFont(renderer); TELY_Render_PopColourV4(renderer); if (TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_Return)) FP_PlayReset(game, os); } Dqn_f32 scanline_gap = 4.0f; Dqn_f32 scanline_thickness = 3.0f; FP_GameRenderScanlines(renderer, scanline_gap, scanline_thickness, window_rect.size); } for (FP_Particle &particle_ : game->play.particles) { FP_Particle *particle = &particle_; if (!particle->alive) continue; Dqn_usize elapsed_ms = game->play.clock_ms - particle->start_ms; Dqn_usize duration_ms = particle->end_ms - particle->start_ms; Dqn_f32 t = DQN_MIN(1.f, elapsed_ms / DQN_CAST(Dqn_f32)duration_ms); Dqn_V4 colour = {}; colour.r = Dqn_Lerp_F32(particle->colour_begin.r, t, particle->colour_end.r); colour.b = Dqn_Lerp_F32(particle->colour_begin.b, t, particle->colour_end.b); colour.g = Dqn_Lerp_F32(particle->colour_begin.g, t, particle->colour_end.g); colour.a = Dqn_Lerp_F32(particle->colour_begin.a, t, particle->colour_end.a); if (particle->anim_name.size) { TELY_AssetSpriteAnimation *anim = TELY_Asset_GetSpriteAnimation(&game->atlas_sprite_sheet, particle->anim_name); uint16_t const raw_anim_frame = DQN_CAST(uint16_t)(elapsed_ms / anim->ms_per_frame); uint16_t anim_frame = raw_anim_frame % anim->count; Dqn_Rect tex_rect = game->atlas_sprite_sheet.rects.data[anim->index + anim_frame]; Dqn_Rect dest_rect = {}; dest_rect.size = tex_rect.size; dest_rect.pos = particle->pos - (tex_rect.size * .5f); TELY_Render_TextureColourV4(renderer, game->atlas_sprite_sheet.tex_handle, tex_rect, dest_rect, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, colour); } else { TELY_Render_CircleColourV4(renderer, particle->pos, 20.f, TELY_RenderShapeMode_Fill, colour); } } // NOTE: Debug UI ============================================================================== if (TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F1)) game->play.debug_ui = !game->play.debug_ui; DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(4127) // Conditional expression is constant 'FP_DEVELOPER_MODE' if (FP_DEVELOPER_MODE && game->play.debug_ui) { DQN_MSVC_WARNING_POP TELY_Render_PushTransform(renderer, Dqn_M2x3_Identity()); DQN_DEFER { TELY_Render_PopTransform(renderer); }; // NOTE: Info bar ========================================================================== Dqn_f32 next_y = 10.f; { TELY_RFui_PushPadding(rfui, Dqn_V2_InitNx1(2)); DQN_DEFER { TELY_RFui_PopPadding(rfui); }; TELY_RFui_PushMargin(rfui, Dqn_V2_InitNx1(1)); DQN_DEFER { TELY_RFui_PopMargin(rfui); }; TELY_RFuiResult info_column = TELY_RFui_ColumnReverse(rfui, DQN_STR8("Info Column")); TELY_RFui_PushParent(rfui, info_column.widget); DQN_DEFER { TELY_RFui_PopParent(rfui); }; Dqn_V4 bg_colour = TELY_Colour_V4Alpha(TELY_RFui_ActiveBackgroundColourV4(rfui), .7f); TELY_RFui_PushBackgroundColourV4(rfui, bg_colour); DQN_DEFER { TELY_RFui_PopBackgroundColourV4(rfui); }; info_column.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute; info_column.widget->semantic_position[TELY_RFuiAxis_X].value = next_y; info_column.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute; info_column.widget->semantic_position[TELY_RFuiAxis_Y].value = os->core.window_size.h - TELY_Render_FontSize(renderer) * os->core.dpi_scale; { TELY_RFuiResult row = TELY_RFui_Row(rfui, DQN_STR8("Info Bar")); TELY_RFui_PushParent(rfui, row.widget); DQN_DEFER { TELY_RFui_PopParent(rfui); }; Dqn_ThreadScratch scratch = Dqn_Thread_GetScratch(nullptr); Dqn_Str8Builder builder = {}; builder.allocator = scratch.allocator; Dqn_Str8Builder_AppendF(&builder, "TELY"); if (Dqn_Str8_IsValid(os->core.os_name)) { Dqn_Str8Builder_AppendF(&builder, " | %.*s", DQN_STR_FMT(os->core.os_name)); } Dqn_Str8Builder_AppendF(&builder, " | %dx%d %.1fHz | TSC %.1f GHz", os->core.window_size.w, os->core.window_size.h, os->core.display.refresh_rate, os->core.tsc_per_second / 1'000'000'000.0); if (os->core.ram_mb) Dqn_Str8Builder_AppendF(&builder, " | RAM %.1fGB", os->core.ram_mb / 1024.0); DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(6272 6271) // warning C6272: Non-float passed as argument '6' when float is required in call to 'Dqn_Str8Builder_AppendF' Actual type: 'unsigned __int64'. // warning C6271: Extra argument passed to 'Dqn_Str8Builder_AppendF'. Dqn_Str8Builder_AppendF(&builder, " | 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); DQN_MSVC_WARNING_POP Dqn_Str8 text = Dqn_Str8Builder_Build(&builder, scratch.allocator); TELY_RFui_TextBackgroundF(rfui, "%.*s", DQN_STR_FMT(text)); } TELY_RFui_TextBackgroundF(rfui, "Mouse: %.1f, %.1f", input->mouse_p.x, input->mouse_p.y); TELY_RFui_TextBackgroundF(rfui, "Camera: %.1f, %.1f", game->play.camera.world_pos.x, game->play.camera.world_pos.y); TELY_RFui_TextBackgroundF(rfui, "Debug Info"); if (TELY_RFui_ButtonF(rfui, "F1 Debug info").clicked) game->play.debug_ui = !game->play.debug_ui; if (TELY_RFui_ButtonF(rfui, "F2 Add coins x10,000").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F2)) { for (FP_GameEntityHandle player_handle : game->play.players) { FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); player->coins += 10'000; } } if (TELY_RFui_ButtonF(rfui, "F3 Win game").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F3)) game->play.state = FP_GameState_WinGame; if (TELY_RFui_ButtonF(rfui, "F4 Lose game").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F4)) game->play.state = FP_GameState_LoseGame; if (TELY_RFui_ButtonF(rfui, "F5 Reset game").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F5)) FP_PlayReset(game, os); if (TELY_RFui_ButtonF(rfui, "F6 Increase health").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F6)) { for (FP_GameEntityHandle player_handle : game->play.players) { FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); player->hp_cap += FP_DEFAULT_DAMAGE; player->hp = player->hp_cap; } } if (TELY_RFui_ButtonF(rfui, "F7 Increase stamina").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F7)) { for (FP_GameEntityHandle player_handle : game->play.players) { FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); player->stamina_cap += DQN_CAST(uint16_t)(FP_TERRY_DASH_STAMINA_COST * .5f); player->stamina = player->stamina_cap; } } if (TELY_RFui_ButtonF(rfui, "F8 Increase mobile data").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F8)) { for (FP_GameEntityHandle player_handle : game->play.players) { FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); player->terry_mobile_data_plan_cap += DQN_KILOBYTES(1); player->terry_mobile_data_plan = player->terry_mobile_data_plan_cap; } } if (TELY_RFui_ButtonF(rfui, "F9 %s god mode", game->play.god_mode ? "Disable" : "Enable").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F9)) game->play.god_mode = !game->play.god_mode; if (TELY_RFui_ButtonF(rfui, "F11 Building inventory +1").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_F11)) { for (FP_GameEntityHandle player_handle : game->play.players) { FP_GameEntity *player = FP_Game_GetEntity(game, player_handle); player->inventory.clubs += 1; player->inventory.airports += 1; player->inventory.churchs += 1; player->inventory.kennels += 1; } } if (TELY_RFui_ButtonF(rfui, "1 %s HUD", game->play.debug_hide_hud ? "Show" : "Hide").clicked || TELY_Input_ScanKeyIsPressed(input, TELY_InputScanKey_1)) game->play.debug_hide_hud = !game->play.debug_hide_hud; if (TELY_RFui_ButtonF(rfui, "%s bounding rects", game->play.debug_hide_bounding_rectangles ? "Show" : "Hide").clicked) game->play.debug_hide_bounding_rectangles = !game->play.debug_hide_bounding_rectangles; if (TELY_RFui_ButtonF(rfui, "%s mob spawning", game->play.debug_disable_mobs ? "Enable" : "Disable").clicked) game->play.debug_disable_mobs = !game->play.debug_disable_mobs; } if (0) { next_y += TELY_RFui_ActiveFont(rfui).size; TELY_RFuiResult bar = TELY_RFui_Column(rfui, DQN_STR8("Memory bar")); bar.widget->semantic_position[TELY_RFuiAxis_X].kind = TELY_RFuiPositionKind_Absolute; bar.widget->semantic_position[TELY_RFuiAxis_X].value = 10.f; bar.widget->semantic_position[TELY_RFuiAxis_Y].kind = TELY_RFuiPositionKind_Absolute; bar.widget->semantic_position[TELY_RFuiAxis_Y].value = next_y; TELY_RFui_PushParent(rfui, bar.widget); DQN_DEFER { TELY_RFui_PopParent(rfui); }; { Dqn_ArenaInfo arena_info = Dqn_Arena_Info(&os->arena); DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(6271) // warning C6271: Extra argument passed to 'TELY_RFui_TextF'. TELY_RFui_TextF(rfui, "Platform Arena[%I64u]: %_$$d/%_$$d (HWM %_$$d, COMMIT %_$$d)", os->arena.blocks, arena_info.used, arena_info.capacity, arena_info.used_hwm, arena_info.commit); DQN_MSVC_WARNING_POP next_y += TELY_RFui_ActiveFont(rfui).size; } for (Dqn_ArenaCatalogItem *item = g_dqn_library->arena_catalog.sentinel.next; item != &g_dqn_library->arena_catalog.sentinel; item = item->next) { if (item != g_dqn_library->arena_catalog.sentinel.next) next_y += TELY_RFui_ActiveFont(rfui).size; Dqn_Arena *arena = item->arena; Dqn_ArenaInfo arena_info = Dqn_Arena_Info(arena); DQN_MSVC_WARNING_PUSH DQN_MSVC_WARNING_DISABLE(6271) // warning C6271: Extra argument passed to 'TELY_RFui_TextF'. TELY_RFui_TextF(rfui, "%.*s[%I64u]: %_$$d/%_$$d (HWM %_$$d, COMMIT %_$$d)", DQN_STR_FMT(arena->label), arena->blocks, arena_info.used, arena_info.capacity, arena_info.used_hwm, arena_info.commit); DQN_MSVC_WARNING_POP } } // NOTE: Profiler if (0) { next_y += TELY_RFui_ActiveFont(rfui).size; TELY_RFuiResult profiler_layout = TELY_RFui_Column(rfui, DQN_STR8("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 = next_y; TELY_RFui_PushParent(rfui, profiler_layout.widget); DQN_DEFER { TELY_RFui_PopParent(rfui); }; // TODO(doyle): On emscripten we need to use Dqn_OS_PerfCounterNow() however those // require OS functions which need to be exposed into the os layer. #if 0 Dqn_f64 const tsc_frequency = DQN_CAST(Dqn_f64)TELY_OS_PerfCounterFrequency(&os->core); 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; uint64_t tsc_exclusive = anchor->tsc_exclusive; uint64_t tsc_inclusive = anchor->tsc_inclusive; Dqn_f64 tsc_exclusive_milliseconds = tsc_exclusive * 1000 / tsc_frequency; if (tsc_exclusive == tsc_inclusive) { TELY_RFui_TextF(rfui, "%.*s[%u]: %.1fms", DQN_STR_FMT(anchor->name), anchor->hit_count, tsc_exclusive_milliseconds); } else { Dqn_f64 tsc_inclusive_milliseconds = tsc_inclusive * 1000 / tsc_frequency; TELY_RFui_TextF(rfui, "%.*s[%u]: %.1f/%.1fms", DQN_STR_FMT(anchor->name), anchor->hit_count, tsc_exclusive_milliseconds, tsc_inclusive_milliseconds); } } #endif } TELY_RFui_Flush(rfui, renderer, input, assets); } } TELY_OS_DLL_FUNCTION void TELY_OS_DLLFrameUpdate(TELY_OS *os) { TELY_Input *input = &os->input; TELY_Renderer *renderer = &os->renderer; FP_Game *game = DQN_CAST(FP_Game *) os->user_data; // ============================================================================================= game->play.prev_clicked_entity = game->play.clicked_entity; game->play.prev_hot_entity = game->play.hot_entity; game->play.prev_active_entity = game->play.active_entity; game->play.hot_entity = {}; game->play.active_entity = {}; Dqn_FArray_Clear(&game->play.parent_entity_stack); Dqn_FArray_Add(&game->play.parent_entity_stack, game->play.root_entity->handle); // ============================================================================================= if (game->play.state == FP_GameState_Play || game->play.state == FP_GameState_Tutorial) { if (TELY_Input_KeyWasDown(input->mouse_keys[TELY_InputMouseKey_Left]) && TELY_Input_KeyIsDown(input->mouse_keys[TELY_InputMouseKey_Left])) { if (game->play.prev_active_entity.id) game->play.active_entity = game->play.prev_active_entity; } else { FP_GameCameraM2x3 const camera_xforms = FP_Game_CameraModelViewM2x3(game->play.camera); Dqn_V2 world_mouse_p = Dqn_M2x3_MulV2(camera_xforms.view_model, input->mouse_p); for (FP_GameEntityIterator it = {}; FP_Game_DFSPreOrderWalkEntityTree(game, &it, game->play.root_entity); ) { DQN_ASSERT(it.entity); FP_GameEntity *entity = it.entity; if (entity->local_hit_box_size.x <= 0 || entity->local_hit_box_size.y <= 0) continue; if ((entity->flags & FP_GameEntityFlag_Clickable) == 0) continue; Dqn_Rect world_hit_box = FP_Game_CalcEntityWorldHitBox(game, entity->handle); if (!Dqn_Rect_ContainsPoint(world_hit_box, world_mouse_p)) continue; game->play.hot_entity = entity->handle; if (TELY_Input_KeyIsPressed(input->mouse_keys[TELY_InputMouseKey_Left])) { game->play.active_entity = entity->handle; game->play.clicked_entity = entity->handle; } } } } TELY_Audio *audio = &os->audio; for (game->play.delta_s_accumulator += DQN_CAST(Dqn_f32)input->delta_s; game->play.delta_s_accumulator > FP_GAME_PHYSICS_STEP; game->play.delta_s_accumulator -= FP_GAME_PHYSICS_STEP) { FP_Update(os, game, input, audio); } FP_Render(game, os, renderer, audio); }