Added: unity project for SOLID
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
[RequireComponent(typeof(PlayerMovement))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private IInput _playerInput;
|
||||
private PlayerMovement _playerMovement;
|
||||
private IShooter _playerShooter;
|
||||
|
||||
// BAD IDEA
|
||||
// [SerializeField] private bool isAi;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// BAD IDEA
|
||||
// don't use concrete classes, use abstraction, e.g. IInput
|
||||
// don't instantiate classes, use factory or dependency injection => use services
|
||||
// if (isAi)
|
||||
// {
|
||||
// _playerInput = new AiInput();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _playerInput = new PlayerInput();
|
||||
// }
|
||||
|
||||
_playerInput = GetComponent<IInput>();
|
||||
Assert.IsNotNull(_playerInput, "No IInput component found on the GameObject!");
|
||||
|
||||
_playerShooter = GetComponent<IShooter>();
|
||||
Assert.IsNotNull(_playerShooter, "No IShooter component found on the GameObject!");
|
||||
|
||||
_playerMovement = GetComponent<PlayerMovement>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_playerInput.Fired += _playerShooter.Fire;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_playerInput.Fired -= _playerShooter.Fire;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
_playerInput.HandleInput();
|
||||
_playerMovement.MoveAndRotate(_playerInput.MovementInput, _playerInput.RotationInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user