Quellcode durchsuchen

出怪表和编辑器连接

wgl vor 5 Monaten
Ursprung
Commit
d5ea87eeb9

+ 114 - 27
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/Editor/GameMapEdPopup.cs

@@ -26,7 +26,8 @@ public class GameMapEdPopup : PopupWindowContent
 	{
 		GC_Head = isSelect ? new GUIContent("Select") : new GUIContent("Save");
 		GC_Add = isSelect ? new GUIContent("新建") : new GUIContent("另存");
-		list = new ReorderableList(Asset.maps, typeof(GameMap), true, false, false, false)
+		List<GameMap> gameMaps = Asset.maps.GetRange(1, Asset.maps.Count - 1);
+		list = new ReorderableList(gameMaps, typeof(GameMap), false, false, false, false)
 		{
 			elementHeight = EditorGUIUtility.singleLineHeight,
 			headerHeight = 0,
@@ -51,10 +52,14 @@ public class GameMapEdPopup : PopupWindowContent
 
 	public override void OnGUI(Rect r)
 	{
+
 		DrawHeader(new Rect(0, 0, r.width, 25));
 		r.y += 25; r.height -= 25;
 		scroll = GUI.BeginScrollView(r, scroll, contentRect, false, true);
-		list.DoList(r);
+		if (list != null)
+		{
+			list.DoList(r);
+		}
 		GUI.EndScrollView();
 	}
 
@@ -92,46 +97,128 @@ public class GameMapEdPopup : PopupWindowContent
         //r.x -= 25;
         if (GUI.Button(r, GC_Add, EditorStyles.miniButtonLeft))
         {
-            //Undo.RecordObject(Asset, "Add Game Map");
-            //Asset.AddMap();
-            //ChangeContentRect();
-            //OnMapSelected(Asset.maps.Count - 1);
-        }
+            if (isSelect)
+            {
+				Asset.ClearMap(0);
+				GameMapEditor.mapSize_w = Asset.maps[0].width;
+				GameMapEditor.mapSize_h = Asset.maps[0].height;
+				OnMapSelected(0);
+			}
+            else
+            {
+				SaveAsNewExcelData(GameMapEditor.createEnemyExcelPath);
+            }
+			editorWindow.Close();
+		}
     }
 
 	private void DrawElement(Rect r, int index, bool isActive, bool isFocused)
 	{
-		GUI.Label(r, Asset.maps[index].ToString());
+		GUI.Label(r, Asset.maps[index + 1].ToString());
 	}
 
 	private void SelectMap(ReorderableList list)
 	{
         if (isSelect)
         {
-			ReloadExcelData(GameMapEditor.createEnemyExcelPath, list.index);
-			OnMapSelected(list.index);
+			ReloadExcelData(GameMapEditor.createEnemyExcelPath, list.index + 1);
+			OnMapSelected(0);
         }
         else
         {
-			//SaveExcelData(GameMapEditor.createEnemyExcelPath, list.index);
+			SaveExcelData(GameMapEditor.createEnemyExcelPath, list.index + 1);
         }
 		editorWindow.Close();
 	}
 
