BREAKING CHANGE: removed IUniverseObject.SetParent and made Parent property settable
This commit is contained in:
parent
11612ff0db
commit
3452194941
@ -47,7 +47,7 @@ public interface IUniverseObject : IEntity, IActive, INameable, IHasBehaviourCon
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The parent <see cref="IUniverseObject"/> of the <see cref="IUniverseObject"/>.
|
/// The parent <see cref="IUniverseObject"/> of the <see cref="IUniverseObject"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
IUniverseObject? Parent { get; }
|
IUniverseObject? Parent { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The <see cref="IUniverseObject"/>s that have this <see cref="IUniverseObject"/> as their <see cref="Parent"/>.
|
/// 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>
|
/// </returns>
|
||||||
internal bool ExitUniverse();
|
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>
|
/// <summary>
|
||||||
/// Adds a child <see cref="IUniverseObject"/> to this <see cref="IUniverseObject"/>.
|
/// Adds a child <see cref="IUniverseObject"/> to this <see cref="IUniverseObject"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -12,7 +12,7 @@ public static class UniverseObjectExtensions
|
|||||||
if (!string.IsNullOrWhiteSpace(name))
|
if (!string.IsNullOrWhiteSpace(name))
|
||||||
universeObject.Name = name;
|
universeObject.Name = name;
|
||||||
if (parent is not null)
|
if (parent is not null)
|
||||||
universeObject.SetParent(parent);
|
universeObject.Parent = parent;
|
||||||
return universeObject;
|
return universeObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +83,7 @@ public class Universe : BaseEntity, IUniverse
|
|||||||
|
|
||||||
public void Remove(IUniverseObject universeObject)
|
public void Remove(IUniverseObject universeObject)
|
||||||
{
|
{
|
||||||
universeObject.SetParent(null);
|
universeObject.Parent = null;
|
||||||
RemoveIncursive(universeObject);
|
RemoveIncursive(universeObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Syntriax.Engine.Core;
|
namespace Syntriax.Engine.Core;
|
||||||
@ -21,8 +20,8 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
private IBehaviourController _behaviourController = null!;
|
private IBehaviourController _behaviourController = null!;
|
||||||
private bool _isActive = false;
|
private bool _isActive = false;
|
||||||
private readonly List<IUniverseObject> _children = [];
|
private readonly List<IUniverseObject> _children = [];
|
||||||
|
private IUniverseObject? _parent = null;
|
||||||
|
|
||||||
public IUniverseObject? Parent { get; private set; } = null;
|
|
||||||
public IReadOnlyList<IUniverseObject> Children => _children;
|
public IReadOnlyList<IUniverseObject> Children => _children;
|
||||||
public IBehaviourController BehaviourController => _behaviourController;
|
public IBehaviourController BehaviourController => _behaviourController;
|
||||||
public IUniverse Universe => _universe;
|
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) { }
|
protected virtual void OnEnteringUniverse(IUniverse universe) { }
|
||||||
bool IUniverseObject.EnterUniverse(IUniverse universe)
|
bool IUniverseObject.EnterUniverse(IUniverse universe)
|
||||||
{
|
{
|
||||||
@ -67,43 +100,13 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
return true;
|
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)
|
public void AddChild(IUniverseObject parent)
|
||||||
{
|
{
|
||||||
if (_children.Contains(parent))
|
if (_children.Contains(parent))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_children.Add(parent);
|
_children.Add(parent);
|
||||||
parent.SetParent(this);
|
parent.Parent = this;
|
||||||
OnChildrenAdded?.Invoke(this, new(parent));
|
OnChildrenAdded?.Invoke(this, new(parent));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +115,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
|
|||||||
if (!_children.Remove(child))
|
if (!_children.Remove(child))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
child.SetParent(null);
|
child.Parent = null;
|
||||||
OnChildrenRemoved?.Invoke(this, new(child));
|
OnChildrenRemoved?.Invoke(this, new(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user