Hello people!
I’m doing a BlockBreaker/Arknoid clone. I’m trying to refactor my code to create the habit of doing the projects in the best ways. I have a hard time using Managers and Controllers. I do not know how to avoid using GetComponent and FindObject, and I’m not sure how to set the specific function for each of them.
I was told to use a EventSystem, but I do not understand anything about it and I prefer to learn it calmly later. I want to optimize it in a way that it remains clear for me.
Could you guys please help me?
Here is my complete project:
https://github.com/thisfn/Breakout/
There is a bunch of scripts here. But few ones show Exactly what I have difficulty on is the BlockController.cs, EffectsController.cs and GameManager.cs.
public class BlockController : MonoBehaviour
{
[SerializeField] private AudioClip _hitAudioClip;
private CameraController _cameraController;
private EffectsController _effectsController;
private LevelManager _levelManager;
private SoundManager _soundManager;
public static int BlockCount;
private void Awake()
{
_cameraController = FindObjectOfType<CameraController>();
_soundManager = FindObjectOfType<SoundManager>();
_levelManager = FindObjectOfType<LevelManager>();
_effectsController = FindObjectOfType<EffectsController>();
}
public void BlockHit()
{
_soundManager.PlaySingle(_hitAudioClip);
_cameraController.ShakeScreen();
}
public void BlockDestroy(Vector3 position, Material material)
{
BlockCount--;
if (BlockCount <= 0)
_levelManager.LoadNextLevel();
_effectsController.BlockExplosion(position, material);
}
}
public class EffectsController : MonoBehaviour
{
[SerializeField] private List<ParticleSystem> _blockParticles;
[SerializeField] private List<ParticleSystem> _explosionParticles;
private int _tempExploCounter;
private static EffectsController _instancEffectsController;
private void Awake()
{
if (_instancEffectsController == null)
_instancEffectsController = this;
else if (_instancEffectsController != this)
Destroy(gameObject);
}
public void Explosion(Vector2 position)
{
//TODO How to optimize?
//TODO reactivate after counting
//_explosionParticles[0].transform.position = position;
//_explosionParticles[0].gameObject.SetActive(true);
//_blockParticles[0].Play();
//_explosionParticles.RemoveAt(0);
_tempExploCounter++;
Debug.Log(_tempExploCounter);
}
public void BlockExplosion(Vector2 position, Material material)
{
//TODO How to optimize?
_blockParticles[0].transform.position = position;
_blockParticles[0].GetComponent<Renderer>().material = material;
_blockParticles[0].gameObject.SetActive(true);
_blockParticles[0].Play();
_blockParticles.RemoveAt(0);
}
}
public class GameManager : MonoBehaviour
{
[SerializeField] private float _ballOffset;
[SerializeField] private GameObject _ballGameObject;
[SerializeField] private GameObject _boardGameObject;
[SerializeField] private GameObject _effectsController;
private bool _isPaused;
private SoundManager _soundManager;
private static GameManager _instanceGameManager;
private void Awake()
{
if (_instanceGameManager == null)
_instanceGameManager = this;
else if (_instanceGameManager != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
_soundManager = FindObjectOfType<SoundManager>();
}
public void InitilizeLevel()
{
BlockController.BlockCount = 0;
var effectsController = Instantiate(_effectsController).GetComponent<EffectsController>();
var board = Instantiate(_boardGameObject).GetComponent<Board>();
var ballPos = board.transform.position + Vector3.up * _ballOffset;
var ball = Instantiate(_ballGameObject, ballPos, Quaternion.identity).GetComponent<Ball>();
ball.Board = board.GetComponent<Board>();
ball.SoundManager = _soundManager;
ball.EffectsController = effectsController;
StateManager.SetWaiting();
}
public void PauseGame()
{
_isPaused = !_isPaused;
if (_isPaused)
{
Time.timeScale = 0f;
StateManager.SetPaused();
}
else
{
Time.timeScale = 1f;
StateManager.ChangeToPreviousState();
}
}
}
Many thanks in every way possible, I really want to improve, so sorry if I’m asking much or stupid questions.