int IComparer.Compare(System.Object _first, System.Object _second)
        {
            TransformComparer f = _first as TransformComparer;
            TransformComparer s = _second as TransformComparer;

            if (f == null && s == null)
            {
                return(0);
            }

            if (f == null)
            {
                return(-1);
            }

            if (s == null)
            {
                return(1);
            }

            return(m_x.CompareTo(s.m_x));
        }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        if (Time.time > nextSquadSensingTime)
        {
            nextSquadSensingTime += squadSensingInterval;

            enemyTransformsInRange.Clear();


            int layermask = 0;

            if (teamID == 0)
            {
                layermask = 1 << 13;
            }
            else if (teamID == 1)
            {
                layermask = 1 << 12;
            }

            enemyCollidersInRange = Physics.OverlapSphere(transform.position, squadSensingRadius, layermask);

            for (int i = 0; i < enemyCollidersInRange.Length; i++)
            {
                enemyTransformsInRange.Add(enemyCollidersInRange[i].GetComponent <Transform>());
            }
            //Debug.Log(enemyTransformsInRange.Count);

            //order them so the units can perform a kind of heuristic which is nearest
            enemies = new List <Transform>(enemyTransformsInRange);

            var comparer = new TransformComparer {
                myTransform = transform
            };
            enemies.Sort(comparer);
        }
    }
        private void Triangulate()
        {
            // From O'Rourke (1994) Computational Geometry in C, section 2.1
            // Monotone Partitioning

            // To identify the chains: The vertices in each chain of a monotone
            // polygon are sorted with respect to the line of monotonicity [y-axis].
            // Then the vertices can be sorted by the y-axis in linear time: Find a
            // Highest vertex, find a lowest, and partition the boundary between the two
            // chains. The vertices in each chain are sorted with respect to y.
            // Two sorted lists of vertices can be merged in linear time into one list
            // sorted by y.
            Debug.Assert(face.EdgeCount > 3);
            var vertices = face.Vertices.Cast <TVertex>();

            IComparer <Point2D> pointComparer = new HighYLowXComparer();
            // TODO: Replace with Transform extension method. Consider removing transform comparer
            IComparer <TVertex>     vertexComparer = new TransformComparer <TVertex, Point2D>(pointComparer, v => v.Position);
            Pair <TVertex, TVertex> minMax         = vertices.MinMax(vertexComparer);

            meshUtilities = new MonotoneMeshUtilities <TVertex>(pointComparer);

            LeftToRightEdgeComparer xEdgeComparer = new LeftToRightEdgeComparer();

            sweeplineUtilities = new SweeplineUtilities(xEdgeComparer);

            // Assign each vertex to the left or right chain
            IEnumerable <ChainVertex> leftChainReversed = TraceLeftAndUp(minMax.First);
            IEnumerable <ChainVertex> leftChain         = leftChainReversed.Reverse();
            IEnumerable <ChainVertex> rightChain        = TraceRightAndDown(minMax.Second);

            // From Berg et al (2000) Computational Geometry Algorithms and Applications

            // 1. Merge the vertices on the right chain with those on the left
            // chain into one sequence sorted on decreasing y-coordinate. If
            // two vertices have the same y-coordinate the left one comes first.
            // Let u1 to un denote the sorted sequence.

            IComparer <ChainVertex>   chainVertexComparer = vertexComparer.Transform <ChainVertex, TVertex>(p => p.Vertex).Invert();
            IEnumerable <ChainVertex> mergedVertices      = rightChain.MergeSorted(leftChain, chainVertexComparer);
            List <ChainVertex>        u = new List <ChainVertex>(mergedVertices);

            // 2. Initialize an empty stack and push u1 and u2 onto it
            // This stack contains all the vertices of the polygon that have
            // already been encountered, but which may require additional diagonals
            Stack <ChainVertex> stack = new Stack <ChainVertex>();

            stack.Push(u[0]);
            stack.Push(u[1]);

            for (int j = 2; j < u.Count - 1; ++j)
            {
                // If u[j] and the vertext on top of the stack are of different chains
                if (u[j].Chain != stack.Peek().Chain)
                {
                    // Insert a diagonal from u[j] to each popped vertex...
                    // We progessively subdivide the new faces created by the splitting
                    // so we must keep track of which face to split
                    FaceBase faceToSplit = face;
                    while (stack.Count > 1)
                    {
                        TEdge edge = splitFace(mesh, faceToSplit, u[j], stack.Pop());
                        faceToSplit = edge.Faces.First; // First is always the new face
                    }
                    // ... except the last one
                    if (stack.Count > 0)
                    {
                        stack.Pop();
                    }
                    // Push u j-1 and uj onto the stack
                    stack.Push(u[j - 1]);
                    stack.Push(u[j]);
                }
                else
                {
                    // Pop one vertex from the stack; this vertex is already connected
                    ChainVertex previous = stack.Pop();
                    // We progressively split triangles from the original face
                    TFace faceToSplit = face;
                    while (stack.Count > 0 && CanInsertDiagonal(u[j], stack.Peek(), previous))
                    {
                        previous = stack.Pop();
                        splitFace(mesh, faceToSplit, u[j], previous);
                    }
                    // Push the last vertex popped back onto the stack
                    stack.Push(previous);
                    // Push uj onto the stack
                    stack.Push(u[j]);
                }
            }
            // Add diagonals from un to all vertices except the first and last one
            stack.Pop();
            // Which chain is the stack of vertices on - affects which face we split
            Chain    stackChain       = stack.Peek().Chain;
            FaceBase finalFaceToSplit = face;

            while (stack.Count > 1)
            {
                TEdge edge = splitFace(mesh, finalFaceToSplit, u[u.Count - 1], stack.Pop());
                finalFaceToSplit = stackChain == Chain.Left ? edge.Faces.First : edge.Faces.Second;
            }
            Debug.Assert(stack.Count == 1);

            triangulated = true;
        }