(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-08-07 16:44:43 +08:00
committed by TheRedApricot
parent 82dc89c890
commit 670f778eee
143 changed files with 9706 additions and 8122 deletions

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using Base;
using Prefab;
using Unity.VisualScripting;
using UnityEngine;
@ -8,25 +10,21 @@ namespace AI
{
public Entity.Entity entity;
protected int timeoutTicks = 300;
public bool Running=>timeoutTicks > 0;
public virtual void StartJob(Entity.Entity target)
{
entity = target;
}
public bool Running => timeoutTicks > 0;
protected abstract void UpdateJob();
public virtual void StartJob(Entity.Entity target)
{
entity = target;
}
public bool Update()
{
if(!Running)
if (!Running)
return false;
UpdateJob();
timeoutTicks--;
if (timeoutTicks <= 0)
{
StopJob();
}
return true;
}
public virtual void StopJob()
@ -39,8 +37,8 @@ namespace AI
public override void StartJob(Entity.Entity target)
{
base.StartJob(target);
Vector3 move=new(Random.Range(-10,10), Random.Range(-10,10));
var targetPosition=entity.transform.position+move;
Vector3 move = new(Random.Range(-10, 10), Random.Range(-10, 10));
var targetPosition = entity.transform.position + move;
entity.SetTarget(targetPosition);
entity.IsChase = false;
}
@ -55,9 +53,9 @@ namespace AI
base.StopJob();
entity.IsChase = true;
}
}
public class IdleJob:JobBase
public class IdleJob : JobBase
{
override public void StartJob(Entity.Entity target)
{
@ -75,4 +73,223 @@ namespace AI
entity.TryMove();
}
}
public class TrackPlayerJob : JobBase
{
private EntityPrefab currentTarget; // 当前追踪的目标玩家
private List<EntityPrefab> players; // 玩家实体列表
public override void StartJob(Entity.Entity target)
{
base.StartJob(target);
UpdateTarget();
}
protected override void UpdateJob()
{
if (currentTarget == null || currentTarget.entity.IsDead)
{
// 如果当前目标无效,则重新查找最近的玩家
UpdateTarget();
}
if (currentTarget != null)
{
var targetPosition = new Vector3(currentTarget.Position.x, currentTarget.Position.y, 0);
entity.SetTarget(targetPosition);
entity.TryMove();
}
}
private void UpdateTarget()
{
players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
if (players == null || players.Count == 0)
{
currentTarget = null;
StopJob();
return;
}
currentTarget = GetNearestPlayer(players);
}
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
{
EntityPrefab nearestPlayer = null;
float minDistance = float.MaxValue;
foreach (var player in players)
{
if (player.entity.IsDead) continue; // 跳过无效玩家
float distance = Vector3.Distance(
new Vector3(player.Position.x, player.Position.y, 0),
new Vector3(entity.Position.x, entity.Position.y, 0)
);
if (distance < minDistance)
{
minDistance = distance;
nearestPlayer = player;
}
}
return nearestPlayer;
}
}
public class AttackPlayerJob : JobBase
{
private EntityPrefab player;
protected override void UpdateJob()
{
if (player == null || !IsPlayerInRange())
{
StopJob(); // 如果玩家不在范围内,停止攻击工作
return;
}
entity.TryAttack();
}
private bool IsPlayerInRange()
{
float distance = Vector3.Distance(
new Vector3(player.Position.x, player.Position.y, 0),
new Vector3(entity.Position.x, entity.Position.y, 0)
);
return distance <= entity.attributes.attackRange;
}
public override void StartJob(Entity.Entity target)
{
base.StartJob(target);
// 查找最近的玩家作为目标
List<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
player = GetNearestPlayer(players);
}
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
{
EntityPrefab nearestPlayer = null;
float minDistance = float.MaxValue;
foreach (var player in players)
{
if (!IsPlayerValid(player)) continue;
float distance = Vector3.Distance(
new Vector3(player.Position.x, player.Position.y, 0),
new Vector3(entity.Position.x, entity.Position.y, 0)
);
if (distance < minDistance)
{
minDistance = distance;
nearestPlayer = player;
}
}
return nearestPlayer;
}
private bool IsPlayerValid(EntityPrefab player)
{
return player != null && !player.entity.IsDead;
}
}
public class RangedAttackJob : JobBase
{
private EntityPrefab player;
protected override void UpdateJob()
{
if (player == null || !IsPlayerValid(player))
{
StopJob(); // 如果当前目标无效,停止工作
return;
}
float distance = Vector3.Distance(
new Vector3(player.Position.x, player.Position.y, 0),
new Vector3(entity.Position.x, entity.Position.y, 0)
);
if (distance <= entity.attributes.attackRange)
{
// 如果在攻击范围内,进行攻击
entity.TryAttack();
}
else if (distance > entity.attributes.attackRange && distance < MaxTrackDistance)
{
// 如果距离过远,靠近目标
MoveTowardsPlayer();
}
else if (distance < entity.attributes.attackRange)
{
// 如果距离过近,远离目标
MoveAwayFromPlayer();
}
}
private void MoveTowardsPlayer()
{
Vector3 targetPosition = new Vector3(player.Position.x, player.Position.y, 0);
entity.SetTarget(targetPosition);
entity.TryMove();
}
private void MoveAwayFromPlayer()
{
Vector3 direction = entity.Position - player.Position;
direction.Normalize();
Vector3 targetPosition = entity.Position + direction * RetreatDistance;
entity.SetTarget(targetPosition);
entity.TryMove();
}
public override void StartJob(Entity.Entity target)
{
base.StartJob(target);
// 查找最近的玩家作为目标
List<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
player = GetNearestPlayer(players);
}
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
{
EntityPrefab nearestPlayer = null;
float minDistance = float.MaxValue;
foreach (var player in players)
{
if (!IsPlayerValid(player)) continue;
float distance = Vector3.Distance(
new Vector3(player.Position.x, player.Position.y, 0),
new Vector3(entity.Position.x, entity.Position.y, 0)
);
if (distance < minDistance)
{
minDistance = distance;
nearestPlayer = player;
}
}
return nearestPlayer;
}
private bool IsPlayerValid(EntityPrefab player)
{
return player != null && !player.entity.IsDead;
}
private const float MaxTrackDistance = 20f; // 最大追踪距离
private const float RetreatDistance = 3f; // 后退距离
}
}