void StartSpawning() { if (blocksSpawningCoroutine != null) { return; } NextBlockToSpawn = new FallingBlockWrapper { Block = spawnerData.BlockPrefabs.GetRandomElement() .GetComponent <FallingBlock>(), RandomTint = GetRandomTintColor() }; blocksSpawningCoroutine = BlocksSpawningCoroutine(); StartCoroutine(blocksSpawningCoroutine); }
IEnumerator BlocksSpawningCoroutine() { while (true) { if (shouldSpawnNextBlock) { var spawnedBlock = Instantiate( NextBlockToSpawn.Block, Vector3.zero, Quaternion.identity, null) .GetComponent <FallingBlock>(); if (UnityEngine.Random.Range(0f, 1f) > spawnerData.ChanceToSpawnAttachment) { var spawnedAttachement = Instantiate(spawnerData.BlockAttachments.GetRandomElement()); spawnedBlock.AttachUpgrade(spawnedAttachement); } var targetPos = CameraController.Instance.GetScreenTopPosition(); targetPos.y += spawnedBlock.SpriteSize.y / 2f; spawnedBlock.transform.position = targetPos; spawnedBlock.SetSpriteTint(nextBlockToSpawn.RandomTint); shouldSpawnNextBlock = false; NextBlockToSpawn = new FallingBlockWrapper { Block = spawnerData.BlockPrefabs.GetRandomElement() .GetComponent <FallingBlock>(), RandomTint = GetRandomTintColor() }; spawnedBlock.Placed += () => { OnAnyBlockPlaced(spawnedBlock); }; spawnedBlock.Destroyed += () => { OnAnyBlockPlaced(spawnedBlock); }; } yield return(waitForSeconds); } }
protected virtual void OnBlockToSpawnChanged(FallingBlockWrapper nextBlock) { BlockToSpawnChanged?.Invoke(nextBlock); }