public FarmConstruct GetNearestFarmWithVector3(Vector3 v) { List <FarmConstruct> farms = FindObjectsOfType <FarmConstruct>().ToList(); FarmConstruct nearestFarm = null; float minDistance = Mathf.Infinity; foreach (var item in farms) { if (item.curPeasant < item.maxPeasant) { if (item._property.colorTeam == _property.colorTeam) { if (Vector3.Distance(item.transform.position, v) < minDistance) { minDistance = Vector3.Distance(item.transform.position, v); nearestFarm = item; } } } } return(nearestFarm); }
public override void ActTo(Unit unit) { if (unit == null) { return; } Property p = unit._property; if (unit._property.colorTeam != _property.colorTeam && unit._property.colorTeam != Team.None) { if (targetTree) { targetTree.curPeasant = 0; } targetAttack = unit; _property.state = State.MoveFight; } else { if (p._name == "Tree") { if (targetTree) { targetTree.curPeasant = 0; } TreeUnit tree = GetNearestTreeWithVector3(unit.transform.position); if (!tree) { return; } if (GetComponent <PeasantSoldier>()) { targetTree = tree; targetTree.curPeasant++; _property.state = State.MoveTree; } } else if (p._name == "Farm") { if (targetTree) { targetTree.curPeasant = 0; } FarmConstruct farm = GetNearestFarmWithVector3(unit.transform.position); if (!farm) { return; } if (GetComponent <PeasantSoldier>()) { targetFarm = farm; targetFarm.curPeasant++; _property.state = State.MoveFarm; } } } }