Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ChunkGen : MonoBehaviour
  6. {
  7.     [Header("Tiles")]
  8.     public GameObject StoneTile;
  9.     public GameObject tileGold;
  10.     public GameObject tileCoal;
  11.  
  12.     [Header("Map Stats")]
  13.     public int width;
  14.     public int height;
  15.  
  16.     [Header("Ores Chances")]
  17.     public float coalChance;
  18.     public float goldChance;
  19.  
  20.     void Start()
  21.     {
  22.         Generate();
  23.     }
  24.  
  25.     public void Generate()
  26.     {
  27.         for (int x = 0; x < width; x++)
  28.         {
  29.             for (int y = 0; y < height; y++)
  30.             {
  31.                 GameObject tile = Instantiate(StoneTile, Vector3.zero, Quaternion.identity);
  32.                 tile.transform.parent = this.transform;
  33.                 tile.transform.localPosition = new Vector3(x, y);            
  34.             }
  35.         }
  36.         Populate();
  37.     }
  38.  
  39.     public void Populate()
  40.     {
  41.         foreach(GameObject t in GameObject.FindGameObjectsWithTag("TileStone"))
  42.         {
  43.             float r = Random.Range(0f, 100f);
  44.             GameObject selectedTile = null;
  45.             if (r < goldChance)
  46.             {
  47.                 selectedTile = tileGold;
  48.             }
  49.  
  50.             if (r < coalChance)
  51.             {
  52.                 selectedTile = tileCoal;
  53.             }
  54.            
  55.             if(selectedTile != null)
  56.             {
  57.                 GameObject ore = Instantiate(selectedTile, t.transform.position, Quaternion.identity);
  58.                 ore.transform.parent = t.transform;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement