Skip to content

Commit

Permalink
Merge pull request #322 from crashkonijn/fix/invalid-target-position-…
Browse files Browse the repository at this point in the history
…being-used

Fix: Invalid target's position being used
  • Loading branch information
crashkonijn authored Dec 12, 2024
2 parents 7434706 + e2536a1 commit bfd38bf
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 52 deletions.
19 changes: 10 additions & 9 deletions Package/Editor/CrashKonijn.Goap.Editor/Drawers/ObjectDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Reflection;
using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Runtime;
using UnityEngine;
using UnityEngine.UIElements;
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -69,4 +70,4 @@ private string GetValueString(object value)
return value.ToString();
}
}
}
}
35 changes: 17 additions & 18 deletions Package/Editor/CrashKonijn.Goap.Editor/Drawers/WorldDataDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CrashKonijn.Agent.Core;
using CrashKonijn.Agent.Runtime;
Expand All @@ -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);
}

Expand All @@ -55,19 +54,19 @@ private string[][] GetTargetValues(ILocalWorldData worldData)
private string[] GetValues<T>(Type key, IWorldDataState<T> 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";
}

Expand All @@ -77,15 +76,15 @@ private string GetValueText<T>(IWorldDataState<T> 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();
Expand All @@ -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);
}

Expand All @@ -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;
Expand All @@ -150,4 +149,4 @@ private VisualElement CreateTable(string[] headers, string[][] rows)
return tableContainer;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
Expand All @@ -73,45 +73,48 @@ 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;
}

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");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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})";
Expand Down
18 changes: 18 additions & 0 deletions Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 3 additions & 0 deletions Package/Runtime/CrashKonijn.Agent.Core/Extensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit bfd38bf

Please sign in to comment.