コード例 #1
0
 private bool Intersects(ElementSphere sphereA, ElementSphere sphereB) {
     return MyMath.DoSpheresIntersect(sphereA.Center, sphereA.Radius, sphereB.Center, sphereB.Radius);
 }
コード例 #2
0
    /// <summary>
    /// Positions the provided elements randomly inside a sphere surrounding the HQ element 
    /// in such a way that the CollisionAvoidanceZones are not in contact.
    /// </summary>
    /// <param name="radius">The radius of the sphere in units.</param>
    /// <param name="hqElement">The hq element.</param>
    /// <param name="elementsToPositionAroundHQ">The non-HQ elements to position.</param>
    /// <returns>
    ///   <c>true</c> if all elements were successfully positioned without overlap.
    /// </returns>
    private bool TryPositionRandomWithinSphere(float radius, AUnitElementItem hqElement, AUnitElementItem[] elementsToPositionAroundHQ) {
        IList<ElementSphere> allElementSpheres = new List<ElementSphere>();

        ElementSphere hqElementSphere = new ElementSphere(hqElement);
        allElementSpheres.Add(hqElementSphere);

        int iterateCount = 0;
        Vector3[] formationStationOffsets = new Vector3[elementsToPositionAroundHQ.Length];
        for (int i = 0; i < elementsToPositionAroundHQ.Length; i++) {
            Vector3 candidateStationOffset = UnityEngine.Random.insideUnitSphere * radius;
            AUnitElementItem elementCandidate = elementsToPositionAroundHQ[i];
            ElementSphere elementCandidateSphere = new ElementSphere(elementCandidate);
            elementCandidateSphere.Center = candidateStationOffset;
            if (allElementSpheres.All(es => !Intersects(es, elementCandidateSphere))) {
                // candidate doesn't intersect with any spheres already present
                allElementSpheres.Add(elementCandidateSphere);
                formationStationOffsets[i] = candidateStationOffset;
                iterateCount = Constants.Zero;
            }
            else {
                i--;
                iterateCount++;
                if (iterateCount >= 10) {    // HACK
                    D.Warn("{0}.{1} had a positioning iteration error.", _unitCmd.FullName, GetType().Name);
                    return false;
                }
            }
        }

        _unitCmd.PositionElementInFormation(hqElement, Vector3.zero);
        for (int i = 0; i < elementsToPositionAroundHQ.Length; i++) {
            _unitCmd.PositionElementInFormation(elementsToPositionAroundHQ[i], formationStationOffsets[i]);
            //elementsToPosition[i].transform.localPosition = localFormationPositions[i];   // won't work as the position of the Element's parent is arbitrary
        }
        return true;
    }