Development Merge 2025.10.18 #4
@@ -7,6 +7,9 @@ public class ActiveBehaviourCollectorOrdered<T> : ActiveBehaviourCollector<T> wh
 | 
			
		||||
{
 | 
			
		||||
    private readonly Event<IBehaviour, IBehaviour.PriorityChangedArguments>.EventHandler delegateOnPriorityChanged = null!;
 | 
			
		||||
 | 
			
		||||
    private readonly LinkedList<T> behaviours = new();
 | 
			
		||||
    private readonly List<T> sortList = [];
 | 
			
		||||
 | 
			
		||||
    private IComparer<T>? _sortBy = null;
 | 
			
		||||
    public IComparer<T>? SortBy
 | 
			
		||||
    {
 | 
			
		||||
@@ -16,43 +19,81 @@ public class ActiveBehaviourCollectorOrdered<T> : ActiveBehaviourCollector<T> wh
 | 
			
		||||
            _sortBy = value;
 | 
			
		||||
 | 
			
		||||
            if (value is not null)
 | 
			
		||||
                activeBehaviours.Sort(value);
 | 
			
		||||
                Sort(value);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public override int Count => behaviours.Count;
 | 
			
		||||
 | 
			
		||||
    public override T this[Index index]
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            int actualIndex = index.IsFromEnd
 | 
			
		||||
                ? behaviours.Count - index.Value
 | 
			
		||||
                : index.Value;
 | 
			
		||||
 | 
			
		||||
            if (actualIndex < 0 || actualIndex >= behaviours.Count)
 | 
			
		||||
                throw new IndexOutOfRangeException();
 | 
			
		||||
 | 
			
		||||
            int currentIndex = 0;
 | 
			
		||||
            foreach (T item in behaviours)
 | 
			
		||||
                if (currentIndex++ == actualIndex)
 | 
			
		||||
                    return item;
 | 
			
		||||
 | 
			
		||||
            throw new IndexOutOfRangeException();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override bool RemoveBehaviour(T tBehaviour) => behaviours.Remove(tBehaviour);
 | 
			
		||||
    protected override void AddBehaviour(T behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        if (SortBy is null)
 | 
			
		||||
        {
 | 
			
		||||
            activeBehaviours.Add(behaviour);
 | 
			
		||||
            behaviours.AddLast(behaviour);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int insertionIndex = activeBehaviours.BinarySearch(behaviour, SortBy);
 | 
			
		||||
        LinkedListNode<T>? insertionNode = FindInsertionNodeFor(behaviour, SortBy);
 | 
			
		||||
 | 
			
		||||
        if (insertionIndex < 0)
 | 
			
		||||
            insertionIndex = ~insertionIndex;
 | 
			
		||||
 | 
			
		||||
        activeBehaviours.Insert(insertionIndex, behaviour);
 | 
			
		||||
        if (insertionNode is not null)
 | 
			
		||||
            behaviours.AddAfter(insertionNode, behaviour);
 | 
			
		||||
        else
 | 
			
		||||
            behaviours.AddLast(behaviour);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnBehaviourAdd(IBehaviour behaviour)
 | 
			
		||||
    private LinkedListNode<T>? FindInsertionNodeFor(T behaviour, IComparer<T> sortBy)
 | 
			
		||||
    {
 | 
			
		||||
        behaviour.OnPriorityChanged.AddListener(delegateOnPriorityChanged);
 | 
			
		||||
        LinkedListNode<T>? node = behaviours.First;
 | 
			
		||||
 | 
			
		||||
        while (node is not null)
 | 
			
		||||
        {
 | 
			
		||||
            int compareValue = sortBy.Compare(node.Value, behaviour);
 | 
			
		||||
            if (compareValue < 0)
 | 
			
		||||
                return node.Previous;
 | 
			
		||||
            node = node.Next;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnBehaviourRemove(IBehaviour behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        behaviour.OnPriorityChanged.RemoveListener(delegateOnPriorityChanged);
 | 
			
		||||
    }
 | 
			
		||||
    protected override void OnBehaviourAdd(IBehaviour behaviour) => behaviour.OnPriorityChanged.AddListener(delegateOnPriorityChanged);
 | 
			
		||||
    protected override void OnBehaviourRemove(IBehaviour behaviour) => behaviour.OnPriorityChanged.RemoveListener(delegateOnPriorityChanged);
 | 
			
		||||
 | 
			
		||||
    private void OnPriorityChanged(IBehaviour sender, IBehaviour.PriorityChangedArguments args)
 | 
			
		||||
    {
 | 
			
		||||
        T behaviour = (T)sender;
 | 
			
		||||
        activeBehaviours.Remove(behaviour);
 | 
			
		||||
        behaviours.Remove(behaviour);
 | 
			
		||||
        AddBehaviour(behaviour);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void Sort(IComparer<T> comparer)
 | 
			
		||||
    {
 | 
			
		||||
        sortList.Sort(comparer);
 | 
			
		||||
        behaviours.Clear();
 | 
			
		||||
        foreach (T item in sortList)
 | 
			
		||||
            behaviours.AddLast(item);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ActiveBehaviourCollectorOrdered() => delegateOnPriorityChanged = OnPriorityChanged;
 | 
			
		||||
    public ActiveBehaviourCollectorOrdered(IUniverse universe, Comparison<T> sortBy) : base(universe)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,13 @@ using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
namespace Engine.Core;
 | 
			
		||||
 | 
			
		||||
public class BehaviourCollectorOrdered<T> : BehaviourCollector<T> where T : class
 | 
			
		||||
public class BehaviourCollectorOrdered<T> : BehaviourCollectorBase<T> where T : class
 | 
			
		||||
{
 | 
			
		||||
    private readonly Event<IBehaviour, IBehaviour.PriorityChangedArguments>.EventHandler delegateOnPriorityChanged = null!;
 | 
			
		||||
 | 
			
		||||
    private readonly LinkedList<T> behaviours = new();
 | 
			
		||||
    private readonly List<T> sortList = [];
 | 
			
		||||
 | 
			
		||||
    private IComparer<T>? _sortBy = null;
 | 
			
		||||
    public IComparer<T>? SortBy
 | 
			
		||||
    {
 | 
			
		||||
@@ -16,35 +19,64 @@ public class BehaviourCollectorOrdered<T> : BehaviourCollector<T> where T : clas
 | 
			
		||||
            _sortBy = value;
 | 
			
		||||
 | 
			
		||||
            if (value is not null)
 | 
			
		||||
                behaviours.Sort(value);
 | 
			
		||||
                Sort(value);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public override int Count => behaviours.Count;
 | 
			
		||||
    public override T this[Index index]
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            int actualIndex = index.IsFromEnd
 | 
			
		||||
                ? behaviours.Count - index.Value
 | 
			
		||||
                : index.Value;
 | 
			
		||||
 | 
			
		||||
            if (actualIndex < 0 || actualIndex >= behaviours.Count)
 | 
			
		||||
                throw new IndexOutOfRangeException();
 | 
			
		||||
 | 
			
		||||
            int currentIndex = 0;
 | 
			
		||||
            foreach (T item in behaviours)
 | 
			
		||||
                if (currentIndex++ == actualIndex)
 | 
			
		||||
                    return item;
 | 
			
		||||
 | 
			
		||||
            throw new IndexOutOfRangeException();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override bool RemoveBehaviour(T tBehaviour) => behaviours.Remove(tBehaviour);
 | 
			
		||||
    protected override void AddBehaviour(T behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        if (SortBy is null)
 | 
			
		||||
        {
 | 
			
		||||
            behaviours.Add(behaviour);
 | 
			
		||||
            behaviours.AddLast(behaviour);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int insertionIndex = behaviours.BinarySearch(behaviour, SortBy);
 | 
			
		||||
        LinkedListNode<T>? insertionNode = FindInsertionNodeFor(behaviour, SortBy);
 | 
			
		||||
 | 
			
		||||
        if (insertionIndex < 0)
 | 
			
		||||
            insertionIndex = ~insertionIndex;
 | 
			
		||||
 | 
			
		||||
        behaviours.Insert(insertionIndex, behaviour);
 | 
			
		||||
        if (insertionNode is not null)
 | 
			
		||||
            behaviours.AddAfter(insertionNode, behaviour);
 | 
			
		||||
        else
 | 
			
		||||
            behaviours.AddLast(behaviour);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnBehaviourAdd(IBehaviour behaviour)
 | 
			
		||||
    private LinkedListNode<T>? FindInsertionNodeFor(T behaviour, IComparer<T> sortBy)
 | 
			
		||||
    {
 | 
			
		||||
        behaviour.OnPriorityChanged.AddListener(delegateOnPriorityChanged);
 | 
			
		||||
        LinkedListNode<T>? node = behaviours.First;
 | 
			
		||||
 | 
			
		||||
        while (node is not null)
 | 
			
		||||
        {
 | 
			
		||||
            int compareValue = sortBy.Compare(node.Value, behaviour);
 | 
			
		||||
            if (compareValue < 0)
 | 
			
		||||
                return node.Previous;
 | 
			
		||||
            node = node.Next;
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnBehaviourRemove(IBehaviour behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        behaviour.OnPriorityChanged.RemoveListener(delegateOnPriorityChanged);
 | 
			
		||||
    }
 | 
			
		||||
    protected override void OnBehaviourAdd(IBehaviour behaviour) => behaviour.OnPriorityChanged.AddListener(delegateOnPriorityChanged);
 | 
			
		||||
    protected override void OnBehaviourRemove(IBehaviour behaviour) => behaviour.OnPriorityChanged.RemoveListener(delegateOnPriorityChanged);
 | 
			
		||||
 | 
			
		||||
    private void OnPriorityChanged(IBehaviour sender, IBehaviour.PriorityChangedArguments args)
 | 
			
		||||
    {
 | 
			
		||||
@@ -53,6 +85,14 @@ public class BehaviourCollectorOrdered<T> : BehaviourCollector<T> where T : clas
 | 
			
		||||
        AddBehaviour(behaviour);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void Sort(IComparer<T> comparer)
 | 
			
		||||
    {
 | 
			
		||||
        sortList.Sort(comparer);
 | 
			
		||||
        behaviours.Clear();
 | 
			
		||||
        foreach (T item in sortList)
 | 
			
		||||
            behaviours.AddLast(item);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public BehaviourCollectorOrdered() => delegateOnPriorityChanged = OnPriorityChanged;
 | 
			
		||||
    public BehaviourCollectorOrdered(IUniverse universe, Comparison<T> sortBy) : base(universe)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user