diff --git a/DungeonShooting_Godot/resource/map/tileData/testGroup/battle/Room1.json b/DungeonShooting_Godot/resource/map/tileData/testGroup/battle/Room1.json index 1b6dd17..b5bdad6 100644 --- a/DungeonShooting_Godot/resource/map/tileData/testGroup/battle/Room1.json +++ b/DungeonShooting_Godot/resource/map/tileData/testGroup/battle/Room1.json @@ -10,83 +10,51 @@ "DoorAreaInfos": [], "NavigationList": [ { - "Type": 0, - "Points": [ - { - "X": -56, - "Y": -72 - }, - { - "X": 280, - "Y": -72 - }, - { - "X": 280, - "Y": 128 - }, - { - "X": -56, - "Y": 128 - } - ] + "PointList": [ + -56, + -72, + 280, + -72, + 280, + 128, + -56, + 128 + ], + "Type": 0 }, { - "Type": 1, - "Points": [ - { - "X": -24, - "Y": -32 - }, - { - "X": 56, - "Y": -32 - }, - { - "X": 56, - "Y": 8 - }, - { - "X": 24, - "Y": 8 - }, - { - "X": 24, - "Y": 24 - }, - { - "X": -24, - "Y": 24 - } - ] + "PointList": [ + -24, + -32, + 56, + -32, + 56, + 8, + 24, + 8, + 24, + 24, + -24, + 24 + ], + "Type": 1 }, { - "Type": 1, - "Points": [ - { - "X": 200, - "Y": 32 - }, - { - "X": 248, - "Y": 32 - }, - { - "X": 248, - "Y": 88 - }, - { - "X": 168, - "Y": 88 - }, - { - "X": 168, - "Y": 48 - }, - { - "X": 200, - "Y": 48 - } - ] + "PointList": [ + 200, + 32, + 248, + 32, + 248, + 88, + 168, + 88, + 168, + 48, + 200, + 48 + ], + "Type": 1 } ], "GroupName": "testGroup", diff --git a/DungeonShooting_Godot/src/framework/common/Utils.cs b/DungeonShooting_Godot/src/framework/common/Utils.cs index 3bf8354..b9a16b7 100644 --- a/DungeonShooting_Godot/src/framework/common/Utils.cs +++ b/DungeonShooting_Godot/src/framework/common/Utils.cs @@ -44,17 +44,23 @@ for (var i = 0; i < polygonData.Length; i++) { var item = polygonData[i]; - if (item.Points.Count >= 2) + var points = item.GetPoints(); + if (points.Length>= 2) { - var array = item.ConvertPointsToVector2Array().ToList(); - array.Add(array[0]); + var array = new Vector2[points.Length + 1]; + for (var j = 0; j < points.Length; j++) + { + array[j] = points[j]; + } + + array[array.Length - 1] = points[0]; if (item.Type == NavigationPolygonType.In) { - canvasItem.DrawPolyline(array.ToArray(), Colors.Yellow, width); + canvasItem.DrawPolyline(array, Colors.Yellow, width); } else { - canvasItem.DrawPolyline(array.ToArray(), Colors.Red, width); + canvasItem.DrawPolyline(array, Colors.Red, width); } } } diff --git a/DungeonShooting_Godot/src/framework/map/DungeonRoomTemplate.cs b/DungeonShooting_Godot/src/framework/map/DungeonRoomTemplate.cs index ce62edd..4acc9a9 100644 --- a/DungeonShooting_Godot/src/framework/map/DungeonRoomTemplate.cs +++ b/DungeonShooting_Godot/src/framework/map/DungeonRoomTemplate.cs @@ -956,13 +956,14 @@ var polygonData = new NavigationPolygonData(); polygonData.Type = (NavigationPolygonType)navigation["Type"].AsInt32(); - polygonData.Points = new List(); + var points = new List(); var pointArray = navigation["Points"].AsGodotArray(); for (var j = 0; j < pointArray.Count; j++) { var point = pointArray[j].AsGodotDictionary(); - polygonData.Points.Add(new SerializeVector2(point["X"].AsInt32(), point["Y"].AsInt32())); + points.Add(new Vector2(point["X"].AsInt32(), point["Y"].AsInt32())); } + polygonData.SetPoints(points.ToArray()); roomInfo.NavigationList.Add(polygonData); } diff --git a/DungeonShooting_Godot/src/framework/map/DungeonTileMap.cs b/DungeonShooting_Godot/src/framework/map/DungeonTileMap.cs index 776ac19..95ccca7 100644 --- a/DungeonShooting_Godot/src/framework/map/DungeonTileMap.cs +++ b/DungeonShooting_Godot/src/framework/map/DungeonTileMap.cs @@ -464,16 +464,16 @@ var x = rect.Position.X * GameConfig.TileCellSize; var y = rect.Position.Y * GameConfig.TileCellSize; - var op1 = new SerializeVector2(x - GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 1.5f); - var op2 = new SerializeVector2(x + GameConfig.TileCellSize * 0.5f, y + GameConfig.TileCellSize * 1.5f); - var op3 = new SerializeVector2(x + GameConfig.TileCellSize * 0.5f, y + GameConfig.TileCellSize * 3f); - var op4 = new SerializeVector2(x - GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 3f); + var op1 = new Vector2(x - GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 1.5f); + var op2 = new Vector2(x + GameConfig.TileCellSize * 0.5f, y + GameConfig.TileCellSize * 1.5f); + var op3 = new Vector2(x + GameConfig.TileCellSize * 0.5f, y + GameConfig.TileCellSize * 3f); + var op4 = new Vector2(x - GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 3f); AddDoorNavigation( doorInfo, op1, op2, op3, op4, - new SerializeVector2(op1), - new SerializeVector2(op1.X + GameConfig.TileCellSize, op2.Y), - new SerializeVector2(op1.X + GameConfig.TileCellSize, op3.Y), - new SerializeVector2(op4) + op1, + new Vector2(op1.X + GameConfig.TileCellSize, op2.Y), + new Vector2(op1.X + GameConfig.TileCellSize, op3.Y), + op4 ); } } @@ -498,16 +498,16 @@ var x = rect.Position.X * GameConfig.TileCellSize; var y = rect.Position.Y * GameConfig.TileCellSize; - var op1 = new SerializeVector2(x - GameConfig.TileCellSize * 1.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 1.5f); - var op2 = new SerializeVector2(x + GameConfig.TileCellSize * 0.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 1.5f); - var op3 = new SerializeVector2(x + GameConfig.TileCellSize * 0.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 3f); - var op4 = new SerializeVector2(x - GameConfig.TileCellSize * 1.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 3f); + var op1 = new Vector2(x - GameConfig.TileCellSize * 1.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 1.5f); + var op2 = new Vector2(x + GameConfig.TileCellSize * 0.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 1.5f); + var op3 = new Vector2(x + GameConfig.TileCellSize * 0.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 3f); + var op4 = new Vector2(x - GameConfig.TileCellSize * 1.5f + (rect.Size.X + 1) * GameConfig.TileCellSize, y + GameConfig.TileCellSize * 3f); AddDoorNavigation( doorInfo, op1, op2, op3, op4, - new SerializeVector2(op2.X - GameConfig.TileCellSize, op1.Y), - new SerializeVector2(op2), - new SerializeVector2(op3), - new SerializeVector2(op2.X - GameConfig.TileCellSize, op4.Y) + new Vector2(op2.X - GameConfig.TileCellSize, op1.Y), + op2, + op3, + new Vector2(op2.X - GameConfig.TileCellSize, op4.Y) ); } } @@ -532,16 +532,16 @@ var x = rect.Position.X * GameConfig.TileCellSize; var y = rect.Position.Y * GameConfig.TileCellSize; - var op1 = new SerializeVector2(x + GameConfig.TileCellSize * 1.5f, y - GameConfig.TileCellSize * 1f); - var op2 = new SerializeVector2(x + GameConfig.TileCellSize * 2.5f, y - GameConfig.TileCellSize * 1f); - var op3 = new SerializeVector2(x + GameConfig.TileCellSize * 2.5f, y + GameConfig.TileCellSize * 0.5f); - var op4 = new SerializeVector2(x + GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 0.5f); + var op1 = new Vector2(x + GameConfig.TileCellSize * 1.5f, y - GameConfig.TileCellSize * 1f); + var op2 = new Vector2(x + GameConfig.TileCellSize * 2.5f, y - GameConfig.TileCellSize * 1f); + var op3 = new Vector2(x + GameConfig.TileCellSize * 2.5f, y + GameConfig.TileCellSize * 0.5f); + var op4 = new Vector2(x + GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 0.5f); AddDoorNavigation( doorInfo, op1, op2, op3, op4, - new SerializeVector2(op1), - new SerializeVector2(op2), - new SerializeVector2(op3.X, op1.Y + GameConfig.TileCellSize), - new SerializeVector2(op4.X, op1.Y + GameConfig.TileCellSize) + op1, + op2, + new Vector2(op3.X, op1.Y + GameConfig.TileCellSize), + new Vector2(op4.X, op1.Y + GameConfig.TileCellSize) ); } } @@ -565,16 +565,16 @@ var x = rect.Position.X * GameConfig.TileCellSize; var y = rect.Position.Y * GameConfig.TileCellSize; - var op1 = new SerializeVector2(x + GameConfig.TileCellSize * 1.5f, y - GameConfig.TileCellSize * 1f + (rect.Size.Y + 1) * GameConfig.TileCellSize); - var op2 = new SerializeVector2(x + GameConfig.TileCellSize * 2.5f, y - GameConfig.TileCellSize * 1f + (rect.Size.Y + 1) * GameConfig.TileCellSize); - var op3 = new SerializeVector2(x + GameConfig.TileCellSize * 2.5f, y + GameConfig.TileCellSize * 0.5f + (rect.Size.Y + 1) * GameConfig.TileCellSize); - var op4 = new SerializeVector2(x + GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 0.5f + (rect.Size.Y + 1) * GameConfig.TileCellSize); + var op1 = new Vector2(x + GameConfig.TileCellSize * 1.5f, y - GameConfig.TileCellSize * 1f + (rect.Size.Y + 1) * GameConfig.TileCellSize); + var op2 = new Vector2(x + GameConfig.TileCellSize * 2.5f, y - GameConfig.TileCellSize * 1f + (rect.Size.Y + 1) * GameConfig.TileCellSize); + var op3 = new Vector2(x + GameConfig.TileCellSize * 2.5f, y + GameConfig.TileCellSize * 0.5f + (rect.Size.Y + 1) * GameConfig.TileCellSize); + var op4 = new Vector2(x + GameConfig.TileCellSize * 1.5f, y + GameConfig.TileCellSize * 0.5f + (rect.Size.Y + 1) * GameConfig.TileCellSize); AddDoorNavigation( doorInfo, op1, op2, op3, op4, - new SerializeVector2(op1.X, op3.Y - GameConfig.TileCellSize), - new SerializeVector2(op2.X, op3.Y - GameConfig.TileCellSize), - new SerializeVector2(op3), - new SerializeVector2(op4) + new Vector2(op1.X, op3.Y - GameConfig.TileCellSize), + new Vector2(op2.X, op3.Y - GameConfig.TileCellSize), + op3, + op4 ); } } @@ -583,23 +583,17 @@ /// 添加房间 /// private void AddDoorNavigation(RoomDoorInfo doorInfo, - SerializeVector2 op1, SerializeVector2 op2, SerializeVector2 op3, SerializeVector2 op4, - SerializeVector2 cp1, SerializeVector2 cp2, SerializeVector2 cp3, SerializeVector2 cp4) + Vector2 op1, Vector2 op2, Vector2 op3, Vector2 op4, + Vector2 cp1, Vector2 cp2, Vector2 cp3, Vector2 cp4) { var openPolygonData = new NavigationPolygonData(); openPolygonData.Type = NavigationPolygonType.Out; - openPolygonData.Points.Add(op1); - openPolygonData.Points.Add(op2); - openPolygonData.Points.Add(op3); - openPolygonData.Points.Add(op4); - + openPolygonData.SetPoints(new []{ op1, op2, op3, op4 }); + var closePolygonData = new NavigationPolygonData(); closePolygonData.Type = NavigationPolygonType.Out; - closePolygonData.Points.Add(cp1); - closePolygonData.Points.Add(cp2); - closePolygonData.Points.Add(cp3); - closePolygonData.Points.Add(cp4); - + closePolygonData.SetPoints(new []{ cp1, cp2, cp3, cp4 }); + _connectNavigationItemList.Add(new DoorNavigationInfo(doorInfo, openPolygonData, closePolygonData)); } @@ -607,11 +601,11 @@ // private void TestData() // { // _polygonDataList.Clear(); - // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new SerializeVector2(-456, 712), new SerializeVector2(-440, 712), new SerializeVector2(-440, 792), new SerializeVector2(-456, 792) })}); - // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.In, Points = new List(new []{ new SerializeVector2(-1048, 744), new SerializeVector2(-840, 744), new SerializeVector2(-840, 840), new SerializeVector2(-1048, 840) })}); - // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new SerializeVector2(488, 920), new SerializeVector2(504, 920), new SerializeVector2(504, 1128), new SerializeVector2(488, 1128) })}); - // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new SerializeVector2(1320, 984), new SerializeVector2(1352, 984), new SerializeVector2(1352, 1096), new SerializeVector2(1432, 1096), new SerializeVector2(1432, 984), new SerializeVector2(1576, 984), new SerializeVector2(1576, 1128), new SerializeVector2(1544, 1128), new SerializeVector2(1544, 1000), new SerializeVector2(1464, 1000), new SerializeVector2(1464, 1128), new SerializeVector2(1320, 1128) })}); - // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new SerializeVector2(712, 1432), new SerializeVector2(984, 1432), new SerializeVector2(984, 1592), new SerializeVector2(712, 1592) })}); + // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new Vector2(-456, 712), new Vector2(-440, 712), new Vector2(-440, 792), new Vector2(-456, 792) })}); + // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.In, Points = new List(new []{ new Vector2(-1048, 744), new Vector2(-840, 744), new Vector2(-840, 840), new Vector2(-1048, 840) })}); + // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new Vector2(488, 920), new Vector2(504, 920), new Vector2(504, 1128), new Vector2(488, 1128) })}); + // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new Vector2(1320, 984), new Vector2(1352, 984), new Vector2(1352, 1096), new Vector2(1432, 1096), new Vector2(1432, 984), new Vector2(1576, 984), new Vector2(1576, 1128), new Vector2(1544, 1128), new Vector2(1544, 1000), new Vector2(1464, 1000), new Vector2(1464, 1128), new Vector2(1320, 1128) })}); + // _polygonDataList.Add(new NavigationPolygonData(){Type = NavigationPolygonType.Out, Points = new List(new []{ new Vector2(712, 1432), new Vector2(984, 1432), new Vector2(984, 1592), new Vector2(712, 1592) })}); // } /// @@ -708,10 +702,11 @@ } } + //创建导航区域 private NavigationRegion2D CreateNavigationRegion(Node2D navigationRoot, NavigationPolygonData polygonData) { var polygon = new NavigationPolygon(); - polygon.AddOutline(polygonData.ConvertPointsToVector2Array()); + polygon.AddOutline(polygonData.GetPoints()); polygon.MakePolygonsFromOutlines(); var navigationPolygon = new NavigationRegion2D(); navigationPolygon.Name = "NavigationRegion" + (navigationRoot.GetChildCount() + 1); @@ -727,6 +722,17 @@ { return _polygonDataList.ToArray(); } + + /// + /// 设置导航网格数据 + /// + /// + public void SetPolygonData(List list) + { + _polygonDataList.Clear(); + _polygonDataList.AddRange(list); + _generateNavigationResult = new GenerateNavigationResult(true); + } /// /// 获取连接门导航数据, 必须要调用 AutoFillRoomTile() 函数才有数据 @@ -768,7 +774,7 @@ { var polygonData = new NavigationPolygonData(); polygonData.Type = NavigationPolygonType.Out; - var points = polygonData.Points; + var points = new List(); // 0:右, 1:下, 2:左, 3:上 var dir = 0; var offset = new Vector2(size.X * 0.5f, size.Y * 0.5f); @@ -791,10 +797,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempJ--; @@ -804,12 +811,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -824,10 +832,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempJ++; @@ -845,10 +854,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempI++; @@ -858,12 +868,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -878,11 +889,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempI--; @@ -900,11 +912,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempJ++; @@ -914,13 +927,14 @@ { if (points.Count == 0) { - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -935,11 +949,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempJ--; @@ -957,11 +972,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempI--; @@ -971,12 +987,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -991,10 +1008,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempI++; @@ -1012,7 +1030,7 @@ { var polygonData = new NavigationPolygonData(); polygonData.Type = NavigationPolygonType.In; - var points = polygonData.Points; + var points = new List(); // 0:右, 1:下, 2:左, 3:上 var dir = 0; var offset = new Vector2(size.X * 0.5f, size.Y * 0.5f); @@ -1036,11 +1054,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempJ++; @@ -1050,13 +1069,14 @@ { if (points.Count == 0) { - //points.Add(new SerializeVector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -1071,11 +1091,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempJ--; @@ -1093,10 +1114,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempI--; @@ -1106,12 +1128,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -1126,11 +1149,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempI++; @@ -1148,10 +1172,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempJ--; @@ -1161,12 +1186,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -1181,10 +1207,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempJ++; @@ -1202,11 +1229,12 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - //points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); + //points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y * 2)); PutUsePoint(pos); tempI++; @@ -1216,12 +1244,13 @@ { if (points.Count == 0) { - points.Add(new SerializeVector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2((tempI - 1) * size.X + offset.X, tempJ * size.Y + offset.Y)); } var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } @@ -1236,10 +1265,11 @@ var pos = new Vector2I(tempI, tempJ); if (points.Count > 1 && pos == startPos) { + polygonData.SetPoints(points.ToArray()); return polygonData; } - points.Add(new SerializeVector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); + points.Add(new Vector2(tempI * size.X + offset.X, tempJ * size.Y + offset.Y)); PutUsePoint(pos); tempI--; diff --git a/DungeonShooting_Godot/src/framework/map/data/NavigationPolygonData.cs b/DungeonShooting_Godot/src/framework/map/data/NavigationPolygonData.cs index 5273a1e..bc142da 100644 --- a/DungeonShooting_Godot/src/framework/map/data/NavigationPolygonData.cs +++ b/DungeonShooting_Godot/src/framework/map/data/NavigationPolygonData.cs @@ -23,59 +23,59 @@ /// /// 导航轮廓类型 /// - [JsonInclude] public NavigationPolygonType Type; + [JsonInclude] + public NavigationPolygonType Type; /// - /// 多边形的顶点, 单位: 像素 + /// 多边形的顶点, 两个为一组, 单位: 像素, 需要获取转为 Vector2[] 的值请调用 GetPoints() 函数 /// - [JsonInclude] public List Points = new List(); + [JsonInclude] + public List Points; + private Vector2[] _pointVector2Array; + public NavigationPolygonData() { } - public NavigationPolygonData(NavigationPolygonType type, List points) + public NavigationPolygonData(NavigationPolygonType type) { Type = type; - Points = points; - } - - /// - /// 将 Points 字段转为 Vector2[] 类型数据并返回 - /// - public Vector2[] ConvertPointsToVector2Array() - { - if (Points == null) - { - return null; - } - - var array = new Vector2[Points.Count]; - for (var i = 0; i < Points.Count; i++) - { - array[i] = Points[i].AsVector2(); - } - - return array; } /// - /// 将 Points 字段转为 Vector2I[] 类型数据并返回 + /// 读取所有的坐标点 /// - public Vector2I[] ConvertPointsToVector2IArray() + public Vector2[] GetPoints() { - if (Points == null) + if (_pointVector2Array == null) { - return null; + if (Points == null) + { + return null; + } + + _pointVector2Array = new Vector2[Points.Count / 2]; + for (var i = 0; i < Points.Count; i += 2) + { + _pointVector2Array[i / 2] = new Vector2(Points[i], Points[i + 1]); + } } - var array = new Vector2I[Points.Count]; - for (var i = 0; i < Points.Count; i++) - { - array[i] = Points[i].AsVector2I(); - } - - return array; + return _pointVector2Array; } + /// + /// 设置所有的坐标点 + /// + public void SetPoints(Vector2[] array) + { + _pointVector2Array = array; + Points = new List(); + foreach (var pos in array) + { + Points.Add(pos.X); + Points.Add(pos.Y); + } + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/room/DungeonManager.cs b/DungeonShooting_Godot/src/game/room/DungeonManager.cs index d02378d..78f1601 100644 --- a/DungeonShooting_Godot/src/game/room/DungeonManager.cs +++ b/DungeonShooting_Godot/src/game/room/DungeonManager.cs @@ -232,22 +232,18 @@ for (var i = 0; i < polygonArray.Length; i++) { var navigationPolygonData = polygonArray[i]; - var polygonPointArray = navigationPolygonData.ConvertPointsToVector2Array(); + var polygonPointArray = navigationPolygonData.GetPoints(); //这里的位置需要加上房间位置 for (var j = 0; j < polygonPointArray.Length; j++) { polygonPointArray[j] = polygonPointArray[j] + roomInfo.GetWorldPosition() - offset; } polygon.AddOutline(polygonPointArray); - - var points = new List(); - for (var j = 0; j < polygonPointArray.Length; j++) - { - points.Add(new SerializeVector2(polygonPointArray[j])); - } - + //存入汇总列表 - _roomStaticNavigationList.Add(new NavigationPolygonData(navigationPolygonData.Type, points)); + var polygonData = new NavigationPolygonData(navigationPolygonData.Type); + polygonData.SetPoints(polygonPointArray); + _roomStaticNavigationList.Add(polygonData); } polygon.MakePolygonsFromOutlines(); var navigationPolygon = new NavigationRegion2D(); diff --git a/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMap.cs b/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMap.cs index f42c4e7..8920dc7 100644 --- a/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMap.cs +++ b/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMap.cs @@ -73,6 +73,8 @@ private Vector2I _checkTerrainErrorPosition = Vector2I.Zero; //是否执行生成地形成功 private bool _isGenerateTerrain = false; + + private bool _initLayer = false; //--------- 配置数据 ------------- private int _sourceId = 0; @@ -83,20 +85,7 @@ public override void _Ready() { - //初始化层级数据 - AddLayer(CustomFloorLayer); - SetLayerZIndex(CustomFloorLayer, CustomFloorLayer); - AddLayer(AutoMiddleLayer); - SetLayerZIndex(AutoMiddleLayer, AutoMiddleLayer); - AddLayer(CustomMiddleLayer); - SetLayerZIndex(CustomMiddleLayer, CustomMiddleLayer); - AddLayer(AutoTopLayer); - SetLayerZIndex(AutoTopLayer, AutoTopLayer); - AddLayer(CustomTopLayer); - SetLayerZIndex(CustomTopLayer, CustomTopLayer); - - _dungeonTileMap = new DungeonTileMap(this); - _dungeonTileMap.SetFloorAtlasCoords(new List(new []{ _autoTileConfig.Floor.AutoTileCoord })); + InitLayer(); } public override void _Process(double delta) @@ -281,53 +270,98 @@ if (eventKey.Pressed && eventKey.Keycode == Key.M) { GD.Print("测试保存地牢房间数据..."); - var tileInfo = new DungeonTileInfo(); - tileInfo.NavigationList = _dungeonTileMap.GetPolygonData().ToList(); - tileInfo.Floor = new List(); - tileInfo.Middle = new List(); - tileInfo.Top = new List(); - - var floor = GetUsedCellsById(AutoFloorLayer, _sourceId); - foreach (var pos in floor) - { - var atlasCoords = GetCellAtlasCoords(AutoFloorLayer, pos); - tileInfo.Floor.Add(pos.X); - tileInfo.Floor.Add(pos.Y); - tileInfo.Floor.Add(_sourceId); - tileInfo.Floor.Add(atlasCoords.X); - tileInfo.Floor.Add(atlasCoords.Y); - } - - var middle = GetUsedCellsById(AutoMiddleLayer, _sourceId); - foreach (var pos in middle) - { - var atlasCoords = GetCellAtlasCoords(AutoMiddleLayer, pos); - tileInfo.Middle.Add(pos.X); - tileInfo.Middle.Add(pos.Y); - tileInfo.Middle.Add(_sourceId); - tileInfo.Middle.Add(atlasCoords.X); - tileInfo.Middle.Add(atlasCoords.Y); - } - - var top = GetUsedCellsById(AutoTopLayer, _sourceId); - foreach (var pos in top) - { - var atlasCoords = GetCellAtlasCoords(AutoTopLayer, pos); - tileInfo.Top.Add(pos.X); - tileInfo.Top.Add(pos.Y); - tileInfo.Top.Add(_sourceId); - tileInfo.Top.Add(atlasCoords.X); - tileInfo.Top.Add(atlasCoords.Y); - } - - // var config = new JsonSerializerOptions(); - // config.WriteIndented = true; - var jsonStr = JsonSerializer.Serialize(tileInfo); - File.WriteAllText("D:\\test.json", jsonStr); + SaveTile(); } } } + //将指定层数据存入list中 + private void PushLayerDataToList(int layer, int sourceId, List list) + { + var layerArray = GetUsedCellsById(layer, sourceId); + foreach (var pos in layerArray) + { + var atlasCoords = GetCellAtlasCoords(layer, pos); + list.Add(pos.X); + list.Add(pos.Y); + list.Add(_sourceId); + list.Add(atlasCoords.X); + list.Add(atlasCoords.Y); + } + } + + private void SetLayerDataFromList(int layer, List list) + { + for (var i = 0; i < list.Count; i += 5) + { + var pos = new Vector2I(list[i], list[i + 1]); + var sourceId = list[i + 2]; + var atlasCoords = new Vector2I(list[i + 3], list[i + 4]); + SetCell(layer, pos, sourceId, atlasCoords); + if (layer == AutoFloorLayer) + { + _autoCellLayerGrid.Set(pos, true); + } + } + } + + //保存地牢 + private void SaveTile() + { + var tileInfo = new DungeonTileInfo(); + tileInfo.NavigationList = _dungeonTileMap.GetPolygonData().ToList(); + tileInfo.Floor = new List(); + tileInfo.Middle = new List(); + tileInfo.Top = new List(); + + PushLayerDataToList(AutoFloorLayer, _sourceId, tileInfo.Floor); + PushLayerDataToList(AutoMiddleLayer, _sourceId, tileInfo.Middle); + PushLayerDataToList(AutoTopLayer, _sourceId, tileInfo.Top); + + var jsonStr = JsonSerializer.Serialize(tileInfo); + File.WriteAllText("D:\\test.json", jsonStr); + } + + //加载地牢 + public void LoadTile() + { + InitLayer(); + var text = ResourceManager.LoadText("D:\\test.json"); + var tileInfo = JsonSerializer.Deserialize(text); + + //地块数据 + SetLayerDataFromList(AutoFloorLayer, tileInfo.Floor); + SetLayerDataFromList(AutoMiddleLayer, tileInfo.Middle); + SetLayerDataFromList(AutoTopLayer, tileInfo.Top); + + //导航网格数据 + _dungeonTileMap.SetPolygonData(tileInfo.NavigationList); + } + + private void InitLayer() + { + if (_initLayer) + { + return; + } + + _initLayer = true; + //初始化层级数据 + AddLayer(CustomFloorLayer); + SetLayerZIndex(CustomFloorLayer, CustomFloorLayer); + AddLayer(AutoMiddleLayer); + SetLayerZIndex(AutoMiddleLayer, AutoMiddleLayer); + AddLayer(CustomMiddleLayer); + SetLayerZIndex(CustomMiddleLayer, CustomMiddleLayer); + AddLayer(AutoTopLayer); + SetLayerZIndex(AutoTopLayer, AutoTopLayer); + AddLayer(CustomTopLayer); + SetLayerZIndex(CustomTopLayer, CustomTopLayer); + + _dungeonTileMap = new DungeonTileMap(this); + _dungeonTileMap.SetFloorAtlasCoords(new List(new []{ _autoTileConfig.Floor.AutoTileCoord })); + } + //缩小 private void Shrink() { diff --git a/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMapBar.cs b/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMapBar.cs index 72b9594..3ee15da 100644 --- a/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMapBar.cs +++ b/DungeonShooting_Godot/src/game/ui/mapEditor/TileView/EditorTileMapBar.cs @@ -12,6 +12,8 @@ _editorTileMap = editorTileMap; _editorPanel = editorPanel; _editorTileMap.Instance.MapEditorPanel = editorPanel; + //测试加载地牢 + _editorTileMap.Instance.LoadTile(); } public void OnShow()