diff --git a/Package/Editor/CrashKonijn.Goap.Editor/Drawers/ObjectDrawer.cs b/Package/Editor/CrashKonijn.Goap.Editor/Drawers/ObjectDrawer.cs index 6409ed54..2b6c9758 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/Drawers/ObjectDrawer.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/Drawers/ObjectDrawer.cs @@ -1,5 +1,6 @@ using System.Linq; using System.Reflection; +using CrashKonijn.Agent.Core; using CrashKonijn.Goap.Runtime; using UnityEngine; using UnityEngine.UIElements; @@ -12,9 +13,9 @@ public ObjectDrawer(object obj) { if (obj is null) return; - + var properties = obj.GetType().GetProperties(); - + var label = new Label(); label.text = this.GetLabelText(properties, obj); this.Add(label); @@ -38,23 +39,23 @@ private string GetValueString(object value) { if (value == null) return "null"; - + if (value is TransformTarget transformTarget) { if (transformTarget.Transform == null) return "null"; - + return transformTarget.Transform.name; } - + if (value is PositionTarget positionTarget) - return positionTarget.Position.ToString(); - + return positionTarget.GetValidPosition().ToString(); + if (value is MonoBehaviour monoBehaviour) { if (monoBehaviour == null) return "null"; - + return monoBehaviour.name; } @@ -69,4 +70,4 @@ private string GetValueString(object value) return value.ToString(); } } -} \ No newline at end of file +} diff --git a/Package/Editor/CrashKonijn.Goap.Editor/Drawers/WorldDataDrawer.cs b/Package/Editor/CrashKonijn.Goap.Editor/Drawers/WorldDataDrawer.cs index 11bfb6c8..46d544f7 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/Drawers/WorldDataDrawer.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/Drawers/WorldDataDrawer.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using CrashKonijn.Agent.Core; using CrashKonijn.Agent.Runtime; @@ -14,26 +13,26 @@ public class WorldDataDrawer : VisualElement public WorldDataDrawer(ILocalWorldData worldData) { this.name = "world-data"; - + var card = new Card((card) => { card.Add(new Header("World Data")); - + var root = new VisualElement(); - + var scrollView = new ScrollView(ScrollViewMode.VerticalAndHorizontal); scrollView.Add(root); - + card.schedule.Execute(() => { root.Clear(); - + root.Add(this.CreateTable(new string[] { "Key", "Value", "Age" }, this.GetValues(worldData))); }).Every(500); - + card.Add(scrollView); }); - + this.Add(card); } @@ -55,19 +54,19 @@ private string[][] GetTargetValues(ILocalWorldData worldData) private string[] GetValues(Type key, IWorldDataState dataState) { var local = dataState.IsLocal ? "Local" : "Global"; - + return new string[] { $"{key.GetGenericTypeName()} ({local})", this.GetValueText(dataState), - this.GetText(dataState.Timer) + this.GetText(dataState.Timer), }; } private string GetText(ITimer timer) { var secondsAgo = timer.GetElapsed(); - + return $"{secondsAgo:0.00}s"; } @@ -77,15 +76,15 @@ private string GetValueText(IWorldDataState dataState) { return $"{intValue}"; } - + if (dataState.Value is ITarget target) { - return $"{target.Position}"; + return $"{target.GetValidPosition()}"; } return ""; } - + private VisualElement CreateTable(string[] headers, string[][] rows) { var tableContainer = new VisualElement(); @@ -107,14 +106,14 @@ private VisualElement CreateTable(string[] headers, string[][] rows) for (var i = 0; i < headers.Length; i++) { var headerLabel = new Label(headers[i]); - headerLabel.style.width = columnWidths[i]; // Set fixed width for each column + headerLabel.style.width = columnWidths[i]; // Set fixed width for each column headerLabel.style.unityFontStyleAndWeight = FontStyle.Bold; headerLabel.style.color = Color.white; headerLabel.style.paddingLeft = 10; headerLabel.style.paddingRight = 10; // headerLabel.style.unityTextAlign = TextAnchor.MiddleCenter; // headerLabel.style.flexWrap = Wrap.Wrap; - headerLabel.style.flexGrow = 1; // Allow columns to expand equally + headerLabel.style.flexGrow = 1; // Allow columns to expand equally headerRow.Add(headerLabel); } @@ -135,7 +134,7 @@ private VisualElement CreateTable(string[] headers, string[][] rows) for (var i = 0; i < row.Length; i++) { var dataLabel = new Label(row[i]); - dataLabel.style.width = columnWidths[i]; // Set fixed width for each column + dataLabel.style.width = columnWidths[i]; // Set fixed width for each column dataLabel.style.paddingLeft = 10; dataLabel.style.paddingRight = 10; dataLabel.style.flexWrap = Wrap.Wrap; @@ -150,4 +149,4 @@ private VisualElement CreateTable(string[] headers, string[][] rows) return tableContainer; } } -} \ No newline at end of file +} diff --git a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/ConnectionElement.cs b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/ConnectionElement.cs index d6f2e0f0..1700f99b 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/ConnectionElement.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/ConnectionElement.cs @@ -21,10 +21,10 @@ public ConnectionElement(NodeElement fromNode, ConditionElement condition, NodeE this.condition = condition; this.toNode = toNode; this.values = values; - + this.Bezier = new BezierDrawer(); this.Add(this.Bezier); - + this.Cost = new Label("") { style = @@ -36,33 +36,33 @@ public ConnectionElement(NodeElement fromNode, ConditionElement condition, NodeE }; this.Cost.AddToClassList("distance-cost"); this.Add(this.Cost); - + this.Update(); - + this.values.OnUpdate += this.Update; - + this.schedule.Execute(this.Update).Every(100); } public void Update() { var magicOffset = 10f; - + var start = this.condition; var end = this.toNode.Node; - + var startPos = new Vector3(this.condition.parent.parent.worldBound.position.x + this.condition.parent.parent.worldBound.width, start.worldBound.position.y - magicOffset, 0); var endPos = new Vector3(end.worldBound.position.x, end.worldBound.position.y - magicOffset, 0); - + var startTan = startPos + (Vector3.right * 40); var endTan = endPos + (Vector3.left * 40); - + this.Bezier.Update(startPos, endPos, startTan, endTan, 2f, this.GetColor()); var center = this.Bezier.GetCenter(); var cost = this.GetCost(); var length = cost.Length * 5; - + this.Cost.style.left = center.x - length; this.Cost.style.top = center.y; this.Cost.text = cost; @@ -73,18 +73,18 @@ private Color GetColor() { if (this.values.SelectedObject == null) return Color.black; - + if (this.values.SelectedObject is not IMonoGoapActionProvider agent) return Color.black; var actions = agent.CurrentPlan?.Plan; - + if (actions == null) return Color.black; - + if (actions.Contains(this.toNode.GraphNode.Action)) return new Color(0, 157, 100); - + return Color.black; } @@ -92,26 +92,29 @@ private string GetCost() { if (this.values.SelectedObject is not IMonoGoapActionProvider agent) return ""; - + if (!Application.isPlaying) return ""; - + var fromAction = this.fromNode.GraphNode.Action as IGoapAction; var toAction = this.toNode.GraphNode.Action as IGoapAction; - + if (fromAction == null || toAction == null) return ""; var startVector = agent.WorldData.GetTarget(fromAction); var endVector = agent.WorldData.GetTarget(toAction); - + if (startVector == null || endVector == null) return ""; - + + if (!startVector.IsValid() || !endVector.IsValid()) + return ""; + var distance = Vector3.Distance(startVector.Position, endVector.Position); var cost = agent.DistanceMultiplier * distance; - + return cost.ToString("F2"); } } -} \ No newline at end of file +} diff --git a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs index 1fe2a5ee..25afedeb 100644 --- a/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs +++ b/Package/Editor/CrashKonijn.Goap.Editor/GraphViewer/NodeElement.cs @@ -164,7 +164,7 @@ private string GetTargetText(EditorWindowValues values, IMonoGoapActionProvider return $"Target: {targetConfig}"; var target = provider.WorldData.GetTarget(action); - var targetText = target != null ? target.Position.ToString() : "null"; + var targetText = target?.GetValidPosition()?.ToString() ?? "null"; if (values.ShowConfig) return $"Target: {targetText} ({targetConfig})"; diff --git a/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs b/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs new file mode 100644 index 00000000..c4ba3dea --- /dev/null +++ b/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace CrashKonijn.Agent.Core +{ + public static class Extensions + { + public static Vector3? GetValidPosition(this ITarget target) + { + if (target == null) + return null; + + if (!target.IsValid()) + return null; + + return target.Position; + } + } +} diff --git a/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs.meta b/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs.meta new file mode 100644 index 00000000..a701bd23 --- /dev/null +++ b/Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 805a515207ff4b0e94abbd8ed8000164 +timeCreated: 1734018533 \ No newline at end of file diff --git a/Package/Runtime/CrashKonijn.Agent.Runtime/VectorDistanceObserver.cs b/Package/Runtime/CrashKonijn.Agent.Runtime/VectorDistanceObserver.cs index 7dccd6b8..c0ce41a2 100644 --- a/Package/Runtime/CrashKonijn.Agent.Runtime/VectorDistanceObserver.cs +++ b/Package/Runtime/CrashKonijn.Agent.Runtime/VectorDistanceObserver.cs @@ -7,10 +7,14 @@ public class VectorDistanceObserver : IAgentDistanceObserver { public float GetDistance(IMonoAgent agent, ITarget target, IComponentReference reference) { + if (agent.transform == null) + return 0f; + if (target == null) - { return 0f; - } + + if (!target.IsValid()) + return 0f; return Vector3.Distance(agent.transform.position, target.Position); } diff --git a/Package/Runtime/CrashKonijn.Goap.Runtime/Classes/Runners/AgentTypeJobRunner.cs b/Package/Runtime/CrashKonijn.Goap.Runtime/Classes/Runners/AgentTypeJobRunner.cs index 4a405aee..6ede5b23 100644 --- a/Package/Runtime/CrashKonijn.Goap.Runtime/Classes/Runners/AgentTypeJobRunner.cs +++ b/Package/Runtime/CrashKonijn.Goap.Runtime/Classes/Runners/AgentTypeJobRunner.cs @@ -142,7 +142,7 @@ private void FillBuilders(IMonoGoapActionProvider actionProvider) this.enabledBuilder.SetEnabled(node, node.IsEnabled(actionProvider.Receiver)); this.costBuilder.SetCost(node, node.GetCost(actionProvider.Receiver, actionProvider.Receiver.Injector, target)); - this.positionBuilder.SetPosition(node, target?.Position); + this.positionBuilder.SetPosition(node, target?.GetValidPosition()); } }