-	void SaveExcelData(string path, int mapIdx)
+	void SaveAsNewExcelData(string path)
+    {
+		FileInfo fileInfo = new FileInfo(path);
+		using (ExcelPackage package = new ExcelPackage(fileInfo))
+        {
+			GameMap gameMap = GameMapEditor.asset.maps[GameMapEditor.mapIdx];
+			// 拷贝 Sheet
+			ExcelWorksheet sourceSheet = package.Workbook.Worksheets[1];
+            // 保存目标文件
+            try
+            {
+				ExcelWorksheet newSheet = package.Workbook.Worksheets.Add(gameMap.showIdent, sourceSheet);
+				package.Save();
+			}
+			catch (System.InvalidOperationException e)
+			{
+				Debug.LogError($"出怪表未关闭或Sheet名重复,请重试");
+				Debug.LogException(e);
+				return;
+			}
+		}
+		SaveExcelData(path, 0,true);
+	}
+
+	void SaveExcelData(string path, int mapIdx, bool isNew = false)
 	{
 		List<string> excelWorksheets = new();
 		GameMap gameMap = GameMapEditor.asset.maps[GameMapEditor.mapIdx];
 		FileInfo fileInfo = new FileInfo(path);
 		using (ExcelPackage package = new ExcelPackage(fileInfo))
 		{
+
 			excelWorksheets = ExcelEditor.ReadExcelSheetsInfo(package);
-			if (!ExcelEditor.RenameSheet(package, excelWorksheets[mapIdx], gameMap.showIdent))
+			if (!isNew)
 			{
-				return;
+				if (!ExcelEditor.RenameSheet(package, excelWorksheets[mapIdx - 1], gameMap.showIdent))
+				{
+					return;
+				}
 			}
-			ExcelEditor.ModifyExcel(package, gameMap.showIdent, "C6", GameMapEditor.mapSize_w.ToString());
+			ExcelEditor.ModifyExcel(package, gameMap.showIdent, "D5", GameMapEditor.mapSize_w.ToString());
 			ExcelEditor.ModifyExcel(package, gameMap.showIdent, "D6", GameMapEditor.mapSize_h.ToString());
+
+			int cell_row = 10;
+			for(int i = 0; i < gameMap.layers.Length; i++)
+            {
+				GameMapLayer layer = gameMap.layers[i];
+				
+				ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"C{cell_row}", layer.name);
+				ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"D{cell_row}", layer.duration.ToString());
+				ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"E{cell_row}", layer.attack.ToString());
+				ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"F{cell_row}", layer.moveSpeed.ToString());
+				ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"G{cell_row}", layer.Hp.ToString());
+				for(int j = 0;j< layer.spawnTimes.Count; j++)
+                {
+					SpawnTime spawnTime = layer.spawnTimes[j];
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"B{cell_row}", (i + 1).ToString());
+					if(j != 0)
+                    {
+						for (int k = 0; k < 5; k++)
+						{
+							ExcelEditor.ModifyExcel(package, gameMap.showIdent, cell_row, 3 + k, "");
+						}
+					}
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"H{cell_row}", Asset.tileAsset.GetTile(spawnTime.id).name);
+					string posString = "";
+					for(int k = 0; k < spawnTime.pos.Count; k++)
+                    {
+						int pos = spawnTime.pos[k] + 1;
+						posString += (pos % GameMapEditor.mapSize_w).ToString();
+						posString += ",";
+						posString += (pos / GameMapEditor.mapSize_w + 1).ToString();
+						if(k == spawnTime.pos.Count - 1)
+                        {
+							break;
+                        }
+						posString += ",";
+                    }
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"I{cell_row}",	posString);
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"J{cell_row}",	spawnTime.startTime.ToString());
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"K{cell_row}",	spawnTime.endTime.ToString());
+					ExcelEditor.ModifyExcel(package, gameMap.showIdent, $"L{cell_row}",	spawnTime.num.ToString());
+					cell_row++;
+				}
+            }
+			ExcelEditor.RemoveExcelRows(package, gameMap.showIdent, cell_row, cell_row + 100);
+
+
 			package.Save();
 		}
 		Debug.Log($"\"{gameMap.showIdent}\"表保存完毕!");
@@ -143,28 +230,28 @@ public class GameMapEdPopup : PopupWindowContent
 		List<string> excelWorksheets = new();
 		Tables allCfgData = new Tables(ExcelEditor.Loader);
 		List<SingleCreateEnemyConfig> cfgCreateEnemy = allCfgData.CfgCreateEnemy.DataList;
-		GameMap gameMap = GameMapEditor.asset.maps[mapIdx];
+		GameMap gameMap = GameMapEditor.asset.maps[0];
 		FileInfo fileInfo = new FileInfo(path);
 		using (ExcelPackage package = new ExcelPackage(fileInfo))
 		{
 			excelWorksheets = ExcelEditor.ReadExcelSheetsInfo(package);
 
 			//导入关卡数据
+			gameMap.ident = GameMapEditor.asset.maps[mapIdx].ident;
 			gameMap.showIdent = gameMap.ident;
-			int.TryParse(ExcelEditor.GetCellData(package, excelWorksheets[mapIdx], "C6"), out GameMapEditor.mapSize_w);
-			int.TryParse(ExcelEditor.GetCellData(package, excelWorksheets[mapIdx], "D6"), out GameMapEditor.mapSize_h);
+			int.TryParse(ExcelEditor.GetCellData(package, excelWorksheets[mapIdx - 1], "D5"), out GameMapEditor.mapSize_w);
+			int.TryParse(ExcelEditor.GetCellData(package, excelWorksheets[mapIdx - 1], "D6"), out GameMapEditor.mapSize_h);
 			gameMap.Resize(GameMapEditor.mapSize_w, GameMapEditor.mapSize_h);
 		}
 
 		//导入波次数据
 		GameMapEditor.currLayer = -1;
 		int mapId = -1;
