Aetheria/Assets/Scripts/Gameplay/AetheriaUnityPresentationEntityIndex.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2026-06-25 18:09:33 +01:00
using System;
using System.Collections.Generic;
2026-06-30 19:14:49 +01:00
public sealed class AetheriaUnityPresentationEntityIndex
2026-06-25 18:09:33 +01:00
{
private readonly Dictionary<string, Entity> _entitiesByRecordKey = new Dictionary<string, Entity>(StringComparer.Ordinal);
private readonly Dictionary<int, Entity> _entitiesByDaemonIndex = new Dictionary<int, Entity>();
2026-06-30 19:14:49 +01:00
public IReadOnlyDictionary<string, Entity> PresentationEntitiesByRecordKey => _entitiesByRecordKey;
2026-06-25 18:09:33 +01:00
2026-06-30 19:14:49 +01:00
public IReadOnlyDictionary<int, Entity> PresentationEntitiesByDaemonIndex => _entitiesByDaemonIndex;
2026-06-25 18:09:33 +01:00
public int Count => _entitiesByRecordKey.Count;
public bool ContainsRecordKey(string recordKey)
{
return !string.IsNullOrWhiteSpace(recordKey) &&
_entitiesByRecordKey.ContainsKey(recordKey);
}
public void Replace(IReadOnlyDictionary<string, Entity> entitiesByRecordKey)
{
_entitiesByRecordKey.Clear();
if (entitiesByRecordKey != null)
{
foreach (var pair in entitiesByRecordKey)
{
if (!string.IsNullOrWhiteSpace(pair.Key) && pair.Value != null)
2026-06-30 19:14:49 +01:00
{
pair.Value.DaemonRecordKey = pair.Key;
2026-06-25 18:09:33 +01:00
_entitiesByRecordKey[pair.Key] = pair.Value;
2026-06-30 19:14:49 +01:00
}
2026-06-25 18:09:33 +01:00
}
}
RebuildDaemonIndex();
}
public void RefreshDaemonIndex()
{
RebuildDaemonIndex();
}
2026-06-30 19:14:49 +01:00
public bool TryGetRecordKeyForPresentationEntity(Entity entity, out string recordKey)
2026-06-25 18:09:33 +01:00
{
recordKey = "";
if (entity == null)
return false;
2026-06-30 19:14:49 +01:00
if (!string.IsNullOrWhiteSpace(entity.DaemonRecordKey) &&
_entitiesByRecordKey.TryGetValue(entity.DaemonRecordKey, out var indexedEntity) &&
ReferenceEquals(indexedEntity, entity))
{
recordKey = entity.DaemonRecordKey;
return true;
}
2026-06-25 18:09:33 +01:00
foreach (var pair in _entitiesByRecordKey)
{
if (ReferenceEquals(pair.Value, entity))
{
recordKey = pair.Key;
return true;
}
}
return false;
}
2026-06-30 19:14:49 +01:00
public bool TryGetPresentationEntityByRecordKey(string recordKey, out Entity entity)
2026-06-25 18:09:33 +01:00
{
entity = null;
return !string.IsNullOrWhiteSpace(recordKey) &&
_entitiesByRecordKey.TryGetValue(recordKey, out entity);
}
2026-06-30 19:14:49 +01:00
public bool TryGetPresentationEntityByDaemonIndex(int daemonEntityIndex, out Entity entity)
2026-06-25 18:09:33 +01:00
{
return _entitiesByDaemonIndex.TryGetValue(daemonEntityIndex, out entity);
}
2026-06-30 19:14:49 +01:00
public bool TryGetPresentationDockingBayByRecordKey(
2026-06-25 18:09:33 +01:00
string parentRecordKey,
int dockingBayIndex,
out EquippedDockingBay dockingBay)
{
dockingBay = null;
if (dockingBayIndex < 0 ||
2026-06-30 19:14:49 +01:00
!TryGetPresentationEntityByRecordKey(parentRecordKey, out var parent) ||
2026-06-25 18:09:33 +01:00
parent?.DockingBays == null ||
dockingBayIndex >= parent.DockingBays.Count)
{
return false;
}
dockingBay = parent.DockingBays[dockingBayIndex];
return dockingBay != null;
}
private void RebuildDaemonIndex()
{
_entitiesByDaemonIndex.Clear();
foreach (var entity in _entitiesByRecordKey.Values)
{
if (entity != null && entity.DaemonEntityIndex >= 0)
_entitiesByDaemonIndex[entity.DaemonEntityIndex] = entity;
}
}
}