internal void BreakLink(GravMesh gravMesh)
    {
        m_Gn1.m_Links.Remove(this);

        int gn1LinkIndex = m_Gn1.neighborIndiciesList.IndexOf(m_Gn2.m_Index);

        m_Gn1.neighborIndiciesList.RemoveAt(gn1LinkIndex);
        m_Gn1.restDistancesList.RemoveAt(gn1LinkIndex);

        int gn2LinkIndex = m_Gn2.neighborIndiciesList.IndexOf(m_Gn1.m_Index);

        m_Gn2.neighborIndiciesList.RemoveAt(gn2LinkIndex);
        m_Gn2.restDistancesList.RemoveAt(gn2LinkIndex);

        gravMesh.RemovePair(m_TrianglesIndex);
        m_Gn1.availibleVertexPositions.Enqueue(m_GravNode1VertexStart);
        m_Gn2.availibleVertexPositions.Enqueue(m_GravNode2VertexStart);

        m_Gn1.m_Connections--;
        m_Gn2.m_Connections--;
    }
    internal void MakeLink(GravNode gn1, GravNode gn2, bool draw, GravMesh gravMesh, float thickness, float spacing)
    {
        m_Gn1 = gn1;
        m_Gn2 = gn2;
        m_GravNode1PosIndex = gn1.m_Index;
        m_GravNode2PosIndex = gn2.m_Index;
        m_RestDistance      = spacing;

        gn1.m_Links.Add(this);

        gn1.neighborIndiciesList.Add(gn2.m_Index);
        gn1.restDistancesList.Add(m_RestDistance);

        gn2.neighborIndiciesList.Add(gn1.m_Index);
        gn2.restDistancesList.Add(m_RestDistance);

        m_Draw      = draw;
        m_Thickness = thickness;

        if (draw)
        {
            if (gn1.availibleVertexPositions.Count == 0)
            {
                Debug.LogError("No more vertices are available for rendering this connection");
            }

            m_GravNode1VertexStart = gn1.availibleVertexPositions.Dequeue();

            if (gn2.availibleVertexPositions.Count == 0)
            {
                Debug.LogError("No more vertices are available for rendering this connection");
            }

            m_GravNode2VertexStart = gn2.availibleVertexPositions.Dequeue();
            m_TrianglesIndex       = gravMesh.AddPair(m_GravNode1VertexStart, m_GravNode2VertexStart);
        }

        gn1.m_Connections++;
        gn2.m_Connections++;
    }
