(client)chore:修改了摄像机的跟踪方式,摄像机控制不再使用单例

This commit is contained in:
m0_75251201
2025-08-23 16:43:28 +08:00
parent 8916440e7e
commit da93368f02
15 changed files with 1215 additions and 329 deletions

View File

@ -54,10 +54,8 @@ namespace Entity
return;
// 计算当前向上方向与目标方向之间的角度
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
// 调整角度因为默认贴图向上是0度而Atan2计算的是相对于x轴的角度
angle -= 90f;
var angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
// 应用旋转
transform.rotation = Quaternion.Euler(0f, 0f, angle);

View File

@ -84,12 +84,20 @@ namespace Entity
{
IsChase = true;
currentJob = null;
if(Program.Instance.focusedEntity)
Program.Instance.focusedEntity.PlayerControlled = false;
Program.Instance.focusedEntity = this;
}
else if (PlayerControlled)
{
Program.Instance.focusedEntity = null;
}
_isPlayerControlled = value;
}
get => _isPlayerControlled;
get => Program.Instance.focusedEntity == this;
}
public bool IsWalking => _walkingTimer > 0;
/// <summary>
/// 获取实体当前位置。
/// </summary>
@ -99,18 +107,16 @@ namespace Entity
/// 表示实体是否已经死亡(生命值小于等于零)。
/// </summary>
public bool IsDead => attributes.health <= 0;
public bool IsShowingHealthBarUI=>hitBarUIShowTimer > 0;
public bool IsAttacking => attackCoroutine != null;
public bool IsShowingHealthBarUI=>_hitBarUIShowTimer > 0;
public bool IsAttacking => _attackCoroutine != null;
private bool _isPlayerControlled = false;
private bool _warning = false;
/// <summary>
/// 存储不同朝向下的动画节点集合。
/// </summary>
public Dictionary<EntityState, Dictionary<Orientation, List<ITick>>> bodyAnimationNode = new();
private List<ITick> currentAnimatorCache=new ();
private List<ITick> _currentAnimatorCache=new ();
/// <summary>
/// 存储不同朝向下的身体节点对象。
/// </summary>
@ -119,30 +125,31 @@ namespace Entity
/// <summary>
/// 当前实体的朝向。
/// </summary>
private Orientation currentOrientation = Orientation.Down;
private Orientation _currentOrientation = Orientation.Down;
/// <summary>
/// 当前实体的状态
/// </summary>
private EntityState currentState = EntityState.Idle;
private EntityState _currentState = EntityState.Idle;
/// <summary>
/// 攻击动画的持续时间(秒)。
/// </summary>
private const float attackAnimationDuration = 0.1f;
private const float AttackAnimationDuration = 0.1f;
/// <summary>
/// 抖动的偏移量。
/// </summary>
private const float shakeOffset = 0.5f;
private const float ShakeOffset = 0.5f;
// 协程引用
private Coroutine attackCoroutine;
private Coroutine _attackCoroutine;
protected EntityDef entityDef;
public float hitBarUIShowTime = 5;
private float hitBarUIShowTimer = 0;
[SerializeField] private float _hitBarUIShowTime = 5;
private float _hitBarUIShowTimer = 0;
private int _walkingTimer = 1;
/// <summary>
@ -258,7 +265,7 @@ namespace Entity
GameObject nodeObject = null;
// 根据纹理数量创建不同类型的节点
switch (drawNode.animationTextures?.Count ?? 0)
switch (drawNode.textures?.Count ?? 0)
{
case 0:
// 无纹理节点
@ -277,12 +284,12 @@ namespace Entity
nodeObject = Instantiate(imagePrefab.gameObject, parent.transform);
var texture =
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID,
drawNode.animationTextures[0]);
drawNode.textures[0]);
if (!texture)
{
Debug.LogWarning(
$"InitBodyPart: 无法获取纹理 (节点名: {drawNode.nodeName}, 纹理ID: {drawNode.animationTextures[0]})");
$"InitBodyPart: 无法获取纹理 (节点名: {drawNode.nodeName}, 纹理ID: {drawNode.textures[0]})");
}
var imagePrefabCom = nodeObject.GetComponent<ImagePrefab>();
@ -316,7 +323,7 @@ namespace Entity
animator.SetFPS(drawNode.FPS);
var animatedSprites = new List<Sprite>();
foreach (var textureId in drawNode.animationTextures)
foreach (var textureId in drawNode.textures)
{
try
{
@ -386,7 +393,15 @@ namespace Entity
/// </summary>
public void Tick()
{
if (_isPlayerControlled)
if (_walkingTimer > 0)
{
_walkingTimer -= 1;
if (_walkingTimer <= 0)
{
SetBodyTexture(EntityState.Idle, _currentOrientation);
}
}
if (PlayerControlled)
{
UpdatePlayerControls();
}
@ -395,9 +410,9 @@ namespace Entity
AutoBehave();
}
if (currentAnimatorCache!=null)
if (_currentAnimatorCache!=null)
{
foreach (var animator in currentAnimatorCache)
foreach (var animator in _currentAnimatorCache)
{
animator.Tick();
}
@ -406,8 +421,8 @@ namespace Entity
if (IsShowingHealthBarUI)
{
hitBarUIShowTimer -= Time.deltaTime;
if (hitBarUIShowTimer <= 0)
_hitBarUIShowTimer -= Time.deltaTime;
if (_hitBarUIShowTimer <= 0)
{
HideHealthBar();
}
@ -420,14 +435,14 @@ namespace Entity
public virtual void TryAttack()
{
if(!IsAttacking)
attackCoroutine = StartCoroutine(AttackFlow());
_attackCoroutine = StartCoroutine(AttackFlow());
}
public virtual void SetBodyTexture(EntityState state, Orientation orientation)
{
if (bodyNodes.TryGetValue(currentState, out var stateNode))
if (bodyNodes.TryGetValue(_currentState, out var stateNode))
{
if (stateNode.TryGetValue(currentOrientation, out var node))
if (stateNode.TryGetValue(_currentOrientation, out var node))
{
node.SetActive(false);
}
@ -441,14 +456,14 @@ namespace Entity
}
}
currentState = state;
currentOrientation = orientation;
_currentState = state;
_currentOrientation = orientation;
if (bodyAnimationNode.TryGetValue(currentState, out var animationNode))
if (bodyAnimationNode.TryGetValue(_currentState, out var animationNode))
{
if (animationNode.TryGetValue(currentOrientation, out var value))
if (animationNode.TryGetValue(_currentOrientation, out var value))
{
currentAnimatorCache=value;
_currentAnimatorCache=value;
}
}
}
@ -461,6 +476,8 @@ namespace Entity
if (IsAttacking)
return;
transform.position += direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
SetBodyTexture(EntityState.Walking,_currentOrientation);
_walkingTimer = 2;
}
/// <summary>
@ -483,7 +500,7 @@ namespace Entity
return;
healthBarPrefab.gameObject.SetActive(true);
healthBarPrefab.Progress = (float)attributes.health / entityDef.attributes.health;
hitBarUIShowTimer=hitBarUIShowTime;
_hitBarUIShowTimer=_hitBarUIShowTime;
}
public void HideHealthBar()
@ -521,7 +538,7 @@ namespace Entity
ori = direction.x > 0 ? Orientation.Right : Orientation.Left;
}
SetBodyTexture(currentState, ori);
SetBodyTexture(_currentState, ori);
}
/// <summary>
@ -608,7 +625,7 @@ namespace Entity
// 调用检测并攻击敌人的方法
DetectAndAttackEnemies();
// 攻击流程结束,清理协程引用
attackCoroutine = null;
_attackCoroutine = null;
}
@ -623,14 +640,14 @@ namespace Entity
StartCoroutine(ShakeInDirectionCoroutine());
// 返回检测敌人的起始时间
return attackAnimationDuration;
return AttackAnimationDuration;
}
private IEnumerator ShakeInDirectionCoroutine()
{
var originalPosition = transform.position; // 记录原始位置
transform.position += direction * shakeOffset;
yield return new WaitForSeconds(attackAnimationDuration);
transform.position += direction * ShakeOffset;
yield return new WaitForSeconds(AttackAnimationDuration);
transform.position = originalPosition;
}

View File

@ -110,14 +110,6 @@ namespace Entity
result.Add(("结束操控", EndControl));
else
result.Add(("手动操控", StartControl));
if (CameraControl.CameraControl.Instance.focusedEntity == entity)
{
result.Add(("取消跟随", ()=>CameraControl.CameraControl.Instance.focusedEntity=null));
}
else
{
result.Add(("视角跟随", ()=>CameraControl.CameraControl.Instance.focusedEntity=entity));
}
result.Add(("杀死", () => entity.Kill()));
result.Add(("变成笨蛋", BecomeDefault));
return result;
@ -132,13 +124,11 @@ namespace Entity
private void StartControl()
{
entity.PlayerControlled = true;
CameraControl.CameraControl.Instance.focusedEntity=entity;
}
private void EndControl()
{
entity.PlayerControlled = false;
CameraControl.CameraControl.Instance.focusedEntity=null;
}
}
}