public override void Generate(TileData data, StopToken stop)
        {
            Coord locality = TownGlobalObject.GetIndexAtCoord(data.area.Coord);

            if (TownGlobalObject.townsData[locality] == null)
            {
                return;
            }

            //SplineSys src = data.ReadInletProduct(splineIn);
            SplineSys src = data.ReadInletProduct(this);

            // MatrixWorld heights = data.ReadInletProduct(heightIn);
            if (src == null)
            {
                return;
            }

            if (!enabled)// || heights == null)
            {
                data.StoreProduct(this, src); return;
            }


            if (stop != null && stop.stop)
            {
                return;
            }
            SplineSys dst = new SplineSys(src);

            // at this point cleanse. it should be clean. but clean anyway?
            dst.Clamp((Vector3)data.area.active.worldPos, (Vector3)data.area.active.worldSize);


            // WE only do main stage
            if (data.isDraft)
            {
                data.StoreProduct(this, dst); return;
            }

            if (dst.lines.Length == 0)
            {
                // null. there are no segments to add.
                //  TownGlobalObject.GetSplineList(data.area.Coord);
                data.StoreProduct(this, dst);
                return;
            }
            // this is already completed processed by all threads /n/ times and marked as done in OnTileApplied()
            else if (TownGlobalObject.isSplinesMeshRenderedOnTile.ContainsKey(data.area.Coord))
            {
                return;
            }

            //{
            //    // we already processed the tile.
            //    data.StoreProduct(this, dst);
            //    return;
            //}


            // these might not be linked but probably are???
            foreach (var item in dst.lines)
            {
                var subdic  = new Dictionary <Coord, List <Vector3> >();
                var sublist = new List <SplineMesh.Spline>();

                // track to ensure we have at a minimum two nodes...
                int nodesInThisSegmentGroup = 0;


                // use this rather than the monobehaviour
                TypedSpline typed = new TypedSpline();

                //   SplineMesh.Spline mysm = new SplineMesh.Spline();

                // these should be linked  - Hopefully this will pickup on a partially aborted WHILE

                for (int segmentindex = 0; segmentindex < item.segments.Length; segmentindex++)
                {
                    // They will be in pairs by now. we need to do the logic to string them back together, here.

                    Segment segmentToProcess = item.segments[segmentindex];

                    // nodesInThisSegmentGroup = AddNodeReturnIncrementedTotalCount(data, nodesInThisSegmentGroup, segmentToProcess, mysm);

                    nodesInThisSegmentGroup = AddNodeToListReturnIncrementedTotalCount(nodesInThisSegmentGroup, segmentToProcess, typed.nodes);

                    segmentindex++;
                }
                typed.chosenType = (int)chosenType;
                typed.tryToFloor = TryToFloor;

                //update a value
                if (TownGlobalObject.splinesNodesDataForTile.ContainsKey(data.area.Coord))
                {
                    TownGlobalObject.splinesNodesDataForTile[data.area.Coord].Add(typed);
                }
                else
                {// create a new one
                    TownGlobalObject.GetSplineList(data.area.Coord).Add(typed);
                }
                //TownGlobalObject.splinesNodesDataForTile.AddOrUpdate(data.area.Coord, TownGlobalObject.GetSplineList(data.area.Coord), (key, current) => { current.Add(typed);  return current; }   );

                //  TownGlobalObject.GetSplineList(data.area.Coord).Add(typed);


                // here we would loop if the segmentindex has not been reached.
                // might have to watch for weird single node edge cases but oh well. Bridge. Come to it.
            }

            // We just store the original.

            //   data.StoreProduct(this, src);

            // We store TRUNCATED copy.

            data.StoreProduct(this, dst);
        }
