diff --git a/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs b/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs index 781c2fe..af2357d 100644 --- a/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs +++ b/Assets/Scripts/Drag&Drop/ObjectPlaceholder.cs @@ -10,19 +10,40 @@ public class ObjectPlaceholder : UnitPlaceholder } public override bool CanBePlacedHere() { + //bool isTileEmpty = !LevelManager.Instance.Has(obj => obj.Position.IsContainedIn(transform.position)); + bool isHarvesterAndAllowed = IsHarvesterAndAllowed(); + var objsOnPosition = LevelManager.Instance.GetAll(obj => obj.Position.IsContainedIn(transform.position)); var positionIsCollidable = objsOnPosition.Exists(obj => obj.IsCollidable); + // Look if tile is already used by an ally + var isTileBusy = false; + Transform[] children = LevelManager.Instance.LevelTransform.GetComponentsInChildren(); + foreach (Transform tr in children) + { + Debug.Log(tr); + if (tr == null || tr.tag != "Ally") break; + float distance = Vector2.Distance(transform.position, tr.position); + Debug.Log("distance : " + distance); + if (distance <= 0.5f) + { + Debug.Log("distance ahah: " + distance); + isTileBusy = true; + break; + } + } + var defaultUnitCost = GlobalConfig.Instance.Current.populationCostPerUnit; - return !positionIsCollidable + return !positionIsCollidable && LevelBoundCache.Contains(transform.position) && ResourceManager.Instance.EnoughFor(Rock, Wood, Food) && (Prefab.GetComponent() - || ResourceManager.Instance.EnoughPopulationFor(defaultUnitCost)) - && IsFarmerAndAllowed(); + || ResourceManager.Instance.EnoughPopulationFor(defaultUnitCost)) + && isHarvesterAndAllowed + && !isTileBusy; } - private bool IsFarmerAndAllowed() + private bool IsHarvesterAndAllowed() { Ally unit = Prefab.GetComponent(); @@ -30,7 +51,6 @@ public class ObjectPlaceholder : UnitPlaceholder 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..e1b8d46 100644 --- a/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs +++ b/Assets/Scripts/Drag&Drop/UnitPlaceholder.cs @@ -11,10 +11,12 @@ public abstract class UnitPlaceholder : DraggablePlaceholder WasPlaced?.Invoke(this); } + // This isn't ever used? public override bool CanBePlacedHere() { 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/General/Extensions.cs b/Assets/Scripts/General/Extensions.cs index a52cfc7..0208efd 100644 --- a/Assets/Scripts/General/Extensions.cs +++ b/Assets/Scripts/General/Extensions.cs @@ -102,7 +102,8 @@ public static class Extensions /// public static bool IsContainedIn(this Vector3 vect, Vector3 tilePosition) { - return Vector2.Distance(vect, tilePosition) < 0.5f; + float distance = Vector2.Distance(vect, tilePosition); + return distance < 0.5f; } public static Vector2 RandomInRectangle(float x, float y) { diff --git a/Assets/Scripts/LevelManager/LevelManagerScript.cs b/Assets/Scripts/LevelManager/LevelManagerScript.cs index 969851e..c162b76 100644 --- a/Assets/Scripts/LevelManager/LevelManagerScript.cs +++ b/Assets/Scripts/LevelManager/LevelManagerScript.cs @@ -50,6 +50,7 @@ public class LevelManagerScript : SingletonBehaviour { //only when the level is loaded do we start updating updateAction = LevelManager.Instance.UpdateLevel; + LevelManager.Instance.LevelTransform = transform; } ///