(client) feat:实现子弹的生成

This commit is contained in:
m0_75251201
2025-08-17 11:16:55 +08:00
parent 12a4f9efaa
commit ed7ecdb226
22 changed files with 436 additions and 182 deletions

View File

@ -3,8 +3,14 @@ using UnityEngine;
namespace Entity
{
public class Bullet:Entity
public class Bullet : Entity
{
public override void SetTarget(Vector3 pos)
{
base.SetTarget(pos);
RotateTransformToDirection(transform, direction);
}
protected override void AutoBehave()
{
TryMove();
@ -13,6 +19,19 @@ namespace Entity
private void OnTriggerEnter2D(Collider2D other)
{
other.GetComponent<Entity>()?.OnHit(this);
attributes.health -= 1;
}
// 旋转对象到指定方向
public static void RotateTransformToDirection(Transform transform, Vector3 targetDirection)
{
// 默认向上方向
Vector3 upDirection = Vector3.up;
// 计算旋转角度
float angle = Mathf.Atan2(targetDirection.x, targetDirection.y) * Mathf.Rad2Deg;
// 设置旋转
transform.rotation = Quaternion.Euler(0, 0, angle);
}
}
}

View File

@ -10,7 +10,7 @@ namespace Entity
public class Character : Entity
{
public CharacterDef characterDef;
private void Start()
{
aiTree = new JobGiver_RandomWander();
@ -22,6 +22,17 @@ namespace Entity
if (characterDef == null)
return;
}
public override void TryAttack()
{
if (IsAttacking)
return;
if (!Managers.DefineManager.Instance.defines.TryGetValue(nameof(BulletDef), out var def))
return;
var buttonDef = def.Values.First();
Vector3 dir = Utils.MousePosition.GetWorldPosition();
Managers.EntityManage.Instance.GenerateBulletEntity((BulletDef)buttonDef, Position,
dir - Position);
}
}
}

View File

@ -134,7 +134,7 @@ namespace Entity
// 协程引用
private Coroutine attackCoroutine;
protected PawnDef entityDef;
protected EntityDef entityDef;
public float hitBarUIShowTime = 5;
@ -144,14 +144,14 @@ namespace Entity
/// <summary>
/// 初始化实体的基本属性和行为树。
/// </summary>
/// <param name="pawnDef">实体的定义数据。</param>
public virtual void Init(PawnDef pawnDef)
/// <param name="entityDef">实体的定义数据。</param>
public virtual void Init(EntityDef entityDef)
{
attributes = pawnDef.attributes.Clone();
aiTree = Utils.BehaviorTree.ConvertToAIBase(pawnDef.behaviorTree);
affiliation = pawnDef.affiliation;
InitBody(pawnDef.drawingOrder);
entityDef = pawnDef;
attributes = entityDef.attributes.Clone();
aiTree = Utils.BehaviorTree.ConvertToAIBase(entityDef.behaviorTree);
affiliation = entityDef.affiliation;
InitBody(entityDef.drawingOrder);
this.entityDef = entityDef;
HideHealthBar();
}
@ -288,9 +288,18 @@ namespace Entity
/// <param name="orientation">新的朝向。</param>
public virtual void SetOrientation(Orientation orientation)
{
bodyNodes[currentOrientation]?.SetActive(false);
// 禁用当前朝向的节点
if (bodyNodes.TryGetValue(currentOrientation, out var currentNode))
{
currentNode.SetActive(false);
}
// 设置新的朝向
currentOrientation = orientation;
bodyNodes[orientation]?.SetActive(true);
// 激活新朝向的节点
if (bodyNodes.TryGetValue(orientation, out var newNode))
{
newNode.SetActive(true);
}
}
/// <summary>
@ -475,20 +484,13 @@ namespace Entity
public void DetectAndAttackEnemies()
{
const int attackRange = 3;
var attackCount = 3;
// 获取攻击范围内的所有碰撞体使用LayerMask过滤掉非敌人
var attackCount = attributes.attackTargetCount;
// 获取攻击范围内的所有碰撞体
var hits = Physics2D.OverlapCircleAll(
transform.position,
attackRange,
attributes.attackRange,
LayerMask.GetMask("Entity"));
// 或者使用标签过滤(如果敌人有特定标签)
// Collider2D[] hits = Physics2D.OverlapCircleAll(transform.position, attackRange);
Debug.Log($"Found {hits.Length} potential targets in attack range");
foreach (var hit in hits)
{
if (attackCount <= 0) break;