/// <summary>Resets values related to pathfinding and group movement</summary> public void ResetValues() { rePathTime = 0; AssignedPosition = null; AssignedGroupId = -1; OnGroupOrder = null; SetDefaultPriorityValues(); }
/// <summary>Returns the unit in given unit list that is closest to the given formation position</summary> private Unit ClosestUnitToFormationPosition(List<Unit> units, FormationPosition position) { float closestSqrMagnitude = float.MaxValue; Unit closest = null; foreach (Unit unit in units) { float sqrmagnitude = (unit.transform.position - position.Point(false)).sqrMagnitude; if (sqrmagnitude < closestSqrMagnitude) { closest = unit; closestSqrMagnitude = sqrmagnitude; } } return closest; }
/// <summary>Updates the formation positions with formation setting</summary> private void UpdateFormationPositions(FormationSetting setting) { ClearFormationPositions(); formationName = setting.Name; gizmoColor = setting.GizmoColor; gizmoRadius = setting.GizmoRadius; for (int i = 0; i < setting.Size; i++) { FormationPosition position = Instantiate(prefabFormationPosition, transform).GetComponent<FormationPosition>(); position.transform.localPosition = setting.LocalPositions[i]; position.SetGizmo(gizmoColor, gizmoRadius); formationPositions.Add(position); } }
/// <summary>Returns the closest formation position in given list to the center of the formation</summary> private FormationPosition ClosestToCenterOfFormation(List<FormationPosition> positions) { Vector3 center = GetCenterOfFormation(); float closestSqrMagnitude = float.MaxValue; FormationPosition closest = null; foreach (FormationPosition position in positions) { float sqrmagnitude = (position.transform.position - center).sqrMagnitude; if (sqrmagnitude < closestSqrMagnitude) { closest = position; closestSqrMagnitude = sqrmagnitude; } } return closest; }
/// <summary>Assigns each unassigned formation position to a unit based on distance</summary> private Unit AssignUnitsToPositions(List<Unit> units) { Unit commander = null; for (int assignments = 0; assignments < units.Count; assignments++) { FormationPosition closestToCenter = ClosestToCenterOfFormation(formationPositions.UnAssigned()); closestToCenter.SetAssigned(true); Unit closestUnit = ClosestUnitToFormationPosition(units.UnAssigned(), closestToCenter); closestUnit.AssignPosition(closestToCenter); float perc = assignments / (float)units.Count; closestUnit.AssignPrioritySpeed(minSpeed + ((translateSpeed * perc) * prioritySpeedMultiplier)); if (assignments == 0) { commander = closestUnit; } } return commander; }
/// <summary>creates a new formation positions adding it to the formation positions list aswell</summary> public void CreateFormationPosition() { FormationPosition position = Instantiate(prefabFormationPosition, transform).GetComponent<FormationPosition>(); position.SetGizmo(gizmoColor, gizmoRadius); formationPositions.Add(position); }
/// <summary>Assigns a formation position to this unit</summary> public void AssignPosition(FormationPosition position) { AssignedPosition = position; }