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
@@ -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