private void Awake() { pathfinder = GameObject.FindObjectOfType <PathfinderAStar>(); pathRequester = GameObject.FindObjectOfType <PathRequester>(); navAgent = GetComponent <NavAgent>(); parentAICore = GetComponent <AICore>(); }
// Start is called before the first frame update private void Awake() { pathFinder = GetComponent <PathfinderAStar>(); pathRenderer = GetComponent <PathRenderer>(); pathVisualizer = GetComponent <PathVisualizer>(); unitHandler = GameObject.FindObjectOfType <CurrentUnitHandler>(); }
/// <summary> /// How the AI unit reacts to sensor stimuli, manages its perception level etc. Acts as a hub, connecting behaviours. /// </summary> // Start is called before the first frame update private void Awake() { pathfinder = GameObject.FindObjectOfType <PathfinderAStar>(); pathRequester = GameObject.FindObjectOfType <PathRequester>(); navAgent = GetComponent <NavAgent>(); patrol = GetComponent <Patrol>(); unit = GetComponent <Unit>(); }
public void GoToCell(Cell target) { if (carrying != null) { world.DropSoftObject(this, currentCell, carrying.Count); } AbandonJob(); isIdling = false; pathfinder = new PathfinderAStar(world, currentCell, target); if (pathfinder.Length != 0) { destinationCell = target; } }
void FindJob() { currentJob = world.JobManager.FindJobForPawn(this); if (currentJob == null) { Console.Write("no jobs found"); return; } isIdling = false; currentJob.SetReserved(true); destinationCell = currentJob.cell; currentJob.RegisterJobCompletedCallback(JobFinished); pathfinder = FindPathToCurrentJob(true); if (pathfinder == null) { currentJob.SetReachable(false); AbandonJob(); } }
PathfinderAStar FindPathToCurrentJob(bool withNeighbors) { PathfinderAStar newPathfinder = new PathfinderAStar(world, currentCell, destinationCell); // May not be used if going to soft object first but need this for verifying that we can reach target area if (newPathfinder.Length == 0) { if (!withNeighbors) { return(null); } foreach (Cell neighbor in destinationCell.GetNeighbors(false)) { newPathfinder = new PathfinderAStar(world, currentCell, neighbor); if (newPathfinder != null && newPathfinder.Length > 0) { return(newPathfinder); } } return(null); } return(newPathfinder); }
void UpdateMovement(float deltaTime) { if (IsIdling && destinationCell == currentCell) { if (idleCooldown <= 0) { Cell randomCell = null; Random rand = new Random(); while (randomCell == null) { int x = currentCell.X; int y = currentCell.Y; x += rand.Next(-2, 2); y += rand.Next(-2, 2); randomCell = world.GetCellAt(x, y); } pathfinder = new PathfinderAStar(world, currentCell, randomCell); destinationCell = pathfinder.LastCell(); idleCooldown = 3f; } else { idleCooldown -= deltaTime; } return; } // If pawn is at destination, remove pathfinder if (currentCell == destinationCell) { pathfinder = null; isIdling = currentJob == null; return; } // Calculate next cell to go to, if there isn't one already in progress if (nextCell == null || nextCell == currentCell) { if (pathfinder == null || pathfinder.Length == 0) { bool withNeighbors = currentJob != null && currentJob.cell == destinationCell; pathfinder = FindPathToCurrentJob(withNeighbors); if (pathfinder == null) { if (!IsIdling) { AbandonJob(); } return; } } NextCell = pathfinder.Dequeue(); } // Increase movement to destination cell int dX = currentCell.X - nextCell.X; int dY = currentCell.Y - nextCell.Y; float travelDistance = (float)Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2)); float frameDistance = (speed * (IsIdling ? 0.5f : 1f)) / nextCell.MovementCost * deltaTime; float framePercentage = frameDistance / travelDistance; movementPercentage += framePercentage; if (movementPercentage >= 1) { CurrentCell = nextCell; movementPercentage = 0f; // TODO: set to 0? test this out } }
void UpdateJob(float deltaTime) { // Find Job if (currentJob == null && IsIdling) { jobSearchCooldown -= deltaTime; if (jobSearchCooldown > 0) { return; } FindJob(); if (currentJob == null) { jobSearchCooldown = 0.5f; //destinationCell = currentCell; return; } } if (!IsIdling && currentJob == null) { return; } if (!currentJob.MeetsBuildRequirements()) { SoftObject next = currentJob.NextRequirement(); if (carrying != null) // We are carrying something { if (carrying.Type == next.Type) // We are carrying the thing that's needed { if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell)) // We are in the job cell, TODO: understand / fix everything in this branch not sure it works with count and stackCount for soft objects { currentJob.DepositSoftObject(carrying); carrying.ReduceCount(next.Count); if (carrying.Count == 0) { carrying = null; } else { world.DropSoftObject(this, currentCell, Carrying.Count); } } else // We aren't in the job cell, keep moving towards the destination { destinationCell = currentJob.cell; return; } } else // We don't have required inventory, drop current thing { world.DropSoftObject(this, currentCell, Carrying.Count); } } else // We aren't carrying anything { if (currentCell.SoftObject != null && currentCell.SoftObject.Type == next.Type) // We're standing on top of a cell with required inventory { world.PickupSoftObject(this, currentCell, currentCell.SoftObject.Count); } else // Need to walk to nearest required inventory { if (currentCell != nextCell) // We're moving somewhere rn so keep doing that { return; } Cell lastCell = pathfinder == null ? null : pathfinder.LastCell(); if (pathfinder == null || lastCell == null || lastCell.SoftObject == null || lastCell.SoftObject.Type != next.Type) // we ain't going somewhere useful so recalculate { PathfinderAStar newPathfinder = world.PathForNearestSoftObject(next.Type, currentCell); if (newPathfinder == null || newPathfinder.Length == 0) { Console.WriteLine("Abandoning because pathfinder unsuccessful for soft object search"); AbandonJob(); return; } pathfinder = newPathfinder; destinationCell = pathfinder.LastCell(); NextCell = pathfinder.Dequeue(); } } } return; } // Work Job // destinationCell = currentJob.cell; if (currentCell == currentJob.cell || currentCell.GetNeighbors(false).Contains(currentJob.cell)) { currentJob.Progress(deltaTime, this); } }
private void Awake() { pathfinder = GameObject.FindObjectOfType <PathfinderAStar>(); }