Search Unity

Cave generator doesnt work.

Discussion in '2D' started by dmitriybee2007, Oct 28, 2020.

  1. dmitriybee2007

    dmitriybee2007

    Joined:
    Oct 28, 2020
    Posts:
    1
    *I have a code for generate a chunk 80x100 blocks with caves and ore and i have a gameObject "CaveChunk" with this script,but if i move my cave chunk gameObject,my caves still generate at the surface.And yes,i know i need to make parents to my block prefabs in script,but i dont know how i can do it in this code,please help me.*
    **How fix that thing?**

    **Link to code**- https://pastebin.com/4hkfQCf9
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CaveChunkGen : MonoBehaviour
    6. {
    7.     public int width;
    8.     public int height;
    9.     public int smoothCycles;
    10.  
    11.     private int[,] cavePoints;
    12.  
    13.     [Range(0, 100)]
    14.     public int randFillPercent;
    15.     [Range(0, 8)]
    16.     public int threshold;
    17.  
    18.     public GameObject BlackGranite;
    19.     public GameObject GoldGraniteOre;
    20.     public GameObject MythryliumGraniteOre;
    21.     public GameObject CaveChunk;
    22.  
    23.     public float chanceGold;
    24.     public float chanceMythrilium;
    25.  
    26.     private void Awake()
    27.     {
    28.         GenerateCave();
    29.     }
    30.  
    31.     void Start()
    32.     {
    33.         PlaceGrid();
    34.     }
    35.  
    36.     private void GenerateCave()
    37.     {
    38.         cavePoints = new int[width, height];
    39.  
    40.         int seed = Random.Range(0, 1000000);
    41.         System.Random randChoice = new System.Random(seed.GetHashCode());
    42.  
    43.         for (int x = 0; x < width; x++)
    44.         {
    45.             for (int y = 0; y < height; y++)
    46.             {
    47.                 if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
    48.                 {
    49.                     cavePoints[x, y] = 1;
    50.                 }else if (randChoice.Next(0, 100) < randFillPercent)
    51.                 {
    52.                     cavePoints[x, y] = 1;
    53.                 }else
    54.                 {
    55.                     cavePoints[x, y] = 0;
    56.                 }
    57.             }
    58.         }
    59.  
    60.         for (int i = 0; i < smoothCycles; i++)
    61.         {
    62.             for (int x = 0; x < width; x++)
    63.             {
    64.                 for (int y = 0; y < height; y++)
    65.                 {
    66.                     int neighboringWalls = GetNeighbors(x, y);
    67.  
    68.                     if (neighboringWalls > threshold)
    69.                     {
    70.                         cavePoints[x, y] = 1;
    71.                     }else if (neighboringWalls < threshold)
    72.                     {
    73.                         cavePoints[x, y] = 0;
    74.                     }
    75.                 }
    76.             }
    77.         }
    78.     }
    79.  
    80.     private int GetNeighbors(int pointX, int pointY)
    81.     {
    82.  
    83.         int wallNeighbors = 0;
    84.  
    85.         for (int x = pointX - 1; x <= pointX + 1; x++)
    86.         {
    87.             for (int y = pointY - 1; y <= pointY + 1; y++)
    88.             {
    89.                 if (x >= 0 && x < width && y >= 0 && y < height)
    90.                 {
    91.                     if (x != pointX || y!= pointY)
    92.                     {
    93.                         if (cavePoints[x,y] == 1)
    94.                         {
    95.                             wallNeighbors++;
    96.                         }
    97.                     }
    98.                 }
    99.                 else
    100.                 {
    101.                     wallNeighbors++;
    102.                 }
    103.             }
    104.         }
    105.  
    106.         return wallNeighbors;
    107.     }
    108.  
    109.     public void PlaceGrid()
    110.     {
    111.         for (int x = 0; x < width; x++)
    112.         {
    113.             for (int y = 0; y < height; y++)
    114.             {
    115.                 if (cavePoints[x,y] == 1)
    116.                 {
    117.                    Instantiate(BlackGranite, new Vector2(x, y), Quaternion.identity,BlackGranite.transform.parent = CaveChunk.transform);
    118.                 }
    119.             }
    120.         }
    121.         Populate();
    122.     }
    123.     public void Populate()
    124.     {
    125.         foreach (GameObject t in GameObject.FindGameObjectsWithTag("BlackGranite")) //ore geneartion
    126.         {
    127.             if (t.transform.parent == this.gameObject.transform)
    128.             {
    129.                 {
    130.                     float r = Random.Range(0f, 100f);
    131.                     GameObject selectedTile = null; //ore chance
    132.  
    133.                     if (r < chanceMythrilium)
    134.                     {
    135.                         selectedTile = MythryliumGraniteOre;
    136.                     }
    137.                     else if (r < chanceGold)
    138.                     {
    139.                         selectedTile = GoldGraniteOre;
    140.                     }
    141.                     if (selectedTile != null)
    142.                     {
    143.                         GameObject newResourceTile = Instantiate(selectedTile, t.transform.position, Quaternion.identity) as GameObject;
    144.                         newResourceTile.transform.parent = transform;
    145.                         Destroy(t); //destroy the black granite
    146.                     }
    147.                 }
    148.             }
    149.         }
    150.     }
    151. }
     

    Attached Files: