Files
isaac-port/src/menu_stats.cpp
T

405 lines
17 KiB
C++

#include "menu_stats.h"
#include "data.h"
#include "manager.h"
#include "menu_manager.h"
#include "render_passes.h"
#include <algorithm>
#include <cstdlib>
#include <format>
constexpr const char* STATS_ANM2 = "gfx/ui/main menu/StatsMenu.anm2";
constexpr const char* ACHIEVEMENTS_ANM2 = "gfx/ui/achievement/achievements.anm2";
constexpr const char* SECRETS_ANM2 = "gfx/ui/main menu/SecretsMenuComponents.anm2";
constexpr const char* LOCKED_SHEET = "gfx/ui/achievement/Achievement_Locked.png";
constexpr const char* ACHIEVEMENTS_XML = "achievements.xml";
constexpr const char* ANIMATION_ICON_FIRST = "Icon1Unlocked";
constexpr const char* ANIMATION_CURSOR_LEFT = "CursorLeft";
constexpr const char* ANIMATION_CURSOR_RIGHT = "CursorRight";
constexpr const char* ANIMATION_APPEAR = "Appear";
constexpr const char* ANIMATION_APPEAR_NEXT = "Appear2";
constexpr const char* ANIMATION_DISAPPEAR = "Dissapear";
constexpr const char* ANIMATION_DISAPPEAR_NEXT = "Dissapear2";
constexpr int ICON_VARIANTS = 3;
constexpr int SELECTED_ICON = SECRET_ICON_COUNT / 2;
constexpr int PICTURE_LAYER = 3;
constexpr int CURSOR_LAYER = 4;
constexpr int DIGIT_LAYER = 7;
constexpr int SECRETS_LOCK_LAYER = 8;
constexpr int SECRETS_LOCK_FRAME = 1;
constexpr int DIGIT_BASE = 10;
constexpr int FAST_HOLD = 60;
constexpr int FASTER_HOLD = 180;
constexpr float SPEED_RAMP_FRAMES = 30;
constexpr float SPEED_MAX = 4;
constexpr float SCROLL_IN_START = 275;
constexpr float SCROLL_IN_END = 240;
constexpr float SCROLL_IN_STEP = 2;
constexpr float ICON_SPACING = 35;
constexpr float ICONS_X = 100;
constexpr float CURSOR_LEFT_X = 95;
constexpr float CURSOR_RIGHT_X = 380;
constexpr float PICTURE_X = 240;
constexpr float PICTURE_Y = 135;
constexpr float COUNT_Y = 30;
constexpr float NUMBERS_X = 40;
constexpr float NUMBERS_Y = 5;
constexpr float NUMBER_LINE_HEIGHT = 22;
constexpr float DIGIT_STEP_X = 11;
constexpr float DIGIT_STEP_Y = 1.5f;
// A sprite color that only sets the alpha.
static ColorMod alpha_color(float alpha)
{
ColorMod color = COLOR_DEFAULT;
color._tint[3] = alpha;
return color;
}
// 009ec2b0 Loads the menu and resets it, at its place on the menu sheet.
void Menu_Stats::Init(Vector menuPosition)
{
LoadGraphics();
position = menuPosition;
}
// 009ec880 Loads the menu, the achievement picture and the secrets page's icons (playing) and cursors, and resets the
// menu.
void Menu_Stats::LoadGraphics()
{
statsMenuSprite.Load(STATS_ANM2, GRAPHICS_LOAD);
achievementsSprite.Load(ACHIEVEMENTS_ANM2, GRAPHICS_LOAD);
for (ANM2& icon : achievementMiniSprites)
{
icon.Load(SECRETS_ANM2, GRAPHICS_LOAD);
icon.Play(ANIMATION_ICON_FIRST);
}
cursorLeftSprite.Load(SECRETS_ANM2, GRAPHICS_LOAD);
cursorRightSprite.Load(SECRETS_ANM2, GRAPHICS_LOAD);
reset();
}
// 009ec600 Drops every sprite, as a run starts.
void Menu_Stats::unload_graphics()
{
statsMenuSprite.Reset();
achievementsSprite.Reset();
for (ANM2& icon : achievementMiniSprites) icon.Reset();
cursorLeftSprite.Reset();
cursorRightSprite.Reset();
}
// 009ed3c0 Plays the menu with the picture's frame hidden and the cursors moving, picks each achievement's icon at
// random, clears the counts and the selection, and reads the achievement list.
void Menu_Stats::reset()
{
statsMenuSprite.Play(ANIMATION_IDLE);
achievementsSprite.GetLayer(0)->isVisible = false;
cursorLeftSprite.Play(ANIMATION_CURSOR_LEFT);
cursorRightSprite.Play(ANIMATION_CURSOR_RIGHT);
for (int& icon : iconIds) icon = (int)(Random() % ICON_VARIANTS) + 1;
countTotalAchievements = 0;
countItemsInCollection = 0;
countDeaths = 0;
countMomKills = 0;
statsMenuCurrentSelection = STATS_COLLECTION;
isAchievementScreenVisible = false;
achievementScrollInPosition = SCROLL_IN_START;
achievementScrollThruListPosition = 0;
achievements_load();
holdInputTimer = 0;
}
// 009ed780 Reads achievements.xml (a mod's first): every listed achievement and its picture under gfxroot. Entries
// from earlier reads stay. The list then holds every listed id in order.
// The original reads attributes of no node when the root is not achievements. Nothing is read then.
void Menu_Stats::achievements_load()
{
Data data;
data_load(data, g_manager->modManager.TryRedirectPath(ACHIEVEMENTS_XML));
if (!data.isLoaded || node_tag(data.root) != "achievements") return;
std::string root(node_attr(data.root, "gfxroot"));
bool isListed = false;
for (DataNode node : node_children(data.root))
{
isListed = isListed || node_tag(node) == "achievement";
std::string id(node_attr(node, "id"));
std::string_view gfx = node_attr(node, "gfx");
int achievementId = id.empty() ? -1 : std::atoi(id.c_str());
if (!isListed || achievementId < 0 || achievementId >= ACHIEVEMENT_COUNT) continue;
isAchievementListed[achievementId] = true;
achievementGfx[achievementId] = gfx.empty() ? std::string() : root + std::string(gfx);
}
achievementIds.clear();
for (int id = 0; id < ACHIEVEMENT_COUNT; id++)
if (isAchievementListed[id]) achievementIds.push_back(id);
}
// 009ecbc0 Steps the menu every other frame and counts the collection, the achievements unlocked, the deaths and the
// Mom kills, locking the secrets entry while none is unlocked. Then the secrets page or the menu takes input, and the
// cursor layer follows.
void Menu_Stats::Update()
{
PersistentGameData& data = g_manager->persistentGameData;
if ((g_manager->framecount & 1) == 0) statsMenuSprite.Update();
countItemsInCollection = (int)std::ranges::count(data.itemsCollection, true);
countTotalAchievements = (int)std::ranges::count_if(achievementIds, [&](int id) { return data.Unlocked(id); });
countDeaths = data.eventCounters[EVENT_COUNTER_DEATHS];
countMomKills = data.eventCounters[EVENT_COUNTER_MOM_KILLS];
statsMenuSprite.SetLayerFrame(SECRETS_LOCK_LAYER, countTotalAchievements < 1 ? SECRETS_LOCK_FRAME : 0);
if (isAchievementScreenVisible) UpdateSecrets();
else stats_update();
statsMenuSprite.SetLayerFrame(CURSOR_LAYER, statsMenuCurrentSelection);
}
// 009ede60 Confirm opens the selected entry, up and down move between them (past the secrets while none is unlocked,
// wrapping) and back leaves.
void Menu_Stats::stats_update()
{
MenuManager& menus = *g_menuManager;
int previous = statsMenuCurrentSelection;
bool isSecretsLocked = countTotalAchievements == 0;
if (is_action(ACTION_MENU_CONFIRM, ACTION_TRIGGERED))
{
switch (statsMenuCurrentSelection)
{
case STATS_COLLECTION: menus.push_menu(MENU_COLLECTION); break;
case STATS_ENDINGS: menus.push_menu(MENU_CUTSCENES); break;
case STATS_SECRETS: secrets_open(); break;
case STATS_BESTIARY: menus.push_menu(MENU_BESTIARY); break;
case STATS_ONLINE_AWARDS: menus.push_menu(MENU_ONLINE_AWARDS); break;
default: break;
}
}
else if (is_action(ACTION_MENU_UP, ACTION_TRIGGERED))
{
if (--statsMenuCurrentSelection < 0) statsMenuCurrentSelection = STATS_ITEM_COUNT - 1;
else if (previous == STATS_BESTIARY && isSecretsLocked) statsMenuCurrentSelection = STATS_ENDINGS;
}
else if (is_action(ACTION_MENU_DOWN, ACTION_TRIGGERED))
{
if (++statsMenuCurrentSelection >= STATS_ITEM_COUNT) statsMenuCurrentSelection = STATS_COLLECTION;
else if (previous == STATS_ENDINGS && isSecretsLocked) statsMenuCurrentSelection = STATS_BESTIARY;
}
else if (is_action(ACTION_MENU_BACK, ACTION_TRIGGERED)) menus.pop_menu();
if (statsMenuCurrentSelection == STATS_SECRETS && isSecretsLocked) statsMenuCurrentSelection = STATS_COLLECTION;
}
// Opens the secrets page on the first unlocked achievement, if any, reporting every unlocked one to Steam: the
// picture appears with the strip's outer icons hidden.
void Menu_Stats::secrets_open()
{
PersistentGameData& data = g_manager->persistentGameData;
int count = (int)achievementIds.size();
selectedAchievement = 0;
while (selectedAchievement < count && !data.Unlocked(achievementIds[selectedAchievement])) selectedAchievement++;
if (selectedAchievement >= count) return;
for (int id = 1; id < ACHIEVEMENT_COUNT; id++)
if (data.achievements[id]) data.UnlockSteamAchievement(id);
isAchievementScreenVisible = true;
achievementsSprite.Play(ANIMATION_APPEAR);
secret_picture_set(selectedAchievement);
achievementScrollThruListPosition = 0;
achievementMiniSprites[0].color = alpha_color(0);
achievementMiniSprites[SECRET_ICON_COUNT - 1].color = alpha_color(0);
achievementMiniSprites[1].color = alpha_color(1);
achievementMiniSprites[SECRET_ICON_COUNT - 2].color = alpha_color(1);
}
// Shows the picture of a listed achievement, or the locked picture while it is locked.
void Menu_Stats::secret_picture_set(int listIndex)
{
int id = achievementIds[listIndex];
achievementsSprite.ReplaceSpritesheet(PICTURE_LAYER, g_manager->persistentGameData.Unlocked(id) ? achievementGfx[id]
: LOCKED_SHEET);
achievementsSprite.LoadGraphics();
}
// 009ee2a0 Steps the page every other frame, or every frame past a second held, faster the longer left or right is
// held. Back closes it. Left and right turn to the previous and next achievement, the picture disappearing the
// matching way. The turn then finishes, the strip scrolls and its icons follow the selection.
void Menu_Stats::UpdateSecrets()
{
if ((g_manager->framecount & 1) == 0 || holdInputTimer > FAST_HOLD)
{
float speed = 1;
if (holdInputTimer > FASTER_HOLD)
speed = std::min((float)(holdInputTimer - FASTER_HOLD) / SPEED_RAMP_FRAMES + 1, SPEED_MAX);
achievementsSprite.Update();
for (ANM2& icon : achievementMiniSprites) icon.Update();
cursorLeftSprite.Update();
cursorRightSprite.Update();
achievementsSprite.playbackSpeed = speed;
for (ANM2& icon : achievementMiniSprites) icon.playbackSpeed = speed;
cursorLeftSprite.playbackSpeed = speed;
cursorRightSprite.playbackSpeed = speed;
scrollSpeed = holdInputTimer > FAST_HOLD ? speed + speed : speed;
}
if (is_action(ACTION_MENU_BACK, ACTION_TRIGGERED))
{
achievementsSprite.Play(ANIMATION_DISAPPEAR);
isAchievementScreenVisible = false;
achievementScrollInPosition = SCROLL_IN_START;
}
else if (is_action(ACTION_MENU_LEFT, ACTION_PRESSED))
{
holdInputTimer++;
if (achievementsSprite.IsPlaying(ANIMATION_IDLE)) achievementsSprite.Play(ANIMATION_DISAPPEAR);
}
else if (is_action(ACTION_MENU_RIGHT, ACTION_PRESSED))
{
holdInputTimer++;
if (achievementsSprite.IsPlaying(ANIMATION_IDLE)) achievementsSprite.Play(ANIMATION_DISAPPEAR_NEXT);
}
else holdInputTimer = 0;
if (achievementsSprite.animState.animData && !achievementsSprite.animState.isPlaying) secret_animation_end();
secret_scroll_update();
secret_icons_update();
}
// Once the picture's animation ends: after disappearing it appears again with the previous or next achievement's
// picture, and after appearing the selection moves to it (when the strip scrolled) and the strip settles.
void Menu_Stats::secret_animation_end()
{
std::string animation = achievementsSprite.GetAnimation();
int count = (int)achievementIds.size();
if (animation == ANIMATION_DISAPPEAR || animation == ANIMATION_DISAPPEAR_NEXT)
{
if (!isAchievementScreenVisible) return;
bool isNext = animation == ANIMATION_DISAPPEAR_NEXT;
achievementsSprite.Play(isNext ? ANIMATION_APPEAR_NEXT : ANIMATION_APPEAR);
secret_picture_set(isNext ? (selectedAchievement + 1 < count ? selectedAchievement + 1 : 0)
: (selectedAchievement > 0 ? selectedAchievement - 1 : count - 1));
return;
}
if (animation != ANIMATION_APPEAR && animation != ANIMATION_APPEAR_NEXT) return;
if (achievementScrollThruListPosition != 0 && animation == ANIMATION_APPEAR)
selectedAchievement = selectedAchievement > 0 ? selectedAchievement - 1 : count - 1;
else if (achievementScrollThruListPosition != 0)
selectedAchievement = selectedAchievement + 1 < count ? selectedAchievement + 1 : 0;
achievementsSprite.Play(ANIMATION_IDLE);
achievementScrollThruListPosition = 0;
achievementMiniSprites[0].color = alpha_color(0);
achievementMiniSprites[SECRET_ICON_COUNT - 1].color = alpha_color(0);
achievementMiniSprites[1].color = alpha_color(1);
achievementMiniSprites[SECRET_ICON_COUNT - 2].color = alpha_color(1);
}
// Scrolls the page in from below, then the strip by an icon while the picture turns: left while turning to the next
// achievement, right to the previous, fading the icons at either end.
void Menu_Stats::secret_scroll_update()
{
if (achievementScrollInPosition > SCROLL_IN_END)
{
achievementScrollInPosition -= SCROLL_IN_STEP;
return;
}
float& scroll = achievementScrollThruListPosition;
float maxStep = scrollSpeed + scrollSpeed;
bool isTurningNext =
achievementsSprite.IsPlaying(ANIMATION_DISAPPEAR_NEXT) || achievementsSprite.IsPlaying(ANIMATION_APPEAR_NEXT);
bool isTurningBack =
achievementsSprite.IsPlaying(ANIMATION_DISAPPEAR) || achievementsSprite.IsPlaying(ANIMATION_APPEAR);
if (isTurningNext && scroll > -ICON_SPACING)
{
scroll -= std::min(scroll + ICON_SPACING, maxStep);
achievementMiniSprites[1].color = alpha_color(scroll / ICON_SPACING + 1);
achievementMiniSprites[SECRET_ICON_COUNT - 1].color = alpha_color(-scroll / ICON_SPACING);
}
if (isTurningBack && scroll < ICON_SPACING)
{
scroll += std::min(ICON_SPACING - scroll, maxStep);
achievementMiniSprites[0].color = alpha_color(scroll / ICON_SPACING);
achievementMiniSprites[SECRET_ICON_COUNT - 2].color = alpha_color(1 - scroll / ICON_SPACING);
}
}
// Plays each icon of the strip the achievement four before to four after the selection, wrapping: its random icon,
// unlocked or locked, the middle one selected while the strip rests.
void Menu_Stats::secret_icons_update()
{
int count = (int)achievementIds.size();
for (int slot = 0; slot < SECRET_ICON_COUNT; slot++)
{
int index = slot + selectedAchievement - SELECTED_ICON;
if (index < 0) index += count;
if (index >= count) index -= count;
int id = achievementIds[index];
bool isSelected = achievementScrollThruListPosition == 0 && slot == SELECTED_ICON;
std::string animation =
std::format("Icon{}{}{}", iconIds[id], g_manager->persistentGameData.Unlocked(id) ? "Unlocked" : "Locked",
isSelected ? "Selected" : "");
achievementMiniSprites[slot].Play(animation, isSelected ? PLAY_UNFORCED : PLAY_FORCED);
}
}
// 009ed5a0 Draws a number right to left from a point with the digit layer, each digit a step left and up.
void Menu_Stats::number_render(uint32_t value, Vector at)
{
statsMenuSprite.GetLayer(DIGIT_LAYER)->isVisible = true;
do
{
statsMenuSprite.SetLayerFrame(DIGIT_LAYER, (int)(value % DIGIT_BASE));
statsMenuSprite.RenderLayer(DIGIT_LAYER, {at});
at = vector_make(at.x - DIGIT_STEP_X, at.y - DIGIT_STEP_Y);
value /= DIGIT_BASE;
} while (value != 0);
}
// 009ecdd0 Draws the menu and its counts. The secrets page darkens the screen under the picture, the selection's
// place in the list, the icon strip and its cursors.
void Menu_Stats::Render()
{
PersistentGameData& data = g_manager->persistentGameData;
Vector origin = g_menuManager->menu_draw_get(position).position;
int counts[] = {countDeaths,
countItemsInCollection,
countMomKills,
countTotalAchievements,
data.eventCounters[EVENT_COUNTER_BEST_STREAK],
data.eventCounters[EVENT_COUNTER_ONLINE_BEST_STREAK]};
statsMenuSprite.GetLayer(DIGIT_LAYER)->isVisible = false;
statsMenuSprite.Render({origin});
for (size_t i = 0; i < std::size(counts); i++)
number_render((uint32_t)counts[i],
vector_make(origin.x + NUMBERS_X, origin.y + NUMBERS_Y + (float)i * NUMBER_LINE_HEIGHT));
if (!isAchievementScreenVisible) return;
render_color_shader_pass(KAGE::Graphics::color_quad_rect(
vector_make(0, 0), vector_make(g_screenWidth, g_screenHeight), kcolor_make(0, 0, 0, 0.5f)));
achievementsSprite.Render({vector_make(origin.x + PICTURE_X, origin.y + PICTURE_Y)});
Font& font = g_manager->font1_TeamMeatEx10;
FontSettings settings;
std::string place = std::format("{}/{}", selectedAchievement + 1, achievementIds.size());
float placeX = origin.x + PICTURE_X - (float)font.GetStringWidth(place) * 0.5f;
font.DrawString(place, vector_make(placeX, origin.y + COUNT_Y), vector_make(1, 1), kcolor_make(0.8f, 0.8f, 0.8f, 1),
settings);
float stripY = origin.y + achievementScrollInPosition;
for (int i = 0; i < SECRET_ICON_COUNT; i++)
achievementMiniSprites[i].Render(
{vector_make((float)i * ICON_SPACING + achievementScrollThruListPosition + ICONS_X + origin.x, stripY)});
cursorLeftSprite.Render({vector_make(origin.x + CURSOR_LEFT_X, stripY)});
cursorRightSprite.Render({vector_make(origin.x + CURSOR_RIGHT_X, stripY)});
}