64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.Assertions;
|
|
|
|
namespace DefaultNamespace.Solid
|
|
{
|
|
[RequireComponent(typeof(PlayerInput), typeof(PlayerMovement))]
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
private PlayerInput _playerInput;
|
|
private PlayerMovement _playerMovement;
|
|
// private BulletShooter _bulletShooter;
|
|
private IShooter _shooter;
|
|
|
|
// [SerializeField] private MonoBehaviour _inputBehaviour;
|
|
// private IInput _input => _inputBehaviour as IInput;
|
|
|
|
// BAD IDEA
|
|
// [SerializeField] private bool _isUsingGamepad = false;
|
|
|
|
private void Awake()
|
|
{
|
|
_playerInput = GetComponent<PlayerInput>();
|
|
|
|
// BAD IDEA - don't use concrete classes, use abstraction instead, e.g. IInput interface
|
|
// don't instantiate concrete classes even if they will be assigned to an interface variable
|
|
// if (_isUsingGamepad)
|
|
// {
|
|
// _playerInput = new GamepadInput();
|
|
// }
|
|
// else
|
|
// {
|
|
// _playerInput = new KeyboardMouseInput();
|
|
// }
|
|
|
|
_playerMovement = GetComponent<PlayerMovement>();
|
|
// _bulletShooter = GetComponent<BulletShooter>();
|
|
_shooter = GetComponent<IShooter>();
|
|
Assert.IsNotNull(_shooter, "No IShooter component attached");
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
_playerInput.IsFired += OnIsFired;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_playerInput.IsFired -= OnIsFired;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_playerInput.HandleInput();
|
|
_playerMovement.MoveAndRotate(_playerInput.Movement, _playerInput.Rotation);
|
|
}
|
|
|
|
private void OnIsFired()
|
|
{
|
|
// _bulletShooter.Fire();
|
|
_shooter.Fire();
|
|
}
|
|
}
|
|
} |