Exemplo n.º 1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (previousPosition != Generator.relativePlayerPosition)
        {
            difference.x    = Mathf.Clamp(Generator.relativePlayerPosition.x - previousPosition.x, -maxDifference, maxDifference);
            difference.y    = Mathf.Clamp(Generator.relativePlayerPosition.y - previousPosition.y, -maxDifference, maxDifference);
            differenceSignX = (int)Mathf.Sign(difference.x);
            differenceSignY = (int)Mathf.Sign(difference.y);
            Vector2 chunkCoordinate = new Vector2();
            for (int x = 0; x < Mathf.Abs(difference.x); x++)
            {
                for (int y = -renderDistance; y < renderDistance + 1; y++)
                {
                    try
                    {
                        Generator.generatedChunks[(int)(previousPosition.x + (x * differenceSignX) - (renderDistance * differenceSignX)), (int)(previousPosition.y + y)].SetActive(false);
                        chunkCoordinate = new Vector2(previousPosition.x - (x * differenceSignX) + (renderDistance + 1) * differenceSignX, previousPosition.y + y);
                        originShift.offsetChunk(Generator.generatedChunks[(int)chunkCoordinate.x, (int)chunkCoordinate.y], chunkCoordinate);
                        Generator.generatedChunks[(int)chunkCoordinate.x, (int)chunkCoordinate.y].SetActive(true);
                    }
                    catch (NullReferenceException)
                    {
                        Debug.Log("Activating Error");
                    }
                }
            }

            for (int x = 0; x < Mathf.Abs(difference.y); x++)
            {
                for (int y = -renderDistance; y < renderDistance + 1; y++)
                {
                    try
                    {
                        Generator.generatedChunks[(int)(previousPosition.x + y), (int)(previousPosition.y + (x * differenceSignY) - renderDistance * differenceSignY)].SetActive(false);
                        chunkCoordinate = new Vector2(previousPosition.x + y, previousPosition.y - (x * differenceSignY) + (renderDistance + 1) * differenceSignY);
                        originShift.offsetChunk(Generator.generatedChunks[(int)chunkCoordinate.x, (int)chunkCoordinate.y], chunkCoordinate);
                        Generator.generatedChunks[(int)chunkCoordinate.x, (int)chunkCoordinate.y].SetActive(true);
                    }
                    catch (NullReferenceException)
                    {
                        Debug.Log("Activating Error");
                    }
                }
            }
            previousPosition = Generator.relativePlayerPosition;
        }
        else
        {
            previousPosition = Generator.relativePlayerPosition;
        }
    }
Exemplo n.º 2
0
 //Checks if a job is completet and applies the generated values to the Mesh and Hands them over to the SceneryPlacer
 void SyncTerrainData()
 {
     if (jobHandles.Count > 0 && jobHandles[jobHandles.Count - 1].IsCompleted)
     {
         jobHandles[jobHandles.Count - 1].Complete();
         TerrainGenerationJob job   = jobs[jobs.Count - 1];
         GameObject           chunk = generatedChunks[(int)(job.chunkArrayCoordinates.x), (int)(job.chunkArrayCoordinates.y)];
         chunk.layer = 8;
         Transform chunkTransform = chunk.transform;
         chunkTransform.parent = worldTransform;
         originShift.offsetChunk(chunk, job.chunkArrayCoordinates);
         chunk.isStatic = true;
         MeshRenderer meshRenderer = chunk.AddComponent <MeshRenderer>();
         meshRenderer.sharedMaterials = new Material[2] {
             terrainMaterial, grassMaterial
         };
         MeshFilter meshFilter = chunk.AddComponent <MeshFilter>();
         meshFilter.mesh = GeneratePlane(job.vertices.ToArray());
         meshRenderer.shadowCastingMode = ShadowCastingMode.TwoSided;
         meshRenderer.lightProbeUsage   = LightProbeUsage.Off;
         MeshCollider chunkCollider = chunk.AddComponent <MeshCollider>();
         chunkCollider.sharedMesh = meshFilter.mesh;
         sceneryPlacer.PlaceScenery(chunk, job.stoneData.ToArray());
         job.Dispose();
         jobs.RemoveAt(jobs.Count - 1);
         jobHandles.RemoveAt(jobHandles.Count - 1);
     }
 }