BREAKING CHANGE: removed IUniverseObject.SetParent and made Parent property settable

This commit is contained in:
Syntriax 2025-08-05 10:46:25 +03:00
parent 11612ff0db
commit 3452194941
4 changed files with 40 additions and 43 deletions

View File

@ -47,7 +47,7 @@ public interface IUniverseObject : IEntity, IActive, INameable, IHasBehaviourCon
/// <summary>
/// The parent <see cref="IUniverseObject"/> of the <see cref="IUniverseObject"/>.
/// </summary>
IUniverseObject? Parent { get; }
IUniverseObject? Parent { get; set; }
/// <summary>
/// The <see cref="IUniverseObject"/>s that have this <see cref="IUniverseObject"/> as their <see cref="Parent"/>.
@ -75,12 +75,6 @@ public interface IUniverseObject : IEntity, IActive, INameable, IHasBehaviourCon
/// </returns>
internal bool ExitUniverse();
/// <summary>
/// Sets the parent <see cref="IUniverseObject"/> of this <see cref="IUniverseObject"/>.
/// </summary>
/// <param name="universeObject">The parent <see cref="IUniverseObject"/> to set.</param>
void SetParent(IUniverseObject? universeObject);
/// <summary>
/// Adds a child <see cref="IUniverseObject"/> to this <see cref="IUniverseObject"/>.
/// </summary>

View File

@ -12,7 +12,7 @@ public static class UniverseObjectExtensions
if (!string.IsNullOrWhiteSpace(name))
universeObject.Name = name;
if (parent is not null)
universeObject.SetParent(parent);
universeObject.Parent = parent;
return universeObject;
}

View File

@ -83,7 +83,7 @@ public class Universe : BaseEntity, IUniverse
public void Remove(IUniverseObject universeObject)
{
universeObject.SetParent(null);
universeObject.Parent = null;
RemoveIncursive(universeObject);
}

View File

@ -1,4 +1,3 @@
using System.Collections;
using System.Collections.Generic;
namespace Syntriax.Engine.Core;
@ -21,8 +20,8 @@ public class UniverseObject : BaseEntity, IUniverseObject
private IBehaviourController _behaviourController = null!;
private bool _isActive = false;
private readonly List<IUniverseObject> _children = [];
private IUniverseObject? _parent = null;
public IUniverseObject? Parent { get; private set; } = null;
public IReadOnlyList<IUniverseObject> Children => _children;
public IBehaviourController BehaviourController => _behaviourController;
public IUniverse Universe => _universe;
@ -42,6 +41,40 @@ public class UniverseObject : BaseEntity, IUniverseObject
}
}
public IUniverseObject? Parent
{
get => _parent;
set
{
if (value == this)
throw new Exceptions.AssignFailedException($"{Name} can not parent itself");
if (_parent == value)
return;
IUniverseObject? previousParent = Parent;
if (previousParent is not null)
{
previousParent.RemoveChild(this);
previousParent.OnActiveChanged.RemoveListener(OnParentActiveChanged);
}
_parent = value;
if (value is not null)
{
if (value.IsInUniverse && !IsInUniverse)
value.Universe.Register(this);
value.AddChild(this);
value.OnActiveChanged.AddListener(OnParentActiveChanged);
}
UpdateActive();
OnParentChanged?.Invoke(this, new(previousParent, value));
}
}
protected virtual void OnEnteringUniverse(IUniverse universe) { }
bool IUniverseObject.EnterUniverse(IUniverse universe)
{
@ -67,43 +100,13 @@ public class UniverseObject : BaseEntity, IUniverseObject
return true;
}
public void SetParent(IUniverseObject? parent)
{
if (parent == this)
throw new Exceptions.AssignFailedException($"{Name} can not parent itself");
if (Parent == parent)
return;
IUniverseObject? previousParent = Parent;
if (previousParent is not null)
{
previousParent.RemoveChild(this);
previousParent.OnActiveChanged.RemoveListener(OnParentActiveChanged);
}
Parent = parent;
if (parent is not null)
{
if (parent.IsInUniverse && !IsInUniverse)
parent.Universe.Register(this);
parent.AddChild(this);
parent.OnActiveChanged.AddListener(OnParentActiveChanged);
}
UpdateActive();
OnParentChanged?.Invoke(this, new(previousParent, parent));
}
public void AddChild(IUniverseObject parent)
{
if (_children.Contains(parent))
return;
_children.Add(parent);
parent.SetParent(this);
parent.Parent = this;
OnChildrenAdded?.Invoke(this, new(parent));
}
@ -112,7 +115,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
if (!_children.Remove(child))
return;
child.SetParent(null);
child.Parent = null;
OnChildrenRemoved?.Invoke(this, new(child));
}