Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Entity/Bullet.cs

62 lines
1.8 KiB
C#

using Base;
using Data;
using UnityEngine;
namespace Entity
{
public class Bullet : Entity
{
public Entity bulletSource;
public float lifeTime = 10;
public override void SetTarget(Vector3 pos)
{
base.SetTarget(pos);
RotateTransformToDirection(transform, direction);
}
protected override void AutoBehave()
{
TryMove();
lifeTime -= Time.deltaTime;
if (lifeTime <= 0)
{
Kill();
}
}
private void OnTriggerEnter2D(Collider2D other)
{
var entity = other.GetComponent<Entity>();
if (!entity || entity == bulletSource || entity is Pickup) return;
if (Managers.AffiliationManager.Instance.GetRelation(bulletSource.affiliation, entity.affiliation) != Relation.Friendly)
{
entity.OnHit(this);
}
else if (Setting.Instance.CurrentSettings.friendlyFire)
{
entity.OnHit(this);
}
else
{
return; // 如果是友好关系且不允许友军伤害,则不处理
}
attributes.health -= 1;
}
// 旋转对象到指定方向
public static void RotateTransformToDirection(Transform transform, Vector3 targetDirection)
{
// 确保目标方向不是零向量
if (targetDirection == Vector3.zero)
return;
// 计算当前向上方向与目标方向之间的角度
var angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
// 应用旋转
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}
}