Exemplo n.º 2
0
    public static void OnTileAppliedRenderMeshSplines(TerrainTile tile, TileData data, StopToken stop)
    {
        /*
         *
         * IN THE NEXT SECTION WE HANDLE THE SPLINE MESH RENDERING
         *
         *
         */

        //TODO: Just make a parent holder for all splines on a tile and delete THAT


        //for (int i = tile.transform.childCount - 1; i > 0; i--)
        //{

        //    if (tile.transform.GetChild(i).name.StartsWith("__SPLINE__"))
        //        DestroyImmediate(tile.transform.GetChild(i).gameObject);

        //}

        // we only do this on Main pass and when we actually have made something to process.
        if (data.isDraft)// || !TownGlobalObject.splinesNodesDataForTile.ContainsKey(data.area.Coord))
        {
            return;
        }

        //  int numberOfSplineNodes = 0;


        if (!TownGlobalObject.splinesNodesDataForTile.ContainsKey(data.area.Coord))
        {
            // this is not a mesh spline tile
            return;
        }

        // By this point this should absolutely exist - unless there is no spline data!
        int totalNumberOfListsOfSplineMeshSplines = TownGlobalObject.splinesNodesDataForTile[data.area.Coord].Count;

        // There is nothing in the list
        if (totalNumberOfListsOfSplineMeshSplines == 0)
        {
            return;
        }


        //public void Test(ConcurrentDictionary<string, int> dictionary)
        //{
        //    dictionary.AddOrUpdate("key", 1, (key, current) => current += 2);
        //}



        //  TownHolder.Instance.SplineMeshWallMasterSpawner.SetActive(false);

        List <GameObject> thingsToActivate = new List <GameObject>();



        Coord locality = TownGlobalObject.GetIndexAtCoord(data.area.Coord);



        // Coord offset = locality - data.area.Coord;

        Coord offset = data.area.Coord;

        // SplinePowerExtended

        var DynamicHolder = TownHolder.Instance.MapMagicObjectReference.transform.Find(string.Format("Tile {0},{1}", offset.x, offset.z));


        // For like the 4th time we check this  TODO: Make it part of the Town Instancing
        TownGlobalObject.townsData[locality].TownGameObject ??= new GameObject(TownGlobalObject.townsData[locality].name);


        //create or use the holder now it has the right name.
        var go = TownGlobalObject.townsData[locality].TownGameObject;



        // We walk over the nodes assuming pairs?

        for (int i = 0; i < totalNumberOfListsOfSplineMeshSplines; i++)
        {
            //SplineMesh.Spline();
            TypedSpline newValue = TownGlobalObject.GetSplineList(data.area.Coord)[i];

            // No splines for us...
            if (newValue.nodes.Count == 0)
            {
                continue;
            }


            if ((newValue.nodes[1].Position - newValue.nodes[0].Position).sqrMagnitude == 0)
            {
                continue;
            }

            //  Transform child = null;


            // TODO make this an actual hash and shove it in a table
            // string hash = string.Format("{0}_{1}_{4}_{5}|{2}_{3}", startvec.x, startvec.y, startvec.z, endvec.x, endvec.y, endvec.z);
            //  string fullhash = string.Format("{0}_{1}|{4}_{5}|{2}_{3}", startvec.x, startvec.y, startvec.z, endvec.x, endvec.y, endvec.z);
            string hash = string.Format("__SPLINE__{0}__{2}|{3}__{5}", newValue.nodes[0].Position.x, newValue.nodes[0].Position.y, newValue.nodes[0].Position.z, newValue.nodes[newValue.nodes.Count - 1].Position.x, newValue.nodes[newValue.nodes.Count - 1].Position.y, newValue.nodes[newValue.nodes.Count - 1].Position.z);


            // TODO replace this with a lookup  SOOOOON


            //foreach (Transform children in DynamicHolder.parent)
            //{
            //   // if(child.IsNull())
            //    child ??= children.Find(hash);
            //}

            //// we already rendered this  remove the gmeobject then and do it again.?
            //if (child !=null)
            //    {
            //        var code = child.GetComponent<SplineMesh.Spline>();
            //        bool wipeit = false;
            //        for (int nodeat = 0; nodeat < code.nodes.Count; nodeat++)
            //        {

            //            var thisnode = code.nodes[nodeat];
            //            if (thisnode.Position.y == 0)
            //            {
            //                wipeit = true;
            //            }
            //        }

            //        if (wipeit)
            //        {
            //            DestroyImmediate(child.gameObject);
            //        }
            //        else
            //        {
            //            continue;
            //        }

            //    }



            var newSpline = ((objectRendered)newValue.chosenType) switch
            {
                objectRendered.wall => GameObject.Instantiate(TownHolder.Instance.SplineMeshWallMasterSpawner, DynamicHolder, true),
                objectRendered.fence => GameObject.Instantiate(TownHolder.Instance.SplineMeshFenceMasterSpawner, DynamicHolder, true),
                objectRendered.gatehouse => GameObject.Instantiate(TownHolder.Instance.SplineMeshGatehouseMasterSpawner, DynamicHolder, true),
                _ => GameObject.Instantiate(TownHolder.Instance.SplineMeshWallMasterSpawner, DynamicHolder, true),
            };

            newSpline.name = hash + ((objectRendered)newValue.chosenType).ToString();

            newSpline.GetComponent <SplineMesh.Spline>().nodes = newValue.nodes;


            if (newValue.tryToFloor)
            {
                var scrp = newSpline.AddComponent <AlignNodesToTerrainOnEnable>();

                //  scrp.checkVal = 0.07f;
            }

            thingsToActivate.Add(newSpline);


            // mark it as finally processed. but only if we havent already.

            //  if (!TownGlobalObject.isSplinesMeshRenderedOnTile.ContainsKey(data.area.Coord))
            //       TownGlobalObject.isSplinesMeshRenderedOnTile.Add(data.area.Coord, true);
        }

        // Activate them as a group.
        for (int i = 0; i < thingsToActivate.Count; i++)
        {
            thingsToActivate[i].SetActive(true);
        }
    }