Add Unity materials and prefabs for game objects for SOLID
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34e471dea99d147cfa16a2326db6203c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace NonSolid
|
||||
{
|
||||
public enum AreaOfEffectType
|
||||
{
|
||||
Circle,
|
||||
Line,
|
||||
Rectangle,
|
||||
Polygon,
|
||||
Hexagon
|
||||
}
|
||||
|
||||
public class AreaOfEffect : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private AreaOfEffectType _areaOfEffectType;
|
||||
[SerializeField] private float _radius = 10.0f; // circle
|
||||
[SerializeField] private float _length = 10.0f; // line
|
||||
[SerializeField] private float _width = 1.0f; // line
|
||||
[SerializeField] private Rect _rect; // rectangle
|
||||
|
||||
private void OnCollisionEnter(Collision other)
|
||||
{
|
||||
float area = 0.0f;
|
||||
|
||||
switch (_areaOfEffectType)
|
||||
{
|
||||
case AreaOfEffectType.Circle:
|
||||
area = Mathf.PI * _radius * _radius;
|
||||
break;
|
||||
case AreaOfEffectType.Line:
|
||||
break;
|
||||
case AreaOfEffectType.Rectangle:
|
||||
break;
|
||||
case AreaOfEffectType.Polygon:
|
||||
break;
|
||||
case AreaOfEffectType.Hexagon:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5f36882c752d4a708fcfd61ab377b21b
|
||||
timeCreated: 1774973643
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace NonSolid
|
||||
{
|
||||
public enum FireType
|
||||
{
|
||||
Bullet,
|
||||
Spell,
|
||||
Arrow
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e545c19b54174187a546da449a82fe4e
|
||||
timeCreated: 1774971768
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NonSolid
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float _speed = 10.0f;
|
||||
[SerializeField] private float _rotationSpeed = 60.0f;
|
||||
|
||||
[Header("Firing")]
|
||||
[SerializeField] private Transform _shootSpawn;
|
||||
[SerializeField] private Rigidbody _projectilePrefab;
|
||||
[SerializeField] private float _shootForce = 10.0f;
|
||||
[SerializeField] private FireType _fireType = FireType.Bullet;
|
||||
|
||||
[Header("Inputs")]
|
||||
[SerializeField] private InputActionReference _movementInputAction;
|
||||
[SerializeField] private InputActionReference _rotationInputAction;
|
||||
[SerializeField] private InputActionReference _fireInputAction;
|
||||
|
||||
private CharacterController _characterController;
|
||||
private Vector2 _movement;
|
||||
private Vector2 _rotation;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_characterController = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_movementInputAction.action.Enable();
|
||||
_rotationInputAction.action.Enable();
|
||||
_fireInputAction.action.Enable();
|
||||
_fireInputAction.action.performed += OnFireActionPerformed;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_movementInputAction.action.Disable();
|
||||
_rotationInputAction.action.Disable();
|
||||
_fireInputAction.action.Disable();
|
||||
_fireInputAction.action.performed -= OnFireActionPerformed;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleInput();
|
||||
MoveAndRotate();
|
||||
}
|
||||
|
||||
private void HandleInput()
|
||||
{
|
||||
_movement = _movementInputAction.action.ReadValue<Vector2>();
|
||||
_rotation = _rotationInputAction.action.ReadValue<Vector2>();
|
||||
}
|
||||
|
||||
private void MoveAndRotate()
|
||||
{
|
||||
transform.Rotate(0, _rotation.x * _rotationSpeed * Time.deltaTime, 0);
|
||||
_characterController.Move(new Vector3(_movement.x, 0, _movement.y) * (_speed * Time.deltaTime));
|
||||
}
|
||||
|
||||
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.Arrow:
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFireActionPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Fire();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a36a3c6df048464da7de1ecc52f7102f
|
||||
timeCreated: 1774966133
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ceb23550fa7c542e888d3bc1a4b36a46
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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