(client) feat:添加设置类,子弹添加敌我识别; chore:修改了右键菜单的初始化方式为独立创建,加载定义报错提供更多信息,动画加载出错也返回默认序列。

This commit is contained in:
m0_75251201
2025-08-19 14:36:22 +08:00
parent f67aca0804
commit f4cd5f4a86
57 changed files with 1000 additions and 788 deletions

View File

@ -1,10 +1,16 @@
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);
@ -14,24 +20,47 @@ namespace Entity
protected override void AutoBehave()
{
TryMove();
lifeTime-=Time.deltaTime;
if (lifeTime <= 0)
{
Kill();
}
}
private void OnTriggerEnter2D(Collider2D other)
{
other.GetComponent<Entity>()?.OnHit(this);
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)
{
// 默认向上方向
Vector3 upDirection = Vector3.up;
// 确保目标方向不是零向量
if (targetDirection == Vector3.zero)
return;
// 计算旋转角度
float angle = Mathf.Atan2(targetDirection.x, targetDirection.y) * Mathf.Rad2Deg;
// 计算当前向上方向与目标方向之间的角度
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
// 设置旋转
transform.rotation = Quaternion.Euler(0, 0, angle);
// 调整角度因为默认贴图向上是0度而Atan2计算的是相对于x轴的角度
angle -= 90f;
// 应用旋转
transform.rotation = Quaternion.Euler(0f, 0f, angle);
}
}
}