Files
Cielonos/Assets/Scripts/SLSUtilities/MapCreator/MapGeneratorBase.cs
SoulliesOfficial 50ee502684 完善
2026-02-13 09:22:11 -05:00

88 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using System.Collections.Generic;
namespace SLSUtilities.Map
{
// 将类声明为 abstract表示它不能直接挂载使用必须被继承
public abstract class MapGeneratorBase : MonoBehaviour
{
[Header("基础配置")] public MapNodeData defaultNodeData;
// 存储生成的地图数据 (protected 使得子类可以访问)
[Header("生成结果 (Debug)")] [SerializeField] // 加上这个是为了在Inspector里能看到方便调试
protected List<MapNode> allNodes = new List<MapNode>();
[SerializeField] protected List<MapEdge> allEdges = new List<MapEdge>();
// 抽象方法:强制子类必须实现这个方法
public abstract void GenerateMap();
[ContextMenu("Clear Map")]
public virtual void ClearMap()
{
allNodes.Clear();
allEdges.Clear();
}
// 通用的清理与重置逻辑
protected void ResetIDs()
{
// 如果需要重新整理ID可以在这里写
}
// 通用的 Gizmos 绘制逻辑
protected virtual void OnDrawGizmos()
{
if (allNodes == null) return;
// 画边
Gizmos.color = Color.gray;
if (allEdges != null)
{
foreach (var edge in allEdges)
{
if (edge.fromNode != null && edge.toNode != null)
Gizmos.DrawLine(edge.fromNode.position, edge.toNode.position);
}
}
// 画节点
foreach (var node in allNodes)
{
Gizmos.color = node.data != null ? node.data.debugColor : Color.white;
Gizmos.DrawSphere(node.position, 0.5f);
}
}
}
[System.Serializable]
public class MapNode
{
public int id; // 唯一ID
public Vector2 position; // 在地图上的坐标
public MapNodeData data; // 引用上面的ScriptableObject数据
// 构造函数
public MapNode(int id, Vector2 pos, MapNodeData data)
{
this.id = id;
this.position = pos;
this.data = data;
}
}
[System.Serializable]
public class MapEdge
{
public MapNode fromNode;
public MapNode toNode;
public bool isBidirectional; // 是否双向
public MapEdge(MapNode from, MapNode to, bool bidirectional)
{
this.fromNode = from;
this.toNode = to;
this.isBidirectional = bidirectional;
}
}
}