Added: unity project for SOLID

This commit is contained in:
2026-09-30 18:38:16 +02:00
parent c528093fdb
commit 285aa10221
89 changed files with 8440 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
using System;
namespace DefaultNamespace
{
public class BaseClass
{
public virtual void DoSomething()
{
Console.WriteLine("I'm here!");
}
}
public class ChildClass : BaseClass
{
public void DoSomethingElse()
{
Console.WriteLine("I'm here too!");
}
public override void DoSomething()
{
Console.WriteLine("I'm back!");
}
}
public delegate void CoVarianceHandler(ChildClass args);
public delegate BaseClass ContraVarianceHandler();
public class Delegates
{
public event CoVarianceHandler OnCoVariance;
public event ContraVarianceHandler OnContraVariance;
public void OnBaseClass(BaseClass args) {}
public void OnChildClass(ChildClass args) {}
public BaseClass OnReturnBaseClass() => new BaseClass();
public ChildClass OnReturnChildClass() => new ChildClass();
public void Init()
{
OnCoVariance += OnBaseClass;
OnCoVariance += OnChildClass;
OnCoVariance?.Invoke(new ChildClass());
OnContraVariance += OnReturnBaseClass;
OnContraVariance += OnReturnChildClass;
OnContraVariance?.Invoke();
}
}
// public class Client
// {
// private Delegates _delegates;
//
// public void Init()
// {
// _delegates = new Delegates();
// _delegates.OnCoVariance += () => { };
// // _delegates.OnCoVariance?.Invoke(); // not possible, because the delegate is an event
// }
// }
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e80d414301f54aefaa4ea3ac4e0e02d7
timeCreated: 1790259063
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9a1bff361b174b938743dcd4d1883192
timeCreated: 1790261807
+17
View File
@@ -0,0 +1,17 @@
using System;
namespace NonSolid
{
[Serializable]
public enum FireType
{
Bullet,
Spell,
Laser,
Grenade,
Rocket,
Arrow,
Fireball,
Plasma,
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3803a0d2873043c5a4e7ed138c4113c1
timeCreated: 1790267552
@@ -0,0 +1,100 @@
using System;
using UnityEngine;
using UnityEngine.InputSystem;
namespace NonSolid
{
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
[Header("Movement")]
[SerializeField] private float speed = 5f;
[SerializeField] private float rotationSpeed = 60f;
[Header("Firing")]
[SerializeField] private Transform shootSpawn;
[SerializeField] private Rigidbody projectilePrefab;
[SerializeField] private float shootForce = 10.0f;
[SerializeField] private FireType fireType;
[Header("Inputs")]
[SerializeField] private InputActionReference movementInputReference;
[SerializeField] private InputActionReference rotationInputReference;
[SerializeField] private InputActionReference fireInputReference;
private CharacterController _characterController;
private Vector2 _movementInput;
private Vector2 _rotationInput;
private void Awake()
{
_characterController = GetComponent<CharacterController>();
}
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;
}
private void FixedUpdate()
{
HandleInput();
MoveAndRotate();
}
private void HandleInput()
{
_movementInput = movementInputReference.action.ReadValue<Vector2>();
_rotationInput = rotationInputReference.action.ReadValue<Vector2>();
}
private void MoveAndRotate()
{
transform.Rotate(0, _rotationInput.x * Time.deltaTime * rotationSpeed, 0);
_characterController.Move(new Vector3(_movementInput.x, 0, _movementInput.y) * (speed * Time.deltaTime));
}
private void OnFireActionPerformed(InputAction.CallbackContext context)
{
Fire();
}
private void Fire()
{
switch (fireType)
{
case FireType.Bullet:
Rigidbody projectile = Instantiate(projectilePrefab, shootSpawn.position, shootSpawn.rotation);
projectile.AddForce(shootSpawn.forward * shootForce, ForceMode.Impulse);
break;
case FireType.Spell:
break;
case FireType.Laser:
break;
case FireType.Grenade:
break;
case FireType.Rocket:
break;
case FireType.Arrow:
break;
case FireType.Fireball:
break;
case FireType.Plasma:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f4418c4eb9374498a19bc4eb30194b85
timeCreated: 1790261826
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eb22851d69244e5ea85ecf7cb6ac41e9
timeCreated: 1790263373
@@ -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
+49
View File
@@ -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
+3
View File
@@ -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