/// <summary> /// Variation of the takeStep method. The unit will move to the center of the targetCell. (x + 0.5f, y + 0.5f) /// </summary> /// <returns>true if at the end of the path, false otherwise.</returns> private bool takeStepMiddle(UnitComponent unit) { bool completed = false; // Check if we are at the center of the target Cell if (unit.PointLocation.X == path[0].X + CENTER && unit.PointLocation.Y == path[0].Y + CENTER) { // Remove the next Cell in the path. If that was the last Cell, we're done path.RemoveAt(0); if (path.Count == 0) { completed = true; } } // If we aren't at the center of the next Cell, and that Cell is vacant, move towards it else if (isNextCellVacant(unit)) { if (waiting) { waiting = false; ticksWaiting = 0; } moveMiddle(unit); } // Otherwise, the next Cell in our path is not vacant; wait for a while or compute a new path else { waiting = true; ticksWaiting++; // We've been waiting long enough, compute a new path. if (ticksWaiting % WAIT_TICKS == 0) { path = FindPath.between(map, map.GetCellAt((int)unit.PointLocation.X, (int)unit.PointLocation.Y), map.GetCellAt((int)targetX, (int)targetY)); } } return(completed); }
/// <summary> /// The Work function. Creates a path if necessary, then moves along it. /// </summary> /// <returns>True once we have reached the destination, false otherwise.</returns> public override bool Work() { // This unit is now moving unit.State = UnitComponent.UnitState.MOVING; // If we have no path, create one if (path == null) { float startX = unit.PointLocation.X; float startY = unit.PointLocation.Y; path = FindPath.between(map, map.GetCellAt((int)startX, (int)startY), map.GetCellAt((int)targetX, (int)targetY)); path.RemoveAt(0); } // Zero length path, done moving bool completed = true; if (path.Count != 0) { if (waiting) { if (ticksSinceLastMove % WAIT_TICKS == 0) { waiting = false; ticksSinceLastMove = 1; } } if (waiting == false && ticksSinceLastMove % TICKS_PER_MOVE == 0) { completed = takeStepMiddle(unit); waiting = true; ticksSinceLastMove = 0; } ticksSinceLastMove++; completed = false; } return(completed); }