77 lines
2.1 KiB
C#
77 lines
2.1 KiB
C#
using Components;
|
|
using GeometryTD.Entity.EntityData;
|
|
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.Entity
|
|
{
|
|
public class BulletEntity : EntityBase
|
|
{
|
|
private ShooterBullet _shooterBullet;
|
|
|
|
protected override void OnInit(object userData)
|
|
{
|
|
base.OnInit(userData);
|
|
|
|
_shooterBullet = GetComponent<ShooterBullet>();
|
|
if (_shooterBullet == null)
|
|
{
|
|
Log.Error("ShooterBullet component is missing on bullet entity '{0}'.", name);
|
|
}
|
|
}
|
|
|
|
protected override void OnShow(object userData)
|
|
{
|
|
base.OnShow(userData);
|
|
|
|
if (_shooterBullet == null)
|
|
{
|
|
GameEntry.Entity.HideEntity(Entity);
|
|
return;
|
|
}
|
|
|
|
if (userData is not BulletData bulletData)
|
|
{
|
|
Log.Warning("BulletData is invalid for bullet entity '{0}'.", Id);
|
|
GameEntry.Entity.HideEntity(Entity);
|
|
return;
|
|
}
|
|
|
|
ConstrainToZRotation();
|
|
_shooterBullet.OnShow(bulletData);
|
|
if (_shooterBullet.TryConsumeDespawnRequest())
|
|
{
|
|
GameEntry.Entity.HideEntity(Entity);
|
|
}
|
|
}
|
|
|
|
protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
|
|
{
|
|
base.OnUpdate(elapseSeconds, realElapseSeconds);
|
|
|
|
if (_shooterBullet == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_shooterBullet.Tick(elapseSeconds);
|
|
if (_shooterBullet.TryConsumeDespawnRequest())
|
|
{
|
|
GameEntry.Entity.HideEntity(Entity);
|
|
}
|
|
}
|
|
|
|
protected override void OnHide(bool isShutdown, object userData)
|
|
{
|
|
_shooterBullet?.OnReset();
|
|
base.OnHide(isShutdown, userData);
|
|
}
|
|
|
|
private void ConstrainToZRotation()
|
|
{
|
|
Vector3 localEulerAngles = CachedTransform.localEulerAngles;
|
|
CachedTransform.localRotation = Quaternion.Euler(0f, 0f, localEulerAngles.z);
|
|
}
|
|
}
|
|
}
|