Forráskód Böngészése

关卡编辑器开始游戏后依然可以保留波次数据

WGL 2 hónapja
szülő
commit
506c58eb07

+ 11 - 5
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/Editor/GameMapEdPopup.cs

@@ -2,11 +2,11 @@
 using UnityEditor;
 using UnityEditorInternal;
 using System.Collections.Generic;
-using cfg;
 using System;
 using System.IO;
 using OfficeOpenXml;
 using SimpleJSON;
+using System.Linq;
 
 public class GameMapEdPopup : PopupWindowContent
 {
@@ -190,9 +190,9 @@ public class GameMapEdPopup : PopupWindowContent
 						break;
 				}
 
-				foreach (var item in layer.spawnTimes)
+				for(int j = 0;j< layer.spawnTimes.Count; j++)
 				{
-					SpawnTime spawnTime = item.Value;
+					SpawnTime spawnTime = layer.spawnTimes[j];
 					try
 					{
 						if (spawnTime.pos.Count == 0)
@@ -372,6 +372,12 @@ public class GameMapEdPopup : PopupWindowContent
                 }
             }
         }
+		for(int i = 0; i < gameMap.layers.Length; i++)
+        {
+			GameMapLayer layer = gameMap.layers[i];
+			layer.spawnTimes = layer.spawnTimes.OrderBy(x => x.id).ToList();
+        }
+		
         // 标记资源为脏数据,以便保存更改
         EditorUtility.SetDirty(GameMapEditor.asset);
         AssetDatabase.SaveAssets();
@@ -387,7 +393,7 @@ public class GameMapEdPopup : PopupWindowContent
             return;
         }
         GameMapTile tile = GameMapEditor.asset.tileAsset.tiles.Find(x => x.name == cfgData.EnemyName);
