2025-08-14 13:26:02 +08:00
|
|
|
|
using System;
|
2025-08-19 14:36:22 +08:00
|
|
|
|
using Base;
|
|
|
|
|
using Data;
|
|
|
|
|
using Prefab;
|
2025-08-14 13:26:02 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Entity
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public class Bullet : Entity
|
2025-08-14 13:26:02 +08:00
|
|
|
|
{
|
2025-08-19 14:36:22 +08:00
|
|
|
|
public Entity bulletSource;
|
|
|
|
|
public float lifeTime = 10;
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public override void SetTarget(Vector3 pos)
|
|
|
|
|
{
|
|
|
|
|
base.SetTarget(pos);
|
|
|
|
|
RotateTransformToDirection(transform, direction);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-14 13:26:02 +08:00
|
|
|
|
protected override void AutoBehave()
|
|
|
|
|
{
|
|
|
|
|
TryMove();
|
2025-08-19 14:36:22 +08:00
|
|
|
|
lifeTime-=Time.deltaTime;
|
|
|
|
|
if (lifeTime <= 0)
|
|
|
|
|
{
|
|
|
|
|
Kill();
|
|
|
|
|
}
|
2025-08-14 13:26:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
|
|
|
{
|
2025-08-19 14:36:22 +08:00
|
|
|
|
var entity = other.GetComponent<Entity>();
|
|
|
|
|
if (!entity || entity == bulletSource) 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; // 如果是友好关系且不允许友军伤害,则不处理
|
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
attributes.health -= 1;
|
|
|
|
|
}
|
2025-08-19 14:36:22 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 旋转对象到指定方向
|
|
|
|
|
public static void RotateTransformToDirection(Transform transform, Vector3 targetDirection)
|
|
|
|
|
{
|
2025-08-19 14:36:22 +08:00
|
|
|
|
// 确保目标方向不是零向量
|
|
|
|
|
if (targetDirection == Vector3.zero)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 计算当前向上方向与目标方向之间的角度
|
|
|
|
|
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-08-19 14:36:22 +08:00
|
|
|
|
// 调整角度,因为默认贴图向上是0度,而Atan2计算的是相对于x轴的角度
|
|
|
|
|
angle -= 90f;
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-08-19 14:36:22 +08:00
|
|
|
|
// 应用旋转
|
|
|
|
|
transform.rotation = Quaternion.Euler(0f, 0f, angle);
|
2025-08-14 13:26:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|