Files
2026-09-30 18:38:16 +02:00

58 lines
1.6 KiB
C#

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);
}
}
}