add unity project
add SOLID examples
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user