WIP fix placement on same tile
This commit is contained in:
parent
56b5dd3361
commit
37568663fb
@ -10,19 +10,40 @@ public class ObjectPlaceholder : UnitPlaceholder
|
|||||||
}
|
}
|
||||||
public override bool CanBePlacedHere()
|
public override bool CanBePlacedHere()
|
||||||
{
|
{
|
||||||
|
//bool isTileEmpty = !LevelManager.Instance.Has<ILevelObject>(obj => obj.Position.IsContainedIn(transform.position));
|
||||||
|
bool isHarvesterAndAllowed = IsHarvesterAndAllowed();
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
// Look if tile is already used by an ally
|
||||||
|
var isTileBusy = false;
|
||||||
|
Transform[] children = LevelManager.Instance.LevelTransform.GetComponentsInChildren<Transform>();
|
||||||
|
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;
|
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
|
||||||
|
&& !isTileBusy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsFarmerAndAllowed()
|
private bool IsHarvesterAndAllowed()
|
||||||
{
|
{
|
||||||
Ally unit = Prefab.GetComponent<Ally>();
|
Ally unit = Prefab.GetComponent<Ally>();
|
||||||
|
|
||||||
@ -30,7 +51,6 @@ public class ObjectPlaceholder : UnitPlaceholder
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This isn't ever used?
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -102,7 +102,8 @@ public static class Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool IsContainedIn(this Vector3 vect, Vector3 tilePosition)
|
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)
|
public static Vector2 RandomInRectangle(float x, float y)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -50,6 +50,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