-		String waveName = "";
 		for (int i = 0; i < cfgCreateEnemy.Count; i++)
 		{
-			if (cfgCreateEnemy[i].WaveName == "占位行")
+			if (cfgCreateEnemy[i].WaveID == -1)
 			{
-				if(mapId == mapIdx)
+				if(mapId == mapIdx - 1)
                 {
 					break;
                 }
@@ -172,17 +259,17 @@ public class GameMapEdPopup : PopupWindowContent
 				ArrayUtility.Clear(ref gameMap.layers);
 				continue;
 			}
-			if(mapId == mapIdx)
+			if(mapId == mapIdx - 1)
             {
-				if (cfgCreateEnemy[i].WaveName != waveName)
-				{
+				if(cfgCreateEnemy[i].WaveID > gameMap.layers.Length)
+                {
 					//波次第一行的波次数据
 					SingleCreateEnemyConfig singleCreateEnemyConfig = cfgCreateEnemy[i];
-					waveName = singleCreateEnemyConfig.WaveName;
+					int id = singleCreateEnemyConfig.WaveID;
 					ArrayUtility.Add(ref gameMap.layers, new GameMapLayer());
 					gameMap.InitLayer(gameMap.layers.Length);
 					GameMapLayer gameMapLayer = gameMap.layers[gameMap.layers.Length - 1];
-					gameMapLayer.name = waveName;
+					gameMapLayer.name = singleCreateEnemyConfig.WaveName; 
 					gameMapLayer.duration = singleCreateEnemyConfig.WaveTime;
 					gameMapLayer.Hp = singleCreateEnemyConfig.HPRatio;
 					gameMapLayer.moveSpeed = singleCreateEnemyConfig.SpeedRatio;
@@ -208,7 +295,7 @@ public class GameMapEdPopup : PopupWindowContent
 
 
 		doRepaint = true;
-		Debug.Log($"\"{excelWorksheets[mapIdx]}\"表导入完毕!");
+		Debug.Log($"\"{excelWorksheets[mapIdx - 1]}\"表导入完毕!");
 	}
 
 

+ 6 - 4
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/Editor/GameMapEditor.cs

@@ -657,7 +657,7 @@ public class GameMapEditor : EditorWindow
 					EditorGUILayout.BeginHorizontal();
 					{
 						EditorGUILayout.PrefixLabel("Ident");
-						GUILayout.Label(mapIdx.ToString() + " => " + asset.maps[mapIdx].showIdent);
+						GUILayout.Label(" => " + asset.maps[mapIdx].showIdent);
 						if (GUILayout.Button(GC_rename, EditorStyles.miniButtonRight)) GameMapTextEd.ShowEd("Rename map", "Enter a unique name", asset.maps[mapIdx].showIdent, OnRenameMap);
 						GUILayout.FlexibleSpace();
 					}
@@ -719,20 +719,22 @@ public class GameMapEditor : EditorWindow
 		{
 			mapIdx = 0;
 		}
+		asset.maps[0] = asset.maps[mapIdx];
+		mapIdx = 0;
 		if (asset.maps.Count > excelWorksheets.Count)
 		{
-			for (int i = asset.maps.Count - 1; i >= excelWorksheets.Count; i--)
+			for (int i = asset.maps.Count - 1; i > excelWorksheets.Count; i--)
 			{
 				asset.RemoveMapAtIndex(i);
 			}
 		}
-		for (int i = 0; i < excelWorksheets.Count; i++)
+		for (int i = 1; i < excelWorksheets.Count + 1; i++)
 		{
 			if (i >= asset.maps.Count)
 			{
 				asset.AddMap();
 			}
-			asset.maps[i].ident = excelWorksheets[i];
+			asset.maps[i].ident = excelWorksheets[i - 1];
 		}
 	}
 

+ 9 - 4
ActionTowerDefense/Assets/GameLevelEditor/GameMap/CoreScripts/GameMapsAsset.cs

@@ -12,7 +12,7 @@ public class GameMapsAsset : ScriptableObject
 	/// <summary> The tiles asset associated with the maps </summary>	
 	[SerializeField] public GameMapTilesAsset tileAsset;
 
-	[SerializeField] private int nextMapId = 1;
+	//[SerializeField] private int nextMapId = 1;
 
 	// *** extra properties related to this maps asset
 	// add any addition serializable types here and they will appear
@@ -25,9 +25,8 @@ public class GameMapsAsset : ScriptableObject
 	/// <summary> Add a new map. Mainly for use by the map editor. </summary>
 	public void AddMap()
 	{
-		GameMap m = new GameMap() { ident = "Map " + nextMapId };
+		GameMap m = new GameMap() { ident = "新增 " + (maps.Count + 1) };
 		m.SetSize(10, 10);
-		nextMapId++;
 		maps.Add(m);
 	}
 
@@ -35,8 +34,14 @@ public class GameMapsAsset : ScriptableObject
 	public void RemoveMapAtIndex(int idx)
 	{
 		maps.RemoveAt(idx);
-		if (maps.Count == 0) nextMapId = 1;
 	}
 
+	public void ClearMap(int idx)
+    {
+		GameMap m = new GameMap() { ident = "新增关卡"};
+		m.SetSize(10, 10);
+		m.showIdent = m.ident;
+		maps[idx] = m; 
+	}
 	// ----------------------------------------------------------------------------------------------------------------
 }

+ 9 - 5
ActionTowerDefense/Assets/Gen/SingleCreateEnemyConfig.cs

@@ -18,6 +18,7 @@ public sealed partial class SingleCreateEnemyConfig :  Bright.Config.BeanBase
 {
     public SingleCreateEnemyConfig(JSONNode _json) 
     {
+        { if(!_json["WaveID"].IsNumber) { throw new SerializationException(); }  WaveID = _json["WaveID"]; }
         { if(!_json["WaveName"].IsString) { throw new SerializationException(); }  WaveName = _json["WaveName"]; }
         { if(!_json["WaveTime"].IsNumber) { throw new SerializationException(); }  WaveTime = _json["WaveTime"]; }
         { if(!_json["AttackRatio"].IsNumber) { throw new SerializationException(); }  AttackRatio = _json["AttackRatio"]; }
@@ -34,8 +35,9 @@ public sealed partial class SingleCreateEnemyConfig :  Bright.Config.BeanBase
         PostInit();
     }
 
-    public SingleCreateEnemyConfig(string WaveName, int WaveTime, float AttackRatio, float SpeedRatio, float HPRatio, string EnemyName, System.Collections.Generic.List<int> Position, int StartTime, int EndTime, int Count, int Type, float BuildingHP, int BuildingID ) 
+    public SingleCreateEnemyConfig(int WaveID, string WaveName, int WaveTime, float AttackRatio, float SpeedRatio, float HPRatio, string EnemyName, System.Collections.Generic.List<int> Position, int StartTime, int EndTime, int Count, int Type, float BuildingHP, int BuildingID ) 
     {
+        this.WaveID = WaveID;
         this.WaveName = WaveName;
         this.WaveTime = WaveTime;
         this.AttackRatio = AttackRatio;
@@ -60,14 +62,15 @@ public sealed partial class SingleCreateEnemyConfig :  Bright.Config.BeanBase
     /// <summary>
     /// 表格参数:
     /// </summary>
-    public string WaveName { get; private set; }
+    public int WaveID { get; private set; }
     /// <summary>
-    /// 宽
+    /// 宽
     /// </summary>
-    public int WaveTime { get; private set; }
+    public string WaveName { get; private set; }
     /// <summary>
-    /// 
+    /// 140
     /// </summary>
+    public int WaveTime { get; private set; }
     public float AttackRatio { get; private set; }
     public float SpeedRatio { get; private set; }
     public float HPRatio { get; private set; }
@@ -95,6 +98,7 @@ public sealed partial class SingleCreateEnemyConfig :  Bright.Config.BeanBase
     public override string ToString()
     {
         return "{ "
+        + "WaveID:" + WaveID + ","
         + "WaveName:" + WaveName + ","
         + "WaveTime:" + WaveTime + ","
         + "AttackRatio:" + AttackRatio + ","

+ 2 - 7
ActionTowerDefense/Assets/Scripts/ExcelEditor.cs

@@ -16,7 +16,7 @@ using SimpleJSON;
         脚本A(package);
         脚本B(package);
         ...
-        package.Save(); //如果要保存就加这一行(记得关闭excel!!!)
+        package.Save(); //如果修改了表格要保存就加这一行(记得关闭excel!!!)
     }
 
 使用方法*/
@@ -46,11 +46,6 @@ public class ExcelEditor
             Debug.Log($"修改Sheet名\"{oldSheetName}\"为\"{newSheetName}\"");
             return true;
         }
-        catch (System.InvalidOperationException e)
-        {
-            Debug.LogError($"请关闭Excel后重试");
-            return false;
-        }
         catch (System.ArgumentException)
         {
             Debug.LogError($"Sheet名重复请重试");
@@ -100,12 +95,12 @@ public class ExcelEditor
         {
             ExcelWorksheet worksheet = package.Workbook.Worksheets[sheetMame];
             worksheet.Cells[cell_row, cell_column].Value = newData; // 通过行列索引
-            package.Save();
             return true;
         }
         catch (System.InvalidOperationException e)
         {
             Debug.LogError($"请关闭Excel后重试");
+            Debug.LogException(e);
             return false;
         }
         catch (System.Exception e)

+ 86 - 7
ActionTowerDefense/GenerateDatas/json/cfgcreateenemy.json

@@ -1,5 +1,6 @@
 [
   {
+    "WaveID": -1,
     "WaveName": "占位行",
     "WaveTime": 0,
     "AttackRatio": 0,
@@ -15,35 +16,56 @@
     "BuildingID": 0
   },
   {
-    "WaveName": "1",
+    "WaveID": 1,
+    "WaveName": "第一波",
     "WaveTime": 0,
     "AttackRatio": 1,
     "SpeedRatio": 1,
     "HPRatio": 1,
-    "EnemyName": "小猪",
+    "EnemyName": "敌方防御塔",
     "Position": [
       1,
       1
     ],
     "StartTime": 0,
     "EndTime": 0,
-    "Count": 10,
+    "Count": 1,
     "Type": 0,
     "BuildingHP": 0,
     "BuildingID": 0
   },
   {
-    "WaveName": "2",
+    "WaveID": 2,
+    "WaveName": "第二波",
     "WaveTime": 0,
     "AttackRatio": 2,
     "SpeedRatio": 1,
     "HPRatio": 1,
     "EnemyName": "飞胖",
     "Position": [
+      1,
       1,
       2,
+      2
+    ],
+    "StartTime": 0,
+    "EndTime": 0,
+    "Count": 10,
+    "Type": 0,
+    "BuildingHP": 0,
+    "BuildingID": 0
+  },
+  {
+    "WaveID": 2,
+    "WaveName": "",
+    "WaveTime": 0,
+    "AttackRatio": 0,
+    "SpeedRatio": 0,
+    "HPRatio": 0,
+    "EnemyName": "小猪",
+    "Position": [
       2,
-      3
+      1
     ],
     "StartTime": 0,
     "EndTime": 0,
@@ -53,13 +75,51 @@
     "BuildingID": 0
   },
   {
-    "WaveName": "2",
+    "WaveID": -1,
+    "WaveName": "占位行",
     "WaveTime": 0,
     "AttackRatio": 0,
     "SpeedRatio": 0,
     "HPRatio": 0,
-    "EnemyName": "防御塔",
+    "EnemyName": "",
+    "Position": [],
+    "StartTime": 0,
+    "EndTime": 0,
+    "Count": 0,
+    "Type": 0,
+    "BuildingHP": 0,
+    "BuildingID": 0
+  },
+  {
+    "WaveID": 1,
+    "WaveName": "第一波",
+    "WaveTime": 0,
+    "AttackRatio": 1,
+    "SpeedRatio": 1,
+    "HPRatio": 1,
+    "EnemyName": "敌方防御塔",
     "Position": [
+      1,
+      1
+    ],
+    "StartTime": 0,
+    "EndTime": 0,
+    "Count": 1,
+    "Type": 0,
+    "BuildingHP": 0,
+    "BuildingID": 0
+  },
+  {
+    "WaveID": 2,
+    "WaveName": "第二波",
+    "WaveTime": 0,
+    "AttackRatio": 2,
+    "SpeedRatio": 1,
+    "HPRatio": 1,
+    "EnemyName": "飞胖",
+    "Position": [
+      1,
+      1,
       2,
       2
     ],
@@ -69,5 +129,24 @@
     "Type": 0,
     "BuildingHP": 0,
     "BuildingID": 0
+  },
+  {
+    "WaveID": 2,
+    "WaveName": "",
+    "WaveTime": 0,
+    "AttackRatio": 0,
+    "SpeedRatio": 0,
+    "HPRatio": 0,
+    "EnemyName": "小猪",
+    "Position": [
+      2,
+      1
+    ],
+    "StartTime": 0,
+    "EndTime": 0,
+    "Count": 10,
+    "Type": 0,
+    "BuildingHP": 0,
+    "BuildingID": 0
   }
 ]

BIN
ActionTowerDefense/Luban/Config/Datas/出怪表.xlsx