From 5159763baa29055445923aac7df44f65584d8a57 Mon Sep 17 00:00:00 2001 From: doyle Date: Wed, 18 Oct 2023 21:48:12 +1100 Subject: [PATCH] fp: Move the buy menu if it overlaps with player --- feely_pona.cpp | 108 ++++++++++++++++++++++++++++++++-------------- feely_pona_game.h | 5 +++ 2 files changed, 81 insertions(+), 32 deletions(-) diff --git a/feely_pona.cpp b/feely_pona.cpp index 24b6d51..9e59f6a 100644 --- a/feely_pona.cpp +++ b/feely_pona.cpp @@ -975,9 +975,8 @@ void FP_EntityActionStateMachine(FP_Game *game, TELY_Audio *audio, TELY_Platform case FP_EntityClingerState_Death: { if (entering_new_state) { - TELY_AssetAnimatedSprite sprite = TELY_Asset_MakeAnimatedSprite(sheet, g_anim_names.clinger_death, TELY_AssetFlip_No); - uint64_t duration_ms = sprite.anim->count * sprite.anim->ms_per_frame; - FP_Game_EntityActionReset(game, entity->handle, duration_ms, sprite); + 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 = {}; } @@ -1240,9 +1239,8 @@ void FP_EntityActionStateMachine(FP_Game *game, TELY_Audio *audio, TELY_Platform case FP_EntityCatfishState_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); + FP_Game_EntityActionReset(game, entity->handle, duration_ms, render_data.sprite); } if (we_are_clicked_entity) { @@ -2155,7 +2153,7 @@ void FP_Update(TELY_Platform *platform, FP_Game *game, TELY_PlatformInput *input } // NOTE: Mob spawner ======================================================================= - if (entity->type == FP_EntityType_MobSpawner) { + if (entity->type == FP_EntityType_MobSpawner && 0) { // 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); @@ -2332,10 +2330,10 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, TELY_RFui_PushFont(rfui, game->jetbrains_mono_font); TELY_RFui_PushLabelColourV4(rfui, TELY_COLOUR_BLACK_MIDNIGHT_V4); - FP_GameCamera shake_camera = game->play.camera; + FP_GameCamera shake_camera = game->play.camera; + FP_GameEntity *camera_entity = FP_Game_GetEntity(game, game->play.camera_tracking_entity); { // NOTE: Calculate camera position based on camera shake - FP_GameEntity *camera_entity = FP_Game_GetEntity(game, game->play.camera_tracking_entity); Dqn_f32 trauma01 = DQN_SQUARED(camera_entity->trauma01); if (camera_entity->type == FP_EntityType_Terry) { @@ -2710,9 +2708,14 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, Dqn_V2 player_pos = FP_Game_CalcEntityWorldPos(game, game->play.player); { static bool sound_played_flags[4] = {false, false, false, false}; + FP_GameInventory *invent = &player->inventory; + FP_GamePlay *play = &game->play; + struct FP_MerchantToMenuMapping { FP_GameEntityHandle merchant; + Dqn_V2 *menu_pos; + Dqn_String8 upgrade_icon; Dqn_String8 menu_anim; Dqn_String8 building; Dqn_V2 building_offset01; @@ -2722,10 +2725,10 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, FP_GameAudio audio_type; bool *sound_played; } merchants[] = { - {game->play.merchant_terry, 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]}, - {game->play.merchant_graveyard, 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]}, - {game->play.merchant_gym, 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]}, - {game->play.merchant_phone_company, 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]}, + {play->merchant_terry, &play->merchant_terry_menu_pos, g_anim_names.icon_stamina, 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, &play->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, &play->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, &play->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; @@ -2740,8 +2743,8 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, } // NOTE: Render animated merchant menu ============================= - activated_merchant = true; - Dqn_Rect merchant_menu_rect = {}; + 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) { @@ -2756,8 +2759,30 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, Dqn_usize sprite_index = sprite->asset.anim->index + anim_frame; Dqn_Rect src_rect = sprite->asset.sheet->rects.data[sprite_index]; - merchant_menu_rect.size = src_rect.size; - merchant_menu_rect.pos = world_pos - (merchant_menu_rect.size * .5f) - Dqn_V2_InitNx2(0.f, src_rect.size.y); + 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: Move the merchant menu if we overlap with it so as to not occlude the player + { + Dqn_V2 target_pos = {}; + Dqn_Rect camera_entity_hit_box = FP_Game_CalcEntityWorldHitBox(game, camera_entity->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)input->timer_s * 3.f); merchant_menu_rect.pos.y += sin_t * 4.f; @@ -2775,7 +2800,7 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, } } - TELY_Render_PushColourV4(renderer, TELY_COLOUR_WHITE_V4); + TELY_Render_PushColourV4(renderer, TELY_COLOUR_BLACK_V4); TELY_Render_PushFont(renderer, game->talkco_font_large); DQN_DEFER { TELY_Render_PopFont(renderer); @@ -2849,8 +2874,8 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, Dqn_V2_Zero /*rotate origin*/, 0.f /*rotation*/, tex_mod_colour); - TELY_Render_TextF(renderer, Dqn_Rect_InterpolatedPoint(dest_rect, Dqn_V2_InitNx2(0.5f, -1)), Dqn_V2_InitNx2(0.5, 0.f), "$%u", *mapping.building_base_price); + } // NOTE: Render the merchant shop item building ================ @@ -2892,7 +2917,6 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, TELY_Audio_Play(audio, game->audio[FP_GameAudio_Ching], 1.f); if (mapping.merchant == game->play.merchant_terry) { - // TODO(doyle): Attack damage? Or increase attack range? 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); @@ -2925,20 +2949,40 @@ void FP_Render(FP_Game *game, TELY_Platform *platform, TELY_Renderer *renderer, // 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); { - 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]; - Dqn_Rect dest_rect = {}; - dest_rect.size = button_rect.size * 1.5f; - dest_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.75f, 0.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); + Dqn_V2 interp_pos01 = {}; + { + 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]; + interp_pos01 = Dqn_V2_InitNx2(0.71f, 0.5f); + Dqn_Rect dest_rect = {}; + dest_rect.size = button_rect.size * 1.5f; + dest_rect.pos = Dqn_Rect_InterpolatedPoint(merchant_menu_rect, interp_pos01); + 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); - TELY_Render_TextF(renderer, Dqn_Rect_InterpolatedPoint(dest_rect, Dqn_V2_InitNx2(0.5f, -1)), Dqn_V2_InitNx2(0.5, 0.f), "$%u", *mapping.upgrade_base_price); + } + + { + 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.12f, 0.08f)) - (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); + } + + TELY_Render_TextF(renderer, Dqn_Rect_InterpolatedPoint(merchant_menu_rect, Dqn_V2_InitNx2(0.78f, 0.28f)), Dqn_V2_InitNx2(0.5, 0.f), "$%u", *mapping.upgrade_base_price); } } } diff --git a/feely_pona_game.h b/feely_pona_game.h index 0cfee92..5d307ff 100644 --- a/feely_pona_game.h +++ b/feely_pona_game.h @@ -324,6 +324,11 @@ struct FP_GamePlay FP_GameEntityHandle merchant_gym; FP_GameEntityHandle merchant_phone_company; + Dqn_V2 merchant_terry_menu_pos; + Dqn_V2 merchant_graveyard_menu_pos; + Dqn_V2 merchant_gym_menu_pos; + Dqn_V2 merchant_phone_company_menu_pos; + FP_GameEntityHandle clicked_entity; FP_GameEntityHandle hot_entity; FP_GameEntityHandle active_entity;