private void Update() { if (this.commandable.IsBusy()) { return; } if (this.resourceToBeGathered != null) { if (!this.interactingSource) { // if the resource we are gathering has depleted, find a new resource close by var next = ResourceSource.GetClosest(this.transform.position, this.maxNextSourceDistance, this.resourceToBeGathered.Value); if (next) { this.MoveTo(next.gameObject); } else { this.resourceToBeGathered = null; } } else if (this.IsInRange(this.interactingSource.gameObject)) { // gather the resource this.actionTimer += Time.deltaTime; if (this.actionTimer >= TownStats.Instance.chopSpeed) { this.actionTimer = 0; if (!this.Carry(this.interactingSource.type, 1)) { // carry items to a building because we are full var closest = Building.GetClosest(this.transform.position, building => building.storeableTypes.Contains(this.CarryingResource.type)); if (closest) { this.MoveTo(closest.gameObject); } } else { // mine the resource this.interactingSource.Mine(); } } } else { this.MoveTo(this.interactingSource.gameObject); } } else if (this.shouldContructBuilding) { // if the building we're constructing is finished, then our work is done if (!this.constructingBuilding || this.constructingBuilding.IsFinished) { if (this.constructingBuilding && this.constructingBuilding.storeableTypes.Length > 0) { // if there's anything to gather after building, do it var next = ResourceSource.GetClosest(this.transform.position, this.maxNextSourceDistance, this.constructingBuilding.storeableTypes); if (next) { this.MoveTo(next.gameObject); } this.shouldContructBuilding = false; } else { // otherwise find the next building to work on var next = Building.GetClosest(this.transform.position, null, false, this.maxNextSourceDistance); if (next) { this.MoveTo(next.gameObject); } else { this.shouldContructBuilding = false; } } this.constructingBuilding = null; } // construct building if it is in range else if (this.IsInRange(this.constructingBuilding.gameObject)) { this.actionTimer += Time.deltaTime; if (this.actionTimer >= TownStats.Instance.buildSpeed) { this.actionTimer = 0; // if the building can be fed the current resource if (this.CarryingResource != null && this.constructingBuilding.FeedResource(this.CarryingResource.type)) { // deposit an item in it this.Deposit(1); } else { // otherwise, find a building that has the required resources bool Filter(Building building) { foreach (var res in this.constructingBuilding.requiredResources) { if (!building.storeableTypes.Contains(res.type)) { continue; } if (ResourceManager.Instance.GetResourceAmount(res.type) > 0) { return(true); } } return(false); } var closest = Building.GetClosest(this.transform.position, Filter); if (closest) { this.MoveTo(closest.gameObject); this.isWaitingForBuildingResources = false; } else { this.isWaitingForBuildingResources = true; } } } } else { this.MoveTo(this.constructingBuilding.gameObject); } } }