Added: unity project for SOLID
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 396893acdf6d48048541ea8638f42b18
|
||||
timeCreated: 1790264420
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Solid.Interfaces
|
||||
{
|
||||
public interface IExplodable
|
||||
{
|
||||
void Explode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6fe181c177dc4785b57b5d9238767d40
|
||||
timeCreated: 1790268080
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid.Interfaces
|
||||
{
|
||||
public interface IInput
|
||||
{
|
||||
Vector2 MovementInput { get; }
|
||||
Vector2 RotationInput { get; }
|
||||
event Action Fired;
|
||||
|
||||
void HandleInput();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35f86d33a6994aa9b367cfc6851dbcc1
|
||||
timeCreated: 1790264428
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Solid.Interfaces
|
||||
{
|
||||
public interface IShooter
|
||||
{
|
||||
void Fire();
|
||||
|
||||
// BAD IDEA => segregate interfaces
|
||||
// void ApplyEffect();
|
||||
// void Explode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58b030ec6de74f8aa019f8564285cc49
|
||||
timeCreated: 1790266519
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c699a29ee8a44d693e9e96ae8df335f
|
||||
timeCreated: 1790263382
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
public class PlayerInput : MonoBehaviour, IInput
|
||||
{
|
||||
[Header("Inputs")]
|
||||
[SerializeField] private InputActionReference movementInputReference;
|
||||
[SerializeField] private InputActionReference rotationInputReference;
|
||||
[SerializeField] private InputActionReference fireInputReference;
|
||||
|
||||
private Vector2 _movementInput;
|
||||
private Vector2 _rotationInput;
|
||||
|
||||
public Vector2 MovementInput => _movementInput;
|
||||
public Vector2 RotationInput => _rotationInput;
|
||||
public event Action Fired;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
movementInputReference.action.Enable();
|
||||
rotationInputReference.action.Enable();
|
||||
fireInputReference.action.Enable();
|
||||
fireInputReference.action.performed += OnFireActionPerformed;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
movementInputReference.action.Disable();
|
||||
rotationInputReference.action.Disable();
|
||||
fireInputReference.action.Disable();
|
||||
fireInputReference.action.performed -= OnFireActionPerformed;
|
||||
}
|
||||
|
||||
public void HandleInput()
|
||||
{
|
||||
_movementInput = movementInputReference.action.ReadValue<Vector2>();
|
||||
_rotationInput = rotationInputReference.action.ReadValue<Vector2>();
|
||||
}
|
||||
|
||||
private void OnFireActionPerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
Fired?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c34b40c805843f3a15d4a49fc4c5621
|
||||
timeCreated: 1790263458
|
||||
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float speed = 5f;
|
||||
[SerializeField] private float rotationSpeed = 60f;
|
||||
|
||||
private CharacterController _characterController;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_characterController = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
public void MoveAndRotate(Vector2 movementInput, Vector2 rotationInput)
|
||||
{
|
||||
transform.Rotate(0, rotationInput.x * Time.deltaTime * rotationSpeed, 0);
|
||||
_characterController.Move(new Vector3(movementInput.x, 0, movementInput.y) * (speed * Time.deltaTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 36e483ece12041f991dbf9f355c1b354
|
||||
timeCreated: 1790263752
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9383dcf57e0044b6868613423eb5ffb0
|
||||
timeCreated: 1790266559
|
||||
@@ -0,0 +1,18 @@
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid.Shooters
|
||||
{
|
||||
public class BulletShooter : MonoBehaviour, IShooter
|
||||
{
|
||||
[SerializeField] private Transform shootSpawn;
|
||||
[SerializeField] private Rigidbody projectilePrefab;
|
||||
[SerializeField] private float shootForce = 10.0f;
|
||||
|
||||
public void Fire()
|
||||
{
|
||||
Rigidbody projectile = Instantiate(projectilePrefab, shootSpawn.position, shootSpawn.rotation);
|
||||
projectile.AddForce(shootSpawn.forward * shootForce, ForceMode.Impulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ba1ecc1f5474dd6baa9d5ac02e15744
|
||||
timeCreated: 1790266576
|
||||
Reference in New Issue
Block a user