From 3fdbbee7656f37eb946988c5f18c03f5fa00e800 Mon Sep 17 00:00:00 2001 From: EliaGingras1 Date: Sun, 6 Apr 2025 22:31:07 +0000 Subject: [PATCH] Fix placing multiple units on the same tile (#2) Reviewed-on: https://gitea.clubconjure.com/Conjure/gather-and-defend/pulls/2 Reviewed-by: Ader_Alisma --- Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs | 18 ++++++++++++++---- Assets/Scripts/Drag&Drop/UnitPlaceholder.cs | 1 + .../Scripts/LevelManager/LevelManagerScript.cs | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs b/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs index 781c2fe..bf95cb7 100644 --- a/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs +++ b/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs @@ -3,34 +3,44 @@ public class ObjectPlaceholder : UnitPlaceholder { public GameObject Prefab { get; set; } + public override void Place() { base.Place(); Prefab.Create(transform.position, parent : LevelManager.Instance.LevelTransform); } + public override bool CanBePlacedHere() { var objsOnPosition = LevelManager.Instance.GetAll(obj => obj.Position.IsContainedIn(transform.position)); var positionIsCollidable = objsOnPosition.Exists(obj => obj.IsCollidable); + // If an ally is on the tile, we can't place anything here + foreach (var item in objsOnPosition) + { + if (item is Ally) + { + return false; + } + } + var defaultUnitCost = GlobalConfig.Instance.Current.populationCostPerUnit; return !positionIsCollidable && LevelBoundCache.Contains(transform.position) && ResourceManager.Instance.EnoughFor(Rock, Wood, Food) && (Prefab.GetComponent() || ResourceManager.Instance.EnoughPopulationFor(defaultUnitCost)) - && IsFarmerAndAllowed(); + && IsHarvesterAndAllowed(); } - private bool IsFarmerAndAllowed() + private bool IsHarvesterAndAllowed() { Ally unit = Prefab.GetComponent(); - //Obtiens info correctement. + // Get info LevelTile pointedTile = LevelManager.Instance.Get(obj => obj.Position.IsContainedIn(transform.position)); return unit is not Harvester || (unit is Harvester && IsAllowedTile(pointedTile)); - //return true; } private bool IsAllowedTile(LevelTile pointedTile) diff --git a/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs b/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs index b553a1f..fbe2f12 100644 --- a/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs +++ b/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs @@ -16,5 +16,6 @@ public abstract class UnitPlaceholder : DraggablePlaceholder return base.CanBePlacedHere() && ResourceManager.Instance.EnoughFor(Rock, Wood, Food) && !LevelManager.Instance.Has(obj => obj.Position.IsContainedIn(transform.position)); } + public event System.Action WasPlaced; } \ No newline at end of file diff --git a/Assets/Scripts/LevelManager/LevelManagerScript.cs b/Assets/Scripts/LevelManager/LevelManagerScript.cs index e3f1139..e022c45 100644 --- a/Assets/Scripts/LevelManager/LevelManagerScript.cs +++ b/Assets/Scripts/LevelManager/LevelManagerScript.cs @@ -59,6 +59,7 @@ public class LevelManagerScript : SingletonBehaviour { //only when the level is loaded do we start updating updateAction = LevelManager.Instance.UpdateLevel; + LevelManager.Instance.LevelTransform = transform; } ///