This commit is contained in:
2026-11-04 15:51:04 +01:00
parent d21f0c9b6c
commit cd65f702ae
111 changed files with 246848 additions and 1021 deletions

View File

@@ -2,67 +2,64 @@ using UnityEngine;
namespace Arena.Combat
{
/// <summary>
/// Moves straight ahead and damages the first IDamageable it touches.
/// Needs a trigger collider and a kinematic Rigidbody.
/// </summary>
[RequireComponent(typeof(Rigidbody))]
public class Projectile : MonoBehaviour
{
[System.Serializable]
public class Settings
{
[Tooltip("Speed, in meters per second.")]
[Min(0f)] public float Speed = 20f;
/// <summary>
/// Moves straight ahead and damages the first IDamageable it touches.
/// Needs a trigger collider and a kinematic Rigidbody.
/// </summary>
[RequireComponent(typeof(Rigidbody))]
public class Projectile : MonoBehaviour
{
[System.Serializable]
public class Settings
{
[Tooltip("Speed, in meters per second.")]
[Min(0f)] public float Speed = 20f;
[Tooltip("Time before an unused projectile is destroyed, in seconds.")]
[Min(0.1f)] public float Lifetime = 2f;
}
[Tooltip("Time before an unused projectile is destroyed, in seconds.")]
[Min(0.1f)] public float Lifetime = 2f;
}
#region Fields
#region FIELDS
[SerializeField] private Settings _settings;
[SerializeField] private Settings _settings;
private int _damage;
private GameObject _owner;
private DamageInfo _damage;
#endregion
#endregion
#region Unity Lifecycle
#region UNITY LIFECYCLES
private void Update()
{
transform.position += transform.forward * _settings.Speed * Time.deltaTime;
}
private void Update()
{
transform.position += transform.forward * _settings.Speed * Time.deltaTime;
}
private void OnTriggerEnter(Collider other)
{
// Ignore the shooter and the other trigger zones
if (other.isTrigger || other.gameObject == _owner)
return;
private void OnTriggerEnter(Collider other)
{
// Ignore the shooter and the other trigger zones
if (other.isTrigger || other.gameObject == _damage.Source)
return;
if (other.TryGetComponent(out IDamageable target))
target.TakeDamage(_damage);
if (other.TryGetComponent(out IDamageable target))
target.TakeDamage(_damage);
Destroy(gameObject);
}
Destroy(gameObject);
}
#endregion
#endregion
#region Public Methods
#region PUBLIC METHODS
/// <summary>
/// Starts the projectile. Called right after Instantiate by whoever fires it.
/// </summary>
/// <param name="damage">Damage dealt to the IDamageable it touches.</param>
/// <param name="owner">Shooter, ignored by the projectile.</param>
public void Launch(int damage, GameObject owner)
{
_damage = damage;
_owner = owner;
Destroy(gameObject, _settings.Lifetime);
}
/// <summary>
/// Starts the projectile. Called right after Instantiate by whoever fires it.
/// </summary>
/// <param name="damage">Damage dealt to the IDamageable it touches. Its source is the shooter, ignored by the projectile.</param>
public void Launch(DamageInfo damage)
{
_damage = damage;
Destroy(gameObject, _settings.Lifetime);
}
#endregion
}
#endregion
}
}