2022-06-26 02:43:05 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using FluentValidation;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Gallium;
|
2022-06-26 02:43:05 +00:00
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
namespace MfGames.Nitride.Entities;
|
2022-06-26 02:43:05 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Links a series of entities together in some manner. This assumes that
|
|
|
|
/// the entities coming into the operation are already ordered.
|
|
|
|
/// </summary>
|
|
|
|
[WithProperties]
|
|
|
|
public partial class LinkEntitySequence : OperationBase, IResolvingOperation
|
|
|
|
{
|
|
|
|
private readonly IValidator<LinkEntitySequence> validator;
|
|
|
|
|
|
|
|
public LinkEntitySequence(IValidator<LinkEntitySequence> validator)
|
|
|
|
{
|
|
|
|
this.validator = validator;
|
2022-07-09 04:52:10 +00:00
|
|
|
|
|
|
|
this.CreateSequenceIndex = (
|
|
|
|
list,
|
|
|
|
index) => new EntitySequence(list, index);
|
|
|
|
|
|
|
|
this.AddSequenceIndex = (
|
|
|
|
entity,
|
|
|
|
sequence) => entity.Add(sequence);
|
2022-06-26 02:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a callback to add a sequence into a given entity.
|
|
|
|
/// </summary>
|
|
|
|
public Func<Entity, EntitySequence, Entity> AddSequenceIndex { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the function used to create the sequence index.
|
|
|
|
/// </summary>
|
2023-01-15 00:19:42 +00:00
|
|
|
public Func<IReadOnlyList<Entity>, int, EntitySequence> CreateSequenceIndex
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
set;
|
|
|
|
}
|
2022-06-26 02:43:05 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override IEnumerable<Entity> Run(IEnumerable<Entity> input)
|
|
|
|
{
|
|
|
|
// Make sure everything is good.
|
|
|
|
this.validator.ValidateAndThrow(this);
|
|
|
|
|
|
|
|
// We pull all the entities into a list so we can walk through them
|
|
|
|
// more than once along with index access. We also don't reorder the
|
|
|
|
// list since we assumed it has been ordered already.
|
|
|
|
var list = input.ToList();
|
|
|
|
|
|
|
|
// Go through the list and assign each entity in order with a unique
|
|
|
|
// copy of the sequence.
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
|
|
{
|
|
|
|
Entity entity = list[i];
|
2023-01-15 00:19:42 +00:00
|
|
|
EntitySequence index = this.CreateSequenceIndex(
|
|
|
|
list.AsReadOnly(),
|
|
|
|
i);
|
2022-06-26 02:43:05 +00:00
|
|
|
|
|
|
|
yield return this.AddSequenceIndex(entity, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|