You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class StraightBullet : BulletBase
|
|
{
|
|
[SerializeField] float maxDistance = 0f;
|
|
|
|
float flyingDistance = 0.0f;
|
|
HashSet<Collider2D> 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<Collider2D> collided)
|
|
{
|
|
foreach (var target in currentColliders)
|
|
{
|
|
collided.Invoke(target);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if(!other.TryGetComponent<Entity>(out var entity)) return;
|
|
|
|
currentColliders.Add(other);
|
|
OnEnterCollider?.Invoke(entity);
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D collision)
|
|
{
|
|
currentColliders.Remove(collision);
|
|
}
|
|
}
|
|
|
|
|
|
|