(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

@ -0,0 +1,12 @@
using Data;
namespace Entity
{
public class BuildingEntity:Entity
{
void Init(BuildingDef def)
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 703125e2813a463d9b714841a3f9995f
timeCreated: 1753702932

View File

@ -9,19 +9,20 @@ using UnityEngine.Serialization;
namespace Entity
{
public class Entity:MonoBehaviour,ITick
public class Entity : MonoBehaviour, ITick
{
public SpriteAnimator animatorPrefab;
public ImagePrefab imagePrefab;
public AIBase aiTree;
public JobBase currentJob;
public AttributesDef attributes=new();
public AttributesDef attributes = new();
public Vector3 direction;
public GameObject body;
public string affiliation;
public bool canSelect = true;
public bool IsChase { set; get; } = true;
public bool PlayerControlled
{
@ -36,15 +37,16 @@ namespace Entity
}
get => _isPlayerControlled;
}
public Vector3 Position => transform.position;
public bool IsDead => attributes.health <= 0;
private bool _isPlayerControlled = false;
private bool _warning = false;
private Dictionary<Orientation,List<ITick>> bodyAnimationNode=new();
public Dictionary<Orientation, List<ITick>> bodyAnimationNode = new();
private Dictionary<Orientation, GameObject> bodyNodes = new();
private Orientation currentOrientation = Orientation.Down;
public virtual void Init(PawnDef pawnDef)
@ -70,7 +72,7 @@ namespace Entity
if (drawNode == null) continue;
var directionRoot = new GameObject(orientation.ToString());
directionRoot.transform.SetParent(body.transform, false);
InitBodyPart(drawNode, directionRoot,drawingOrder.texturePath);
InitBodyPart(drawNode, directionRoot, drawingOrder.texturePath);
bodyNodes[orientation] = directionRoot;
}
currentOrientation = Orientation.Down;
@ -83,9 +85,9 @@ namespace Entity
}
// 递归初始化单个绘图节点及其子节点
public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent,string folderPath)
public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent, string folderPath)
{
if(drawNode==null) return;
if (drawNode == null) return;
GameObject nodeObject;
if (drawNode.nodeName == "noName")
@ -102,8 +104,11 @@ namespace Entity
var texture =
Managers.PackagesImageManager.Instance.FindBodyTextures(drawNode.packID, folderPath,
$"{drawNode.nodeName}_{currentOrientation}");
var image = nodeObject.GetComponent<ImagePrefab>();
image.SetSprite(texture[0]);
if (texture.Length > 0)
{
var image = nodeObject.GetComponent<ImagePrefab>();
image.SetSprite(texture[0]);
}
break;
case DrawNodeType.Animation:
@ -126,7 +131,7 @@ namespace Entity
// 递归初始化子节点
foreach (var child in drawNode.children)
{
InitBodyPart(child, nodeObject,folderPath);
InitBodyPart(child, nodeObject, folderPath);
}
}
public void Tick()
@ -139,7 +144,6 @@ namespace Entity
{
AutoBehave();
}
if (bodyAnimationNode.TryGetValue(currentOrientation, out var ticks))
{
foreach (var tick in ticks)
@ -149,9 +153,9 @@ namespace Entity
}
}
public virtual void TryAttck()
public virtual void TryAttack()
{
}
public virtual void SetOrientation(Orientation orientation)
@ -174,7 +178,7 @@ namespace Entity
if (hit < 0)
hit = from.attributes.attack / 100;
attributes.health -= hit;
currentJob.StopJob();
}
@ -190,7 +194,7 @@ namespace Entity
private void AutoBehave()
{
if(aiTree == null)
if (aiTree == null)
return;
if (currentJob == null || !currentJob.Running)
{
@ -206,7 +210,7 @@ namespace Entity
}
currentJob.StartJob(this);
}
currentJob.Update();
}
@ -277,7 +281,7 @@ namespace Entity
return (AIBase)Activator.CreateInstance(typeof(AIBase));
}
// 定义可能的命名空间列表
var possibleNamespaces = new[] { "AI"};
var possibleNamespaces = new[] { "AI" };
foreach (var ns in possibleNamespaces)
{
@ -305,4 +309,5 @@ namespace Entity
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
}
}
}

View File

@ -9,14 +9,4 @@ namespace Entity
}
}
public class MonsterAttributes
{
public int health = 10;
public int moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
}

View File

@ -14,6 +14,8 @@ namespace Entity
public Entity entity;
private bool _select = false;
public static Vector3 minimum=new(0.5f,0.5f,0.5f);
public void Init()
{
@ -42,20 +44,32 @@ namespace Entity
/// </returns>
public Vector3 GetSize()
{
// 获取所有子对象的 Renderer 组件
var renderers = body.GetComponentsInChildren<Renderer>();
// 如果没有找到任何 Renderer返回一个默认值 (-1, -1, -1)
if (renderers.Length == 0)
{
return new(-1, -1);
return minimum;
}
// 初始化 totalBounds 为第一个 Renderer 的 bounds
var totalBounds = renderers[0].bounds;
// 遍历剩余的 Renderer将它们的 bounds 合并到 totalBounds 中
for (var i = 1; i < renderers.Length; i++)
{
totalBounds.Encapsulate(renderers[i].bounds);
}
// 获取合并后的包围盒的大小
var size = totalBounds.size;
// 确保每个维度的大小都不小于 0.5
size.x = Mathf.Max(size.x, 0.5f);
size.y = Mathf.Max(size.y, 0.5f);
size.z = Mathf.Max(size.z, 0.5f);
return size;
}
@ -78,7 +92,7 @@ namespace Entity
{
var rightMenu = Prefab.RightMenuPrefab.Instance;
rightMenu.Init(GetMenu());
rightMenu.transform.position=Input.mousePosition;
rightMenu.transform.position = Input.mousePosition;
rightMenu.Show();
}
}
@ -86,12 +100,31 @@ namespace Entity
private List<(string name, UnityAction callback)> GetMenu()
{
var result = new List<(string name, UnityAction callback)>();
if(entity.PlayerControlled)
result.Add(("结束操控",()=>entity.PlayerControlled=false));
if (entity.PlayerControlled)
result.Add(("结束操控", EndControl));
else
result.Add(("手动操控",()=>entity.PlayerControlled=true));
result.Add(("杀死",()=>entity.Kill()));
result.Add(("手动操控", StartControl));
result.Add(("杀死", () => entity.Kill()));
result.Add(("变成笨蛋", BecomeDefault));
return result;
}
private void BecomeDefault()
{
entity.Kill();
Managers.EntityManage.Instance.GenerateDefaultEntity(entity.Position);
}
private void StartControl()
{
entity.PlayerControlled = true;
CameraControl.CameraControl.Instance.focusedEntity=entity;
}
private void EndControl()
{
entity.PlayerControlled = false;
CameraControl.CameraControl.Instance.focusedEntity=null;
}
}
}