public void GatherResource(ResourceType resourceType) { if (resourceType == ResourceType.None) { return; } StopCurrentState(); var gatherState = new GatherState(unit, resourceType); PushNewStateHandler(gatherState); }
private bool GatherSomething() { var resourceTypes = Resource.GetResourceTypes(); var priorityGatherRadius = unit.CurrentJob.priorityGatherRadius; var basePosition = unit.UnitTeam.baseBuilding.transform.position; var nodesInPriorityRadius = resourceTypes.ToDictionary(type => type, type => Map.Instance.GetResourceNodesOfType(type).Where(node => Vector3.Distance(basePosition, node.transform.position) <= priorityGatherRadius)); var possibleTypesToGather = nodesInPriorityRadius.Where(n => n.Value.Any()).Select(n => n.Key).Distinct().ToArray(); ResourceType typeToGather; if (possibleTypesToGather.Contains(unit.carriedResource.type)) { typeToGather = unit.carriedResource.type; } else if (possibleTypesToGather.Length == 0) { // Find closest resource type. var resourceNodes = new List <ResourceNode>(); foreach (var type in resourceTypes) { resourceNodes.AddRange(Map.Instance.GetResourceNodesOfType(type)); } if (resourceNodes.Count == 0) { return(false); } ResourceNode closestNode = null; var minDistance = float.MaxValue; foreach (var node in resourceNodes) { var distance = Vector3.Distance(node.transform.position, basePosition); if (!(distance < minDistance)) { continue; } closestNode = node; minDistance = distance; } typeToGather = closestNode.Type; } else { typeToGather = possibleTypesToGather[Random.Range(0, possibleTypesToGather.Length)]; } gatherState = new GatherState(unit, typeToGather); gatherState.OnStateFinish += GatherStateFinishedHandler; PushState(gatherState); return(true); }
private void GatherStateFinishedHandler(State state) { gatherState = null; }