-        if (!layer.spawnTimes.ContainsKey(tile.id))
+        if (!layer.spawnTimes.Exists(x => x.id == tile.id))
         {
             SpawnTime spawnTime = new();
 
@@ -411,7 +417,7 @@ public class GameMapEdPopup : PopupWindowContent
             spawnTime.moveSpeed = cfgData.SpeedRatio;
             spawnTime.attack = cfgData.AttackRatio;
 
-            layer.spawnTimes.Add(tile.id, spawnTime);
+            layer.spawnTimes.Add(spawnTime);
         }
         else
         {

+ 51 - 60
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/Editor/GameMapEditor.cs

@@ -6,7 +6,7 @@ using UnityEditor;
 using cfg;
 using System.IO;
 using OfficeOpenXml;
-using SimpleJSON;
+using System.Linq;
 
 public class GameMapEditor : EditorWindow
 {
@@ -983,77 +983,72 @@ public class GameMapEditor : EditorWindow
 				buildingHp = EditorGUILayout.FloatField("剩余血量", layer.buildingHp);
 				break;
 		}
-
-
-		Dictionary<int, SpawnTime> newSpawnTimes = new();
+		List<SpawnTime> newSpawnTimes = new List<SpawnTime>();
+		for(int i = 0; i < asset.tileAsset.tiles.Count; i++)
+        {
+			SpawnTime spawnTime = new SpawnTime();
+			spawnTime.pos = new List<int>();
+			newSpawnTimes.Add(spawnTime);
+        }
 		EditorGUILayout.BeginVertical("box");
 		{
 			EditorGUILayout.LabelField("Spawn Times", EditorStyles.boldLabel);
 			GUILayout.Space(5f);
-			try
+			int[] grids = layer.grid; 
+			for (int i = 0; i < grids.Length; i++)
 			{
-				int[] grids = layer.grid;
-				for (int i = 0; i < grids.Length; i++)
+				if (grids[i] == -1)
 				{
-					if (grids[i] == -1)
-					{
-						continue;
-					}
-					SpawnTime spawnTime = new SpawnTime();
-					if (newSpawnTimes.ContainsKey(grids[i]))
-					{
-						spawnTime = newSpawnTimes[grids[i]];
-						spawnTime.pos.Add(i);
-					}
-					else
-					{
-						spawnTime.id = grids[i];
-						spawnTime.pos = new List<int> { i };
-						newSpawnTimes.Add(grids[i], spawnTime);
-					}
+					continue;
 				}
+				SpawnTime spawnTime = newSpawnTimes[grids[i]];
+				spawnTime.id = grids[i];
+				spawnTime.pos.Add(i);
+				newSpawnTimes[grids[i]] = spawnTime;
+			}
 
-				if (newSpawnTimes.Count >= 1)
-				{
-					Dictionary<int, SpawnTime> copyNewSpawnTime = new Dictionary<int, SpawnTime>(newSpawnTimes);
-					foreach (var item in copyNewSpawnTime)
-					{
-						SpawnTime newSpawnTime = item.Value;
-						EditorGUILayout.LabelField($"{asset.tileAsset.GetTile(newSpawnTime.id).name} - {newSpawnTime.pos.Count}", EditorStyles.boldLabel);
-						if (layer.spawnTimes.ContainsKey(item.Key))
-						{
-							newSpawnTime.startTime = EditorGUILayout.IntField("Start Time", layer.spawnTimes[item.Key].startTime);
-							newSpawnTime.endTime = EditorGUILayout.IntField("End Time", layer.spawnTimes[item.Key].endTime);
-							newSpawnTime.num = EditorGUILayout.IntField("Number", layer.spawnTimes[item.Key].num);
-						}
-						else
-						{
+			int spawnTimeIndex = 0;
+			for (int i = 0; i < newSpawnTimes.Count; i++)
+			{
+				SpawnTime newSpawnTime = newSpawnTimes[i];
+				if(newSpawnTime.pos.Count > 0)
+                {
+					EditorGUILayout.LabelField($"{asset.tileAsset.GetTile(newSpawnTime.id).name} - {newSpawnTime.pos.Count}", EditorStyles.boldLabel);
+					while(spawnTimeIndex < layer.spawnTimes.Count)
+                    {
+						if(layer.spawnTimes[spawnTimeIndex].id == newSpawnTime.id)
+                        {
+							newSpawnTime.startTime = EditorGUILayout.IntField("Start Time", layer.spawnTimes[spawnTimeIndex].startTime);
+							newSpawnTime.endTime = EditorGUILayout.IntField("End Time", layer.spawnTimes[spawnTimeIndex].endTime);
+							newSpawnTime.num = EditorGUILayout.IntField("Number", layer.spawnTimes[spawnTimeIndex].num);
+							spawnTimeIndex++;
+							break;
+                        }
+						else if(layer.spawnTimes[spawnTimeIndex].id < newSpawnTime.id)
+                        {
+							spawnTimeIndex++;
+                        }
+                        else
+                        {
 							newSpawnTime.startTime = EditorGUILayout.IntField("Start Time", 0);
 							newSpawnTime.endTime = EditorGUILayout.IntField("End Time", 0);
-							newSpawnTime.num = EditorGUILayout.IntField("Number", 0);
+							newSpawnTime.num = EditorGUILayout.IntField("Number", 1);
+							spawnTimeIndex++;
+							break;
 						}
-
-						if (GUILayout.Button("Remove"))
+                    }
+					if (GUILayout.Button("Remove"))
+					{
+						for (int j = 0; j < newSpawnTime.pos.Count; j++)
 						{
-							for (int j = 0; j < newSpawnTime.pos.Count; j++)
-							{
-								layer.grid[newSpawnTime.pos[j]] = -1;
-							}
+							layer.grid[newSpawnTime.pos[j]] = -1;
 						}
-						newSpawnTimes[item.Key] = newSpawnTime;
 					}
 				}
-			}
-			catch (Exception e)
-            {
-				Debug.LogError(e);
-				newSpawnTimes = layer.spawnTimes;
-			}
-
-        }
-		EditorGUILayout.EndVertical();
 
+			}
 
+		}
 		if (EditorGUI.EndChangeCheck() || haveChangeGrid)
 		{
 			Undo.RecordObject(asset, "Change Layer Information");
@@ -1066,11 +1061,7 @@ public class GameMapEditor : EditorWindow
 			layer.waveType = waveType;
 			AssetDatabase.SaveAssets();
 		}
-		// 标记资源为脏数据,以便保存更改
-		// 显示其他图层信息(根据需求扩展)
-		//EditorGUILayout.LabelField("Other Properties:");
-		//EditorGUILayout.LabelField($"Grid Size: {layer.gridSize}");
-		//EditorGUILayout.LabelField($"Is Visible: {layer.isVisible}");
+		EditorGUILayout.EndVertical();
 	}
 	private void DrawTileProperties(Event ev)
 	{

+ 1 - 30
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/GameMap.cs

@@ -1,10 +1,5 @@
 using UnityEngine;
 
-
-/// <summary>
-/// A map/grid of tiles. The GameMapsAsset contains a list of these maps. 
-/// The map's 0x0 point is considered to be at the bottom-left and WxH at top-right
-/// </summary>
 [System.Serializable]
 public class GameMap
 {
@@ -14,19 +9,11 @@ public class GameMap
 	/// <summary> a unique name by which map can be identified </summary>
 	public string ident;
 	[HideInInspector] public string showIdent;
-	/// <summary> width of the map (determines how big grid is) </summary>
 	public int width;
-
-	/// <summary> height of map (determines how big grid is) </summary>
 	public int height;
 	public int levelId;
 	public float expRatio;
-	/// <summary> The grid/map. -1 is an empty tile, else a value related to GameMapTile.id will be present 
-    /// This is also known as layer-0 or the default layer when there are movethan one layer in the map.
-    /// </summary>
 	public int[] grid = new int[0];
-
-    /// <summary> The GameMap can have additional layers. In that case the grid[] field above will be the default, or layer-0. </summary>
     public GameMapLayer[] layers = new GameMapLayer[0];
 
 	// *** extra properties related to this map
@@ -36,36 +23,24 @@ public class GameMap
 	//public Color backgroundColor = Color.black;
 
 	// ----------------------------------------------------------------------------------------------------------------
-
-	/// <summary> Return the grid index of a tile at position (x,y). No error checking, x or y value is out of bounds, to improve performance. </summary>
 	public int PositionToIdx(int x, int y)
 	{
 		return (y * width + x);
 	}
-
-	/// <summary> Get an x and y position, given specific index into grid[]. No error checking, idx out of bounds, to improve performance. </summary>
 	public void IdxToPosition(int idx, out int x, out int y)
 	{
 		y = idx / width;
 		x = idx - (y * width);
 	}
-
-    /// <summary> This returns layers.Length + 1 since grid[] is layer-0. </summary>
     public int LayerCount { get { return layers.Length + 1;  } }
-
-    /// <summary> Returns data from a layer. layerIdx=0 is the same as reading GameMap.grid[] while any higher value (1+) will be data from GameMap.layers[layerIdx-1].grid </summary>
     public int[] GetlayerData(int layerIdx)
     {
         return (layerIdx == 0 ? grid : layers[layerIdx - 1].grid);
     }
 
-	/// <summary> Return an array of GameMapIdxIdPair with the ID and Index of tiles neighbouring the one at x,y.
-	/// Array is always length 4 else 8 if includeDiagonal=true.
-	/// idx or id = -1 represents no tile or areas outside of map grid.
-	/// Result starts at tile above (north) and go around clockwise. </summary>
 	public GameMapIdxIdPair[] GetNeighbours(int x, int y, bool includeDiagonal = false, int layerIdx = 0)
 	{
-        int[] g = (layerIdx == 0 ? grid : layers[layerIdx - 1].grid);
+        int[] g = GetlayerData(layerIdx);
 		if (includeDiagonal)
 		{
 			return new GameMapIdxIdPair[]
@@ -148,7 +123,6 @@ public class GameMap
 		height = h;
 	}
 
-	/// <summary> Fill grid with empty tiles (-1). Mainly used by editor. </summary>
 	public void ClearMap()
 	{
         for (int i = 0; i < grid.Length; i++)
@@ -165,15 +139,12 @@ public class GameMap
         }
     }
 
-    /// <summary> This set the size of the layer. This will destroy any exsiting tile placements in the layer.
-    /// idx=0 refers to the grid[] while anything higher will be layers[idx-1].grid </summary>
     public void InitLayer(int idx)
     {
 		layers[idx-1].grid = new int[width * height];
 		ClearLayer(idx);
 	}
 
-    /// <summary> idx=0 refers to the grid[] while anything higher will be layers[idx-1].grid </summary>
     public void ClearLayer(int idx)
     {
         if (idx == 0)

+ 2 - 6
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/GameMapLayer.cs

@@ -23,20 +23,16 @@ public enum WaveType
 	Tower = 1,		//塔波
 	FromTower = 2,	//根据塔血量出
 }
-/// <summary>
-/// A layer consists of a series of values in a grid.
-/// Each GameMap can have one or more layers.
-/// </summary>
+
 [System.Serializable]
 public class GameMapLayer
 {
-	/// <summary> The layer's grid of tile values. -1 is an empty tile, else a value related to GameMapTile.id will be present </summary>
 	public int[] grid = new int[0];
 	public int duration;
 	public string buildingId;
 	public float buildingHp;
 	public string name;
-	public Dictionary<int, SpawnTime> spawnTimes = new();
+	public List<SpawnTime> spawnTimes = new();
 	public WaveType waveType;
 	// ----------------------------------------------------------------------------------------------------------------
 }