Advertisement
sergezhu

Untitled

Dec 15th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.51 KB | None | 0 0
  1. public class Spawner : MonoBehaviour
  2. {
  3.     [Header("General")]
  4.     [SerializeField] private Transform _container;
  5.     [SerializeField] private int _repeatCount;
  6.     [SerializeField] private int _distanceBetweenFullLine;
  7.     [SerializeField] private int _distanceBetweenRandomLine;
  8.  
  9.     [Header("Blocks")]
  10.     [SerializeField] private Block _blockTemplate;
  11.     [SerializeField] private int _blockSpawnChance;
  12.  
  13.     [Header("Walls")]
  14.     [SerializeField] private Wall _wallTemplate;
  15.     [SerializeField] private int _wallSpawnChance;
  16.     [Space]
  17.     [SerializeField] private Wall _sideWallTemplate;
  18.     [SerializeField] private int _additionalDistance;
  19.  
  20.     [Header("Bonuses")]
  21.     [SerializeField] private Bonus _bonusTemplate;
  22.     [SerializeField] private int _bonusSpawnChance;
  23.  
  24.     [Header("Finish")]
  25.     [SerializeField] private Finish _finishPoint;
  26.  
  27.  
  28.     private BlockSpawnPoint[] _blockSpawnPoints;
  29.     private WallSpawnPoint[] _wallSpawnPoints;
  30.  
  31.     private float _wallInitScaleX, _wallInitScaleY;
  32.     private float _bonusInitScale = 0.23f;
  33.     private float _spawnPointsCorrectionMultiplier = 1f;
  34.  
  35.     private void Start()
  36.     {
  37.         _blockSpawnPoints = GetComponentsInChildren<BlockSpawnPoint>();
  38.         _wallSpawnPoints = GetComponentsInChildren<WallSpawnPoint>();
  39.  
  40.         _wallInitScaleX = _wallTemplate.transform.localScale.x * 0.75f;
  41.         _wallInitScaleY = _wallTemplate.transform.localScale.y * 0.98f;
  42.  
  43.         GenerateSideWalls();
  44.  
  45.         for (int i = 0; i < _repeatCount; i++)
  46.         {
  47.             MoveSpawner(_distanceBetweenFullLine);
  48.             GenerateRandomElements(_wallSpawnPoints, _wallTemplate.gameObject, _wallSpawnChance, new Vector2(_spawnPointsCorrectionMultiplier * _wallInitScaleX, _distanceBetweenFullLine * _wallInitScaleY), -0.05f + _distanceBetweenFullLine * _wallInitScaleY / 2f);
  49.             GenerateFullLine(_blockSpawnPoints, _blockTemplate.gameObject, _spawnPointsCorrectionMultiplier * Vector2.one);
  50.             GenerateRandomElements(_blockSpawnPoints, _bonusTemplate.gameObject, _bonusSpawnChance, _bonusInitScale * Vector2.one, _distanceBetweenFullLine / 2f);
  51.  
  52.  
  53.             MoveSpawner(_distanceBetweenRandomLine);
  54.             GenerateRandomElements(_wallSpawnPoints, _wallTemplate.gameObject, _wallSpawnChance, new Vector2(_spawnPointsCorrectionMultiplier * _wallInitScaleX, _distanceBetweenRandomLine * _wallInitScaleY), -0.05f + _distanceBetweenRandomLine * _wallInitScaleY / 2f);
  55.             GenerateRandomElements(_blockSpawnPoints, _blockTemplate.gameObject, _blockSpawnChance, _spawnPointsCorrectionMultiplier * Vector2.one);
  56.             GenerateRandomElements(_blockSpawnPoints, _bonusTemplate.gameObject, _bonusSpawnChance, _bonusInitScale * Vector2.one, _distanceBetweenRandomLine / 2f);
  57.         }
  58.  
  59.         MoveSpawner(_distanceBetweenFullLine);
  60.         _finishPoint.transform.position = transform.position;
  61.         _finishPoint.gameObject.SetActive(true);
  62.  
  63.     }
  64.  
  65.     private void GenerateFullLine(SpawnPoint[] spawnPoints, GameObject generatedElement, Vector2 scale)
  66.     {
  67.         for (int i = 0; i < spawnPoints.Length; i++)
  68.         {
  69.             GenerateElement(spawnPoints[i].transform.position, generatedElement, scale);
  70.         }
  71.     }
  72.  
  73.     private void GenerateRandomElements(SpawnPoint[] spawnPoints, GameObject generatedElement, int spawnChance, Vector2 scale, float offsetY = 0)
  74.     {
  75.         for (int i = 0; i < spawnPoints.Length; i++)
  76.         {
  77.             if(Random.Range(0, 100) < spawnChance)
  78.             {
  79.                 GenerateElement(spawnPoints[i].transform.position, generatedElement, scale, offsetY);
  80.             }
  81.         }
  82.     }
  83.  
  84.     private GameObject GenerateElement(Vector3 spawnPoint, GameObject generatedElement, Vector2 scale, float offsetY = 0)
  85.     {
  86.         spawnPoint.y -= offsetY;
  87.         spawnPoint.x *= _spawnPointsCorrectionMultiplier;
  88.  
  89.         GameObject element = Instantiate(generatedElement, spawnPoint, Quaternion.identity, _container);        
  90.  
  91.         element.transform.localScale = new Vector3(scale.x, scale.y, element.transform.localScale.z);
  92.  
  93.         return element;
  94.     }
  95.  
  96.     private void MoveSpawner(int distanceY)
  97.     {
  98.         transform.position = new Vector3(transform.position.x, transform.position.y + distanceY, transform.position.z);
  99.     }
  100.  
  101.     private void GenerateSideWalls()
  102.     {
  103.         Vector2[] viewporCorners = new Vector2[]
  104.         {
  105.             new Vector2(0, 0),
  106.             new Vector2(0, 1),
  107.             new Vector2(1, 0),
  108.             new Vector2(1, 1)
  109.         };
  110.  
  111.         Vector3[] worldAreaCorners = new Vector3[viewporCorners.Length];
  112.  
  113.  
  114.  
  115.         for(int i = 0; i < worldAreaCorners.Length; i++)
  116.         {
  117.             worldAreaCorners[i] = Camera.main.ViewportToWorldPoint(viewporCorners[i]);
  118.         }
  119.  
  120.         int viewPortWallsCount = Mathf.RoundToInt(worldAreaCorners[1].y - worldAreaCorners[0].y);
  121.         int totalWallsCount = viewPortWallsCount + (_distanceBetweenFullLine + _distanceBetweenRandomLine) * _repeatCount + _additionalDistance;
  122.  
  123.         Vector3 bottomLeftPosition = worldAreaCorners[0];
  124.         Vector3 bottomRightPosition = worldAreaCorners[2];
  125.         float leftWallWidth = 0, rightWallWidth = 0;
  126.        
  127.         for (int i = 0; i < totalWallsCount; i++)
  128.         {
  129.             Vector3 offsetY = Vector3.up * i;
  130.  
  131.             Wall leftWall = Instantiate(_sideWallTemplate, _container);
  132.             leftWallWidth = leftWall.GetComponent<BoxCollider2D>().bounds.size.x;
  133.             SideWallSetPosition(leftWall, bottomLeftPosition + offsetY, leftWallWidth);
  134.  
  135.             Wall rightWall = Instantiate(_sideWallTemplate, _container);
  136.             rightWallWidth = rightWall.GetComponent<BoxCollider2D>().bounds.size.x;
  137.             SideWallSetPosition(rightWall, bottomRightPosition + offsetY, rightWallWidth);            
  138.         }
  139.  
  140.         _spawnPointsCorrectionMultiplier = (bottomRightPosition - bottomLeftPosition - leftWallWidth * Vector3.right - rightWallWidth * Vector3.right).magnitude /
  141.             (bottomRightPosition - bottomLeftPosition).magnitude;
  142.     }
  143.  
  144.     private void SideWallSetPosition(Wall sideWall, Vector3 initialPosition, float wallWidth)
  145.     {
  146.         Transform wallTransform = sideWall.transform;
  147.        
  148.         Vector3 offsetX = Vector3.right * ((initialPosition.x < 0) ? 1 : -1) * wallWidth / 2;
  149.         initialPosition.z = 0;
  150.         wallTransform.position = initialPosition + offsetX;
  151.  
  152.         if (wallTransform.position.x > 0)
  153.         {
  154.             sideWall.GetComponent<SpriteRenderer>().flipX = true;
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement