地图初步
This commit is contained in:
85
Assets/Scripts/MainGame/GameRun/Map/Zone/SpawnPoint.cs
Normal file
85
Assets/Scripts/MainGame/GameRun/Map/Zone/SpawnPoint.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using Sirenix.OdinInspector;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using Sirenix.Utilities.Editor;
|
||||
using UnityEditor;
|
||||
#endif
|
||||
|
||||
namespace Cielonos.MainGame.Map
|
||||
{
|
||||
public partial class SpawnPoint : MonoBehaviour
|
||||
{
|
||||
[HorizontalGroup("SpawnPointInfo", 0.5f)]
|
||||
[LabelText("Identification")]
|
||||
public string groupName = "Default";
|
||||
|
||||
[HorizontalGroup("SpawnPointInfo", 0.2f, MarginLeft = 0.05f), ReadOnly, HideLabel]
|
||||
[SerializeField] private int index = -1;
|
||||
public int Index => index;
|
||||
|
||||
[HorizontalGroup("SpawnPointInfo", 0.1f, MarginLeft = 0.05f), Button(Icon = SdfIconType.Command), HideLabel]
|
||||
public void Refresh()
|
||||
{
|
||||
ZoneManager.Instance.RebuildMapData();
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
Color color = Color.white;
|
||||
if (MapBaseCollection.Instance != null)
|
||||
{
|
||||
color = MapBaseCollection.Instance.GetGroupColor(groupName);
|
||||
}
|
||||
|
||||
Gizmos.color = color;
|
||||
Gizmos.DrawSphere(transform.position, 0.25f);
|
||||
|
||||
//Draw Direction
|
||||
Vector3 forward = transform.forward * 0.25f;
|
||||
Gizmos.DrawCube(transform.position + forward, new Vector3(0.1f, 0.1f, 0.25f));
|
||||
|
||||
#if UNITY_EDITOR
|
||||
// Draw text label using Handles - automatically faces camera
|
||||
string text = $"{groupName}_{index}";
|
||||
|
||||
// Create a custom style if needed, or use default skin
|
||||
GUIStyle style = new GUIStyle();
|
||||
style.normal.textColor = color;
|
||||
|
||||
float distance = Vector3.Distance(SceneView.lastActiveSceneView.camera.transform.position, transform.position);
|
||||
|
||||
if (distance > 100f)
|
||||
{
|
||||
return; // 太远了不显示
|
||||
}
|
||||
|
||||
float sizeFactor = Mathf.Clamp(100 / distance, 0.2f, 1f);
|
||||
style.fontSize = Mathf.RoundToInt(10 * sizeFactor);
|
||||
style.fontStyle = FontStyle.Bold;
|
||||
style.alignment = TextAnchor.MiddleCenter;
|
||||
Handles.Label(transform.position + Vector3.up * 0.5f, text, style);
|
||||
#endif
|
||||
}
|
||||
|
||||
// 被 Manager 调用
|
||||
public void SetData(int newIndex)
|
||||
{
|
||||
index = newIndex;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public partial class SpawnPoint
|
||||
{
|
||||
public void GetTransform(out Vector3 position, out Quaternion rotation)
|
||||
{
|
||||
position = transform.position;
|
||||
rotation = transform.rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc9f3701417662c4789e8d8ef866a7e5
|
||||
384
Assets/Scripts/MainGame/GameRun/Map/Zone/ZoneData.cs
Normal file
384
Assets/Scripts/MainGame/GameRun/Map/Zone/ZoneData.cs
Normal file
@@ -0,0 +1,384 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Sirenix.OdinInspector;
|
||||
using Sirenix.Utilities;
|
||||
using SLSUtilities.General;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using Sirenix.OdinInspector.Editor;
|
||||
using Sirenix.Utilities.Editor;
|
||||
#endif
|
||||
|
||||
namespace Cielonos.MainGame.Map
|
||||
{
|
||||
[CreateAssetMenu(fileName = "ZoneData", menuName = "Cielonos/MainGame/Map/ZoneData", order = 1)]
|
||||
public partial class ZoneData : SerializedScriptableObject
|
||||
{
|
||||
[InlineProperty]
|
||||
public struct SpawnPointKey : System.IEquatable<SpawnPointKey>
|
||||
{
|
||||
[HorizontalGroup("Row")] [LabelText("Spawn Point")] [LabelWidth(90)]
|
||||
public string group;
|
||||
|
||||
[HorizontalGroup("Row", Width = 50, MarginLeft = 10)] [HideLabel]
|
||||
public int index;
|
||||
|
||||
public SpawnPointKey(string group, int index)
|
||||
{
|
||||
this.group = group;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public bool Equals(SpawnPointKey other)
|
||||
{
|
||||
return group == other.group && index == other.index;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is SpawnPointKey other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return ((group != null ? group.GetHashCode() : 0) * 397) ^ index;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator ==(SpawnPointKey left, SpawnPointKey right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(SpawnPointKey left, SpawnPointKey right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
|
||||
public override string ToString() => $"{group}_{index}";
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
[InlineProperty]
|
||||
[HideReferenceObjectPicker]
|
||||
public class EnemySpawnMapping
|
||||
{
|
||||
[HideInInspector] [System.NonSerialized]
|
||||
public ZoneData parent;
|
||||
|
||||
[FormerlySerializedAs("Group")] [HorizontalGroup("SpawnInfo", 0.25f), HideLabel] [ReadOnly]
|
||||
public string group;
|
||||
|
||||
[FormerlySerializedAs("Index")] [HorizontalGroup("SpawnInfo", 0.15f), HideLabel] [ReadOnly]
|
||||
public int index;
|
||||
|
||||
[FormerlySerializedAs("EnemyID")]
|
||||
[HorizontalGroup("SpawnInfo", 0.5f), HideLabel]
|
||||
[ValueDropdown("GetEnemyKeys")]
|
||||
[OnValueChanged("OnEnemyIDChanged")]
|
||||
public string enemyID;
|
||||
|
||||
public EnemySpawnMapping()
|
||||
{
|
||||
}
|
||||
|
||||
public EnemySpawnMapping(SpawnPointKey key, string val, ZoneData parent)
|
||||
{
|
||||
group = key.group;
|
||||
index = key.index;
|
||||
enemyID = val;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnEnemyIDChanged()
|
||||
{
|
||||
if (parent != null)
|
||||
{
|
||||
parent.UpdateDictionaryEntry(group, index, enemyID);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
private IEnumerable<string> GetEnemyKeys()
|
||||
{
|
||||
return MainGameBaseCollection.Instance.enemiesCollection.Keys;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
private static void AddButtonOnTitleBarGUI(Action buttonAction, EditorIcon icon = null)
|
||||
{
|
||||
icon ??= EditorIcons.MagnifyingGlass;
|
||||
if (SirenixEditorGUI.ToolbarButton(icon))
|
||||
{
|
||||
buttonAction?.Invoke();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
[Title("Scene Info")]
|
||||
[ValueDropdown("GetAllSceneNames"), PropertyOrder(-1)]
|
||||
[InfoBox("当前场景和ZoneData的关联场景不匹配!", InfoMessageType.Error, VisibleIf = "@!IsCurrentSceneMatch")]
|
||||
public string sceneName;
|
||||
|
||||
public bool IsCurrentSceneMatch => UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == sceneName;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private List<string> GetAllSceneNames()
|
||||
{
|
||||
List<string> sceneNames = new List<string>();
|
||||
foreach (var scene in EditorBuildSettings.scenes)
|
||||
{
|
||||
if (scene.enabled)
|
||||
{
|
||||
string name = System.IO.Path.GetFileNameWithoutExtension(scene.path);
|
||||
sceneNames.Add(name);
|
||||
}
|
||||
}
|
||||
|
||||
return sceneNames;
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
private void CollectSpawnPoints(List<SpawnPointKey> list, string groupName)
|
||||
{
|
||||
ZoneManager zoneManager = ZoneManager.Instance;
|
||||
zoneManager.RebuildMapData();
|
||||
list.Clear();
|
||||
if (zoneManager.spawnPoints.TryGetValue(groupName, out List<SpawnPoint> points))
|
||||
{
|
||||
for (int i = 0; i < points.Count; i++)
|
||||
{
|
||||
list.Add(new SpawnPointKey(groupName, i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
[Title("Spawn Settings")]
|
||||
[Title("Player", HorizontalLine = false, Bold = false)]
|
||||
[ListDrawerSettings(ShowIndexLabels = false, AddCopiesLastElement = true), PropertyOrder(10), LabelText("Player Spawns")]
|
||||
public List<SpawnPointKey> playerSpawns = new List<SpawnPointKey>() { new SpawnPointKey("Player", 0) };
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
[SerializeField, HideInInspector] public Dictionary<SpawnPointKey, string> enemySpawns = new Dictionary<SpawnPointKey, string>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[HideInInspector] private List<string> selectedGroupsToSync = new List<string>();
|
||||
|
||||
[Title("Enemy", HorizontalLine = false)]
|
||||
[ShowInInspector]
|
||||
[ListDrawerSettings(
|
||||
OnTitleBarGUI = "DrawEnemySpawnTitleBarGUI",
|
||||
ShowFoldout = true,
|
||||
CustomRemoveIndexFunction = "RemoveEnemySpawnsIndex",
|
||||
CustomRemoveElementFunction = "RemoveEnemySpawnsElement")]
|
||||
[LabelText("Enemy Spawns"), PropertyOrder(12)]
|
||||
private List<EnemySpawnMapping> EnemySpawnsInEditor
|
||||
{
|
||||
get => GetSortedMappings();
|
||||
set
|
||||
{
|
||||
enemySpawns = new Dictionary<SpawnPointKey, string>();
|
||||
foreach (EnemySpawnMapping item in value)
|
||||
{
|
||||
enemySpawns[new SpawnPointKey(item.group, item.index)] = item.enemyID;
|
||||
}
|
||||
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnemySyncSettingsChanged()
|
||||
{
|
||||
ZoneManager zoneManager = ZoneManager.Instance;
|
||||
zoneManager.RebuildMapData();
|
||||
Dictionary<SpawnPointKey, string> newDict = new Dictionary<SpawnPointKey, string>();
|
||||
foreach (string group in selectedGroupsToSync)
|
||||
{
|
||||
if (zoneManager.spawnPoints.TryGetValue(group, out List<SpawnPoint> points))
|
||||
{
|
||||
for (int i = 0; i < points.Count; i++)
|
||||
{
|
||||
SpawnPointKey key = new SpawnPointKey(group, i);
|
||||
newDict[key] = enemySpawns != null ? enemySpawns.GetValueOrDefault(key, "") : "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enemySpawns = newDict;
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
|
||||
private List<EnemySpawnMapping> GetSortedMappings()
|
||||
{
|
||||
List<EnemySpawnMapping> list = new List<EnemySpawnMapping>();
|
||||
if (enemySpawns == null) return list;
|
||||
|
||||
list.AddRange(enemySpawns.Select(kvp => new EnemySpawnMapping(kvp.Key, kvp.Value, this)));
|
||||
list.Sort((a, b) =>
|
||||
{
|
||||
int comp = string.Compare(a.group, b.group, System.StringComparison.Ordinal);
|
||||
if (comp != 0) return comp;
|
||||
return a.index.CompareTo(b.index);
|
||||
});
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private void RemoveEnemySpawnsElement(EnemySpawnMapping element)
|
||||
{
|
||||
SpawnPointKey key = new SpawnPointKey(element.group, element.index);
|
||||
if (enemySpawns.Remove(key))
|
||||
{
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveEnemySpawnsIndex(int index)
|
||||
{
|
||||
var list = GetSortedMappings();
|
||||
if (index >= 0 && index < list.Count)
|
||||
{
|
||||
EnemySpawnMapping item = list[index];
|
||||
SpawnPointKey key = new SpawnPointKey(item.group, item.index);
|
||||
if (enemySpawns.Remove(key))
|
||||
{
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateDictionaryEntry(string group, int index, string newEnemyID)
|
||||
{
|
||||
SpawnPointKey key = new SpawnPointKey(group, index);
|
||||
if (enemySpawns.ContainsKey(key))
|
||||
{
|
||||
enemySpawns[key] = newEnemyID;
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawEnemySpawnTitleBarGUI() => AddButtonOnTitleBarGUI(OpenEnemySyncSelector);
|
||||
|
||||
private void OpenEnemySyncSelector()
|
||||
{
|
||||
ZoneManager zoneManager = ZoneManager.Instance;
|
||||
zoneManager.RebuildMapData();
|
||||
GroupSyncPopup popup = new GroupSyncPopup(zoneManager.spawnPoints.Keys, selectedGroupsToSync, (result) =>
|
||||
{
|
||||
selectedGroupsToSync = result;
|
||||
OnEnemySyncSettingsChanged();
|
||||
});
|
||||
|
||||
OdinEditorWindow window = OdinEditorWindow.InspectObject(popup);
|
||||
float width = 400;
|
||||
float height = 200;
|
||||
var centerRect = GUIHelper.GetEditorWindowRect().AlignCenter(width, height);
|
||||
window.position = centerRect;
|
||||
window.ShowPopup();
|
||||
}
|
||||
|
||||
private class GroupSyncPopup
|
||||
{
|
||||
[LabelText("Available Groups")]
|
||||
[ListDrawerSettings(IsReadOnly = true, ShowIndexLabels = false, HideAddButton = true, HideRemoveButton = true)]
|
||||
[TableList(AlwaysExpanded = true, HideToolbar = true)]
|
||||
[ShowInInspector, PropertyOrder(1)]
|
||||
public List<GroupItem> toggles = new List<GroupItem>();
|
||||
|
||||
private readonly System.Action<List<string>> onConfirm;
|
||||
|
||||
public GroupSyncPopup(IEnumerable<string> available, List<string> current, System.Action<List<string>> onConfirm)
|
||||
{
|
||||
this.onConfirm = onConfirm;
|
||||
foreach (string name in available)
|
||||
{
|
||||
toggles.Add(new GroupItem(name, current.Contains(name)));
|
||||
}
|
||||
}
|
||||
|
||||
[Button, PropertyOrder(2)]
|
||||
public void Confirm()
|
||||
{
|
||||
List<string> result = toggles.Where(t => t.sync).Select(t => t.name).ToList();
|
||||
this.onConfirm?.Invoke(result);
|
||||
GUIHelper.CurrentWindow?.Close();
|
||||
}
|
||||
|
||||
public class GroupItem
|
||||
{
|
||||
[HideLabel, TableColumnWidth(30, Resizable = false)]
|
||||
public bool sync;
|
||||
|
||||
[HideLabel, ReadOnly] public string name;
|
||||
|
||||
public GroupItem(string name, bool sync)
|
||||
{
|
||||
this.name = name;
|
||||
this.sync = sync;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
[Title("Specified Locations", HorizontalLine = false)]
|
||||
[PropertyOrder(20)]
|
||||
[LabelText("Exit Spawns")]
|
||||
[ListDrawerSettings(ShowIndexLabels = false, OnTitleBarGUI = "DrawExitSpawnsTitleBarGUI")]
|
||||
public List<SpawnPointKey> exitSpawns = new List<SpawnPointKey>() { new SpawnPointKey("Exit", 0) };
|
||||
#if UNITY_EDITOR
|
||||
private void DrawExitSpawnsTitleBarGUI() =>
|
||||
AddButtonOnTitleBarGUI(() => CollectSpawnPoints(exitSpawns, "Exit"), EditorIcons.Refresh);
|
||||
#endif
|
||||
|
||||
[PropertySpace]
|
||||
[PropertyOrder(21)]
|
||||
[LabelText("Tech Center Spawns")]
|
||||
[ListDrawerSettings(ShowIndexLabels = false, OnTitleBarGUI = "DrawTechCenterSpawnsTitleBarGUI")]
|
||||
public List<SpawnPointKey> techCenterSpawns = new List<SpawnPointKey>();
|
||||
#if UNITY_EDITOR
|
||||
private void DrawTechCenterSpawnsTitleBarGUI() =>
|
||||
AddButtonOnTitleBarGUI(() => CollectSpawnPoints(techCenterSpawns, "TechCenter"), EditorIcons.Refresh);
|
||||
#endif
|
||||
}
|
||||
|
||||
public partial class ZoneData
|
||||
{
|
||||
[Title("Random Locations", HorizontalLine = false)]
|
||||
[ListDrawerSettings(ShowIndexLabels = false, AddCopiesLastElement = true), PropertyOrder(50), LabelText("Random Location Spawns")]
|
||||
public List<SpawnPointKey> randomLocationSpawns = new List<SpawnPointKey>();
|
||||
|
||||
[ValueDropdown("GetLocationIDs"), PropertyOrder(51), LabelText("Available Random Locations")]
|
||||
public List<string> availableRandomLocations = new List<string>();
|
||||
|
||||
public List<string> GetLocationIDs()
|
||||
{
|
||||
return new List<string>() { "Factory", "Warehouse", "Office", "Laboratory", "Hangar", "Storage" }
|
||||
.Exclude(availableRandomLocations).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 228f5129b3e4fab4b9738801fd21d9e7
|
||||
84
Assets/Scripts/MainGame/GameRun/Map/Zone/ZoneManager.cs
Normal file
84
Assets/Scripts/MainGame/GameRun/Map/Zone/ZoneManager.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Sirenix.OdinInspector;
|
||||
using SLSUtilities.General;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cielonos.MainGame.Map
|
||||
{
|
||||
public partial class ZoneManager : Singleton<ZoneManager>
|
||||
{
|
||||
[Title("Editor Tools")]
|
||||
[Button("Rebuild Spawn Points"), PropertyOrder(-100)]
|
||||
public void RebuildMapData()
|
||||
{
|
||||
if(spawnPointContainer == null)
|
||||
{
|
||||
Debug.LogWarning("[ZoneManager] Spawn Point Container is not assigned.");
|
||||
return;
|
||||
}
|
||||
|
||||
spawnPoints.Clear();
|
||||
|
||||
// 1. 查找场景中所有生成点
|
||||
SpawnPoint[] allPoints = spawnPointContainer.GetComponentsInChildren<SpawnPoint>(true);
|
||||
|
||||
// 2. 按 groupName 分组,并按 Hierarchy 顺序排序 (保证 Index 确定性)
|
||||
var grouped = allPoints
|
||||
.GroupBy(p => p.groupName)
|
||||
.OrderBy(g => g.Key);
|
||||
|
||||
foreach (var group in grouped)
|
||||
{
|
||||
// 按在 Hierarchy 中的顺序排序列表
|
||||
var list = group.OrderBy(p => p.transform.GetSiblingIndex()).ToList();
|
||||
spawnPoints[group.Key] = list;
|
||||
|
||||
// 3. 将计算好的 Index 写入到具体物体上(持久化)
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
list[i].SetData(i); // 调用子物体的方法设置数据
|
||||
}
|
||||
}
|
||||
|
||||
//Debug.Log($"[ZoneManager] Rebuilt map: Found {allPoints.Length} points in {spawnPoints.Count} groups.");
|
||||
}
|
||||
}
|
||||
|
||||
// 继承 Singleton 保持运行时单例特性
|
||||
public partial class ZoneManager
|
||||
{
|
||||
// 运行时查找用的字典
|
||||
[Title("Runtime Data")]
|
||||
[Required]
|
||||
public GameObject spawnPointContainer;
|
||||
public Dictionary<string, List<SpawnPoint>> spawnPoints = new Dictionary<string, List<SpawnPoint>>();
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
RebuildMapData();
|
||||
}
|
||||
|
||||
public void SetupZone(ZoneData data)
|
||||
{
|
||||
if (data.enemySpawns != null)
|
||||
{
|
||||
foreach (KeyValuePair<ZoneData.SpawnPointKey, string> enemySpawn in data.enemySpawns)
|
||||
{
|
||||
string group = enemySpawn.Key.group;
|
||||
int index = enemySpawn.Key.index;
|
||||
|
||||
if (spawnPoints.TryGetValue(group, out List<SpawnPoint> points))
|
||||
{
|
||||
SpawnPoint point = points[index];
|
||||
point.GetTransform(out Vector3 position, out Quaternion rotation);
|
||||
GameObject enemyPrefab = MainGameBaseCollection.Instance.enemiesCollection[enemySpawn.Value];
|
||||
Instantiate(enemyPrefab, position, rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef86af874de4c0842925ab52256e256e
|
||||
Reference in New Issue
Block a user