add unity project

add SOLID examples
This commit is contained in:
2025-04-09 07:49:12 +02:00
parent ff37858d12
commit a4f498a33d
102 changed files with 8244 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
namespace DefaultNamespace
{
public class BaseClass {}
public class ChildClass : BaseClass {}
public delegate void CoVarianceHandler(ChildClass args);
public delegate BaseClass ContraVarianceHandler();
public class Delegates
{
public event CoVarianceHandler CoVariance;
public event ContraVarianceHandler ContraVariance;
public void OnBaseClass(BaseClass args) {}
public void OnChildClass(ChildClass args) {}
public BaseClass OnReturnBaseClass()
{
return new BaseClass();
}
public ChildClass OnReturnChildClass()
{
return new ChildClass();
}
public void Init()
{
CoVariance += OnBaseClass;
CoVariance += OnChildClass;
CoVariance?.Invoke(new ChildClass());
// CoVariance?.Invoke(new BaseClass()); // is not working, because delegate expect an instance of ChildClass
ContraVariance += OnReturnBaseClass;
ContraVariance += OnReturnChildClass;
BaseClass baseClass = ContraVariance?.Invoke();
// ChildClass childClass = ContraVariance?.Invoke(); // is not working, because delegate returns only an instance of BaseClass
}
}
}
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fb17c5699e2a4a79831b04f0a1ff598d
timeCreated: 1742915301
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 61639bdbdb09247fdbde9f74cf808b9b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,39 @@
using System;
using UnityEngine;
namespace DefaultNamespace.NonSolid
{
public class AreaOfEffect : MonoBehaviour
{
public enum AreaOfEffectType
{
Circle,
Rectangle,
Pentagon,
Hexagon,
}
[SerializeField] private AreaOfEffectType _areaOfEffectType;
[SerializeField] private float _radius;
private void OnCollisionEnter(Collision other)
{
float area = 0.0f;
switch (_areaOfEffectType)
{
case AreaOfEffectType.Circle:
area = Mathf.Pow(_radius, 2.0f) * Mathf.PI;
break;
case AreaOfEffectType.Rectangle:
break;
case AreaOfEffectType.Pentagon:
break;
case AreaOfEffectType.Hexagon:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b68af98e555247c78e25c77f6d3be08b
timeCreated: 1742921835
@@ -0,0 +1,9 @@
namespace DefaultNamespace.NonSolid
{
public enum FireType
{
Bullet,
Spell,
Arrow
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d1fa0acb052c4955a864dd86e2e57aa7
timeCreated: 1742920117
@@ -0,0 +1,73 @@
using System;
using UnityEngine;
namespace DefaultNamespace.NonSolid
{
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
[SerializeField] private float _speed = 10.0f;
[SerializeField] private float _rotationSpeed = 60.0f;
[SerializeField] private Transform _shootSpawn;
[SerializeField] private Rigidbody _projectilePrefab;
[SerializeField] private float _shootForce = 10.0f;
[SerializeField] private FireType _fireType = FireType.Bullet;
private CharacterController _controller;
private Vector3 _movement = Vector3.zero;
private float _rotation = 0.0f;
private bool _isFiring = false;
private void Awake()
{
_controller = GetComponent<CharacterController>();
}
private void Update()
{
HandleInput();
Move();
Fire();
}
private void HandleInput()
{
_movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
_movement *= _speed * Time.deltaTime;
_rotation = Input.GetAxis("Mouse X") * _rotationSpeed * Time.deltaTime;
_isFiring = _isFiring || Input.GetButtonDown("Fire1");
}
private void Move()
{
_controller.Move(_movement);
_controller.transform.Rotate(Vector3.up, _rotation);
}
private void Fire()
{
if (!_isFiring) return;
_isFiring = false;
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.Arrow:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 30b03ab4d982417e8e22d58811009924
timeCreated: 1742916420
+3
View File
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3f475ccbcc24c6d9e7143b685db1556
timeCreated: 1742918125
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bfd022384a38437d9251332a249070bf
timeCreated: 1742922156
@@ -0,0 +1,30 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
public abstract class AreaOfEffect : MonoBehaviour
{
[SerializeField] private ParticleSystem _particleSystem;
[SerializeField] private AudioClip _audioClip;
[SerializeField] private float _duration;
private AudioSource _audioSource;
protected abstract bool IsInsideOfEffectArea(Collision other);
private void OnCollisionEnter(Collision other)
{
if (!IsInsideOfEffectArea(other)) return;
_particleSystem.Play();
_audioSource.PlayOneShot(_audioClip);
ApplyEffect();
}
private void ApplyEffect()
{
// apply effect to something
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c53bc1a1659c4d01b1d720a0039108e8
timeCreated: 1742922168
@@ -0,0 +1,17 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
public class BulletShooter : MonoBehaviour, IShooter
{
[SerializeField] private Transform _shootSpawn;
[SerializeField] private Rigidbody _projectilePrefab;
[SerializeField] private float _shootForce = 100.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: 44a3071142bd4f029da48c1862acc1cc
timeCreated: 1742918277
@@ -0,0 +1,16 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
public class CircleAreaOfEffect : AreaOfEffect
{
[SerializeField] private float _radius;
protected override bool IsInsideOfEffectArea(Collision other)
{
// do some calculations here
return false;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f9caf160a09a493f8e0eb82e287d9a9a
timeCreated: 1742922396
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a8227b0c9bc34311a104e8b0b76dc31c
timeCreated: 1742920524
@@ -0,0 +1,7 @@
namespace DefaultNamespace.Solid
{
public interface IDamageable
{
void TakeDamage(float damage);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c5914f06c3394359bc5943113e36c8c0
timeCreated: 1742921379
@@ -0,0 +1,9 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
public interface IEffectTrigger
{
void ApplyEffect();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 40fc6b97c8a64f359e5eb024c96a166e
timeCreated: 1742921330
@@ -0,0 +1,7 @@
namespace DefaultNamespace.Solid
{
public interface IExplodable
{
void Explode();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6dd9e4f0b84148589e7e20e8e1cef27d
timeCreated: 1742921363
@@ -0,0 +1,7 @@
namespace DefaultNamespace.Solid
{
public interface IHealth
{
void Heal(float healAmount);
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 791cec6c848c4d7d9ec0f0e498389bb0
timeCreated: 1742921404
@@ -0,0 +1,11 @@
namespace DefaultNamespace.Solid
{
public interface IShooter
{
void Fire();
// BAD IDEA - segregate interfaces
// void ApplyEffect();
// void Explode();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cc6f6606157e44a4b77936fc2e025899
timeCreated: 1742920554
@@ -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();
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 917fddf598d04ebbbc279653f77628d9
timeCreated: 1742918135
+29
View File
@@ -0,0 +1,29 @@
using System;
using UnityEngine;
namespace DefaultNamespace.Solid
{
public class PlayerInput : MonoBehaviour
{
[SerializeField] private float _speed = 10.0f;
[SerializeField] private float _rotationSpeed = 60.0f;
public Vector3 Movement { get; private set; }
public float Rotation { get; private set; }
public event Action IsFired;
public void HandleInput()
{
Movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Movement *= _speed * Time.deltaTime;
Rotation = Input.GetAxis("Mouse X") * _rotationSpeed * Time.deltaTime;
if (Input.GetButtonDown("Fire1"))
{
IsFired?.Invoke();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a979a309ce924263a48251cb3936c16d
timeCreated: 1742918256
@@ -0,0 +1,21 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour
{
private CharacterController _controller;
private void Awake()
{
_controller = GetComponent<CharacterController>();
}
public void MoveAndRotate(Vector3 movement, float rotation)
{
_controller.Move(movement);
_controller.transform.Rotate(Vector3.up, rotation);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4f8b579757eb41e1bf4932c80f1b02d6
timeCreated: 1742918266
@@ -0,0 +1,17 @@
using UnityEngine;
namespace DefaultNamespace.Solid
{
public class RectangleAreaOfEffect : AreaOfEffect
{
[SerializeField] private float _width;
[SerializeField] private float _height;
protected override bool IsInsideOfEffectArea(Collision other)
{
// do some calculations
return false;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a0ecf7b8d0d34b5799accbf1adec3de5
timeCreated: 1742922487