using System; using System.Collections.Generic; using UnityEngine; public class StraightBullet : BulletBase { [SerializeField] float maxDistance = 0f; float flyingDistance = 0.0f; HashSet currentColliders = new(); protected override bool UpdateBullet(float deltaTime) { float moveUnit = Speed * deltaTime; if (!UpdateFlyingDistance(moveUnit)) return false; transform.position += (Vector3)HeadDirection * moveUnit; return true; } private bool UpdateFlyingDistance(float moveUnit) { if(flyingDistance <= 0) return true; flyingDistance += moveUnit; return flyingDistance < maxDistance; } public override void Fire(Transform target = null, bool resetLifeTime = true) { base.Fire(target, resetLifeTime); flyingDistance = 0; } public void CheckColliding(Action collided) { foreach (var target in currentColliders) { collided.Invoke(target); } } private void OnTriggerEnter2D(Collider2D other) { if(!other.TryGetComponent(out var entity)) return; currentColliders.Add(other); OnEnterCollider?.Invoke(entity); } private void OnTriggerExit2D(Collider2D collision) { currentColliders.Remove(collision); } }