Add Unity materials and prefabs for game objects for SOLID
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c41094ad7cd4abba73a4265199ac20d
|
||||
timeCreated: 1774973951
|
||||
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid.Abstracts
|
||||
{
|
||||
public abstract class AreaOfEffect : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ParticleSystem _particleSystem;
|
||||
[SerializeField] private AudioClip _audioClip;
|
||||
[SerializeField] private float _duration = 0.5f;
|
||||
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
if (!IsInsideOfEffectArea(other.collider)) return;
|
||||
|
||||
_particleSystem.Play();
|
||||
AudioSource.PlayClipAtPoint(_audioClip, transform.position, _duration);
|
||||
|
||||
ApplyEffect();
|
||||
}
|
||||
|
||||
protected abstract bool IsInsideOfEffectArea(Collider other);
|
||||
|
||||
private void ApplyEffect()
|
||||
{
|
||||
// apply effect here
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 596149fd95654692bd453bd98ea669e5
|
||||
timeCreated: 1774973960
|
||||
@@ -0,0 +1,17 @@
|
||||
using Solid.Abstracts;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
public class CircleAreaOfEffect : AreaOfEffect
|
||||
{
|
||||
[SerializeField] private float _radius = 10.0f;
|
||||
|
||||
protected override bool IsInsideOfEffectArea(Collider other)
|
||||
{
|
||||
// do some calculation here
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c40a35b3cad49c6a5d70358ad1e6a97
|
||||
timeCreated: 1774974187
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d90ea988a184494782e5d2a1aa059b31
|
||||
timeCreated: 1774969660
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Solid.Interfaces
|
||||
{
|
||||
public interface IExplodable
|
||||
{
|
||||
void Explode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f87bee86aa74481bb8c4e5fcf14524b5
|
||||
timeCreated: 1774974514
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid.Interfaces
|
||||
{
|
||||
public interface IInput
|
||||
{
|
||||
public Vector2 Movement { get; }
|
||||
public Vector2 Rotation { get; }
|
||||
public event Action Fired;
|
||||
|
||||
public void HandleInput();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41b0ac172fdd4519908c7dc44b7ea4e5
|
||||
timeCreated: 1774969668
|
||||
@@ -0,0 +1,64 @@
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
[RequireComponent(typeof(PlayerInput), typeof(PlayerMovement))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private IInput _playerInput;
|
||||
private PlayerMovement _playerMovement;
|
||||
private IShooter _shooter;
|
||||
|
||||
// BAD IDEA
|
||||
// [SerializeField] private bool _isUsingGamepad = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// BAD IDEA
|
||||
// don't use concrete classes, use abstraction instead, e.g. IInput interface
|
||||
// don't instantiate concrete classes, use dependency injection instead => use services
|
||||
// if (_isUsingGamepad)
|
||||
// {
|
||||
// _playerInput = new GamepadInput();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// _playerInput = new KeyboardMouseInput();
|
||||
// }
|
||||
|
||||
_playerInput = GetComponent<IInput>();
|
||||
_playerMovement = GetComponent<PlayerMovement>();
|
||||
|
||||
// _shooter = GetComponent<IShooter>() ?? gameObject.AddComponent<BulletShooter>(); // default option, not so good
|
||||
|
||||
// better solution
|
||||
_shooter = GetComponent<IShooter>();
|
||||
Assert.IsNotNull(_shooter, "No IShooter component found on the GameObject.");
|
||||
|
||||
// best solution will be again to use dependency injection
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_playerInput.Fired += OnFired;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_playerInput.Fired -= OnFired;
|
||||
}
|
||||
|
||||
private void OnFired()
|
||||
{
|
||||
_shooter.Fire();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_playerInput.HandleInput();
|
||||
_playerMovement.MoveAndRotate(_playerInput.Movement, _playerInput.Rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9033477791e94b288558fefd566db663
|
||||
timeCreated: 1774968306
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
public class PlayerInput : MonoBehaviour, IInput
|
||||
{
|
||||
[SerializeField] private InputActionReference _movementInputAction;
|
||||
[SerializeField] private InputActionReference _rotationInputAction;
|
||||
[SerializeField] private InputActionReference _fireInputAction;
|
||||
|
||||
private Vector2 _movement;
|
||||
private Vector2 _rotation;
|
||||
|
||||
public Vector2 Movement => _movement;
|
||||
public Vector2 Rotation => _rotation;
|
||||
public event Action Fired;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_movementInputAction.action.Enable();
|
||||
_rotationInputAction.action.Enable();
|
||||
_fireInputAction.action.performed += OnFireActionPerformed;
|
||||
_fireInputAction.action.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_movementInputAction.action.Disable();
|
||||
_rotationInputAction.action.Disable();
|
||||
_fireInputAction.action.Disable();
|
||||
_fireInputAction.action.performed -= OnFireActionPerformed;
|
||||
}
|
||||
|
||||
public void HandleInput()
|
||||
{
|
||||
_movement = _movementInputAction.action.ReadValue<Vector2>();
|
||||
_rotation = _rotationInputAction.action.ReadValue<Vector2>();
|
||||
}
|
||||
|
||||
protected virtual void OnFireActionPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Fired?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75c2ef32ca8748a2985046c4860e770b
|
||||
timeCreated: 1774968347
|
||||
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float _speed = 10.0f;
|
||||
[SerializeField] private float _rotationSpeed = 60.0f;
|
||||
|
||||
private CharacterController _characterController;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_characterController = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
public void MoveAndRotate(Vector2 movement, Vector2 rotation)
|
||||
{
|
||||
transform.Rotate(0, rotation.x * _rotationSpeed * Time.deltaTime, 0);
|
||||
_characterController.Move(new Vector3(movement.x, 0, movement.y) * (_speed * Time.deltaTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88f9115ce5c04b42b07b3f2f9a03f1bc
|
||||
timeCreated: 1774968335
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 00b316ca7c0946b3b201585d2100c5f5
|
||||
timeCreated: 1774974319
|
||||
@@ -0,0 +1,18 @@
|
||||
using Solid.Interfaces;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Solid
|
||||
{
|
||||
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: d159adaa7cac4a7981971ad8f8bf61dd
|
||||
timeCreated: 1774972320
|
||||
@@ -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: 50a37d913948492fa7a36ccd491319a3
|
||||
timeCreated: 1774972093
|
||||
Reference in New Issue
Block a user