示例#3
0
    void Start()
    {
        gravGridCam           = Camera.main;
        fixedDeltaTimeSeconds = (float)fixedDeltaTime / 1000.0f;
        gravGrid  = new GravNode[gravGridDimensions, gravGridDimensions];
        gravNodes = new List <GravNode>(gravGridDimensions * gravGridDimensions);
        float gridSizeWorld = gravGridDimensions * spacing;

        m_GravMesh = new GravMesh(new Vector3(gridSizeWorld, gridSizeWorld, 0.5f), gridSizeWorld, GetComponent <MeshFilter>());

        m_MeshMatcher = new MeshMatcher(gravGridDimensions / sectorSize, parentLayer, spacing, gravGridDimensions);
        m_MeshMatcher.SetAdjacentNodes(rootCoord);

        availableConnectionIndex = new Queue <int>();
        offsets                = new NativeArray <float2>(gravGridDimensions * gravGridDimensions, Allocator.Persistent);
        coordinates            = new NativeArray <int2>(gravGridDimensions * gravGridDimensions, Allocator.Persistent);
        rootCoord              = new int2(startingRootCoordinate.x, startingRootCoordinate.y);
        colliderTransformArray = new TransformAccessArray(gravGridDimensions * gravGridDimensions);

        for (int j = 0, index = 0; j < gravGridDimensions; ++j)
        {
            int   yOffset = (j + mStartWorldY) - rootCoord.y;
            float s       = ((float)yOffset / (float)gravGridDimensions + 1.0f) / 2.0f;


            for (int i = 0; i < gravGridDimensions; ++i, index++)
            {
                int   xOffset = (i + mStartWorldX) - rootCoord.x;
                float t       = ((float)xOffset / (float)gravGridDimensions + 1.0f) / 2.0f;

                Vector3 position         = new Vector3(i * spacing, j * spacing, 0);
                int     vertexStartIndex = m_GravMesh.AddNode();
                offsets[index]     = new float2(t, s);
                coordinates[index] = new int2(i, j);

                var gn = new GravNode(position, spacing, index, transform, vertexStartIndex, gameObject.layer);
                colliderTransformArray.Add(gn.gravNodeColliderParent.transform);
                gn.AddForce          = ApplyForce;
                gn.SetTargetLocation = SetTargetLocation;
                gn.Return            = SetTargetToReturnPosition;
                gn.GetNodePosition   = GetNodePosition;
                gn.GetReturnPosition = GetReturnPosition;
                gravGrid[i, j]       = gn;
                gravNodes.Add(gn);
            }
        }

        nodeNumConnections    = new NativeArray <int>(gravNodes.Count, Allocator.Persistent);
        startConnectionsIndex = new NativeArray <int>(gravNodes.Count, Allocator.Persistent);
        allNodeConnections    = new NativeArray <int>(gravNodes.Count * MAX_CONNECTIONS, Allocator.Persistent);
        allRestsDistances     = new NativeArray <float>(gravNodes.Count * MAX_CONNECTIONS, Allocator.Persistent);

        targetPositions     = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        returnPositions     = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        affected            = new NativeArray <bool>(gravNodes.Count, Allocator.Persistent);
        prevPositions       = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        positions           = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        newPositions        = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        accelerations       = new NativeArray <float3>(gravNodes.Count, Allocator.Persistent);
        moveables           = new NativeArray <bool>(gravNodes.Count, Allocator.Persistent);
        forceUpdatePosition = new NativeArray <bool>(gravNodes.Count, Allocator.Persistent);

        for (int j = 0; j < gravNodes.Count; j++)
        {
            accelerations[j]   = gravNodes[j].m_Acceleration;
            prevPositions[j]   = gravNodes[j].m_PrevPosition;
            targetPositions[j] = gravNodes[j].m_TargetPosition;
            returnPositions[j] = gravNodes[j].m_ReturnPosition;
            affected[j]        = false;
            moveables[j]       = gravNodes[j].m_Moveable;
            positions[j]       = gravNodes[j].m_Position;
        }

        ResetConnections();
    }
示例#4
0
    public void AddAndSetupConnectionAtNextIndex(GravNode gn1, GravNode gn2, bool draw, GravMesh gravMesh)
    {
        if (availableConnectionIndex.Count == 0)
        {
            Debug.LogError("There are no available connections.");
        }
        s_CreateNewLink.Begin();
        int index = availableConnectionIndex.Dequeue();

        //We're not going to update the thickness data as to preserve the sector location.
        connections[index].MakeLink(gn1, gn2, draw, gravMesh, 0, spacing);
        s_CreateNewLink.End();

        gn1Index[index]              = connections[index].m_GravNode1PosIndex;
        gn2Index[index]              = connections[index].m_GravNode2PosIndex;
        drawables[index]             = connections[index].m_Draw;
        gn1VertexStartIndices[index] = connections[index].m_GravNode1VertexStart;
        gn2VertexStartIndices[index] = connections[index].m_GravNode2VertexStart;

        int gn1I = gn1.m_Index;
        int gn2I = gn2.m_Index;

        nodeNumConnections[gn1I] = gn1.m_Connections;
        nodeNumConnections[gn2I] = gn2.m_Connections;
        int connectionsStart = startConnectionsIndex[gn1I];

        for (int i = 0; i < gn1.m_Connections; i++)
        {
            allNodeConnections[i + connectionsStart] = gn1.neighborIndiciesList[i];
            allRestsDistances[i + connectionsStart]  = gn1.restDistancesList[i];
        }

        connectionsStart = startConnectionsIndex[gn2I];
        for (int i = 0; i < gn2.m_Connections; i++)
        {
            allNodeConnections[i + connectionsStart] = gn2.neighborIndiciesList[i];
            allRestsDistances[i + connectionsStart]  = gn2.restDistancesList[i];
        }
    }
示例#5
0
    public void AddConnection(GravNode gn1, GravNode gn2, bool draw, float thickness, GravMesh gravMesh)
    {
        Link l = new Link(gn1, gn2, draw, gravMesh, thickness, connections.Count, spacing);

        connections.Add(l);
    }
 public Link(GravNode gn1, GravNode gn2, bool draw, GravMesh gravMesh, float thickness, int index, float spacing)
 {
     m_Index = index;
     MakeLink(gn1, gn2, draw, gravMesh, thickness, spacing);
 }