Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Spawner : MonoBehaviour
- {
- [Header("General")]
- [SerializeField] private Transform _container;
- [SerializeField] private int _repeatCount;
- [SerializeField] private int _distanceBetweenFullLine;
- [SerializeField] private int _distanceBetweenRandomLine;
- [Header("Blocks")]
- [SerializeField] private Block _blockTemplate;
- [SerializeField] private int _blockSpawnChance;
- [Header("Walls")]
- [SerializeField] private Wall _wallTemplate;
- [SerializeField] private int _wallSpawnChance;
- [Space]
- [SerializeField] private Wall _sideWallTemplate;
- [SerializeField] private int _additionalDistance;
- [Header("Bonuses")]
- [SerializeField] private Bonus _bonusTemplate;
- [SerializeField] private int _bonusSpawnChance;
- [Header("Finish")]
- [SerializeField] private Finish _finishPoint;
- private BlockSpawnPoint[] _blockSpawnPoints;
- private WallSpawnPoint[] _wallSpawnPoints;
- private float _wallInitScaleX, _wallInitScaleY;
- private float _bonusInitScale = 0.23f;
- private float _spawnPointsCorrectionMultiplier = 1f;
- private void Start()
- {
- _blockSpawnPoints = GetComponentsInChildren<BlockSpawnPoint>();
- _wallSpawnPoints = GetComponentsInChildren<WallSpawnPoint>();
- _wallInitScaleX = _wallTemplate.transform.localScale.x * 0.75f;
- _wallInitScaleY = _wallTemplate.transform.localScale.y * 0.98f;
- GenerateSideWalls();
- for (int i = 0; i < _repeatCount; i++)
- {
- MoveSpawner(_distanceBetweenFullLine);
- GenerateRandomElements(_wallSpawnPoints, _wallTemplate.gameObject, _wallSpawnChance, new Vector2(_spawnPointsCorrectionMultiplier * _wallInitScaleX, _distanceBetweenFullLine * _wallInitScaleY), -0.05f + _distanceBetweenFullLine * _wallInitScaleY / 2f);
- GenerateFullLine(_blockSpawnPoints, _blockTemplate.gameObject, _spawnPointsCorrectionMultiplier * Vector2.one);
- GenerateRandomElements(_blockSpawnPoints, _bonusTemplate.gameObject, _bonusSpawnChance, _bonusInitScale * Vector2.one, _distanceBetweenFullLine / 2f);
- MoveSpawner(_distanceBetweenRandomLine);
- GenerateRandomElements(_wallSpawnPoints, _wallTemplate.gameObject, _wallSpawnChance, new Vector2(_spawnPointsCorrectionMultiplier * _wallInitScaleX, _distanceBetweenRandomLine * _wallInitScaleY), -0.05f + _distanceBetweenRandomLine * _wallInitScaleY / 2f);
- GenerateRandomElements(_blockSpawnPoints, _blockTemplate.gameObject, _blockSpawnChance, _spawnPointsCorrectionMultiplier * Vector2.one);
- GenerateRandomElements(_blockSpawnPoints, _bonusTemplate.gameObject, _bonusSpawnChance, _bonusInitScale * Vector2.one, _distanceBetweenRandomLine / 2f);
- }
- MoveSpawner(_distanceBetweenFullLine);
- _finishPoint.transform.position = transform.position;
- _finishPoint.gameObject.SetActive(true);
- }
- private void GenerateFullLine(SpawnPoint[] spawnPoints, GameObject generatedElement, Vector2 scale)
- {
- for (int i = 0; i < spawnPoints.Length; i++)
- {
- GenerateElement(spawnPoints[i].transform.position, generatedElement, scale);
- }
- }
- private void GenerateRandomElements(SpawnPoint[] spawnPoints, GameObject generatedElement, int spawnChance, Vector2 scale, float offsetY = 0)
- {
- for (int i = 0; i < spawnPoints.Length; i++)
- {
- if(Random.Range(0, 100) < spawnChance)
- {
- GenerateElement(spawnPoints[i].transform.position, generatedElement, scale, offsetY);
- }
- }
- }
- private GameObject GenerateElement(Vector3 spawnPoint, GameObject generatedElement, Vector2 scale, float offsetY = 0)
- {
- spawnPoint.y -= offsetY;
- spawnPoint.x *= _spawnPointsCorrectionMultiplier;
- GameObject element = Instantiate(generatedElement, spawnPoint, Quaternion.identity, _container);
- element.transform.localScale = new Vector3(scale.x, scale.y, element.transform.localScale.z);
- return element;
- }
- private void MoveSpawner(int distanceY)
- {
- transform.position = new Vector3(transform.position.x, transform.position.y + distanceY, transform.position.z);
- }
- private void GenerateSideWalls()
- {
- Vector2[] viewporCorners = new Vector2[]
- {
- new Vector2(0, 0),
- new Vector2(0, 1),
- new Vector2(1, 0),
- new Vector2(1, 1)
- };
- Vector3[] worldAreaCorners = new Vector3[viewporCorners.Length];
- for(int i = 0; i < worldAreaCorners.Length; i++)
- {
- worldAreaCorners[i] = Camera.main.ViewportToWorldPoint(viewporCorners[i]);
- }
- int viewPortWallsCount = Mathf.RoundToInt(worldAreaCorners[1].y - worldAreaCorners[0].y);
- int totalWallsCount = viewPortWallsCount + (_distanceBetweenFullLine + _distanceBetweenRandomLine) * _repeatCount + _additionalDistance;
- Vector3 bottomLeftPosition = worldAreaCorners[0];
- Vector3 bottomRightPosition = worldAreaCorners[2];
- float leftWallWidth = 0, rightWallWidth = 0;
- for (int i = 0; i < totalWallsCount; i++)
- {
- Vector3 offsetY = Vector3.up * i;
- Wall leftWall = Instantiate(_sideWallTemplate, _container);
- leftWallWidth = leftWall.GetComponent<BoxCollider2D>().bounds.size.x;
- SideWallSetPosition(leftWall, bottomLeftPosition + offsetY, leftWallWidth);
- Wall rightWall = Instantiate(_sideWallTemplate, _container);
- rightWallWidth = rightWall.GetComponent<BoxCollider2D>().bounds.size.x;
- SideWallSetPosition(rightWall, bottomRightPosition + offsetY, rightWallWidth);
- }
- _spawnPointsCorrectionMultiplier = (bottomRightPosition - bottomLeftPosition - leftWallWidth * Vector3.right - rightWallWidth * Vector3.right).magnitude /
- (bottomRightPosition - bottomLeftPosition).magnitude;
- }
- private void SideWallSetPosition(Wall sideWall, Vector3 initialPosition, float wallWidth)
- {
- Transform wallTransform = sideWall.transform;
- Vector3 offsetX = Vector3.right * ((initialPosition.x < 0) ? 1 : -1) * wallWidth / 2;
- initialPosition.z = 0;
- wallTransform.position = initialPosition + offsetX;
- if (wallTransform.position.x > 0)
- {
- sideWall.GetComponent<SpriteRenderer>().flipX = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement