refactor: TileRegistry replaces parallel tile/view collections
This commit is contained in:
40
Assets/Scripts/Infrastructure/Unity/TileRegistry.cs
Normal file
40
Assets/Scripts/Infrastructure/Unity/TileRegistry.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Core.Domain;
|
||||
|
||||
namespace Infrastructure.Unity
|
||||
{
|
||||
public class TileRegistry
|
||||
{
|
||||
private readonly List<Tile> _tiles = new();
|
||||
private readonly Dictionary<string, TileViewAdapter> _views = new();
|
||||
|
||||
public IReadOnlyList<Tile> AllTiles => _tiles;
|
||||
|
||||
public void Register(Tile tile, TileViewAdapter view = null)
|
||||
{
|
||||
_tiles.Add(tile);
|
||||
if (view != null) _views[tile.Id] = view;
|
||||
}
|
||||
|
||||
public bool TryGetView(string tileId, out TileViewAdapter view)
|
||||
=> _views.TryGetValue(tileId, out view);
|
||||
|
||||
public List<Tile> FindTiles(Predicate<Tile> predicate)
|
||||
=> _tiles.FindAll(predicate);
|
||||
|
||||
public List<List<TileViewAdapter>> GroupViewsByFloor(int floorCount)
|
||||
{
|
||||
var floors = new List<List<TileViewAdapter>>(floorCount);
|
||||
for (var i = 0; i < floorCount; i++) floors.Add(new List<TileViewAdapter>());
|
||||
|
||||
foreach (var tile in _tiles)
|
||||
{
|
||||
if (tile.Floor < floorCount && _views.TryGetValue(tile.Id, out var view))
|
||||
floors[tile.Floor].Add(view);
|
||||
}
|
||||
|
||||
return floors;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user