Skip to content

Commit

Permalink
UI: book functionality (#289)
Browse files Browse the repository at this point in the history
* UI: book functionality

* Add fade

* Requested changes

* Replace FindIndex with IndexOf
  • Loading branch information
AcipenserSturio authored Jan 27, 2024
1 parent 7aa2faa commit a6f1856
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 7 deletions.
5 changes: 5 additions & 0 deletions zzre/game/components/ui/ScrBookMenu.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
using zzio.db;

namespace zzre.game.components.ui;

public struct ScrBookMenu
{
public Inventory Inventory;
public FairyRow[] Fairies;
public Dictionary<components.ui.ElementId, FairyRow> FairyButtons;
public DefaultEcs.Entity Sidebar;
public DefaultEcs.Entity Crosshair;
}
144 changes: 137 additions & 7 deletions zzre/game/systems/ui/ScrBookMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ public partial class ScrBookMenu : BaseScreen<components.ui.ScrBookMenu, message
{
private readonly MappedDB db;

private static readonly UID[] UIDStatNames = new UID[]
{
new(0x3D26ACB1), // Hit Points
new(0xAB46B8B1), // Dexterity
new(0xB031B8B1), // Jump Ability
new(0xB6CA5A11) // Special
};
private static readonly UID[] UIDClassNames = new UID[]
{
new(0x448DD8A1), // Nature
new(0x30D5D8A1), // Air
new(0xC15AD8A1), // Water
new(0x6EE2D8A1), // Light
new(0x44AAD8A1), // Energy
new(0xEC31D8A1), // Psi
new(0xAD78D8A1), // Stone
new(0x6483DCA1), // Ice
new(0x8EC9DCA1), // Fire
new(0x8313DCA1), // Dark
new(0xC659DCA1), // Chaos
new(0x3CE1DCA1) // Metal
};
private static readonly UID UIDEvol = new (0x69226721); // Evolution at level

public ScrBookMenu(ITagContainer diContainer) : base(diContainer, BlockFlags.All)
{
db = diContainer.GetTag<MappedDB>();
Expand All @@ -27,8 +51,15 @@ protected override void HandleOpen(in messages.ui.OpenBookMenu message)

var entity = World.CreateEntity();
entity.Set<components.ui.ScrBookMenu>();
ref var bookMenu = ref entity.Get<components.ui.ScrBookMenu>();
bookMenu.Inventory = inventory;
ref var book = ref entity.Get<components.ui.ScrBookMenu>();
book.Inventory = inventory;

preload.CreateFullBackOverlay(entity);

book.Fairies = db.Fairies.OrderBy(fairyRow => fairyRow.CardId.EntityId).ToArray();
book.FairyButtons = new Dictionary<components.ui.ElementId, FairyRow>();
book.Sidebar = default;
book.Crosshair = default;

preload.CreateImage(entity)
.With(-new Vector2(320, 240))
Expand All @@ -42,27 +73,112 @@ protected override void HandleOpen(in messages.ui.OpenBookMenu message)
.Build();

CreateTopButtons(preload, entity, inventory, IDOpenFairybook);
CreateFairyButtons(preload, entity, inventory);
CreateFairyButtons(preload, entity, inventory, ref book);
}

private void CreateFairyButtons(UIPreloader preload, in DefaultEcs.Entity entity, Inventory inventory)
private void CreateFairyButtons(UIPreloader preload, in DefaultEcs.Entity entity, Inventory inventory, ref components.ui.ScrBookMenu book)
{
var fairies = db.Fairies.OrderBy(fairyRow => fairyRow.CardId.EntityId).ToArray();
var fairies = book.Fairies;
for (int i = 0; i < fairies.Length; i++)
{
if (inventory.Contains(fairies[i].CardId))
{
var element = new components.ui.ElementId(1 + i);
preload.CreateButton(entity)
var button = preload.CreateButton(entity)
.With(element)
.With(Mid + new Vector2(226 + 45 * (i % 9), 66 + 45 * (i / 9)))
.With(Mid + FairyButtonPos(i))
.With(new components.ui.ButtonTiles(fairies[i].CardId.EntityId))
.With(preload.Wiz000)
.Build();
book.FairyButtons.Add(element, fairies[i]);

// In the original engine, only the first fairy is checked for isInUse
// This is an intentional bug fix
if (inventory.Fairies.Any(c => fairies[i].CardId == c.cardId && c.isInUse)) {
preload.CreateImage(entity)
.With(Mid + FairyButtonPos(i))
.With(preload.Inf000, 16)
.WithRenderOrder(-1)
.Build();
}
}
}
}

private DefaultEcs.Entity CreateSidebar(UIPreloader preload, in DefaultEcs.Entity parent, FairyRow fairyRow, ref components.ui.ScrBookMenu book)
{
var entity = World.CreateEntity();
entity.Set(new components.Parent(parent));

var fairyI = Array.IndexOf(book.Fairies, fairyRow) + 1;

var element = new components.ui.ElementId(0);
preload.CreateButton(entity)
.With(element)
.With(Mid + new Vector2(160, 218))
.With(new components.ui.ButtonTiles(fairyRow.CardId.EntityId))
.With(preload.Wiz000)
.Build();

preload.CreateLabel(entity)
.With(Mid + new Vector2(21, 57))
.WithText($"#{fairyI} {fairyRow.Name}")
.With(preload.Fnt000)
.Build();

preload.CreateImage(entity)
.With(Mid + new Vector2(22, 81))
.With(preload.Cls000, (int)fairyRow.Class0)
.Build();

preload.CreateLabel(entity)
.With(Mid + new Vector2(36, 80))
.WithText(db.GetText(UIDClassNames[(int)fairyRow.Class0-1]).Text)
.With(preload.Fnt002)
.Build();

if (fairyRow.EvolVar != -1)
preload.CreateLabel(entity)
.With(Mid + new Vector2(22, 246))
.WithText($"{db.GetText(UIDEvol).Text} {fairyRow.EvolVar}")
.With(preload.Fnt002)
.Build();

CreateStat(preload, entity, 0, Math.Min(500, fairyRow.MHP) / 100);
CreateStat(preload, entity, 1, fairyRow.MovSpeed + 1);
CreateStat(preload, entity, 2, fairyRow.JumpPower + 1);
CreateStat(preload, entity, 3, fairyRow.CriticalHit + 1);

const float MaxTextWidth = 190f;
preload.CreateLabel(entity)
.With(Mid + new Vector2(21, 346))
.WithText(fairyRow.Info)
.With(preload.Fnt002)
.WithLineWrap(MaxTextWidth)
.Build();

return entity;
}

private void CreateStat(UIPreloader preload, in DefaultEcs.Entity entity, int index, int value)
{
preload.CreateLabel(entity)
.With(Mid + new Vector2(21, 271 + index*17))
.WithText(db.GetText(UIDStatNames[index]).Text)
.With(preload.Fnt002)
.Build();

preload.CreateLabel(entity)
.With(Mid + new Vector2(111, 266 + index*17))
.WithText(string.Concat(Enumerable.Repeat("{1017}", value)) + string.Concat(Enumerable.Repeat("{1018}", 5-value)))
.With(preload.Fnt001)
.Build();
}

private Vector2 FairyButtonPos(int fairyI) {
return new Vector2(226 + 45 * (fairyI % 9), 66 + 45 * (fairyI / 9));
}

protected override void Update(float timeElapsed, in DefaultEcs.Entity entity, ref components.ui.ScrBookMenu bookMenu)
{
base.Update(timeElapsed, entity, ref bookMenu);
Expand All @@ -71,6 +187,20 @@ protected override void Update(float timeElapsed, in DefaultEcs.Entity entity, r
private void HandleElementDown(DefaultEcs.Entity entity, components.ui.ElementId id)
{
var bookMenuEntity = Set.GetEntities()[0];
ref var book = ref bookMenuEntity.Get<components.ui.ScrBookMenu>();

if (book.FairyButtons.TryGetValue(id, out var fairyRow))
{
book.Sidebar.Dispose();
book.Sidebar = CreateSidebar(preload, entity, fairyRow, ref book);
book.Crosshair.Dispose();
book.Crosshair = preload.CreateImage(entity)
.With(Mid + new Vector2(-2, -2) + FairyButtonPos(book.Fairies.IndexOf(fairyRow)))
.With(preload.Dnd000, 0)
.WithRenderOrder(-2)
.Build();
}

if (id == IDOpenDeck)
{
bookMenuEntity.Dispose();
Expand Down

0 comments on commit a6f1856

Please sign in to comment.