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.
64 lines
1.5 KiB
64 lines
1.5 KiB
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<BulletBase> OnBeforeDestroy;
|
|
public Action<Entity> 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;
|
|
}
|
|
}
|