matbiz01

Chunk

Jan 21st, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class Chunk : MonoBehaviour
  5. {
  6.     public GameObject floorTile;
  7.     public static int NeighboursCount = 9, Params = 4, ChunkSize;
  8.     public static float fromCenter;
  9.  
  10.     private Vector3[] direction;
  11.     private Dictionary<string, int> directions = new Dictionary<string, int>();
  12.     public GameObject[] neighbours = new GameObject[NeighboursCount];
  13.  
  14.     void Awake()
  15.     {
  16.         ChunkSize = Mathf.RoundToInt(gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size.x); //width and height are the same
  17.         fromCenter = ChunkSize / 2f - 0.1f; //0.1f is just an offset
  18.         direction = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 0, 0), new Vector3(1, -1, 0), new Vector3(0, -1, 0), new Vector3(-1, -1, 0), new Vector3(-1, 0, 0), new Vector3(-1, 1, 0) };
  19.         for (int x = 0; x < NeighboursCount; x++)
  20.         {
  21.             directions.Add(VecToString(direction[x]), x);
  22.         }
  23.     }
  24.  
  25.     public void OnTriggerEnter2D(Collider2D collision)
  26.     {
  27.         if(collision.gameObject.tag == "Player")
  28.         {
  29.             HandleNeighbours(); //new chunks + terrain generation
  30.         }
  31.         if(collision.gameObject.tag == "Enemy") //changing enemys parent so that it doesn't disappear with its previous chunk
  32.         {
  33.             collision.gameObject.transform.parent = gameObject.transform;
  34.         }
  35.     }
  36.  
  37.     public void HandleNeighbours()
  38.     {
  39.         List<int> newChunks = ActivateAndInstantiate(); //activates deactivated chunks, instantiates new ones and stores indexes of new chunks in a list
  40.  
  41.         for (int x = 0; x < NeighboursCount; x++)
  42.         {
  43.             for (int y = 0; y < NeighboursCount; y++)
  44.             {
  45.                 string neighDir = VecToString((neighbours[y].transform.position - neighbours[x].transform.position) / ChunkSize);
  46.                 if (directions.ContainsKey(neighDir))
  47.                 {
  48.                     neighbours[x].GetComponent<Chunk>().SetNeighbour(neighbours[y], directions[neighDir]);
  49.                 }
  50.             }
  51.         }
  52.  
  53.         for (int x = 0; x < NeighboursCount; x++)
  54.         {
  55.             if (newChunks.Contains(x))
  56.             {
  57.                 List<int[]> seeds = neighbours[x].GetComponent<Chunk>().CollectSeeds();
  58.                 neighbours[x].GetComponent<TerrainGenerator>().Initialize(seeds);
  59.             }
  60.         }
  61.     }
  62.  
  63.     public List<int> ActivateAndInstantiate()
  64.     {
  65.         List<int> newChunks = new List<int>();
  66.         neighbours[0] = gameObject;
  67.         for (int x = 1; x < NeighboursCount; x++)
  68.         {
  69.             if (neighbours[x] == null)
  70.             {
  71.                 newChunks.Add(x);
  72.                 floorTile = Resources.Load<GameObject>("Prefabs/FloorTile");
  73.                 neighbours[x] = Instantiate(floorTile, transform.position + direction[x] * ChunkSize, Quaternion.identity);
  74.             }
  75.  
  76.             if (neighbours[x] != null && !neighbours[x].activeSelf)
  77.             {
  78.                 neighbours[x].SetActive(true);
  79.             }
  80.         }
  81.         return newChunks;
  82.     }
  83.  
  84.     public void SetNeighbour(GameObject tile, int direction)
  85.     {
  86.         neighbours[direction] = tile;
  87.     }
  88.  
  89.     public string VecToString(Vector3 vector)
  90.     {
  91.         string result = "";
  92.         result += Mathf.RoundToInt(vector.x);
  93.         result += Mathf.RoundToInt(vector.y);
  94.         result += Mathf.RoundToInt(vector.z);
  95.         return result;
  96.     }
  97.  
  98.     public List<int[]> CollectSeeds()
  99.     {
  100.         List<int[]> seeds = new List<int[]>();
  101.         for (int x = 1; x < NeighboursCount; x++)
  102.         {
  103.             if (neighbours[x] != null)
  104.             {
  105.                 seeds.Add(neighbours[x].GetComponent<TerrainGenerator>().GetSeed());
  106.             }
  107.         }
  108.         return seeds;
  109.     }
  110.  
  111.     public void OnTriggerStay2D(Collider2D collision)
  112.     {
  113.         if (collision.gameObject.tag == "Player")
  114.         {
  115.             for (int x = 0; x < NeighboursCount; x++)
  116.             {
  117.                 if (!neighbours[x].activeSelf)
  118.                 {
  119.                     neighbours[x].SetActive(true);
  120.                 }
  121.             }
  122.         }
  123.     }
  124.  
  125.     public void OnTriggerExit2D(Collider2D collision)
  126.     {
  127.         if (collision.gameObject.tag == "Player")
  128.         {
  129.             Vector3 playerPos = collision.gameObject.transform.position;
  130.             Vector3 chunkPos = transform.position;
  131.  
  132.             if (playerPos.y < chunkPos.y - fromCenter)
  133.             {
  134.                 DisableChunks(8, 1, 2);
  135.             }
  136.             if (playerPos.y > chunkPos.y + fromCenter)
  137.             {
  138.                 DisableChunks(4, 5, 6);
  139.             }
  140.             if (playerPos.x < chunkPos.x - fromCenter)
  141.             {
  142.                 DisableChunks(2, 3, 4);
  143.             }
  144.             if (playerPos.x > chunkPos.x + fromCenter)
  145.             {
  146.                 DisableChunks(8, 7, 6);
  147.             }
  148.         }
  149.     }
  150.  
  151.     private void DisableChunks(params int[] chunks)
  152.     {
  153.         foreach (int x in chunks)
  154.         {
  155.             neighbours[x].SetActive(false);
  156.         }
  157.     }
  158. }
  159.  
Add Comment
Please, Sign In to add comment