Advertisement
apieceoffruit

Taro Example Grid

Jul 16th, 2023 (edited)
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using Random = System.Random;
  5.  
  6. // This source code is used for the video. It obviously requires heavy alterations to be used in a real project.
  7. public class ExampleGrid : MonoBehaviour
  8. {
  9.     [SerializeField] private Vector2Int _size;
  10.     [SerializeField] private Vector2 _gap;
  11.     [SerializeField, Range(0, 0.8f)] private float _skipAmount = 0.1f;
  12.     [SerializeField, Range(0, 1)] private float _forestAmount = 0.3f;
  13.     [SerializeField] private GridType _gridType;
  14.     [SerializeField] private ScriptableGridConfig[] _configs;
  15.    
  16.     private bool _requiresGeneration = true;
  17.     private Camera _cam;
  18.     private Grid _grid;
  19.  
  20.     private Vector3 _cameraPositionTarget;
  21.     private float _cameraSizeTarget;
  22.     private Vector3 _moveVel;
  23.     private float _cameraSizeVel;
  24.  
  25.     private Vector2 _currentGap;
  26.     private Vector2 _gapVel;
  27.    
  28.  
  29.     private  void Awake()
  30.     {
  31.         _grid = GetComponent<Grid>();
  32.         _cam = Camera.main;
  33.         _currentGap = _gap;
  34.     }
  35.    
  36.     private void OnValidate() => _requiresGeneration = true;
  37.    
  38.     private void LateUpdate()
  39.     {
  40.         if (Vector2.Distance(_currentGap, _gap) > 0.01f)
  41.         {
  42.             _currentGap = Vector2.SmoothDamp(_currentGap, _gap,ref _gapVel, 0.1f);
  43.             _requiresGeneration = true;
  44.         }
  45.        
  46.         if (_requiresGeneration) Generate();
  47.  
  48.         _cam.transform.position = Vector3.SmoothDamp(_cam.transform.position, _cameraPositionTarget, ref _moveVel, 0.5f);
  49.         _cam.orthographicSize = Mathf.SmoothDamp(_cam.orthographicSize, _cameraSizeTarget, ref _cameraSizeVel, 0.5f);
  50.     }
  51.    
  52.     private void Generate()
  53.     {
  54.         foreach (Transform child in transform)
  55.         {
  56.             Destroy(child.gameObject);
  57.         }
  58.  
  59.         var config = _configs.First(c => c.Type == _gridType);
  60.  
  61.         _grid.cellLayout = config.Layout;
  62.         _grid.cellSize = config.CellSize;
  63.         if (_grid.cellLayout != GridLayout.CellLayout.Hexagon) _grid.cellGap = _currentGap;
  64.         _grid.cellSwizzle = config.GridSwizzle;
  65.  
  66.         var coordinates = new List<Vector3Int>();
  67.  
  68.         for (int x = 0; x < _size.x; x++)
  69.         {
  70.             for (int y = 0; y < _size.y; y++)
  71.             {
  72.                 coordinates.Add(new Vector3Int(x, y));
  73.             }
  74.         }
  75.        
  76.         var bounds = new Bounds();
  77.         var skipCount = Mathf.FloorToInt(coordinates.Count * _skipAmount);
  78.         var forestCount = Mathf.FloorToInt(coordinates.Count * _forestAmount);
  79.         var index = 0;
  80.         var rand = new Random(420);
  81.  
  82.         foreach (var coordinate in coordinates.OrderBy(t => rand.Next()).Take(coordinates.Count - skipCount))
  83.         {
  84.             var isForest = index++ < forestCount;
  85.             var prefab = isForest ? config.ForestPrefab : config.GrassPrefab;
  86.             var position = _grid.GetCellCenterWorld(coordinate);
  87.             var spawned = Instantiate(prefab, position, Quaternion.identity, transform);
  88.             spawned.Init(coordinate);
  89.             bounds.Encapsulate(position);
  90.         }
  91.  
  92.         SetCamera(bounds);
  93.  
  94.         _requiresGeneration = false;
  95.     }
  96.  
  97.     private void SetCamera(Bounds bounds)
  98.     {
  99.         bounds.Expand(2);
  100.  
  101.         var vertical = bounds.size.y;
  102.         var horizontal = bounds.size.x * _cam.pixelHeight / _cam.pixelWidth;
  103.  
  104.         _cameraPositionTarget = bounds.center + Vector3.back;
  105.         _cameraSizeTarget = Mathf.Max(horizontal, vertical) * 0.5f;
  106.     }
  107. }
  108.  
  109.  
  110. [CreateAssetMenu]
  111. public class ScriptableGridConfig : ScriptableObject
  112. {
  113.     public GridType Type;
  114.     [Space(10)]
  115.     public GridLayout.CellLayout Layout;
  116.     public GridTileBase GrassPrefab, ForestPrefab;
  117.     public Vector3 CellSize;
  118.     public GridLayout.CellSwizzle GridSwizzle;
  119. }
  120.  
  121. [Serializable]
  122. public enum GridType
  123. {
  124.     Rectangle,
  125.     Isometric,
  126.     HexagonPointy,
  127.     HexagonFlat
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement