Fix placing multiple units on the same tile
This commit is contained in:
parent
a6778d8b72
commit
adf8ad313a
@ -3,34 +3,44 @@
|
|||||||
public class ObjectPlaceholder : UnitPlaceholder
|
public class ObjectPlaceholder : UnitPlaceholder
|
||||||
{
|
{
|
||||||
public GameObject Prefab { get; set; }
|
public GameObject Prefab { get; set; }
|
||||||
|
|
||||||
public override void Place()
|
public override void Place()
|
||||||
{
|
{
|
||||||
base.Place();
|
base.Place();
|
||||||
Prefab.Create(transform.position, parent : LevelManager.Instance.LevelTransform);
|
Prefab.Create(transform.position, parent : LevelManager.Instance.LevelTransform);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool CanBePlacedHere()
|
public override bool CanBePlacedHere()
|
||||||
{
|
{
|
||||||
var objsOnPosition = LevelManager.Instance.GetAll<ILevelObject>(obj => obj.Position.IsContainedIn(transform.position));
|
var objsOnPosition = LevelManager.Instance.GetAll<ILevelObject>(obj => obj.Position.IsContainedIn(transform.position));
|
||||||
var positionIsCollidable = objsOnPosition.Exists(obj => obj.IsCollidable);
|
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;
|
var defaultUnitCost = GlobalConfig.Instance.Current.populationCostPerUnit;
|
||||||
return !positionIsCollidable
|
return !positionIsCollidable
|
||||||
&& LevelBoundCache.Contains(transform.position)
|
&& LevelBoundCache.Contains(transform.position)
|
||||||
&& ResourceManager.Instance.EnoughFor(Rock, Wood, Food)
|
&& ResourceManager.Instance.EnoughFor(Rock, Wood, Food)
|
||||||
&& (Prefab.GetComponent<Building>()
|
&& (Prefab.GetComponent<Building>()
|
||||||
|| ResourceManager.Instance.EnoughPopulationFor(defaultUnitCost))
|
|| ResourceManager.Instance.EnoughPopulationFor(defaultUnitCost))
|
||||||
&& IsFarmerAndAllowed();
|
&& IsHarvesterAndAllowed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsFarmerAndAllowed()
|
private bool IsHarvesterAndAllowed()
|
||||||
{
|
{
|
||||||
Ally unit = Prefab.GetComponent<Ally>();
|
Ally unit = Prefab.GetComponent<Ally>();
|
||||||
|
|
||||||
//Obtiens info correctement.
|
// Get info
|
||||||
LevelTile pointedTile = LevelManager.Instance.Get<LevelTile>(obj => obj.Position.IsContainedIn(transform.position));
|
LevelTile pointedTile = LevelManager.Instance.Get<LevelTile>(obj => obj.Position.IsContainedIn(transform.position));
|
||||||
|
|
||||||
return unit is not Harvester || (unit is Harvester && IsAllowedTile(pointedTile));
|
return unit is not Harvester || (unit is Harvester && IsAllowedTile(pointedTile));
|
||||||
//return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsAllowedTile(LevelTile pointedTile)
|
private bool IsAllowedTile(LevelTile pointedTile)
|
||||||
|
|||||||
@ -11,10 +11,12 @@ public abstract class UnitPlaceholder : DraggablePlaceholder
|
|||||||
WasPlaced?.Invoke(this);
|
WasPlaced?.Invoke(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Not actually used because overriden.
|
||||||
public override bool CanBePlacedHere()
|
public override bool CanBePlacedHere()
|
||||||
{
|
{
|
||||||
return base.CanBePlacedHere() && ResourceManager.Instance.EnoughFor(Rock, Wood, Food)
|
return base.CanBePlacedHere() && ResourceManager.Instance.EnoughFor(Rock, Wood, Food)
|
||||||
&& !LevelManager.Instance.Has<ILevelObject>(obj => obj.Position.IsContainedIn(transform.position));
|
&& !LevelManager.Instance.Has<ILevelObject>(obj => obj.Position.IsContainedIn(transform.position));
|
||||||
}
|
}
|
||||||
|
|
||||||
public event System.Action<UnitPlaceholder> WasPlaced;
|
public event System.Action<UnitPlaceholder> WasPlaced;
|
||||||
}
|
}
|
||||||
@ -59,6 +59,7 @@ public class LevelManagerScript : SingletonBehaviour<LevelManagerScript>
|
|||||||
{
|
{
|
||||||
//only when the level is loaded do we start updating
|
//only when the level is loaded do we start updating
|
||||||
updateAction = LevelManager.Instance.UpdateLevel;
|
updateAction = LevelManager.Instance.UpdateLevel;
|
||||||
|
LevelManager.Instance.LevelTransform = transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user