Skip to content

Commit

Permalink
Merge pull request #163 from crashkonijn/fix/improve-gc
Browse files Browse the repository at this point in the history
Improved GC
  • Loading branch information
crashkonijn authored Jun 17, 2024
2 parents 81c6bfe + 8df3460 commit 3902575
Show file tree
Hide file tree
Showing 27 changed files with 2,259 additions and 1,289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,22 @@ public int Count(bool canBeInBox, bool canBeHeld)

public IHoldable Closest(Vector3 position, bool canBeInBox, bool canBeHeld, bool canBeClaimed)
{
return this.Filtered(canBeInBox, canBeHeld, canBeClaimed)
.OrderBy(x => Vector3.Distance(x.gameObject.transform.position, position))
.FirstOrDefault();
var filteredItems = this.Filtered(canBeInBox, canBeHeld, canBeClaimed);
IHoldable closest = null;
var closestDistance = float.MaxValue; // Start with the largest possible distance

foreach (var item in filteredItems)
{
var distance = Vector3.Distance(item.gameObject.transform.position, position);

if (!(distance < closestDistance))
continue;

closest = item;
closestDistance = distance;
}

return closest;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public void Remove(AppleBehaviour apple)
this.apples.Remove(apple);
}

public AppleBehaviour[] Get()
public List<AppleBehaviour> Get()
{
return this.apples.ToArray();
return this.apples;
}

public bool Any()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ private void Update()

private void FixedUpdate()
{
this.appleCountText.text = $"+ Apple ({this.apples.Get().Length})";
this.agentCountText.text = $"+ Agent ({this.goap.Agents.Length})";
this.appleCountText.text = $"+ Apple ({this.apples.Get().Count})";
this.agentCountText.text = $"+ Agent ({this.goap.Agents.Count})";
}

public void SetDebug(bool value)
Expand Down Expand Up @@ -90,7 +90,7 @@ public void SpawnAgent()
{
this.SetDebug(false);

var agentCount = this.goap.Agents.Length;
var agentCount = this.goap.Agents.Count;
var count = agentCount < 50 ? 50 - agentCount : 50;

for (var i = 0; i < count; i++)
Expand Down
34 changes: 28 additions & 6 deletions Demo/Assets/CrashKonijn/GOAP/Demos/Simple/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,39 @@ public static class Extensions
{
public static GameObject Closest(this IEnumerable<GameObject> items, Vector3 position)
{
return items
.OrderBy(x => Vector3.Distance(x.transform.position, position))
.FirstOrDefault();
GameObject closest = null;
var closestDistance = float.MaxValue;

foreach (var item in items)
{
var distance = Vector3.Distance(item.transform.position, position);
if (!(distance < closestDistance))
continue;

closest = item;
closestDistance = distance;
}

return closest;
}

public static T Closest<T>(this IEnumerable<T> items, Vector3 position)
where T : MonoBehaviour
{
return items
.OrderBy(x => Vector3.Distance(x.transform.position, position))
.FirstOrDefault();
T closest = null;
var closestDistance = float.MaxValue;

foreach (var item in items)
{
var distance = Vector3.Distance(item.transform.position, position);
if (!(distance < closestDistance))
continue;

closest = item;
closestDistance = distance;
}

return closest;
}
}
}
3 changes: 2 additions & 1 deletion Demo/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"com.unity.ai.navigation": "1.1.1",
"com.unity.collab-proxy": "2.0.0",
"com.unity.collections": "1.2.4",
"com.unity.ide.rider": "3.0.18",
"com.unity.ide.visualstudio": "2.0.17",
"com.unity.ide.vscode": "1.2.5",
"com.unity.memoryprofiler": "1.0.0",
"com.unity.render-pipelines.universal": "14.0.6",
"com.unity.test-framework": "1.1.33",
"com.unity.testtools.codecoverage": "1.2.2",
"com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.2",
"com.unity.ugui": "1.0.0",
"com.unity.visualscripting": "1.8.0",
"com.unity.ide.rider": "3.0.18",
"net.tnrd.nsubstitute": "https://github.com/Thundernerd/Unity3D-NSubstitute.git",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",
Expand Down
16 changes: 16 additions & 0 deletions Demo/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
},
"url": "https://packages.unity.com"
},
"com.unity.editorcoroutines": {
"version": "1.0.0",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.ext.nunit": {
"version": "1.0.6",
"depth": 1,
Expand Down Expand Up @@ -95,6 +102,15 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.memoryprofiler": {
"version": "1.0.0",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.editorcoroutines": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.render-pipelines.core": {
"version": "14.0.6",
"depth": 1,
Expand Down
Loading

0 comments on commit 3902575

Please sign in to comment.