(client)feat:实现子弹定义以及生成,实现初始化动画,实现血条 (#43)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -11,7 +11,7 @@ namespace AI
|
||||
public abstract JobBase GetJob(Entity.Entity target);
|
||||
}
|
||||
|
||||
public class Selector : AIBase
|
||||
public class ThinkNode_Selector : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
@ -24,13 +24,13 @@ namespace AI
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public class ConditionalAI : Selector
|
||||
public class ThinkNode_Conditional : ThinkNode_Selector
|
||||
{
|
||||
// 条件函数,返回 true 表示满足条件
|
||||
private Func<Entity.Entity, bool> condition;
|
||||
|
||||
// 构造函数,传入条件函数
|
||||
public ConditionalAI(Func<Entity.Entity, bool> conditionFunc)
|
||||
public ThinkNode_Conditional(Func<Entity.Entity, bool> conditionFunc)
|
||||
{
|
||||
condition = conditionFunc;
|
||||
}
|
||||
@ -48,7 +48,7 @@ namespace AI
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public class SequenceAI : AIBase
|
||||
public class ThinkNode_Sequence : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
@ -61,33 +61,5 @@ namespace AI
|
||||
return null; // 所有子节点完成时返回 null
|
||||
}
|
||||
}
|
||||
public class ContinuousMove : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new MoveJob();
|
||||
}
|
||||
}
|
||||
|
||||
public class TrackPlayer : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new TrackPlayerJob();
|
||||
}
|
||||
}
|
||||
public class RandomWander : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new WanderJob();
|
||||
}
|
||||
}
|
||||
public class Idel : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new IdleJob();
|
||||
}
|
||||
}
|
||||
}
|
@ -77,7 +77,7 @@ namespace AI
|
||||
public class TrackPlayerJob : JobBase
|
||||
{
|
||||
private EntityPrefab currentTarget; // 当前追踪的目标玩家
|
||||
private List<EntityPrefab> players; // 玩家实体列表
|
||||
private LinkedList<EntityPrefab> players; // 玩家实体列表
|
||||
|
||||
public override void StartJob(Entity.Entity target)
|
||||
{
|
||||
@ -87,13 +87,13 @@ namespace AI
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (currentTarget == null || currentTarget.entity.IsDead)
|
||||
if (!currentTarget || currentTarget.entity.IsDead)
|
||||
{
|
||||
// 如果当前目标无效,则重新查找最近的玩家
|
||||
UpdateTarget();
|
||||
}
|
||||
|
||||
if (currentTarget != null)
|
||||
if (currentTarget)
|
||||
{
|
||||
var targetPosition = new Vector3(currentTarget.Position.x, currentTarget.Position.y, 0);
|
||||
entity.SetTarget(targetPosition);
|
||||
@ -114,7 +114,7 @@ namespace AI
|
||||
currentTarget = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
@ -144,7 +144,7 @@ namespace AI
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (player == null || !IsPlayerInRange())
|
||||
if (!player || !IsPlayerInRange())
|
||||
{
|
||||
StopJob(); // 如果玩家不在范围内,停止攻击工作
|
||||
return;
|
||||
@ -167,11 +167,11 @@ namespace AI
|
||||
base.StartJob(target);
|
||||
|
||||
// 查找最近的玩家作为目标
|
||||
List<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
LinkedList<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
player = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
@ -197,7 +197,7 @@ namespace AI
|
||||
|
||||
private bool IsPlayerValid(EntityPrefab player)
|
||||
{
|
||||
return player != null && !player.entity.IsDead;
|
||||
return player && !player.entity.IsDead;
|
||||
}
|
||||
}
|
||||
public class RangedAttackJob : JobBase
|
||||
@ -206,7 +206,7 @@ namespace AI
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
if (player == null || !IsPlayerValid(player))
|
||||
if (!player || !IsPlayerValid(player))
|
||||
{
|
||||
StopJob(); // 如果当前目标无效,停止工作
|
||||
return;
|
||||
@ -256,11 +256,11 @@ namespace AI
|
||||
base.StartJob(target);
|
||||
|
||||
// 查找最近的玩家作为目标
|
||||
List<EntityPrefab> players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
var players = Managers.EntityManage.Instance.FindEntitiesByFaction("Player");
|
||||
player = GetNearestPlayer(players);
|
||||
}
|
||||
|
||||
private EntityPrefab GetNearestPlayer(List<EntityPrefab> players)
|
||||
private EntityPrefab GetNearestPlayer(LinkedList<EntityPrefab> players)
|
||||
{
|
||||
EntityPrefab nearestPlayer = null;
|
||||
float minDistance = float.MaxValue;
|
||||
@ -286,7 +286,7 @@ namespace AI
|
||||
|
||||
private bool IsPlayerValid(EntityPrefab player)
|
||||
{
|
||||
return player != null && !player.entity.IsDead;
|
||||
return player && !player.entity.IsDead;
|
||||
}
|
||||
|
||||
private const float MaxTrackDistance = 20f; // 最大追踪距离
|
||||
|
33
Client/Assets/Scripts/AI/JobGiver.cs
Normal file
33
Client/Assets/Scripts/AI/JobGiver.cs
Normal file
@ -0,0 +1,33 @@
|
||||
namespace AI
|
||||
{
|
||||
public class JobGiver_ContinuousMove : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new MoveJob();
|
||||
}
|
||||
}
|
||||
|
||||
public class JobGiver_Enemies : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new TrackPlayerJob();
|
||||
}
|
||||
}
|
||||
public class JobGiver_RandomWander : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new WanderJob();
|
||||
}
|
||||
}
|
||||
public class JobGiver_Idel : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return new IdleJob();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
3
Client/Assets/Scripts/AI/JobGiver.cs.meta
Normal file
3
Client/Assets/Scripts/AI/JobGiver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30648f750dce43e493f5e94cb735988c
|
||||
timeCreated: 1754974329
|
151
Client/Assets/Scripts/Base/GradientColor.cs
Normal file
151
Client/Assets/Scripts/Base/GradientColor.cs
Normal file
@ -0,0 +1,151 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
// 定义渐变方向枚举
|
||||
[AddComponentMenu("UI/Effects/Gradient")]
|
||||
public enum Dir
|
||||
{
|
||||
Horizontal, // 水平方向
|
||||
Vertical, // 垂直方向
|
||||
}
|
||||
|
||||
// 自定义梯度效果类,继承自BaseMeshEffect
|
||||
public class Gradient : BaseMeshEffect
|
||||
{
|
||||
[SerializeField] // 序列化字段,可在Inspector中编辑
|
||||
private Dir dir = Dir.Vertical; // 渐变方向,默认垂直
|
||||
|
||||
[SerializeField]
|
||||
public Color32 color1 = Color.white; // 渐变起始颜色,默认白色
|
||||
|
||||
[SerializeField]
|
||||
public Color32 color2 = Color.white; // 渐变结束颜色,默认白色
|
||||
|
||||
[SerializeField]
|
||||
private float range = 0f; // 渐变范围,控制颜色的过渡区域,默认无范围(完全渐变)
|
||||
|
||||
[SerializeField]
|
||||
private bool isFlip = false; // 是否翻转渐变方向,默认不翻转
|
||||
|
||||
|
||||
public void Refresh()
|
||||
{
|
||||
graphic.SetVerticesDirty();
|
||||
}
|
||||
|
||||
// 重写ModifyMesh方法,用于修改UI元素的网格
|
||||
public override void ModifyMesh(VertexHelper vh)
|
||||
{
|
||||
if (!IsActive()) // 如果组件未激活,则不执行后续操作
|
||||
{
|
||||
return;
|
||||
}
|
||||
var count = vh.currentVertCount; // 获取当前顶点数量
|
||||
if (count > 0) // 如果有顶点,则进行处理
|
||||
{
|
||||
var vertices = new List<UIVertex>(); // 创建顶点列表
|
||||
|
||||
// 遍历所有顶点并添加到列表中
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var uIVertex = new UIVertex();
|
||||
vh.PopulateUIVertex(ref uIVertex, i); // 填充顶点信息
|
||||
vertices.Add(uIVertex);
|
||||
}
|
||||
|
||||
// 根据渐变方向调用相应的绘制方法
|
||||
switch (dir)
|
||||
{
|
||||
case Dir.Horizontal:
|
||||
DrawHorizontal(vh, vertices, count);
|
||||
break;
|
||||
case Dir.Vertical:
|
||||
DrawVertical(vh, vertices, count);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 绘制垂直方向的渐变
|
||||
private void DrawVertical(VertexHelper vh, List<UIVertex> vertices, int count)
|
||||
{
|
||||
// 初始化顶部和底部Y坐标
|
||||
var topY = vertices[0].position.y;
|
||||
var bottomY = vertices[0].position.y;
|
||||
|
||||
// 遍历所有顶点,找到最高和最低的Y坐标
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var y = vertices[i].position.y;
|
||||
if (y > topY) topY = y;
|
||||
else if (y < bottomY) bottomY = y;
|
||||
}
|
||||
|
||||
var height = topY - bottomY; // 计算高度差
|
||||
|
||||
// 遍历所有顶点,设置颜色渐变
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var vertex = vertices[i];
|
||||
Color32 color = Color.white;
|
||||
|
||||
// 根据是否翻转,计算当前顶点的颜色
|
||||
if (isFlip)
|
||||
{
|
||||
color = Color32.Lerp(color2, color1, 1 - (vertex.position.y - bottomY) / height * (1f - range));
|
||||
}
|
||||
else
|
||||
{
|
||||
color = Color32.Lerp(color2, color1, (vertex.position.y - bottomY) / height * (1f - range));
|
||||
}
|
||||
|
||||
vertex.color = color; // 设置顶点颜色
|
||||
vh.SetUIVertex(vertex, i); // 更新网格中的顶点
|
||||
}
|
||||
}
|
||||
|
||||
// 绘制水平方向的渐变
|
||||
private void DrawHorizontal(VertexHelper vh, List<UIVertex> vertices, int count)
|
||||
{
|
||||
// 注意:这里应该是找到最左和最右的X坐标,注释中存在笔误
|
||||
var leftX = vertices[0].position.x;
|
||||
var rightX = vertices[0].position.x;
|
||||
|
||||
// 遍历所有顶点,找到最左和最右的X坐标
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var x = vertices[i].position.x;
|
||||
if (x > rightX) rightX = x;
|
||||
else if (x < leftX) leftX = x;
|
||||
}
|
||||
|
||||
var width = rightX - leftX; // 计算宽度差
|
||||
|
||||
// 遍历所有顶点,设置颜色渐变
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var vertex = vertices[i];
|
||||
Color32 color = Color.white;
|
||||
|
||||
// 根据是否翻转,计算当前顶点的颜色
|
||||
if (isFlip)
|
||||
{
|
||||
color = Color32.Lerp(color2, color1, 1 - (vertex.position.x - leftX) / width * (1f - range));
|
||||
}
|
||||
else
|
||||
{
|
||||
color = Color32.Lerp(color2, color1, (vertex.position.x - leftX) / width * (1f - range));
|
||||
}
|
||||
|
||||
vertex.color = color; // 设置顶点颜色
|
||||
vh.SetUIVertex(vertex, i); // 更新网格中的顶点
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Base/GradientColor.cs.meta
Normal file
2
Client/Assets/Scripts/Base/GradientColor.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8df3d16a358d74644b86e92ca5177fa1
|
150
Client/Assets/Scripts/Base/Launcher.cs
Normal file
150
Client/Assets/Scripts/Base/Launcher.cs
Normal file
@ -0,0 +1,150 @@
|
||||
using System.Collections;
|
||||
using Logging;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
|
||||
public class Launcher : MonoBehaviour
|
||||
{
|
||||
public GameObject loadingUI;
|
||||
public Gradient progressBar; // 渐变色条
|
||||
public TMP_Text describeText; // 描述文本
|
||||
|
||||
public float duration = 0.5f; // 过渡时间
|
||||
public float fadeDuration = 2f; // 不透明度渐隐的时间
|
||||
private float _currentProgress = 0f; // 当前进度
|
||||
|
||||
private Color textColor;
|
||||
|
||||
|
||||
private readonly string[] _loadingSteps =
|
||||
{
|
||||
"初始化日志", "正在载入定义", "正在加载图片资源", "正在切割瓦片", "正在加载区分派系",
|
||||
"正在加载物品"
|
||||
};
|
||||
|
||||
public float Progress
|
||||
{
|
||||
set
|
||||
{
|
||||
_currentProgress = value;
|
||||
if (value < 0.5f)
|
||||
{
|
||||
progressBar.color2 = Color.Lerp(Color.black, Color.white, value * 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
progressBar.color2 = Color.white;
|
||||
progressBar.color1 = Color.Lerp(Color.black, Color.white, (value - 0.5f) * 2);
|
||||
}
|
||||
|
||||
progressBar.Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
public float Opacity
|
||||
{
|
||||
set
|
||||
{
|
||||
var alpha = (byte)(value * 255);
|
||||
progressBar.color1.a = alpha;
|
||||
progressBar.color2.a = alpha;
|
||||
describeText.color = value > 0.5f ? Color.Lerp(new Color(1, 1, 1, 0), textColor, (value - 0.5f) * 2) : new Color(1, 1, 1, 0);
|
||||
progressBar.Refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Base.Setting.Instance.Init();
|
||||
#if !DEBUG
|
||||
duration = Base.Setting.Instance.progressStepDuration;
|
||||
fadeDuration = Base.Setting.Instance.exitAnimationDuration;
|
||||
#endif
|
||||
loadingUI.SetActive(true);
|
||||
textColor = describeText.color;
|
||||
StartCoroutine(LoadAllManagers());
|
||||
}
|
||||
|
||||
private IEnumerator LoadAllManagers()
|
||||
{
|
||||
for (var i = 0; i < _loadingSteps.Length; i++)
|
||||
{
|
||||
// 更新描述文本
|
||||
describeText.text = _loadingSteps[i];
|
||||
|
||||
// 获取当前阶段的目标进度
|
||||
var targetProgress = (float)(i + 1) / _loadingSteps.Length;
|
||||
|
||||
// 平滑过渡到下一个阶段
|
||||
yield return SmoothTransitionTo(targetProgress);
|
||||
|
||||
// 初始化对应的管理器
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
UnityLogger.Init();
|
||||
break;
|
||||
case 1:
|
||||
Managers.DefineManager.Instance.Init();
|
||||
break;
|
||||
case 2:
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
break;
|
||||
case 3:
|
||||
Managers.TileManager.Instance.Init();
|
||||
break;
|
||||
case 4:
|
||||
Managers.AffiliationManager.Instance.Init();
|
||||
break;
|
||||
case 5:
|
||||
Managers.ItemResourceManager.Instance.Init();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 加载完成后的处理
|
||||
describeText.text = "加载完成!";
|
||||
Progress = 1f;
|
||||
|
||||
// 开始渐隐效果
|
||||
yield return FadeOutProgressBar();
|
||||
}
|
||||
|
||||
private IEnumerator SmoothTransitionTo(float targetProgress)
|
||||
{
|
||||
var startProgress = _currentProgress;
|
||||
var elapsedTime = 0f;
|
||||
|
||||
while (elapsedTime < duration)
|
||||
{
|
||||
elapsedTime += Time.deltaTime;
|
||||
var t = Mathf.SmoothStep(0f, 1f, elapsedTime / duration); // 使用 SmoothStep 实现平滑过渡
|
||||
Progress = Mathf.Lerp(startProgress, targetProgress, t);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// 确保最终进度达到目标值
|
||||
Progress = targetProgress;
|
||||
}
|
||||
|
||||
private IEnumerator FadeOutProgressBar()
|
||||
{
|
||||
var elapsedTime = 0f;
|
||||
|
||||
while (elapsedTime < fadeDuration)
|
||||
{
|
||||
elapsedTime += Time.deltaTime;
|
||||
var t = Mathf.SmoothStep(0f, 1f, elapsedTime / fadeDuration); // 使用 SmoothStep 实现平滑渐隐
|
||||
Opacity = 1f - t; // 不透明度从 1 到 0
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// 确保最终不透明度为 0
|
||||
Opacity = 0f;
|
||||
loadingUI.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Base/Launcher.cs.meta
Normal file
3
Client/Assets/Scripts/Base/Launcher.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1966ff90f1854aee9ca89d700abef90d
|
||||
timeCreated: 1755438497
|
90
Client/Assets/Scripts/Base/Setting.cs
Normal file
90
Client/Assets/Scripts/Base/Setting.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
|
||||
|
||||
public class Setting : Utils.Singleton<Setting>
|
||||
{
|
||||
// 游戏设置数据类(用于序列化)
|
||||
[System.Serializable]
|
||||
public class GameSettings
|
||||
{
|
||||
public float progressStepDuration = 1f;
|
||||
public float exitAnimationDuration = 2f;
|
||||
public bool developerMode = false;
|
||||
public bool friendlyFire = false;
|
||||
public float globalVolume = 1.0f;
|
||||
public WindowMode currentWindowMode = WindowMode.Fullscreen;
|
||||
public Vector2Int windowResolution = new(1920, 1080);
|
||||
}
|
||||
|
||||
// 当前游戏设置
|
||||
public GameSettings CurrentSettings = new();
|
||||
|
||||
// 窗口模式枚举
|
||||
public enum WindowMode { Fullscreen, Windowed, Borderless }
|
||||
|
||||
// 常用分辨率选项
|
||||
public static readonly Vector2Int[] CommonResolutions = new Vector2Int[]
|
||||
{
|
||||
new(800, 600),
|
||||
new(1024, 768),
|
||||
new(1280, 720),
|
||||
new(1366, 768),
|
||||
new(1600, 900),
|
||||
new(1920, 1080),
|
||||
new(2560, 1440),
|
||||
new(3840, 2160)
|
||||
};
|
||||
|
||||
// 初始化加载设置
|
||||
public void Init()
|
||||
{
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
public void SaveSettings()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(CurrentSettings);
|
||||
PlayerPrefs.SetString("GameSettings", json);
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
public void LoadSettings()
|
||||
{
|
||||
if (PlayerPrefs.HasKey("GameSettings"))
|
||||
{
|
||||
string json = PlayerPrefs.GetString("GameSettings");
|
||||
CurrentSettings = JsonConvert.DeserializeObject<GameSettings>(json);
|
||||
}
|
||||
|
||||
// 应用加载的设置
|
||||
ApplyAudioSettings();
|
||||
ApplyWindowSettings();
|
||||
}
|
||||
|
||||
// 应用音频设置
|
||||
private void ApplyAudioSettings()
|
||||
{
|
||||
AudioListener.volume = Mathf.Clamp01(CurrentSettings.globalVolume);
|
||||
}
|
||||
|
||||
// 应用窗口设置
|
||||
private void ApplyWindowSettings()
|
||||
{
|
||||
switch (CurrentSettings.currentWindowMode)
|
||||
{
|
||||
case WindowMode.Fullscreen:
|
||||
Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.FullScreenWindow);
|
||||
break;
|
||||
case WindowMode.Windowed:
|
||||
Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.Windowed);
|
||||
break;
|
||||
case WindowMode.Borderless:
|
||||
Screen.SetResolution(CurrentSettings.windowResolution.x, CurrentSettings.windowResolution.y, FullScreenMode.MaximizedWindow);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Base/Setting.cs.meta
Normal file
3
Client/Assets/Scripts/Base/Setting.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9eb8b1554550478c85cc87558920ec6b
|
||||
timeCreated: 1755524879
|
@ -1,7 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public enum Relation
|
||||
{
|
||||
Neutral,
|
||||
Hostile,
|
||||
Friendly,
|
||||
}
|
||||
|
||||
public class AffiliationDef : Define
|
||||
{
|
||||
|
||||
public Relation defaultRelation = Relation.Neutral;
|
||||
public List<string> hostileFactions = new();
|
||||
public List<string> neutralFactions = new();
|
||||
public List<string> friendlyFactions = new();
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ namespace Data
|
||||
public class BehaviorTreeDef : Define
|
||||
{
|
||||
public BehaviorTreeDef[] childTree;
|
||||
public string className="Selector";
|
||||
public string className= "ThinkNode_Selector";
|
||||
public string value;
|
||||
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
namespace Data
|
||||
{
|
||||
public class BuildingDef:PawnDef
|
||||
public class BuildingDef : EntityDef
|
||||
{
|
||||
public bool IsBlocked = true;
|
||||
public float slowDown = 0f;
|
||||
}
|
||||
}
|
8
Client/Assets/Scripts/Data/BulletDef.cs
Normal file
8
Client/Assets/Scripts/Data/BulletDef.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Data
|
||||
{
|
||||
public class BulletDef:EntityDef
|
||||
{
|
||||
public string className;
|
||||
public string value;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Data/BulletDef.cs.meta
Normal file
3
Client/Assets/Scripts/Data/BulletDef.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f32fe058231409aaef631564bc51317
|
||||
timeCreated: 1755173999
|
@ -7,7 +7,7 @@ using UnityEngine;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class CharacterDef : PawnDef
|
||||
public class CharacterDef : EntityDef
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@ -279,10 +280,14 @@ namespace Data
|
||||
value = reference;
|
||||
}
|
||||
}
|
||||
else if(field.FieldType.IsArray)
|
||||
else if(field.FieldType.IsArray||typeof(IList).IsAssignableFrom(field.FieldType))
|
||||
{
|
||||
value = ProcessArrayField(field, element);
|
||||
}
|
||||
else if (field.FieldType.IsEnum)
|
||||
{
|
||||
value = Enum.Parse(field.FieldType, element.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ChangeType(element.Value, field.FieldType);
|
||||
@ -291,13 +296,14 @@ namespace Data
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogWarning($"Error setting field {field.Name}: {ex.Message}");
|
||||
Debug.LogWarning($"Error setting field ,field name:{field.Name}; value: {element.Value}; error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static object ProcessArrayField(FieldInfo field, XElement element)
|
||||
{
|
||||
Type elementType = field.FieldType.GetElementType();
|
||||
var elementType = field.FieldType.GetElementType();
|
||||
if (elementType == null) return null;
|
||||
|
||||
var arrayElements = new List<object>();
|
||||
@ -305,7 +311,7 @@ namespace Data
|
||||
{
|
||||
if (elementType.IsSubclassOf(typeof(Define)))
|
||||
{
|
||||
Define nestedDefine = (Define)Activator.CreateInstance(elementType);
|
||||
var nestedDefine = (Define)Activator.CreateInstance(elementType);
|
||||
DefaultInitDefine(nestedDefine, liElement, elementType);
|
||||
arrayElements.Add(nestedDefine);
|
||||
}
|
||||
@ -326,8 +332,8 @@ namespace Data
|
||||
}
|
||||
|
||||
// 构建结果数组
|
||||
Array resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
||||
for (int i = 0; i < arrayElements.Count; i++)
|
||||
var resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
||||
for (var i = 0; i < arrayElements.Count; i++)
|
||||
{
|
||||
resultArray.SetValue(arrayElements[i], i);
|
||||
}
|
||||
|
@ -26,37 +26,63 @@ namespace Data
|
||||
public DrawNodeDef drawingOrder_left;
|
||||
public DrawNodeDef drawingOrder_right;
|
||||
public string texturePath;
|
||||
public int pixelsPerUnit = 16;
|
||||
public float pixelsPerUnit = 16;
|
||||
|
||||
public DrawNodeDef GetDrawingOrder(Orientation orientation)
|
||||
public DrawNodeDef GetDrawingOrder(Orientation orientation, out Orientation sourceOrientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
DrawNodeDef result = null;
|
||||
|
||||
// 初始化 sourceOrientation 为默认值
|
||||
sourceOrientation = Orientation.Down;
|
||||
|
||||
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
|
||||
switch (orientation)
|
||||
{
|
||||
case Orientation.Down:
|
||||
result = drawingOrder_down;
|
||||
sourceOrientation = Orientation.Down;
|
||||
break;
|
||||
case Orientation.Up:
|
||||
result = drawingOrder_up;
|
||||
sourceOrientation = Orientation.Up;
|
||||
break;
|
||||
case Orientation.Left:
|
||||
result = drawingOrder_left;
|
||||
sourceOrientation = Orientation.Left;
|
||||
break;
|
||||
case Orientation.Right:
|
||||
result = drawingOrder_right;
|
||||
sourceOrientation = Orientation.Right;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid orientation value.");
|
||||
}
|
||||
|
||||
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
|
||||
if (result == null) result = drawingOrder_down;
|
||||
if (result == null)
|
||||
{
|
||||
result = drawingOrder_down;
|
||||
sourceOrientation = Orientation.Down; // 更新 sourceOrientation
|
||||
}
|
||||
|
||||
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
|
||||
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
|
||||
if (result != null) return result;
|
||||
if (drawingOrder_up != null)
|
||||
{
|
||||
result = drawingOrder_up;
|
||||
sourceOrientation = Orientation.Up; // 更新 sourceOrientation
|
||||
}
|
||||
else if (drawingOrder_left != null)
|
||||
{
|
||||
result = drawingOrder_left;
|
||||
sourceOrientation = Orientation.Left; // 更新 sourceOrientation
|
||||
}
|
||||
else if (drawingOrder_right != null)
|
||||
{
|
||||
result = drawingOrder_right;
|
||||
sourceOrientation = Orientation.Right; // 更新 sourceOrientation
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -5,13 +5,13 @@ using System.Xml.Linq;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class PawnDef : Define
|
||||
public class EntityDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public DrawingOrderDef drawingOrder;
|
||||
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
public string affiliation;
|
||||
public AffiliationDef affiliation;
|
||||
|
||||
|
||||
}
|
@ -1,13 +1,23 @@
|
||||
namespace Data
|
||||
{
|
||||
public enum ItemRarity
|
||||
{
|
||||
Common,
|
||||
Uncommon,
|
||||
Rare,
|
||||
Epic,
|
||||
Legendary
|
||||
}
|
||||
public class ItemDef:Define
|
||||
{
|
||||
public ImageDef texture;
|
||||
public AttributesDef attributes;
|
||||
public ItemRarity rarity = ItemRarity.Common;
|
||||
public int maxStack = 1; // 最大堆叠数量,默认为1
|
||||
public bool ssEquippable = false; // 是否可装备
|
||||
}
|
||||
|
||||
public class WeaponDef : ItemDef
|
||||
{
|
||||
|
||||
public AttributesDef attributes;
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Linq;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
@ -7,6 +8,7 @@ namespace Data
|
||||
{
|
||||
public ImageDef texture;
|
||||
public string name = "";
|
||||
public Tile.ColliderType collider = Tile.ColliderType.None;
|
||||
|
||||
public override bool Init(XElement xmlDef)
|
||||
{
|
||||
|
@ -1,6 +1,6 @@
|
||||
namespace Data
|
||||
{
|
||||
public class MonsterDef:PawnDef
|
||||
public class MonsterDef:EntityDef
|
||||
{
|
||||
|
||||
}
|
||||
|
40
Client/Assets/Scripts/Entity/Building.cs
Normal file
40
Client/Assets/Scripts/Entity/Building.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Building:Entity
|
||||
{
|
||||
public override void SetTarget(Vector3 pos)
|
||||
{
|
||||
}
|
||||
|
||||
public override void TryMove()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void UpdatePlayerControls()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
transform.position += Vector3.up;
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.A))
|
||||
{
|
||||
transform.position += Vector3.left;
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.S))
|
||||
{
|
||||
transform.position += Vector3.down;
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.D))
|
||||
{
|
||||
transform.position += Vector3.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/Building.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/Building.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2ea66d65f9414d18988cbdc1a322e334
|
||||
timeCreated: 1754650409
|
@ -1,10 +0,0 @@
|
||||
namespace Entity
|
||||
{
|
||||
public class BuildingBase:Entity
|
||||
{
|
||||
public override void TryMove()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eb881a08fe004eb4ab0a6fa9d5d86d33
|
||||
timeCreated: 1753100586
|
@ -1,12 +0,0 @@
|
||||
using Data;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class BuildingEntity:Entity
|
||||
{
|
||||
void Init(BuildingDef def)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 703125e2813a463d9b714841a3f9995f
|
||||
timeCreated: 1753702932
|
20
Client/Assets/Scripts/Entity/BuildingOutline.cs
Normal file
20
Client/Assets/Scripts/Entity/BuildingOutline.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class BuildingOutline:Outline
|
||||
{
|
||||
public BoxCollider2D boxCollider;
|
||||
override public void Init()
|
||||
{
|
||||
var size = GetSize();
|
||||
outlineRenderer.size = size;
|
||||
boxCollider.size = size;
|
||||
if (progressBarPrefab)
|
||||
{
|
||||
progressBarPrefab.transform.localPosition += new Vector3(0f, size.y * 2 / 3, 0f);
|
||||
progressBarPrefab.transform.localScale = new Vector3(size.x, 1f / 10f, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Entity/BuildingOutline.cs.meta
Normal file
2
Client/Assets/Scripts/Entity/BuildingOutline.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba93dffd1aec4bd5b1ef57df2597ba56
|
66
Client/Assets/Scripts/Entity/Bullet.cs
Normal file
66
Client/Assets/Scripts/Entity/Bullet.cs
Normal file
@ -0,0 +1,66 @@
|
||||
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);
|
||||
RotateTransformToDirection(transform, direction);
|
||||
}
|
||||
|
||||
protected override void AutoBehave()
|
||||
{
|
||||
TryMove();
|
||||
lifeTime-=Time.deltaTime;
|
||||
if (lifeTime <= 0)
|
||||
{
|
||||
Kill();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 确保目标方向不是零向量
|
||||
if (targetDirection == Vector3.zero)
|
||||
return;
|
||||
|
||||
// 计算当前向上方向与目标方向之间的角度
|
||||
float angle = Mathf.Atan2(targetDirection.y, targetDirection.x) * Mathf.Rad2Deg;
|
||||
|
||||
// 调整角度,因为默认贴图向上是0度,而Atan2计算的是相对于x轴的角度
|
||||
angle -= 90f;
|
||||
|
||||
// 应用旋转
|
||||
transform.rotation = Quaternion.Euler(0f, 0f, angle);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/Bullet.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/Bullet.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ccb9e42ac9e4492bff031709d2d2e92
|
||||
timeCreated: 1755146614
|
@ -10,10 +10,10 @@ namespace Entity
|
||||
public class Character : Entity
|
||||
{
|
||||
public CharacterDef characterDef;
|
||||
|
||||
|
||||
private void Start()
|
||||
{
|
||||
aiTree = new RandomWander();
|
||||
aiTree = new JobGiver_RandomWander();
|
||||
attributes = new AttributesDef();
|
||||
}
|
||||
|
||||
@ -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, this);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using AI;
|
||||
using Base;
|
||||
@ -7,23 +8,73 @@ using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
/// <summary>
|
||||
/// 表示游戏中的实体类,继承自 MonoBehaviour 并实现 ITick 接口。
|
||||
/// </summary>
|
||||
public class Entity : MonoBehaviour, ITick
|
||||
{
|
||||
/// <summary>
|
||||
/// 动画预制体,用于管理实体的动画逻辑。
|
||||
/// </summary>
|
||||
public SpriteAnimator animatorPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// 图像预制体,用于管理实体的静态图像显示。
|
||||
/// </summary>
|
||||
public ImagePrefab imagePrefab;
|
||||
|
||||
public ProgressBarPrefab healthBarPrefab;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 人工智能行为树,定义实体的行为逻辑。
|
||||
/// </summary>
|
||||
public AIBase aiTree;
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体正在执行的任务。
|
||||
/// </summary>
|
||||
public JobBase currentJob;
|
||||
|
||||
/// <summary>
|
||||
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
||||
/// </summary>
|
||||
public AttributesDef attributes = new();
|
||||
|
||||
/// <summary>
|
||||
/// 实体当前的移动方向。
|
||||
/// </summary>
|
||||
public Vector3 direction;
|
||||
|
||||
/// <summary>
|
||||
/// 实体的身体部分,用于挂载动画和图像节点。
|
||||
/// </summary>
|
||||
public GameObject body;
|
||||
|
||||
/// <summary>
|
||||
/// 实体所属的阵营或派系。
|
||||
/// </summary>
|
||||
public string affiliation;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否可以被选择。
|
||||
/// </summary>
|
||||
public bool canSelect = true;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否处于追逐状态(影响移动速度)。
|
||||
/// </summary>
|
||||
public bool IsChase { set; get; } = true;
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否由玩家控制。
|
||||
/// </summary>
|
||||
public bool PlayerControlled
|
||||
{
|
||||
set
|
||||
@ -37,26 +88,78 @@ namespace Entity
|
||||
}
|
||||
get => _isPlayerControlled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取实体当前位置。
|
||||
/// </summary>
|
||||
public Vector3 Position => transform.position;
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否已经死亡(生命值小于等于零)。
|
||||
/// </summary>
|
||||
public bool IsDead => attributes.health <= 0;
|
||||
|
||||
public bool IsShowingOfHitBarUI=>hitBarUIShowTimer > 0;
|
||||
public bool IsAttacking => attackCoroutine != null;
|
||||
|
||||
|
||||
private bool _isPlayerControlled = false;
|
||||
private bool _warning = false;
|
||||
|
||||
/// <summary>
|
||||
/// 存储不同朝向下的动画节点集合。
|
||||
/// </summary>
|
||||
public Dictionary<Orientation, List<ITick>> bodyAnimationNode = new();
|
||||
|
||||
/// <summary>
|
||||
/// 存储不同朝向下的身体节点对象。
|
||||
/// </summary>
|
||||
private Dictionary<Orientation, GameObject> bodyNodes = new();
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的朝向。
|
||||
/// </summary>
|
||||
private Orientation currentOrientation = Orientation.Down;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 攻击动画的持续时间(秒)。
|
||||
/// </summary>
|
||||
private const float attackAnimationDuration = 0.1f;
|
||||
|
||||
public virtual void Init(PawnDef pawnDef)
|
||||
/// <summary>
|
||||
/// 抖动的偏移量。
|
||||
/// </summary>
|
||||
private const float shakeOffset = 0.5f;
|
||||
// 协程引用
|
||||
private Coroutine attackCoroutine;
|
||||
|
||||
protected EntityDef entityDef;
|
||||
|
||||
|
||||
public float hitBarUIShowTime = 5;
|
||||
private float hitBarUIShowTimer = 0;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的基本属性和行为树。
|
||||
/// </summary>
|
||||
/// <param name="entityDef">实体的定义数据。</param>
|
||||
public virtual void Init(EntityDef entityDef)
|
||||
{
|
||||
attributes = pawnDef.attributes.Clone();
|
||||
aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
affiliation = pawnDef.affiliation;
|
||||
InitBody(pawnDef.drawingOrder);
|
||||
attributes = entityDef.attributes.Clone();
|
||||
aiTree = Utils.BehaviorTree.ConvertToAIBase(entityDef.behaviorTree);
|
||||
affiliation = entityDef.affiliation?.defName;
|
||||
InitBody(entityDef.drawingOrder);
|
||||
this.entityDef = entityDef;
|
||||
|
||||
HideHealthBar();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化实体的身体部分,包括不同朝向下的绘图节点。
|
||||
/// </summary>
|
||||
/// <param name="drawingOrder">绘制顺序定义。</param>
|
||||
public virtual void InitBody(DrawingOrderDef drawingOrder)
|
||||
{
|
||||
// 定义方向枚举和对应的 GetDrawingOrder 调用
|
||||
@ -67,12 +170,12 @@ namespace Entity
|
||||
currentOrientation = orientation;
|
||||
bodyAnimationNode[orientation] = new();
|
||||
// 获取当前方向的绘图节点
|
||||
var drawNode = drawingOrder.GetDrawingOrder(orientation);
|
||||
var drawNode = drawingOrder.GetDrawingOrder(orientation, out var realOrientation);
|
||||
|
||||
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,realOrientation);
|
||||
bodyNodes[orientation] = directionRoot;
|
||||
}
|
||||
currentOrientation = Orientation.Down;
|
||||
@ -84,8 +187,13 @@ namespace Entity
|
||||
SetOrientation(Orientation.Down);
|
||||
}
|
||||
|
||||
// 递归初始化单个绘图节点及其子节点
|
||||
public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent, string folderPath)
|
||||
/// <summary>
|
||||
/// 递归初始化单个绘图节点及其子节点。
|
||||
/// </summary>
|
||||
/// <param name="drawNode">绘图节点定义。</param>
|
||||
/// <param name="parent">父节点对象。</param>
|
||||
/// <param name="folderPath">纹理资源路径。</param>
|
||||
public virtual void InitBodyPart(DrawNodeDef drawNode, GameObject parent, string folderPath,Orientation realOrientation)
|
||||
{
|
||||
if (drawNode == null) return;
|
||||
|
||||
@ -103,12 +211,11 @@ namespace Entity
|
||||
nodeObject = Instantiate(imagePrefab.gameObject, parent.transform);
|
||||
var texture =
|
||||
Managers.PackagesImageManager.Instance.FindBodyTextures(drawNode.packID, folderPath,
|
||||
$"{drawNode.nodeName}_{currentOrientation}");
|
||||
if (texture.Length > 0)
|
||||
{
|
||||
var image = nodeObject.GetComponent<ImagePrefab>();
|
||||
image.SetSprite(texture[0]);
|
||||
}
|
||||
$"{drawNode.nodeName}_{realOrientation}");
|
||||
var image = nodeObject.GetComponent<ImagePrefab>();
|
||||
image.SetSprite(texture.Length > 0
|
||||
? texture[0]
|
||||
: Managers.PackagesImageManager.Instance.defaultSprite);
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
@ -118,7 +225,7 @@ namespace Entity
|
||||
bodyAnimationNode[currentOrientation].Add(tick);
|
||||
var textures = Managers.PackagesImageManager.Instance.FindBodyTextures(drawNode.packID,
|
||||
folderPath,
|
||||
$"{drawNode.nodeName}_{currentOrientation}");
|
||||
$"{drawNode.nodeName}_{realOrientation}");
|
||||
var animator = nodeObject.GetComponent<SpriteAnimator>();
|
||||
animator.SetSprites(textures);
|
||||
break;
|
||||
@ -131,9 +238,13 @@ namespace Entity
|
||||
// 递归初始化子节点
|
||||
foreach (var child in drawNode.children)
|
||||
{
|
||||
InitBodyPart(child, nodeObject, folderPath);
|
||||
InitBodyPart(child, nodeObject, folderPath,realOrientation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新实体的逻辑,包括玩家控制和自动行为。
|
||||
/// </summary>
|
||||
public void Tick()
|
||||
{
|
||||
if (_isPlayerControlled)
|
||||
@ -151,48 +262,120 @@ namespace Entity
|
||||
tick.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
if (IsShowingOfHitBarUI)
|
||||
{
|
||||
hitBarUIShowTimer -= Time.deltaTime;
|
||||
if (hitBarUIShowTimer <= 0)
|
||||
{
|
||||
HideHealthBar();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试攻击目标实体。
|
||||
/// </summary>
|
||||
public virtual void TryAttack()
|
||||
{
|
||||
|
||||
if(!IsAttacking)
|
||||
attackCoroutine = StartCoroutine(AttackFlow());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置实体的朝向。
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// 往对应朝向移动moveSpeed*deltaTime的距离
|
||||
/// 根据方向尝试移动实体。
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
if (IsAttacking)
|
||||
return;
|
||||
transform.position += direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理实体受到攻击的逻辑。
|
||||
/// </summary>
|
||||
/// <param name="from">攻击来源实体。</param>
|
||||
public virtual void OnHit(Entity from)
|
||||
{
|
||||
var hit = from.attributes.attack - attributes.defense;
|
||||
if (hit < 0)
|
||||
hit = from.attributes.attack / 100;
|
||||
attributes.health -= hit;
|
||||
|
||||
currentJob.StopJob();
|
||||
currentJob?.StopJob();
|
||||
ShowHealthBar();
|
||||
}
|
||||
|
||||
public virtual void SetTarget(Vector3 pos)
|
||||
public void ShowHealthBar()
|
||||
{
|
||||
direction = (pos - transform.position).normalized;
|
||||
if(!healthBarPrefab)
|
||||
return;
|
||||
healthBarPrefab.gameObject.SetActive(true);
|
||||
healthBarPrefab.Progress = (float)attributes.health / entityDef.attributes.health;
|
||||
hitBarUIShowTimer=hitBarUIShowTime;
|
||||
}
|
||||
|
||||
public void HideHealthBar()
|
||||
{
|
||||
if(!healthBarPrefab)
|
||||
return;
|
||||
healthBarPrefab.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 杀死实体,设置生命值为零。
|
||||
/// </summary>
|
||||
public virtual void Kill()
|
||||
{
|
||||
attributes.health = 0;
|
||||
}
|
||||
|
||||
private void AutoBehave()
|
||||
/// <summary>
|
||||
/// 设置实体的目标位置。
|
||||
/// </summary>
|
||||
/// <param name="pos">目标位置。</param>
|
||||
public virtual void SetTarget(Vector3 pos)
|
||||
{
|
||||
direction = (pos - transform.position).normalized;
|
||||
Orientation ori;
|
||||
// 判断方向向量最接近哪个朝向
|
||||
if (Mathf.Abs(direction.y) > Mathf.Abs(direction.x))
|
||||
{
|
||||
// 垂直方向优先
|
||||
ori = direction.y > 0 ? Orientation.Up : Orientation.Down;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 水平方向优先
|
||||
ori = direction.x > 0 ? Orientation.Right : Orientation.Left;
|
||||
}
|
||||
SetOrientation(ori);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自动行为逻辑,根据行为树执行任务。
|
||||
/// </summary>
|
||||
protected virtual void AutoBehave()
|
||||
{
|
||||
if (aiTree == null)
|
||||
return;
|
||||
@ -213,13 +396,17 @@ namespace Entity
|
||||
|
||||
currentJob.Update();
|
||||
}
|
||||
|
||||
|
||||
private void UpdatePlayerControls()
|
||||
/// <summary>
|
||||
/// 更新玩家控制的逻辑,处理输入和移动。
|
||||
/// </summary>
|
||||
protected virtual void UpdatePlayerControls()
|
||||
{
|
||||
// 检测 Shift 键状态
|
||||
var isHoldingShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
|
||||
IsChase = !isHoldingShift; // 按住 Shift 时 IsChase = false,否则 true
|
||||
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
||||
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
||||
var inputDirection = Vector2.zero;
|
||||
|
||||
// 检测 WASD 或方向键输入
|
||||
@ -239,7 +426,10 @@ namespace Entity
|
||||
{
|
||||
inputDirection += Vector2.right; // 向右移动(X 轴正方向)
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
TryAttack();
|
||||
}
|
||||
// 如果有输入方向,则设置目标位置并尝试移动
|
||||
if (inputDirection == Vector2.zero) return;
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
@ -253,61 +443,69 @@ namespace Entity
|
||||
|
||||
// 调用 TryMove 方法处理实际移动逻辑
|
||||
TryMove();
|
||||
|
||||
|
||||
}
|
||||
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
|
||||
// 攻击流程协程
|
||||
IEnumerator AttackFlow()
|
||||
{
|
||||
if (behaviorTreeDef == null)
|
||||
return null;
|
||||
var aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
|
||||
if (behaviorTreeDef.childTree != null)
|
||||
{
|
||||
foreach (var child in behaviorTreeDef.childTree)
|
||||
{
|
||||
if (child != null)
|
||||
{
|
||||
aiBase.children.Add(ConvertToAIBase(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return aiBase;
|
||||
// 播放攻击动画并获取动画持续时间
|
||||
var animationDuration = PlayAttackAnimation();
|
||||
// 等待动画执行完毕
|
||||
yield return new WaitForSeconds(animationDuration);
|
||||
// 调用检测并攻击敌人的方法
|
||||
DetectAndAttackEnemies();
|
||||
// 攻击流程结束,清理协程引用
|
||||
attackCoroutine = null;
|
||||
}
|
||||
// 使用反射根据 className 创建具体的 AIBase 子类实例
|
||||
private static AIBase CreateAIBaseInstance(string className)
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 播放攻击动画。
|
||||
/// </summary>
|
||||
/// <returns>开始检测攻击范围内敌人的时间。</returns>
|
||||
public float PlayAttackAnimation()
|
||||
{
|
||||
if (string.IsNullOrEmpty(className))
|
||||
throw new ArgumentException("className 不能为空");
|
||||
if (className.Equals("AIBase", StringComparison.OrdinalIgnoreCase))
|
||||
// 启动协程来执行攻击动画
|
||||
StartCoroutine(ShakeInDirectionCoroutine());
|
||||
|
||||
// 返回检测敌人的起始时间
|
||||
return attackAnimationDuration;
|
||||
}
|
||||
|
||||
private IEnumerator ShakeInDirectionCoroutine()
|
||||
{
|
||||
var originalPosition = transform.position; // 记录原始位置
|
||||
transform.position += direction * shakeOffset;
|
||||
yield return new WaitForSeconds(attackAnimationDuration);
|
||||
transform.position = originalPosition;
|
||||
}
|
||||
|
||||
public void DetectAndAttackEnemies()
|
||||
{
|
||||
var attackCount = attributes.attackTargetCount;
|
||||
// 获取攻击范围内的所有碰撞体
|
||||
var hits = Physics2D.OverlapCircleAll(
|
||||
transform.position,
|
||||
attributes.attackRange,
|
||||
LayerMask.GetMask("Entity"));
|
||||
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
return (AIBase)Activator.CreateInstance(typeof(AIBase));
|
||||
if (attackCount <= 0) break;
|
||||
|
||||
// 检查是否是自身(额外安全措施)
|
||||
if (hit.gameObject == this.gameObject) continue;
|
||||
|
||||
// 获取Entity组件
|
||||
var entity = hit.GetComponent<Entity>();
|
||||
if (!entity) continue;
|
||||
|
||||
// 执行攻击
|
||||
entity.OnHit(this);
|
||||
attackCount--;
|
||||
}
|
||||
// 定义可能的命名空间列表
|
||||
var possibleNamespaces = new[] { "AI" };
|
||||
|
||||
foreach (var ns in possibleNamespaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前程序集
|
||||
var assembly = typeof(AIBase).Assembly;
|
||||
|
||||
// 尝试查找类型
|
||||
var type = assembly.GetType($"{ns}.{className}");
|
||||
|
||||
if (type != null && typeof(AIBase).IsAssignableFrom(type))
|
||||
{
|
||||
// 如果找到合适的类型,则创建实例并返回
|
||||
return (AIBase)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略单个命名空间的错误,继续尝试下一个命名空间
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有命名空间都未找到对应的类型,抛出异常
|
||||
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
70
Client/Assets/Scripts/Entity/Inventory.cs
Normal file
70
Client/Assets/Scripts/Entity/Inventory.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using Item;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Inventory
|
||||
{
|
||||
public Entity from; // 物品所属实体
|
||||
public List<ItemBase> items = new List<ItemBase>(); // 背包中的物品列表
|
||||
|
||||
/// <summary>
|
||||
/// 添加物品到背包
|
||||
/// </summary>
|
||||
/// <param name="item">要添加的物品</param>
|
||||
/// <param name="count">添加的数量</param>
|
||||
public void AddItem(ItemResource resource, int count)
|
||||
{
|
||||
if (count <= 0) return; // 如果数量小于等于0,直接返回
|
||||
|
||||
// 检查背包中是否已存在相同物品
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.resource.Equals(resource))
|
||||
{
|
||||
item.count += count; // 增加数量
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到相同物品,则创建新物品并添加到背包
|
||||
var newItem = new ItemBase { resource = resource, count = count };
|
||||
items.Add(newItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从背包中取出物品
|
||||
/// </summary>
|
||||
/// <param name="itemName">物品名称</param>
|
||||
/// <param name="count">取出的数量</param>
|
||||
/// <returns>是否成功取出</returns>
|
||||
public bool RemoveItem(string itemName, int count)
|
||||
{
|
||||
if (count <= 0) return false; // 如果数量小于等于0,直接返回失败
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.resource.name == itemName)
|
||||
{
|
||||
if (item.count >= count)
|
||||
{
|
||||
item.count -= count; // 减少数量
|
||||
if (item.count == 0)
|
||||
{
|
||||
items.Remove(item); // 如果数量为0,则移除该物品
|
||||
}
|
||||
|
||||
return true; // 成功取出
|
||||
}
|
||||
else
|
||||
{
|
||||
return false; // 数量不足
|
||||
}
|
||||
}
|
||||
}
|
||||
return false; // 未找到物品
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/Inventory.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/Inventory.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3da96d312e8e4c65b9157548281f7826
|
||||
timeCreated: 1755061766
|
@ -1,12 +1,9 @@
|
||||
namespace Entity
|
||||
{
|
||||
public class Monster
|
||||
public class Monster:Entity
|
||||
{
|
||||
public Protocol.MonsterPack ToPack()
|
||||
{
|
||||
var pack= new Protocol.MonsterPack();
|
||||
return pack;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Managers;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
@ -7,9 +8,12 @@ namespace Entity
|
||||
{
|
||||
public class Outline : MonoBehaviour
|
||||
{
|
||||
public RightMenuPrefab rightMenuPrefab;
|
||||
|
||||
public GameObject body;
|
||||
public SpriteRenderer outlineRenderer;
|
||||
public CapsuleCollider2D outlineCollider;
|
||||
public ProgressBarPrefab progressBarPrefab;
|
||||
|
||||
public Entity entity;
|
||||
|
||||
@ -17,12 +21,17 @@ namespace Entity
|
||||
|
||||
public static Vector3 minimum=new(0.5f,0.5f,0.5f);
|
||||
|
||||
public void Init()
|
||||
public virtual void Init()
|
||||
{
|
||||
var size = GetSize();
|
||||
outlineRenderer.size = size;
|
||||
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
|
||||
outlineCollider.size = size;
|
||||
if (progressBarPrefab)
|
||||
{
|
||||
progressBarPrefab.transform.localPosition += new Vector3(0f, size.y * 2 / 3, 0f);
|
||||
progressBarPrefab.transform.localScale = new Vector3(size.x, 1f / 10f, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void Show()
|
||||
@ -76,24 +85,21 @@ namespace Entity
|
||||
private void OnMouseEnter()
|
||||
{
|
||||
Show();
|
||||
_select = true;
|
||||
}
|
||||
|
||||
private void OnMouseExit()
|
||||
{
|
||||
Hide();
|
||||
_select = false;
|
||||
}
|
||||
|
||||
private void OnMouseOver()
|
||||
{
|
||||
if (!entity.canSelect)
|
||||
return;
|
||||
// 检测是否按下的是鼠标右键
|
||||
if (Input.GetMouseButtonDown(1)) // 鼠标右键对应的是按钮索引 1
|
||||
{
|
||||
var rightMenu = Prefab.RightMenuPrefab.Instance;
|
||||
rightMenu.Init(GetMenu());
|
||||
rightMenu.transform.position = Input.mousePosition;
|
||||
rightMenu.Show();
|
||||
RightMenuManager.GenerateRightMenu(GetMenu(), Input.mousePosition);
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,6 +110,14 @@ 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;
|
||||
|
3
Client/Assets/Scripts/Item.meta
Normal file
3
Client/Assets/Scripts/Item.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89870f1a36ce43558b90c49513f55f10
|
||||
timeCreated: 1755061695
|
12
Client/Assets/Scripts/Item/ItemBase.cs
Normal file
12
Client/Assets/Scripts/Item/ItemBase.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Item
|
||||
{
|
||||
public class ItemBase
|
||||
{
|
||||
public ItemResource resource;
|
||||
|
||||
public int count=0;
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Item/ItemBase.cs.meta
Normal file
3
Client/Assets/Scripts/Item/ItemBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 01a2843d5856483fa5b6967e2e01db62
|
||||
timeCreated: 1755061705
|
12
Client/Assets/Scripts/Item/ItemResource.cs
Normal file
12
Client/Assets/Scripts/Item/ItemResource.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Item
|
||||
{
|
||||
public class ItemResource
|
||||
{
|
||||
public string name;
|
||||
public string description;
|
||||
|
||||
public Sprite icon;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Item/ItemResource.cs.meta
Normal file
3
Client/Assets/Scripts/Item/ItemResource.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e85c1b3671d1471e85a7ec96eadfabe2
|
||||
timeCreated: 1755061828
|
164
Client/Assets/Scripts/Managers/AffiliationManager.cs
Normal file
164
Client/Assets/Scripts/Managers/AffiliationManager.cs
Normal file
@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class AffiliationManager:Utils.Singleton<AffiliationManager>
|
||||
{
|
||||
//定义名,阵营定义
|
||||
private readonly Dictionary<string, AffiliationDef> _affiliations = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var affiliationList = Managers.DefineManager.Instance.QueryDefinesByType<AffiliationDef>();
|
||||
if (affiliationList == null ||affiliationList.Length==0)
|
||||
return;
|
||||
foreach (var affiliation in affiliationList)
|
||||
{
|
||||
_affiliations.Add(affiliation.defName, affiliation);
|
||||
}
|
||||
ValidateAndFixRelationships();
|
||||
}
|
||||
|
||||
public string GetAffiliationName(string defName)
|
||||
{
|
||||
return _affiliations[defName].defName;
|
||||
}
|
||||
public Relation GetRelation(AffiliationDef affiliation1, AffiliationDef affiliation2)
|
||||
{
|
||||
if (affiliation1 == null || affiliation2 == null)
|
||||
{
|
||||
return Relation.Neutral; // 如果任一阵营不存在,返回中立关系
|
||||
}
|
||||
return GetRelation(affiliation1.defName, affiliation2.defName);
|
||||
}
|
||||
public Relation GetRelation(string factionName1, string factionName2)
|
||||
{
|
||||
// 如果查询的是同一个派系,默认是友好关系
|
||||
if (factionName1 == factionName2)
|
||||
{
|
||||
return Relation.Friendly;
|
||||
}
|
||||
|
||||
// 尝试获取两个派系的定义
|
||||
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
|
||||
!_affiliations.TryGetValue(factionName2, out _))
|
||||
{
|
||||
if (faction1 != null) return faction1.defaultRelation;
|
||||
return Relation.Neutral;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为敌对
|
||||
if (faction1.hostileFactions != null && faction1.hostileFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Hostile;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为友好
|
||||
if (faction1.friendlyFactions != null && faction1.friendlyFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Friendly;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为中立
|
||||
if (faction1.neutralFactions != null && faction1.neutralFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Neutral;
|
||||
}
|
||||
|
||||
// 如果faction1没有明确设置与faction2的关系,则使用faction1的默认关系
|
||||
return faction1.defaultRelation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置两个阵营之间的关系
|
||||
/// </summary>
|
||||
/// <param name="factionName1">第一个阵营名称</param>
|
||||
/// <param name="factionName2">第二个阵营名称</param>
|
||||
/// <param name="relation">要设置的关系</param>
|
||||
public void SetRelation(string factionName1, string factionName2, Relation relation)
|
||||
{
|
||||
// 不能设置自己与自己的关系
|
||||
if (factionName1 == factionName2)
|
||||
{
|
||||
throw new ArgumentException("Cannot set relation between the same faction");
|
||||
}
|
||||
|
||||
// 确保两个阵营都存在
|
||||
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
|
||||
!_affiliations.TryGetValue(factionName2, out _))
|
||||
{
|
||||
throw new ArgumentException("One or both factions do not exist");
|
||||
}
|
||||
|
||||
// 确保关系列表已初始化
|
||||
faction1.hostileFactions ??= new List<string>();
|
||||
faction1.friendlyFactions ??= new List<string>();
|
||||
faction1.neutralFactions ??= new List<string>();
|
||||
|
||||
// 先移除所有现有关系
|
||||
faction1.hostileFactions.Remove(factionName2);
|
||||
faction1.friendlyFactions.Remove(factionName2);
|
||||
faction1.neutralFactions.Remove(factionName2);
|
||||
|
||||
// 添加新关系
|
||||
switch (relation)
|
||||
{
|
||||
case Relation.Hostile:
|
||||
faction1.hostileFactions.Add(factionName2);
|
||||
break;
|
||||
case Relation.Friendly:
|
||||
faction1.friendlyFactions.Add(factionName2);
|
||||
break;
|
||||
case Relation.Neutral:
|
||||
faction1.neutralFactions.Add(factionName2);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(relation), relation, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查并修复派系关系,确保没有冲突(按友好 > 敌对 > 中立 的优先级)
|
||||
/// </summary>
|
||||
private void ValidateAndFixRelationships()
|
||||
{
|
||||
foreach (var faction in _affiliations.Values)
|
||||
{
|
||||
// 确保所有关系列表已初始化
|
||||
faction.hostileFactions ??= new List<string>();
|
||||
faction.friendlyFactions ??= new List<string>();
|
||||
faction.neutralFactions ??= new List<string>();
|
||||
|
||||
// 检查所有敌对派系
|
||||
foreach (var hostileFaction in faction.hostileFactions.ToList())
|
||||
{
|
||||
// 如果敌对派系同时存在于友好列表中,移除敌对关系(友好优先)
|
||||
if (faction.friendlyFactions.Contains(hostileFaction))
|
||||
{
|
||||
faction.hostileFactions.Remove(hostileFaction);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果敌对派系同时存在于中立列表中,移除中立关系(敌对优先)
|
||||
if (faction.neutralFactions.Contains(hostileFaction))
|
||||
{
|
||||
faction.neutralFactions.Remove(hostileFaction);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查所有中立派系
|
||||
foreach (var neutralFaction in faction.neutralFactions.ToList())
|
||||
{
|
||||
// 如果中立派系同时存在于友好列表中,移除中立关系(友好优先)
|
||||
if (faction.friendlyFactions.Contains(neutralFaction))
|
||||
{
|
||||
faction.neutralFactions.Remove(neutralFaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31017ce35a6b441fb36e085dd3fd7c27
|
||||
timeCreated: 1755407963
|
@ -158,20 +158,20 @@ namespace Managers
|
||||
{
|
||||
if (defRef.Item1 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item1 为 null!");
|
||||
Debug.LogError("被引用定义为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defRef.Item2 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item2 为 null!");
|
||||
Debug.LogError("被引用定义的字段引用为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
var value = FindDefine(defRef.Item3.description, defRef.Item3.defName);
|
||||
if (value == null)
|
||||
{
|
||||
Debug.LogError($"FindDefine 返回 null: description={defRef.Item3.description}, defName={defRef.Item3.defName}");
|
||||
Debug.LogError($"未找到引用,出错的定义:定义类型:{defRef.Item1.GetType().Name}, 定义名:{defRef.Item1.defName} ; 类型:{defRef.Item3.description}, 定义名:{defRef.Item3.defName}");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -2,27 +2,36 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
using Entity;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class EntityManage:Utils.MonoSingleton<EntityManage>,ITick
|
||||
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
|
||||
{
|
||||
public Dictionary<string, List<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public EntityPrefab characterPrefab;
|
||||
public EntityPrefab buildingPrefab;
|
||||
public EntityPrefab bulletPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
|
||||
private Dictionary<string, Transform> layerCache = new Dictionary<string, Transform>();
|
||||
private List<Tuple<string, EntityPrefab>> pendingAdditions = new();
|
||||
|
||||
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
{
|
||||
if (factionEntities.TryGetValue(factionKey, out var entities))
|
||||
{
|
||||
return entities; // 如果找到,返回对应的实体列表
|
||||
}
|
||||
return new List<EntityPrefab>(); // 如果未找到,返回一个空列表
|
||||
|
||||
return new(); // 如果未找到,返回一个空列表
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var faction in factionEntities)
|
||||
@ -41,6 +50,7 @@ namespace Managers
|
||||
itike.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有标记为死亡的实体
|
||||
foreach (var entityToRemove in entitiesToRemove)
|
||||
{
|
||||
@ -48,102 +58,240 @@ namespace Managers
|
||||
Destroy(entityToRemove.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (pendingAdditions.Any())
|
||||
{
|
||||
foreach (var entity in pendingAdditions)
|
||||
{
|
||||
if (!factionEntities.ContainsKey(entity.Item1))
|
||||
{
|
||||
factionEntities[entity.Item1] = new LinkedList<EntityPrefab>();
|
||||
}
|
||||
|
||||
factionEntities[entity.Item1].AddLast(entity.Item2);
|
||||
}
|
||||
|
||||
pendingAdditions.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据给定的PawnDef生成一个实体对象。
|
||||
/// 根据给定的Def生成实体对象(内部通用方法)。
|
||||
/// </summary>
|
||||
/// <param name="pawnDef">定义实体属性的PawnDef对象。</param>
|
||||
/// <param name="pos">实体生成的位置。</param>
|
||||
/// <remarks>
|
||||
/// 1. 如果entityPrefab或pawnDef为null,则不会生成实体。
|
||||
/// 2. 实体将被创建在entityLevel.transform下。
|
||||
/// 3. 使用EntityPrefab组件初始化实体。
|
||||
/// </remarks>
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
/// <param name="prefab">要实例化的预制体</param>
|
||||
/// <param name="parent">生成的父级Transform</param>
|
||||
/// <param name="pos">生成位置</param>
|
||||
/// <param name="def">实体定义对象</param>
|
||||
/// <param name="extraInit">额外的初始化操作(如子弹方向设置)</param>
|
||||
/// <returns>成功时返回EntityPrefab组件,失败时返回null</returns>
|
||||
private EntityPrefab GenerateEntityInternal(
|
||||
GameObject prefab,
|
||||
Transform parent,
|
||||
Vector3 pos,
|
||||
Data.EntityDef def, // 所有Def类型需继承自BaseDef
|
||||
Action<EntityPrefab> extraInit = null)
|
||||
{
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查 pawnDef 是否为空
|
||||
if (pawnDef == null)
|
||||
{
|
||||
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
GameObject instantiatedEntity = null; // 用于跟踪已实例化的对象
|
||||
|
||||
GameObject instantiatedEntity = null;
|
||||
try
|
||||
{
|
||||
// 实例化实体对象
|
||||
instantiatedEntity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// 实例化实体
|
||||
instantiatedEntity = Instantiate(prefab, pos, Quaternion.identity, parent);
|
||||
|
||||
// 获取 EntityPrefab 组件
|
||||
// 获取并验证EntityPrefab组件
|
||||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查 EntityPrefab 组件是否存在
|
||||
if (entityComponent == null)
|
||||
if (!entityComponent)
|
||||
{
|
||||
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
|
||||
throw new InvalidOperationException(
|
||||
$"EntityPrefab component missing on: {instantiatedEntity.name}");
|
||||
}
|
||||
|
||||
// 初始化实体组件
|
||||
entityComponent.Init(pawnDef);
|
||||
// 初始化核心数据
|
||||
entityComponent.Init(def);
|
||||
|
||||
// 确保派系键存在,并初始化对应的列表
|
||||
var factionKey = pawnDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
// 执行类型特有的额外初始化
|
||||
extraInit?.Invoke(entityComponent);
|
||||
|
||||
// 管理派系列表
|
||||
var factionKey = def.attributes.defName ?? "default";
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
return entityComponent;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity != null)
|
||||
{
|
||||
Destroy(instantiatedEntity); // 删除已创建的对象
|
||||
}
|
||||
// 清理失败实例
|
||||
if (instantiatedEntity) Destroy(instantiatedEntity);
|
||||
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
|
||||
|
||||
// 调用默认生成方法
|
||||
GenerateDefaultEntity(pos);
|
||||
Debug.LogError($"Entity generation failed: {ex.Message}\n{ex.StackTrace}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态创建层(如果层不存在)
|
||||
/// </summary>
|
||||
private Transform EnsureLayerExists(string layerName)
|
||||
{
|
||||
// 先从缓存中查找
|
||||
if (layerCache.TryGetValue(layerName, out var layerTransform))
|
||||
{
|
||||
return layerTransform;
|
||||
}
|
||||
|
||||
// 如果缓存中没有,尝试通过 transform.Find 查找
|
||||
layerTransform = transform.Find(layerName);
|
||||
|
||||
if (!layerTransform)
|
||||
{
|
||||
// 如果层不存在,动态创建
|
||||
var layerObject = new GameObject(layerName);
|
||||
layerTransform = layerObject.transform;
|
||||
layerTransform.SetParent(transform, false); // 将层附加到当前管理器下
|
||||
}
|
||||
|
||||
// 将新创建的层加入缓存
|
||||
layerCache[layerName] = layerTransform;
|
||||
|
||||
return layerTransform;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据PawnDef生成普通实体
|
||||
/// </summary>
|
||||
public void GenerateEntity(Data.EntityDef entityDef, Vector3 pos)
|
||||
{
|
||||
// 验证关键参数
|
||||
if (!characterPrefab)
|
||||
{
|
||||
Debug.LogError("entityPrefab is null! Assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if (entityDef == null)
|
||||
{
|
||||
Debug.LogError("EntityDef is null! Cannot generate entity.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保层存在
|
||||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||||
|
||||
// 调用通用生成逻辑
|
||||
var result = GenerateEntityInternal(
|
||||
characterPrefab.gameObject,
|
||||
entityLevelTransform,
|
||||
pos,
|
||||
entityDef
|
||||
);
|
||||
|
||||
if (!result) GenerateDefaultEntity(pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成建筑实体(位置使用Vector3Int)
|
||||
/// </summary>
|
||||
public void GenerateBuildingEntity(Data.BuildingDef buildingDef, Vector3Int pos)
|
||||
{
|
||||
// 修正:检查正确的预制体 (buildingPrefab)
|
||||
if (!buildingPrefab)
|
||||
{
|
||||
Debug.LogError("buildingPrefab is null! Assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if (buildingDef == null)
|
||||
{
|
||||
Debug.LogError("BuildingDef is null! Cannot generate building.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
var worldPos = new Vector3(pos.x, pos.y, pos.z);
|
||||
|
||||
// 确保层存在
|
||||
var buildingLevelTransform = EnsureLayerExists("BuildingLevel");
|
||||
|
||||
var result = GenerateEntityInternal(
|
||||
buildingPrefab.gameObject,
|
||||
buildingLevelTransform,
|
||||
worldPos,
|
||||
buildingDef
|
||||
);
|
||||
|
||||
if (!result) GenerateDefaultEntity(worldPos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成子弹实体(含方向设置)
|
||||
/// </summary>
|
||||
public void GenerateBulletEntity(Data.BulletDef bulletDef, Vector3 pos, Vector3 dir,
|
||||
Entity.Entity source = null)
|
||||
{
|
||||
// 修正:检查正确的预制体 (bulletPrefab)
|
||||
if (!bulletPrefab)
|
||||
{
|
||||
Debug.LogError("bulletPrefab is null! Assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if (bulletDef == null)
|
||||
{
|
||||
Debug.LogError("BulletDef is null! Cannot generate bullet.");
|
||||
GenerateDefaultEntity(pos);
|
||||
return;
|
||||
}
|
||||
|
||||
// 确保层存在
|
||||
var bulletLevelTransform = EnsureLayerExists("BulletLevel");
|
||||
|
||||
var result = GenerateEntityInternal(
|
||||
bulletPrefab.gameObject,
|
||||
bulletLevelTransform,
|
||||
pos,
|
||||
bulletDef,
|
||||
// 子弹特有的方向设置
|
||||
entityComponent => entityComponent.entity.SetTarget(pos + dir)
|
||||
);
|
||||
if (result.entity is Bullet bullet)
|
||||
{
|
||||
bullet.bulletSource = source;
|
||||
if (source) bullet.affiliation = source.affiliation;
|
||||
}
|
||||
|
||||
if (!result) GenerateDefaultEntity(pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成默认实体(错误回退)
|
||||
/// </summary>
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(defaultEntityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// 确保层存在
|
||||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||||
|
||||
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevelTransform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
|
||||
const string factionKey = "default";
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
|
||||
entityComponent.DefaultInit();
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
factionEntities.Clear();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
||||
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
||||
|
||||
layerCache.Clear();
|
||||
}
|
||||
}
|
||||
}
|
47
Client/Assets/Scripts/Managers/ItemResourceManager.cs
Normal file
47
Client/Assets/Scripts/Managers/ItemResourceManager.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
using Item;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class ItemResourceManager:Utils.Singleton<ItemResourceManager>
|
||||
{
|
||||
//定义名,物品
|
||||
public Dictionary<string,Item.ItemResource> items;
|
||||
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var itemDefs = Managers.DefineManager.Instance.QueryDefinesByType<ItemDef>();
|
||||
if(itemDefs==null||itemDefs.Length==0)
|
||||
return;
|
||||
foreach (var itemDef in itemDefs)
|
||||
{
|
||||
var item=new Item.ItemResource();
|
||||
item.name = itemDef.label;
|
||||
item.description = itemDef.description;
|
||||
|
||||
item.icon = Managers.PackagesImageManager.Instance.GetSprite(itemDef.texture);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemResource GetItem(string defName)
|
||||
{
|
||||
return items.GetValueOrDefault(defName,null);
|
||||
}
|
||||
// <summary>
|
||||
/// 按物品名称查找物品
|
||||
/// </summary>
|
||||
/// <param name="itemName">要查找的物品名称</param>
|
||||
/// <returns>找到的物品对象,如果未找到则返回 null</returns>
|
||||
public ItemResource FindItemByName(string itemName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(itemName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return items.Values.FirstOrDefault(item => item.name == itemName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eafd50f3a0594a6e845ecf87b04744ce
|
||||
timeCreated: 1755062360
|
@ -90,16 +90,16 @@ namespace Managers
|
||||
}
|
||||
|
||||
// 判断是否为 Unity 资源路径
|
||||
bool isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
|
||||
string rootPath = packRootSite[drawOrder.packID];
|
||||
var isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
|
||||
var rootPath = packRootSite[drawOrder.packID];
|
||||
|
||||
if (isUnityResource)
|
||||
{
|
||||
// 移除 "res:" 前缀并适配 Unity 资源路径规则
|
||||
string resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
|
||||
var resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
|
||||
|
||||
// 加载文件夹下的所有纹理资源
|
||||
Texture2D[] textures = Resources.LoadAll<Texture2D>(resourceFolder);
|
||||
var textures = Resources.LoadAll<Texture2D>(resourceFolder);
|
||||
if (textures == null || textures.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"No textures found in Unity resource folder: {resourceFolder}");
|
||||
@ -124,7 +124,6 @@ namespace Managers
|
||||
new Vector2(0.5f, 0.5f), // 中心点
|
||||
drawOrder.pixelsPerUnit
|
||||
);
|
||||
|
||||
var name = image.name;
|
||||
|
||||
// 插入纹理
|
||||
@ -269,7 +268,11 @@ namespace Managers
|
||||
sprites.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
|
||||
public Sprite GetSprite(ImageDef ima)
|
||||
{
|
||||
return GetSprite(ima.packID,ima.name);
|
||||
}
|
||||
public Sprite GetSprite(string packID, string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(packID))
|
||||
@ -343,14 +346,14 @@ namespace Managers
|
||||
if (!bodyTexture.TryGetValue(packageName, out var packageDict))
|
||||
{
|
||||
Debug.LogWarning($"Package '{packageName}' not found.");
|
||||
return Array.Empty<Sprite>();
|
||||
return new[] { defaultSprite };
|
||||
}
|
||||
|
||||
// 检查文件路径是否存在
|
||||
if (!packageDict.TryGetValue(filePath, out var pathDict))
|
||||
{
|
||||
Debug.LogWarning($"File path '{filePath}' not found in package '{packageName}'.");
|
||||
return Array.Empty<Sprite>();
|
||||
return new[] { defaultSprite };
|
||||
}
|
||||
|
||||
// 收集所有匹配的Sprite
|
||||
@ -374,7 +377,7 @@ namespace Managers
|
||||
// 尝试解析编号
|
||||
if (int.TryParse(suffix, out var number))
|
||||
{
|
||||
sprites.Add((number, Value: value));
|
||||
sprites.Add((number, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
Client/Assets/Scripts/Managers/RightMenuManager.cs
Normal file
63
Client/Assets/Scripts/Managers/RightMenuManager.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class RightMenuManager:Utils.MonoSingleton<RightMenuManager>
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _canvas;
|
||||
|
||||
[SerializeField]
|
||||
private RightMenuPrefab _rightMenuPrefab;
|
||||
|
||||
public GameObject Canvas
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_canvas == null)
|
||||
{
|
||||
_canvas = GameObject.Find("Canvas"); // 根据你的实际场景修改查找条件
|
||||
if (_canvas == null)
|
||||
{
|
||||
Debug.LogError("RightMenu Canvas not found in scene!");
|
||||
}
|
||||
}
|
||||
return _canvas;
|
||||
}
|
||||
}
|
||||
|
||||
public RightMenuPrefab RightMenuPrefab
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_rightMenuPrefab == null)
|
||||
{
|
||||
_rightMenuPrefab = Resources.Load<RightMenuPrefab>("Prefab/RightMenu");
|
||||
if (_rightMenuPrefab == null)
|
||||
{
|
||||
Debug.LogError("RightMenuPrefab not found in Resources!");
|
||||
}
|
||||
}
|
||||
return _rightMenuPrefab;
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateRightMenu(List<(string name, UnityAction callback)> buttons,Vector3 position)
|
||||
{
|
||||
var rightMenuObj = Instantiate(RightMenuManager.Instance.RightMenuPrefab.gameObject,
|
||||
RightMenuManager.Instance.Canvas.transform);
|
||||
var rightMenu=rightMenuObj.GetComponent<RightMenuPrefab>();
|
||||
rightMenu.Init(buttons);
|
||||
rightMenu.transform.position = position;
|
||||
rightMenu.Show();
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/RightMenuManager.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/RightMenuManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 409b8017bbd6443eb2dde17ea6fd5e29
|
||||
timeCreated: 1755526878
|
@ -5,30 +5,57 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class TileManager:Utils.Singleton<TileManager>
|
||||
/// <summary>
|
||||
/// 瓦片管理器,用于加载、初始化和管理瓦片资源。
|
||||
/// </summary>
|
||||
public class TileManager : Utils.Singleton<TileManager>
|
||||
{
|
||||
public Dictionary<string,TileBase> tileBaseMapping = new();
|
||||
/// <summary>
|
||||
/// 存储瓦片名称与瓦片对象的映射关系。
|
||||
/// </summary>
|
||||
public Dictionary<string, TileBase> tileBaseMapping = new();
|
||||
|
||||
/// <summary>
|
||||
/// 存储瓦片索引与瓦片对象的映射关系。
|
||||
/// 索引由四个整数组成,表示瓦片的组合方式。
|
||||
/// </summary>
|
||||
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
|
||||
|
||||
/// <summary>
|
||||
/// 存储瓦片名称与唯一 ID 的映射关系。
|
||||
/// </summary>
|
||||
public Dictionary<string, int> tileID = new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化瓦片管理器。
|
||||
/// 加载所有瓦片定义、纹理映射表,并生成对应的瓦片对象。
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
if (tileToTileBaseMapping.Count > 0)
|
||||
return;
|
||||
|
||||
// 初始化图像包管理器
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
var imagePack = Managers.PackagesImageManager.Instance;
|
||||
|
||||
// 获取所有瓦片定义
|
||||
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
|
||||
for (var i = 0; i < tileType.Length; i++)
|
||||
{
|
||||
tileID.Add(tileType[i].name, i);
|
||||
}
|
||||
|
||||
var tileTextureMappingDef=Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
|
||||
// 处理瓦片纹理映射表定义
|
||||
var tileTextureMappingDef = Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
foreach (var mappingTableDef in tileTextureMappingDef)
|
||||
{
|
||||
foreach (var keyVal in mappingTableDef.tileDict)
|
||||
{
|
||||
var key = keyVal.Key;
|
||||
var val = keyVal.Value;
|
||||
|
||||
// 检查键值格式是否合法
|
||||
var parts = key.Split('_');
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
@ -37,6 +64,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查键值中是否存在未定义的瓦片名称
|
||||
if (!(tileID.TryGetValue(parts[0], out var k1) &&
|
||||
tileID.TryGetValue(parts[1], out var k2) &&
|
||||
tileID.TryGetValue(parts[2], out var k3) &&
|
||||
@ -47,7 +75,8 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID,val);
|
||||
// 获取对应精灵
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID, val);
|
||||
if (sprite == null)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
@ -55,6 +84,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查是否存在重复索引
|
||||
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
@ -62,6 +92,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 加载瓦片并存储到映射表中
|
||||
var tile = LoadTile(sprite);
|
||||
tileToTileBaseMapping[(k1, k2, k3, k4)] = tile;
|
||||
tileBaseMapping[val] = tile;
|
||||
@ -69,18 +100,28 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新加载瓦片管理器。
|
||||
/// 清空当前的瓦片映射表并重新初始化。
|
||||
/// </summary>
|
||||
public void Reload()
|
||||
{
|
||||
tileToTileBaseMapping.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
public TileBase LoadTile(Sprite sprite)
|
||||
/// <summary>
|
||||
/// 将精灵加载为瓦片对象。
|
||||
/// </summary>
|
||||
/// <param name="sprite">要加载的精灵。</param>
|
||||
/// <param name="colliderType">瓦片的碰撞体类型,默认为 None。</param>
|
||||
/// <returns>返回加载成功的瓦片对象。</returns>
|
||||
public TileBase LoadTile(Sprite sprite, Tile.ColliderType colliderType = Tile.ColliderType.None)
|
||||
{
|
||||
var newTile = ScriptableObject.CreateInstance<Tile>();
|
||||
newTile.sprite = sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = Tile.ColliderType.Sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = colliderType;
|
||||
return newTile;
|
||||
}
|
||||
}
|
||||
|
@ -9,103 +9,155 @@ namespace Map
|
||||
{
|
||||
public class DoubleMap : MonoBehaviour
|
||||
{
|
||||
public List<List<int>> mapData = new();
|
||||
public List<List<int>> mapData = new List<List<int>>();
|
||||
public Tilemap textureLevel;
|
||||
|
||||
public Dictionary<string, TileBase> tileDict = new();
|
||||
|
||||
private int offsetX = 0; // 地图数据的 X 偏移量
|
||||
private int offsetY = 0; // 地图数据的 Y 偏移量
|
||||
|
||||
void Start()
|
||||
public Vector2Int dataOffset = Vector2Int.zero; // 数据起始点偏移变量
|
||||
// 初始化地图数据大小
|
||||
public void InitializeData(int width, int height, int defaultValue = 0)
|
||||
{
|
||||
mapData.Clear();
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
List<int> column = new List<int>();
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
column.Add(defaultValue);
|
||||
}
|
||||
mapData.Add(column);
|
||||
}
|
||||
}
|
||||
// 设置指定数据坐标的瓦片值并刷新相关瓦片
|
||||
public void SetTile(int dataX, int dataY, int value)
|
||||
{
|
||||
// 检查坐标是否有效
|
||||
if (dataX < 0 || dataY < 0 || dataX >= mapData.Count || dataY >= mapData[0].Count)
|
||||
{
|
||||
Debug.LogError($"SetTile: 坐标({dataX},{dataY})超出范围");
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新数据
|
||||
mapData[dataX][dataY] = value;
|
||||
|
||||
UpdateTexture();
|
||||
// 刷新受影响的瓦片(当前点作为四个角影响的瓦片)
|
||||
RefreshTile(dataX, dataY);
|
||||
}
|
||||
|
||||
public void UpdateTexture()
|
||||
// 获取指定数据坐标的瓦片值
|
||||
public int GetTile(int dataX, int dataY)
|
||||
{
|
||||
for (int i = 0; i < mapData.Count; i++)
|
||||
if (dataX < 0 || dataY < 0 || dataX >= mapData.Count || dataY >= mapData[0].Count)
|
||||
{
|
||||
for (int j = 0; j < mapData[i].Count; j++)
|
||||
Debug.LogError($"GetTile: 坐标({dataX},{dataY})超出范围");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return mapData[dataX][dataY];
|
||||
}
|
||||
|
||||
// 刷新指定数据点影响的瓦片
|
||||
public void RefreshTile(int dataX, int dataY)
|
||||
{
|
||||
// 计算该数据点影响的四个瓦片位置(该点作为四个角)
|
||||
Vector2Int[] affectedTiles = new Vector2Int[]
|
||||
{
|
||||
new Vector2Int(dataX - 1, dataY - 1), // 作为右下角
|
||||
new Vector2Int(dataX - 1, dataY), // 作为右上角
|
||||
new Vector2Int(dataX, dataY - 1), // 作为左下角
|
||||
new Vector2Int(dataX, dataY) // 作为左上角
|
||||
};
|
||||
|
||||
foreach (var tilePos in affectedTiles)
|
||||
{
|
||||
UpdateTileAtTilemapPosition(tilePos.x, tilePos.y);
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新整个瓦片地图
|
||||
public void RefreshAllTiles()
|
||||
{
|
||||
if (mapData.Count == 0 || mapData[0].Count == 0) return;
|
||||
|
||||
// 计算瓦片地图的有效范围(考虑偏移)
|
||||
int startX = dataOffset.x;
|
||||
int startY = dataOffset.y;
|
||||
int endX = startX + mapData.Count - 1;
|
||||
int endY = startY + mapData[0].Count - 1;
|
||||
|
||||
// 遍历所有瓦片位置
|
||||
for (int x = startX; x <= endX; x++)
|
||||
{
|
||||
for (int y = startY; y <= endY; y++)
|
||||
{
|
||||
UpdateTexture(i, j);
|
||||
UpdateTileAtTilemapPosition(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int GetTile(int x, int y)
|
||||
// 更新指定瓦片位置的显示
|
||||
private void UpdateTileAtTilemapPosition(int tileX, int tileY)
|
||||
{
|
||||
// 转换为相对于 mapData 的索引
|
||||
int relativeX = x - offsetX;
|
||||
int relativeY = y - offsetY;
|
||||
// 计算对应的数据坐标(考虑偏移)
|
||||
int dataX = tileX - dataOffset.x;
|
||||
int dataY = tileY - dataOffset.y;
|
||||
|
||||
if (relativeX < 0 || relativeX >= mapData.Count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// 获取四个角的数据坐标
|
||||
int topLeftX = dataX;
|
||||
int topLeftY = dataY;
|
||||
int topRightX = dataX + 1;
|
||||
int topRightY = dataY;
|
||||
int bottomLeftX = dataX;
|
||||
int bottomLeftY = dataY + 1;
|
||||
int bottomRightX = dataX + 1;
|
||||
int bottomRightY = dataY + 1;
|
||||
|
||||
var col = mapData[relativeX];
|
||||
if (relativeY < 0 || relativeY >= mapData.Count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
// 检查边界并获取值
|
||||
int topLeft = GetDataValue(topLeftX, topLeftY);
|
||||
int topRight = GetDataValue(topRightX, topRightY);
|
||||
int bottomLeft = GetDataValue(bottomLeftX, bottomLeftY);
|
||||
int bottomRight = GetDataValue(bottomRightX, bottomRightY);
|
||||
|
||||
return col[relativeY];
|
||||
// 获取对应的瓦片
|
||||
TileBase tile = GetTileFromManager(topLeft, topRight, bottomLeft, bottomRight);
|
||||
|
||||
// 设置到瓦片地图
|
||||
Vector3Int position = new Vector3Int(tileX, tileY, 0);
|
||||
textureLevel.SetTile(position, tile);
|
||||
}
|
||||
|
||||
public void SetTile(int x, int y, string tileName)
|
||||
// 安全获取数据值(处理边界)
|
||||
private int GetDataValue(int dataX, int dataY)
|
||||
{
|
||||
SetTile(x, y, TileManager.Instance.tileID.GetValueOrDefault(tileName));
|
||||
if (dataX < 0 || dataY < 0 || dataX >= mapData.Count || dataY >= mapData[0].Count)
|
||||
return 0; // 边界外返回默认值
|
||||
return mapData[dataX][dataY];
|
||||
}
|
||||
|
||||
public void SetTile(int x, int y, int id)
|
||||
// 从TileManager获取对应瓦片
|
||||
private TileBase GetTileFromManager(int topLeft, int topRight, int bottomLeft, int bottomRight)
|
||||
{
|
||||
// 转换为相对于 mapData 的索引
|
||||
int relativeX = x - offsetX;
|
||||
int relativeY = y - offsetY;
|
||||
|
||||
if (relativeX >= 0 && relativeX < mapData.Count &&
|
||||
relativeY >= 0 && relativeY < mapData[relativeX].Count)
|
||||
TileManager manager = TileManager.Instance;
|
||||
if (manager == null)
|
||||
{
|
||||
mapData[relativeX][relativeY] = id;
|
||||
UpdateTexture(x, y);
|
||||
UpdateTexture(x, y - 1);
|
||||
UpdateTexture(x - 1, y);
|
||||
UpdateTexture(x - 1, y - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新对应坐标的贴图
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <returns></returns>
|
||||
public void UpdateTexture(int x, int y)
|
||||
{
|
||||
// 转换为相对于 mapData 的索引
|
||||
int relativeX = x - offsetX;
|
||||
int relativeY = y - offsetY;
|
||||
|
||||
if (relativeX < 0 || relativeX >= mapData.Count ||
|
||||
relativeY < 0 || relativeY >= mapData[relativeX].Count)
|
||||
{
|
||||
return; // 如果超出范围,直接返回
|
||||
Debug.LogError("TileManager实例未找到");
|
||||
return null;
|
||||
}
|
||||
|
||||
var lt = GetTile(x, y + 1);
|
||||
var rt = GetTile(x + 1, y + 1);
|
||||
var lb = GetTile(x, y);
|
||||
var rb = GetTile(x + 1, y);
|
||||
|
||||
if (TileManager.Instance.tileToTileBaseMapping.ContainsKey((lt, rt, lb, rb)))
|
||||
// 尝试获取组合键对应的瓦片
|
||||
var key = (topLeft, topRight, bottomLeft, bottomRight);
|
||||
if (manager.tileToTileBaseMapping.TryGetValue(key, out TileBase tile))
|
||||
{
|
||||
textureLevel.SetTile(new Vector3Int(x, y, 0),
|
||||
TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]);
|
||||
return tile;
|
||||
}
|
||||
|
||||
// 备用方案:尝试获取默认瓦片
|
||||
if (manager.tileBaseMapping.TryGetValue("Default", out TileBase defaultTile))
|
||||
{
|
||||
return defaultTile;
|
||||
}
|
||||
|
||||
Debug.LogError($"未找到对应瓦片: {key}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Map
|
||||
{
|
||||
public class MapGenerator:MonoBehaviour
|
||||
public class MapGenerator:Utils.MonoSingleton<MapGenerator>
|
||||
{
|
||||
public DoubleMap baseLevel;
|
||||
public Tilemap buildLevel;
|
||||
@ -14,17 +14,27 @@ namespace Map
|
||||
{
|
||||
var perline= Utils.PerlinNoise.Instance;
|
||||
int size = 100;
|
||||
for (int i = -size; i <= size; i++)
|
||||
{
|
||||
for (int j = -size; j <= size; j++)
|
||||
{
|
||||
var val = perline.Noise(i, j);
|
||||
if (val < 0)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
baseLevel.InitializeData(size, size);
|
||||
baseLevel.RefreshAllTiles();
|
||||
}
|
||||
|
||||
public bool CanPassThrough(int x, int y)
|
||||
{
|
||||
return GetTilePassCost(x, y) < 1;
|
||||
}
|
||||
public float GetTilePassCost(int x, int y)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public float GetTileBulletCover(int x, int y)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -26,9 +26,9 @@ namespace Prefab
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(Data.PawnDef pawnDef)
|
||||
public void Init(Data.EntityDef entityDef)
|
||||
{
|
||||
entity.Init(pawnDef);
|
||||
entity.Init(entityDef);
|
||||
|
||||
outline.Init();
|
||||
outline.Hide();
|
||||
|
18
Client/Assets/Scripts/Prefab/ProgressBarPrefab.cs
Normal file
18
Client/Assets/Scripts/Prefab/ProgressBarPrefab.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class ProgressBarPrefab:MonoBehaviour
|
||||
{
|
||||
public GameObject _progress;
|
||||
public float Progress
|
||||
{
|
||||
get => _progress.transform.localScale.x;
|
||||
set
|
||||
{
|
||||
var x=Mathf.Clamp01(value);
|
||||
_progress.transform.localScale = new Vector3(x, _progress.transform.localScale.y, _progress.transform.localScale.z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/ProgressBarPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/ProgressBarPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 605f185650fe46d89a6e0d60fb8fb11c
|
||||
timeCreated: 1755091112
|
@ -5,7 +5,7 @@ using UnityEngine.EventSystems;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class RightMenuPrefab: Utils.MonoSingleton<RightMenuPrefab>,IPointerExitHandler
|
||||
public class RightMenuPrefab: MonoBehaviour,IPointerExitHandler
|
||||
{
|
||||
public GameObject menu;
|
||||
public ButtonPrefab buttonPrefab;
|
||||
@ -17,7 +17,7 @@ namespace Prefab
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
gameObject.SetActive(false);
|
||||
Destroy(this.gameObject);
|
||||
}
|
||||
|
||||
public void Init(List<(string name, UnityAction callback)> buttons)
|
||||
@ -27,8 +27,6 @@ namespace Prefab
|
||||
Debug.LogError("Menu or ButtonPrefab is not assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
ClearMenu();
|
||||
foreach (var (label, callback) in buttons)
|
||||
{
|
||||
// 实例化按钮预制体
|
||||
@ -68,19 +66,6 @@ namespace Prefab
|
||||
}
|
||||
}
|
||||
}
|
||||
public void ClearMenu()
|
||||
{
|
||||
// 遍历菜单下的所有子对象并销毁它们
|
||||
foreach (Transform child in menu.transform)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
}
|
||||
protected override void OnStart()
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData eventData)
|
||||
{
|
||||
Hide();
|
||||
|
@ -8,6 +8,7 @@ public class Program : MonoBehaviour
|
||||
UnityLogger.Init();
|
||||
Managers.DefineManager.Instance.Init();
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
Managers.TileManager.Instance.Init();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
|
@ -25,6 +25,7 @@ namespace UI
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
InitMonster();
|
||||
InitBuilding();
|
||||
}
|
||||
|
||||
|
||||
@ -32,6 +33,10 @@ namespace UI
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "事件菜单";
|
||||
|
||||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "未定义任何事件";
|
||||
title.text.color = Color.red;
|
||||
// for (int i = 0; i < 30; i++)
|
||||
// {
|
||||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
@ -46,6 +51,36 @@ namespace UI
|
||||
title.Label = "生成人物";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
|
||||
if (defList == null || defList.Length == 0)
|
||||
{
|
||||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "未定义任何角色";
|
||||
title.text.color = Color.red;
|
||||
}
|
||||
else
|
||||
foreach (var def in defList)
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = def.label;
|
||||
var pawnDef = def;
|
||||
button.AddListener(() => GenerateEntityCallback(pawnDef));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void InitMonster()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "生成怪物";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
|
||||
if (defList == null || defList.Length == 0)
|
||||
{
|
||||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "未定义任何怪物";
|
||||
title.text.color = Color.red;
|
||||
}
|
||||
else
|
||||
foreach (var def in defList)
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
@ -55,19 +90,26 @@ namespace UI
|
||||
}
|
||||
}
|
||||
|
||||
private void InitMonster()
|
||||
private void InitBuilding()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "生成怪物";
|
||||
title.Label = "生成建筑";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
|
||||
foreach (var def in defList)
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.BuildingDef>();
|
||||
if (defList == null || defList.Length == 0)
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = def.label;
|
||||
var pawnDef = def;
|
||||
button.AddListener(() => GenerateEntityCallback(pawnDef));
|
||||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "未定义任何建筑";
|
||||
title.text.color = Color.red;
|
||||
}
|
||||
else
|
||||
foreach (var def in defList)
|
||||
{
|
||||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = def.label;
|
||||
var pawnDef = def;
|
||||
button.AddListener(() => GenerateBuildingCallback(pawnDef));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -99,17 +141,25 @@ namespace UI
|
||||
return instantiatedComponent;
|
||||
}
|
||||
|
||||
private void GenerateEntityCallback(PawnDef pawnDef)
|
||||
private void GenerateEntityCallback(EntityDef entityDef)
|
||||
{
|
||||
entityPlacementUI.currentAction = () =>
|
||||
{
|
||||
// 将鼠标屏幕坐标转换为世界坐标,并确保 Z 值为 0
|
||||
if (!Camera.main) return;
|
||||
var worldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
worldPosition.z = 0;
|
||||
Managers.EntityManage.Instance.GenerateEntity(pawnDef, worldPosition);
|
||||
Managers.EntityManage.Instance.GenerateEntity(entityDef, Utils.MousePosition.GetWorldPosition());
|
||||
};
|
||||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{pawnDef.label}\n描述:{pawnDef.description}";
|
||||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{entityDef.label}\n描述:{entityDef.description}";
|
||||
entityPlacementUI.snapEnabled = false;
|
||||
Base.UIInputControl.Instance.Show(entityPlacementUI);
|
||||
}
|
||||
|
||||
private void GenerateBuildingCallback(BuildingDef def)
|
||||
{
|
||||
entityPlacementUI.currentAction = () =>
|
||||
{
|
||||
Managers.EntityManage.Instance.GenerateBuildingEntity(def, Utils.MousePosition.GetSnappedWorldPosition());
|
||||
};
|
||||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{def.label}\n描述:{def.description}";
|
||||
entityPlacementUI.snapEnabled = true;
|
||||
Base.UIInputControl.Instance.Show(entityPlacementUI);
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,9 @@ namespace UI
|
||||
|
||||
public NonReturnCallback currentAction;
|
||||
|
||||
public GameObject focusBox;
|
||||
public bool snapEnabled = false;
|
||||
|
||||
public string Prompt
|
||||
{
|
||||
get => promptText.text;
|
||||
@ -30,7 +33,28 @@ namespace UI
|
||||
{
|
||||
currentAction();
|
||||
}
|
||||
|
||||
if (snapEnabled)
|
||||
{
|
||||
focusBox.transform.position = Utils.MousePosition.GetSnappedWorldPosition();
|
||||
}
|
||||
else
|
||||
{
|
||||
focusBox.transform.position = Utils.MousePosition.GetWorldPosition();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void Show()
|
||||
{
|
||||
base.Show();
|
||||
focusBox.SetActive(true);
|
||||
}
|
||||
|
||||
override public void Hide()
|
||||
{
|
||||
base.Hide();
|
||||
focusBox.SetActive(false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
75
Client/Assets/Scripts/Utils/BehaviorTree.cs
Normal file
75
Client/Assets/Scripts/Utils/BehaviorTree.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using AI;
|
||||
using Data;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
public static class BehaviorTree
|
||||
{
|
||||
/// <summary>
|
||||
/// 将行为树定义转换为 AIBase 类型。
|
||||
/// </summary>
|
||||
/// <param name="behaviorTreeDef">行为树定义。</param>
|
||||
/// <returns>转换后的 AIBase 实例。</returns>
|
||||
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
|
||||
{
|
||||
if (behaviorTreeDef == null)
|
||||
return null;
|
||||
var aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
|
||||
if (behaviorTreeDef.childTree != null)
|
||||
{
|
||||
foreach (var child in behaviorTreeDef.childTree)
|
||||
{
|
||||
if (child != null)
|
||||
{
|
||||
aiBase.children.Add(ConvertToAIBase(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return aiBase;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用反射根据类名创建 AIBase 的具体子类实例。
|
||||
/// </summary>
|
||||
/// <param name="className">类名。</param>
|
||||
/// <returns>创建的 AIBase 子类实例。</returns>
|
||||
private static AIBase CreateAIBaseInstance(string className)
|
||||
{
|
||||
if (string.IsNullOrEmpty(className))
|
||||
throw new ArgumentException("className 不能为空");
|
||||
if (className.Equals("AIBase", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return (AIBase)Activator.CreateInstance(typeof(AIBase));
|
||||
}
|
||||
// 定义可能的命名空间列表
|
||||
var possibleNamespaces = new[] { "AI" };
|
||||
|
||||
foreach (var ns in possibleNamespaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前程序集
|
||||
var assembly = typeof(AIBase).Assembly;
|
||||
|
||||
// 尝试查找类型
|
||||
var type = assembly.GetType($"{ns}.{className}");
|
||||
|
||||
if (type != null && typeof(AIBase).IsAssignableFrom(type))
|
||||
{
|
||||
// 如果找到合适的类型,则创建实例并返回
|
||||
return (AIBase)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略单个命名空间的错误,继续尝试下一个命名空间
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有命名空间都未找到对应的类型,抛出异常
|
||||
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Utils/BehaviorTree.cs.meta
Normal file
3
Client/Assets/Scripts/Utils/BehaviorTree.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6b711456a12f4bf6a29b0de14a2d7d8f
|
||||
timeCreated: 1754982046
|
53
Client/Assets/Scripts/Utils/MousePosition.cs
Normal file
53
Client/Assets/Scripts/Utils/MousePosition.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public static class MousePosition
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取鼠标的屏幕位置(以像素为单位)。
|
||||
/// </summary>
|
||||
/// <returns>鼠标的屏幕位置 (Vector2)。</returns>
|
||||
public static Vector2 GetScreenPosition()
|
||||
{
|
||||
return Input.mousePosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取鼠标的二维世界位置(基于主摄像机的屏幕到世界转换)。
|
||||
/// </summary>
|
||||
/// <param name="camera">用于计算的摄像机,默认为主摄像机。</param>
|
||||
/// <returns>鼠标的二维世界位置 (Vector2)。</returns>
|
||||
public static Vector2 GetWorldPosition(Camera camera = null)
|
||||
{
|
||||
// 如果未指定摄像机,则使用主摄像机
|
||||
if (!camera)
|
||||
{
|
||||
camera = Camera.main;
|
||||
}
|
||||
|
||||
// 获取鼠标屏幕位置
|
||||
var mouseScreenPosition = Input.mousePosition;
|
||||
|
||||
// 将屏幕坐标转换为世界坐标
|
||||
var worldPosition = camera.ScreenToWorldPoint(mouseScreenPosition);
|
||||
|
||||
// 返回二维坐标 (忽略 z 轴)
|
||||
return new Vector2(worldPosition.x, worldPosition.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取鼠标的二维世界位置并进行整数吸附(基于主摄像机的屏幕到世界转换)。
|
||||
/// </summary>
|
||||
/// <param name="camera">用于计算的摄像机,默认为主摄像机。</param>
|
||||
/// <returns>吸附后的二维世界位置 (Vector2Int)。</returns>
|
||||
public static Vector3Int GetSnappedWorldPosition(Camera camera = null)
|
||||
{
|
||||
// 获取世界位置
|
||||
var worldPosition = GetWorldPosition(camera);
|
||||
|
||||
// 对世界位置进行整数吸附
|
||||
return Vector3Int.RoundToInt(worldPosition);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Utils/MousePosition.cs.meta
Normal file
3
Client/Assets/Scripts/Utils/MousePosition.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b85631dc1524743aa492d8f9b4bb7e1
|
||||
timeCreated: 1754840873
|
183
Client/Assets/Scripts/Utils/Pathfinder.cs
Normal file
183
Client/Assets/Scripts/Utils/Pathfinder.cs
Normal file
@ -0,0 +1,183 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
public static class Pathfinder
|
||||
{
|
||||
public static List<Vector2> FindPath(Entity.Entity entity, Vector2 target, float maxDistance)
|
||||
{
|
||||
Vector2 start = entity.Position;
|
||||
|
||||
// 计算起点和终点所在的瓦片坐标
|
||||
var startTile = GetTileCoord(start);
|
||||
var endTile = GetTileCoord(target);
|
||||
|
||||
// 如果超出最大距离,直接返回直线路径或空路径
|
||||
if (Vector2.Distance(start, target) > maxDistance)
|
||||
{
|
||||
return new List<Vector2> { start, target };
|
||||
}
|
||||
|
||||
// A*算法数据结构
|
||||
var cameFrom = new Dictionary<Vector2Int, Vector2Int>();
|
||||
var gScore = new Dictionary<Vector2Int, float>();
|
||||
var fScore = new Dictionary<Vector2Int, float>();
|
||||
var openSet = new List<Vector2Int>();
|
||||
|
||||
// 初始化
|
||||
gScore[startTile] = 0;
|
||||
fScore[startTile] = Heuristic(startTile, endTile);
|
||||
openSet.Add(startTile);
|
||||
|
||||
var closestNode = startTile;
|
||||
var closestDist = Vector2.Distance(start, target);
|
||||
|
||||
while (openSet.Count > 0)
|
||||
{
|
||||
// 获取fScore最小的节点
|
||||
var current = openSet[0];
|
||||
foreach (var node in openSet)
|
||||
{
|
||||
if (fScore.GetValueOrDefault(node, float.MaxValue) <
|
||||
fScore.GetValueOrDefault(current, float.MaxValue))
|
||||
{
|
||||
current = node;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否到达目标
|
||||
if (current == endTile)
|
||||
{
|
||||
return ReconstructPath(cameFrom, current, start, target);
|
||||
}
|
||||
|
||||
openSet.Remove(current);
|
||||
|
||||
// 检查最大距离限制
|
||||
var currentDist = Vector2.Distance(
|
||||
new Vector2(current.x, current.y),
|
||||
target);
|
||||
|
||||
if (currentDist < closestDist)
|
||||
{
|
||||
closestDist = currentDist;
|
||||
closestNode = current;
|
||||
}
|
||||
|
||||
if (gScore[current] > maxDistance)
|
||||
{
|
||||
return ReconstructPath(cameFrom, closestNode, start, target);
|
||||
}
|
||||
|
||||
// 遍历邻居(8方向)
|
||||
for (var dx = -1; dx <= 1; dx++)
|
||||
{
|
||||
for (var dy = -1; dy <= 1; dy++)
|
||||
{
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
|
||||
var neighbor = new Vector2Int(current.x + dx, current.y + dy);
|
||||
|
||||
// 跳过不可通行区域
|
||||
if (!Map.MapGenerator.Instance.CanPassThrough(neighbor.x, neighbor.y))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算移动成本
|
||||
var moveCost = GetMovementCost(current, neighbor);
|
||||
var tentativeGScore = gScore[current] + moveCost;
|
||||
|
||||
// 跳过超出最大距离的路径
|
||||
if (tentativeGScore > maxDistance)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 发现新节点或找到更好路径
|
||||
if (tentativeGScore < gScore.GetValueOrDefault(neighbor, float.MaxValue))
|
||||
{
|
||||
cameFrom[neighbor] = current;
|
||||
gScore[neighbor] = tentativeGScore;
|
||||
fScore[neighbor] = tentativeGScore + Heuristic(neighbor, endTile);
|
||||
|
||||
if (!openSet.Contains(neighbor))
|
||||
{
|
||||
openSet.Add(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 无法找到完整路径时返回局部最优解
|
||||
return ReconstructPath(cameFrom, closestNode, start, target);
|
||||
}
|
||||
|
||||
// 获取瓦片坐标(每个瓦片覆盖±0.5范围)
|
||||
private static Vector2Int GetTileCoord(Vector2 position)
|
||||
{
|
||||
return new Vector2Int(
|
||||
Mathf.RoundToInt(position.x),
|
||||
Mathf.RoundToInt(position.y)
|
||||
);
|
||||
}
|
||||
|
||||
// 计算启发式估值(欧几里得距离)
|
||||
private static float Heuristic(Vector2Int a, Vector2Int b)
|
||||
{
|
||||
return Vector2.Distance(
|
||||
new Vector2(a.x, a.y),
|
||||
new Vector2(b.x, b.y)
|
||||
);
|
||||
}
|
||||
|
||||
// 获取移动成本
|
||||
private static float GetMovementCost(Vector2Int from, Vector2Int to)
|
||||
{
|
||||
// 计算基础距离(正交=1,对角=√2)
|
||||
var distance = (from.x == to.x || from.y == to.y) ? 1f : 1.4142f;
|
||||
|
||||
// 应用目标瓦片的速度削减率
|
||||
var costModifier = Map.MapGenerator.Instance.GetTilePassCost(to.x, to.y);
|
||||
|
||||
// 成本 = 距离 × (1 + 速度削减率)
|
||||
return distance * (1 + costModifier);
|
||||
}
|
||||
|
||||
// 重建路径
|
||||
private static List<Vector2> ReconstructPath(
|
||||
Dictionary<Vector2Int, Vector2Int> cameFrom,
|
||||
Vector2Int current,
|
||||
Vector2 start,
|
||||
Vector2 end)
|
||||
{
|
||||
// 构建瓦片路径
|
||||
var tilePath = new List<Vector2Int>();
|
||||
tilePath.Add(current);
|
||||
|
||||
while (cameFrom.ContainsKey(current))
|
||||
{
|
||||
current = cameFrom[current];
|
||||
tilePath.Add(current);
|
||||
}
|
||||
|
||||
tilePath.Reverse();
|
||||
|
||||
// 转换为实际坐标路径
|
||||
var path = new List<Vector2>();
|
||||
path.Add(start); // 添加精确起点
|
||||
|
||||
// 添加路径点(瓦片中心)
|
||||
foreach (var tile in tilePath)
|
||||
{
|
||||
path.Add(new Vector2(tile.x, tile.y));
|
||||
}
|
||||
|
||||
path.Add(end); // 添加精确终点
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Utils/Pathfinder.cs.meta
Normal file
3
Client/Assets/Scripts/Utils/Pathfinder.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 586ed74166c04dae862f096bdc52f63b
|
||||
timeCreated: 1754750432
|
Reference in New Issue
Block a user