Starter
This commit is contained in:
@@ -3,105 +3,106 @@ using UnityEngine;
|
||||
|
||||
namespace Arena.Combat
|
||||
{
|
||||
/// <summary>
|
||||
/// Fires projectiles from a muzzle, limited by the fire interval and the ammo count.
|
||||
/// Placed on the root of the shooter, which the projectiles ignore.
|
||||
/// </summary>
|
||||
public class ProjectileLauncher : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
[Tooltip("Weapon data: damage, fire interval, ammo, projectile.")]
|
||||
public WeaponData Weapon;
|
||||
}
|
||||
/// <summary>
|
||||
/// Fires projectiles from a muzzle, limited by the fire interval and the ammo count.
|
||||
/// Placed on the root of the shooter, which the projectiles ignore.
|
||||
/// </summary>
|
||||
public class ProjectileLauncher : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class Settings
|
||||
{
|
||||
[Tooltip("Weapon data: damage, fire interval, ammo, projectile.")]
|
||||
public WeaponData Weapon;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public struct References
|
||||
{
|
||||
[Tooltip("Spawn point of the projectiles. Its forward axis gives the direction.")]
|
||||
public Transform Muzzle;
|
||||
}
|
||||
[System.Serializable]
|
||||
public struct References
|
||||
{
|
||||
[Tooltip("Spawn point of the projectiles. Its forward axis gives the direction.")]
|
||||
public Transform Muzzle;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
public int Ammo;
|
||||
}
|
||||
[System.Serializable]
|
||||
public class StateContainer
|
||||
{
|
||||
public int Ammo;
|
||||
}
|
||||
|
||||
#region Fields
|
||||
#region FIELDS
|
||||
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private StateContainer _state;
|
||||
[SerializeField] private Settings _settings;
|
||||
[SerializeField] private References _references;
|
||||
[SerializeField] private StateContainer _state;
|
||||
|
||||
/// <summary>
|
||||
/// Raised with the current and max ammo each time the ammo changes.
|
||||
/// </summary>
|
||||
public event Action<int, int> OnAmmoChange;
|
||||
/// <summary>
|
||||
/// Raised with the current and max ammo each time the ammo changes.
|
||||
/// </summary>
|
||||
public event Action<int, int> OnAmmoChange;
|
||||
|
||||
public StateContainer State => _state;
|
||||
public int MaxAmmo => _settings.Weapon.MaxAmmo;
|
||||
public StateContainer State => _state;
|
||||
public int MaxAmmo => _settings.Weapon.MaxAmmo;
|
||||
|
||||
private float _nextFireTime;
|
||||
private float _nextFireTime;
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Unity Lifecycle
|
||||
#region UNITY LIFECYCLES
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_state.Ammo = MaxAmmo;
|
||||
}
|
||||
private void Awake()
|
||||
{
|
||||
_state.Ammo = MaxAmmo;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
#region PUBLIC METHODS
|
||||
|
||||
/// <summary>
|
||||
/// Fires one projectile if the weapon is ready and has ammo. Returns true if a shot was fired.
|
||||
/// </summary>
|
||||
public bool TryFire()
|
||||
{
|
||||
if (Time.time < _nextFireTime || _state.Ammo <= 0)
|
||||
return false;
|
||||
/// <summary>
|
||||
/// Fires one projectile if the weapon is ready and has ammo. Returns true if a shot was fired.
|
||||
/// </summary>
|
||||
public bool TryFire()
|
||||
{
|
||||
if (Time.time < _nextFireTime || _state.Ammo <= 0)
|
||||
return false;
|
||||
|
||||
_nextFireTime = Time.time + _settings.Weapon.FireInterval;
|
||||
SetAmmo(_state.Ammo - 1);
|
||||
_nextFireTime = Time.time + _settings.Weapon.FireInterval;
|
||||
|
||||
Transform muzzle = _references.Muzzle;
|
||||
Projectile projectile = Instantiate(_settings.Weapon.ProjectilePrefab, muzzle.position, muzzle.rotation);
|
||||
projectile.Launch(_settings.Weapon.Damage, gameObject);
|
||||
return true;
|
||||
}
|
||||
SetAmmo(_state.Ammo - 1);
|
||||
|
||||
/// <summary>
|
||||
/// Adds ammo without exceeding the weapon maximum.
|
||||
/// </summary>
|
||||
public void AddAmmo(int amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
return;
|
||||
Transform muzzle = _references.Muzzle;
|
||||
Projectile projectile = Instantiate(_settings.Weapon.ProjectilePrefab, muzzle.position, muzzle.rotation);
|
||||
projectile.Launch(new DamageInfo(_settings.Weapon.Damage, _settings.Weapon.DamageType, gameObject));
|
||||
return true;
|
||||
}
|
||||
|
||||
SetAmmo(Mathf.Min(_state.Ammo + amount, MaxAmmo));
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds ammo without exceeding the weapon maximum.
|
||||
/// </summary>
|
||||
public void AddAmmo(int amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
return;
|
||||
|
||||
#endregion
|
||||
SetAmmo(Mathf.Min(_state.Ammo + amount, MaxAmmo));
|
||||
}
|
||||
|
||||
#region Private Methods
|
||||
#endregion
|
||||
|
||||
private void SetAmmo(int value)
|
||||
{
|
||||
_state.Ammo = value;
|
||||
OnAmmoChange?.Invoke(_state.Ammo, MaxAmmo);
|
||||
}
|
||||
#region PRIVATE METHODS
|
||||
|
||||
[ContextMenu("Debug/Try Fire")]
|
||||
private void DebugTryFire()
|
||||
{
|
||||
TryFire();
|
||||
}
|
||||
private void SetAmmo(int value)
|
||||
{
|
||||
_state.Ammo = value;
|
||||
OnAmmoChange?.Invoke(_state.Ammo, MaxAmmo);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
[ContextMenu("Debug/Try Fire")]
|
||||
private void DebugTryFire()
|
||||
{
|
||||
TryFire();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user