using System; using UnityEngine; public abstract class BulletBase : Entity { [SerializeField] float speed = 10.0f; [SerializeField] float lifeTime = 5.0f; Vector2 headDirection; float elapsedLifeTime = 0.0f; public float Speed { get => speed; set => speed = value; } public Vector2 HeadDirection { get { return Utility.GetDirection2D(transform.eulerAngles.z); } set { headDirection = value.normalized; transform.eulerAngles = new Vector3(0, 0, Utility.GetAngle2D(headDirection)); } } public bool IsFired { get; protected set; } = false; public Action OnBeforeDestroy; public Action OnEnterCollider; private void LateUpdate() { if (!IsFired) return; bool shouldDestroy = !UpdateLifeTime(); shouldDestroy |= !UpdateBullet(Time.fixedDeltaTime); if (shouldDestroy) { OnBeforeDestroy?.Invoke(this); Destroy(gameObject); } } private bool UpdateLifeTime() { elapsedLifeTime += Time.fixedDeltaTime; return elapsedLifeTime < lifeTime; } protected abstract bool UpdateBullet(float deltaTime); public virtual void Fire(Transform target = null, bool resetLifeTime = true) { if (target != null) HeadDirection = target.position - transform.position; if(resetLifeTime) elapsedLifeTime = 0; IsFired = true; } }