diff --git a/src/GroveGames.BehaviourTree/Nodes/BehaviourNode.cs b/src/GroveGames.BehaviourTree/Nodes/BehaviourNode.cs index d37c39d..53f66ad 100644 --- a/src/GroveGames.BehaviourTree/Nodes/BehaviourNode.cs +++ b/src/GroveGames.BehaviourTree/Nodes/BehaviourNode.cs @@ -15,9 +15,9 @@ public abstract class BehaviourNode : INode public NodeState State => _nodeState; public string Name => _name; - public BehaviourNode() + public BehaviourNode(string? name = null) { - _name = GetType().Name; + _name = name ?? GetType().Name; } public virtual void SetParent(IParent parent) diff --git a/src/GroveGames.BehaviourTree/Nodes/Composites/Composite.cs b/src/GroveGames.BehaviourTree/Nodes/Composites/Composite.cs index c4bfe62..49682bd 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Composites/Composite.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Composites/Composite.cs @@ -6,7 +6,7 @@ public abstract class Composite : BehaviourNode, IParent protected IReadOnlyList Children => _children; - public Composite() + public Composite(string? name = null) : base(name) { _children = []; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Composites/Parallel.cs b/src/GroveGames.BehaviourTree/Nodes/Composites/Parallel.cs index febf88d..0e37efb 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Composites/Parallel.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Composites/Parallel.cs @@ -4,7 +4,7 @@ public sealed class Parallel : Composite { private readonly ParallelPolicy _policy; - public Parallel(ParallelPolicy policy) + public Parallel(ParallelPolicy policy, string? name = null) : base(name) { _policy = policy; } @@ -55,9 +55,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Parallel(this IParent parent, ParallelPolicy policy) + public static IParent Parallel(this IParent parent, ParallelPolicy policy, string? name = null) { - var parallel = new Parallel(policy); + var parallel = new Parallel(policy, name); parent.Attach(parallel); return parallel; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Composites/Selector.cs b/src/GroveGames.BehaviourTree/Nodes/Composites/Selector.cs index 1989f25..56d593b 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Composites/Selector.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Composites/Selector.cs @@ -4,7 +4,7 @@ public sealed class Selector : Composite { private int _processingChildIndex; - public Selector() + public Selector(string? name = null) : base(name) { _processingChildIndex = 0; } @@ -59,9 +59,9 @@ public override void Abort() public static partial class ParentExtensions { - public static IParent Selector(this IParent parent) + public static IParent Selector(this IParent parent, string? name = null) { - var selector = new Selector(); + var selector = new Selector(name); parent.Attach(selector); return selector; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Composites/Sequence.cs b/src/GroveGames.BehaviourTree/Nodes/Composites/Sequence.cs index dcfe835..25cdba8 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Composites/Sequence.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Composites/Sequence.cs @@ -4,7 +4,7 @@ public sealed class Sequence : Composite { private int _processingChildIndex; - public Sequence() + public Sequence(string? name = null) : base(name) { _processingChildIndex = 0; } @@ -64,9 +64,9 @@ public override void Abort() public static partial class ParentExtensions { - public static IParent Sequence(this IParent parent) + public static IParent Sequence(this IParent parent, string? name = null) { - var sequence = new Sequence(); + var sequence = new Sequence(name); parent.Attach(sequence); return sequence; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Abort.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Abort.cs index 76ba8eb..9875dda 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Abort.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Abort.cs @@ -4,7 +4,7 @@ public class Abort : Decorator { private readonly Func _condition; - public Abort(Func condition) + public Abort(Func condition, string? name = null) : base(name) { _condition = condition; } @@ -23,9 +23,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Abort(this IParent parent, Func condition) + public static IParent Abort(this IParent parent, Func condition, string? name = null) { - var abort = new Abort(condition); + var abort = new Abort(condition, name); parent.Attach(abort); return abort; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Conditional.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Conditional.cs index 1d78f7f..d3f9d54 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Conditional.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Conditional.cs @@ -4,7 +4,7 @@ public sealed class Conditional : Decorator { private readonly Func _condition; - public Conditional(Func condition) + public Conditional(Func condition, string? name = null) : base(name) { _condition = condition; } @@ -17,9 +17,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Conditional(this IParent parent, Func condition) + public static IParent Conditional(this IParent parent, Func condition, string? name = null) { - var conditional = new Conditional(condition); + var conditional = new Conditional(condition, name); parent.Attach(conditional); return conditional; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Cooldown.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Cooldown.cs index f9e8de7..49515dd 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Cooldown.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Cooldown.cs @@ -5,7 +5,7 @@ public sealed class Cooldown : Decorator private readonly float _waitTime; private float _remainingTime; - public Cooldown(float waitTime) + public Cooldown(float waitTime, string? name = null) : base(name) { _waitTime = waitTime; _remainingTime = 0; @@ -40,9 +40,9 @@ public override void Abort() public static partial class ParentExtensions { - public static IParent Cooldown(this IParent parent, float waitTime) + public static IParent Cooldown(this IParent parent, float waitTime, string? name = null) { - var cooldown = new Cooldown(waitTime); + var cooldown = new Cooldown(waitTime, name); parent.Attach(cooldown); return cooldown; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Decorator.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Decorator.cs index 7d8a127..e7e17b1 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Decorator.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Decorator.cs @@ -4,7 +4,7 @@ public abstract class Decorator : BehaviourNode, IParent { private INode _child; - public Decorator() + public Decorator(string? name = null) : base(name) { _child = Empty; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Delayer.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Delayer.cs index 8bfbf47..5ab42b7 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Delayer.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Delayer.cs @@ -5,7 +5,7 @@ public sealed class Delayer : Decorator private readonly float _waitTime; private float _interval; - public Delayer(float waitTime) + public Delayer(float waitTime, string? name = null) : base(name) { _waitTime = waitTime; _interval = 0f; @@ -40,9 +40,9 @@ public override void Reset() public static partial class ParentExtensions { - public static IParent Delayer(this IParent parent, float waitTime) + public static IParent Delayer(this IParent parent, float waitTime, string? name = null) { - var delayer = new Delayer(waitTime); + var delayer = new Delayer(waitTime, name); parent.Attach(delayer); return delayer; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Failer.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Failer.cs index 72032d8..069ac7e 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Failer.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Failer.cs @@ -2,6 +2,8 @@ public sealed class Failer : Decorator { + public Failer(string? name = null) : base(name) { } + public override NodeState Evaluate(float deltaTime) { var status = base.Evaluate(deltaTime); @@ -12,9 +14,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Failer(this IParent parent) + public static IParent Failer(this IParent parent, string? name = null) { - var failer = new Failer(); + var failer = new Failer(name); parent.Attach(failer); return failer; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Inverter.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Inverter.cs index 919b91d..d41fefd 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Inverter.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Inverter.cs @@ -2,6 +2,8 @@ public sealed class Inverter : Decorator { + public Inverter(string? name = null) : base(name) { } + public override NodeState Evaluate(float deltaTime) { var status = base.Evaluate(deltaTime); @@ -18,9 +20,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Inverter(this IParent parent) + public static IParent Inverter(this IParent parent, string? name = null) { - var inverter = new Inverter(); + var inverter = new Inverter(name); parent.Attach(inverter); return inverter; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Repeater.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Repeater.cs index c148f53..f1ff87e 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Repeater.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Repeater.cs @@ -6,7 +6,7 @@ public sealed class Repeater : Decorator private readonly int _maxCount; private int _currentCount; - public Repeater(RepeatMode repeatMode, int maxCount = -1) + public Repeater(RepeatMode repeatMode, int maxCount = -1, string? name = null) : base(name) { _repeatMode = repeatMode; _maxCount = maxCount; @@ -69,9 +69,9 @@ public override void Abort() public static partial class ParentExtensions { - public static IParent Repeater(this IParent parent, RepeatMode repeatMode) + public static IParent Repeater(this IParent parent, RepeatMode repeatMode, string? name = null) { - var repeater = new Repeater(repeatMode); + var repeater = new Repeater(repeatMode, name: name); parent.Attach(repeater); return repeater; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Reset.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Reset.cs index a811f6e..93bef17 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Reset.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Reset.cs @@ -4,7 +4,7 @@ public class Reset : Decorator { private readonly Func _condition; - public Reset(Func condition) + public Reset(Func condition, string? name = null) : base(name) { _condition = condition; } @@ -23,9 +23,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Reset(this IParent parent, Func condition) + public static IParent Reset(this IParent parent, Func condition, string? name = null) { - var reset = new Reset(condition); + var reset = new Reset(condition, name); parent.Attach(reset); return reset; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/Succeeder.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/Succeeder.cs index ff38d3a..a82ae2a 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/Succeeder.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/Succeeder.cs @@ -2,6 +2,8 @@ public sealed class Succeeder : Decorator { + public Succeeder(string? name = null) : base(name) { } + public override NodeState Evaluate(float deltaTime) { var status = base.Evaluate(deltaTime); @@ -12,9 +14,9 @@ public override NodeState Evaluate(float deltaTime) public static partial class ParentExtensions { - public static IParent Succeeder(this IParent parent) + public static IParent Succeeder(this IParent parent, string? name = null) { - var succeeder = new Succeeder(); + var succeeder = new Succeeder(name); parent.Attach(succeeder); return succeeder; } diff --git a/src/GroveGames.BehaviourTree/Nodes/Decorators/SuccessOnce.cs b/src/GroveGames.BehaviourTree/Nodes/Decorators/SuccessOnce.cs index 392c9ae..4457d8d 100644 --- a/src/GroveGames.BehaviourTree/Nodes/Decorators/SuccessOnce.cs +++ b/src/GroveGames.BehaviourTree/Nodes/Decorators/SuccessOnce.cs @@ -4,7 +4,7 @@ public class SuccessOnce : Decorator { private bool _hasSucceeded; - public SuccessOnce() + public SuccessOnce(string? name = null) : base(name) { _hasSucceeded = false; } diff --git a/tests/GroveGames.BehaviourTree.Tests/Nodes/NodeTests.cs b/tests/GroveGames.BehaviourTree.Tests/Nodes/NodeTests.cs index 4d09192..311cdd1 100644 --- a/tests/GroveGames.BehaviourTree.Tests/Nodes/NodeTests.cs +++ b/tests/GroveGames.BehaviourTree.Tests/Nodes/NodeTests.cs @@ -42,6 +42,32 @@ public void Clear() { } private sealed class TestNode : BehaviourNode { + public TestNode(string? name = null) : base(name) { } + } + + [Fact] + public void Name_ShouldReturnTypeName_WhenNoNameProvided() + { + var node = new TestNode(); + + Assert.Equal(nameof(TestNode), node.Name); + } + + [Fact] + public void Name_ShouldReturnCustomName_WhenNameProvidedInConstructor() + { + var node = new TestNode("MyNode"); + + Assert.Equal("MyNode", node.Name); + } + + [Fact] + public void SetName_ShouldOverrideName() + { + var node = new TestNode(); + node.SetName("UpdatedName"); + + Assert.Equal("UpdatedName", node.Name); } [Fact]