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

66 lines
1.9 KiB
C#
Raw Normal View History

using System;
using Base;
using Data;
using Prefab;
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) 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;
// 计算当前向上方向与目标方向之间的角度
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
// 调整角度因为默认贴图向上是0度而Atan2计算的是相对于x轴的角度
angle -= 90f;
// 应用旋转
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}
}