diff --git a/DungeonShooting_Godot/src/framework/common/WeightRandom.cs b/DungeonShooting_Godot/src/framework/common/WeightRandom.cs index 2c432cc..161e1f1 100644 --- a/DungeonShooting_Godot/src/framework/common/WeightRandom.cs +++ b/DungeonShooting_Godot/src/framework/common/WeightRandom.cs @@ -14,6 +14,14 @@ { _random = random; } + + /// <summary> + /// 设置随机数生成器 + /// </summary> + public void SetSeedRandom(SeedRandom random) + { + _random = random; + } /// <summary> /// 初始化权重列表 diff --git a/DungeonShooting_Godot/src/framework/map/DungeonConfig.cs b/DungeonShooting_Godot/src/framework/map/DungeonConfig.cs index 2fc4437..8781e28 100644 --- a/DungeonShooting_Godot/src/framework/map/DungeonConfig.cs +++ b/DungeonShooting_Godot/src/framework/map/DungeonConfig.cs @@ -102,7 +102,7 @@ /// </summary> public bool HasDesignatedRoom => DesignatedRoom != null && DesignatedRoom.Count > 0; /// <summary> - /// 指定房间类型 + /// 指定预设的房间类型 /// </summary> public DungeonRoomType DesignatedType; /// <summary> diff --git a/DungeonShooting_Godot/src/framework/map/DungeonGenerator.cs b/DungeonShooting_Godot/src/framework/map/DungeonGenerator.cs index feb8ca2..3f82e5d 100644 --- a/DungeonShooting_Godot/src/framework/map/DungeonGenerator.cs +++ b/DungeonShooting_Godot/src/framework/map/DungeonGenerator.cs @@ -163,78 +163,148 @@ //当前尝试次数 var currTryCount = 0; - //如果房间数量不够, 就一直生成 - while (!_rule.CanOverGenerator()) + if (Config.HasDesignatedRoom) //指定房间列表 { - if (_nextRoomType == DungeonRoomType.None) + for (var i = 0; i < Config.DesignatedRoom.Count; i++) { - _nextRoomType = _rule.GetNextRoomType(prevRoomInfo); - } - var nextRoomType = _nextRoomType; - - //上一个房间 - var tempPrevRoomInfo = _rule.GetConnectPrevRoom(prevRoomInfo, nextRoomType); - - //生成下一个房间 - var errorCode = GenerateRoom(tempPrevRoomInfo, nextRoomType, out var nextRoom); - if (errorCode == GenerateRoomErrorCode.NoError) //生成成功 - { - _failCount = 0; - RoomInfos.Add(nextRoom); - if (nextRoomType == DungeonRoomType.Inlet) + var roomSplit = Config.DesignatedRoom[i]; + var nextRoomType = roomSplit.RoomInfo.RoomType; + var errorCode = GenerateRoom(prevRoomInfo, roomSplit.RoomInfo.RoomType, out var nextRoom); + if (errorCode == GenerateRoomErrorCode.NoError) //生成成功 { - StartRoomInfo = nextRoom; - } - else if (nextRoomType == DungeonRoomType.Boss) //boss房间 - { - BossRoomInfos.Add(nextRoom); - } - else if (nextRoomType == DungeonRoomType.Outlet) - { - EndRoomInfos.Add(nextRoom); - } - else if (nextRoomType == DungeonRoomType.Battle) - { - BattleRoomInfos.Add(nextRoom); - } - else if (nextRoomType == DungeonRoomType.Reward) - { - RewardRoomInfos.Add(nextRoom); - } - else if (nextRoomType == DungeonRoomType.Shop) - { - ShopRoomInfos.Add(nextRoom); - } - prevRoomInfo = nextRoom; - _rule.GenerateRoomSuccess(tempPrevRoomInfo, nextRoom); - _nextRoomType = _rule.GetNextRoomType(nextRoom); - } - else //生成失败 - { - _rule.GenerateRoomFail(tempPrevRoomInfo, nextRoomType); - - //Debug.Log("生成第" + (_count + 1) + "个房间失败! 失败原因: " + errorCode); - if (errorCode == GenerateRoomErrorCode.OutArea) - { - _failCount++; - Debug.Log("超出区域失败次数: " + _failCount); - if (_failCount >= _maxFailCount) + _failCount = 0; + RoomInfos.Add(nextRoom); + if (nextRoomType == DungeonRoomType.Inlet) { - //_enableLimitRange = false; - _failCount = 0; - _rangeX += 50; - _rangeY += 50; - Debug.Log("生成房间失败次数过多, 增大区域"); + StartRoomInfo = nextRoom; } + else if (nextRoomType == DungeonRoomType.Boss) //boss房间 + { + BossRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Outlet) + { + EndRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Battle) + { + BattleRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Reward) + { + RewardRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Shop) + { + ShopRoomInfos.Add(nextRoom); + } + + prevRoomInfo = nextRoom; } - currTryCount++; - if (currTryCount >= maxTryCount) + else //生成失败 { - return false; + //Debug.Log("生成第" + (_count + 1) + "个房间失败! 失败原因: " + errorCode); + if (errorCode == GenerateRoomErrorCode.OutArea) + { + _failCount++; + Debug.Log("超出区域失败次数: " + _failCount); + if (_failCount >= _maxFailCount) + { + //_enableLimitRange = false; + _failCount = 0; + _rangeX += 50; + _rangeY += 50; + Debug.Log("生成房间失败次数过多, 增大区域"); + } + } + + currTryCount++; + if (currTryCount >= maxTryCount) + { + return false; + } + + i--; } } } - + else //正常随机生成 + { + //如果房间数量不够, 就一直生成 + while (!_rule.CanOverGenerator()) + { + if (_nextRoomType == DungeonRoomType.None) + { + _nextRoomType = _rule.GetNextRoomType(prevRoomInfo); + } + + var nextRoomType = _nextRoomType; + + //上一个房间 + var tempPrevRoomInfo = _rule.GetConnectPrevRoom(prevRoomInfo, nextRoomType); + //生成下一个房间 + var errorCode = GenerateRoom(tempPrevRoomInfo, nextRoomType, out var nextRoom); + if (errorCode == GenerateRoomErrorCode.NoError) //生成成功 + { + _failCount = 0; + RoomInfos.Add(nextRoom); + if (nextRoomType == DungeonRoomType.Inlet) + { + StartRoomInfo = nextRoom; + } + else if (nextRoomType == DungeonRoomType.Boss) //boss房间 + { + BossRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Outlet) + { + EndRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Battle) + { + BattleRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Reward) + { + RewardRoomInfos.Add(nextRoom); + } + else if (nextRoomType == DungeonRoomType.Shop) + { + ShopRoomInfos.Add(nextRoom); + } + + prevRoomInfo = nextRoom; + _rule.GenerateRoomSuccess(tempPrevRoomInfo, nextRoom); + _nextRoomType = _rule.GetNextRoomType(nextRoom); + } + else //生成失败 + { + _rule.GenerateRoomFail(tempPrevRoomInfo, nextRoomType); + + //Debug.Log("生成第" + (_count + 1) + "个房间失败! 失败原因: " + errorCode); + if (errorCode == GenerateRoomErrorCode.OutArea) + { + _failCount++; + Debug.Log("超出区域失败次数: " + _failCount); + if (_failCount >= _maxFailCount) + { + //_enableLimitRange = false; + _failCount = 0; + _rangeX += 50; + _rangeY += 50; + Debug.Log("生成房间失败次数过多, 增大区域"); + } + } + + currTryCount++; + if (currTryCount >= maxTryCount) + { + return false; + } + } + } + } + _roomGrid.Clear(); Debug.Log("房间总数: " + RoomInfos.Count); Debug.Log("尝试次数: " + currTryCount); @@ -251,9 +321,9 @@ // } DungeonRoomSplit roomSplit; - if (Config.HasDesignatedRoom && Config.DesignatedType == roomType) //执行指定了房间 + if (Config.HasDesignatedRoom && Config.DesignatedRoom[RoomInfos.Count] != null) //执行指定了房间 { - roomSplit = Random.RandomChoose(Config.DesignatedRoom); + roomSplit = Config.DesignatedRoom[RoomInfos.Count]; } else //没有指定房间 { diff --git a/DungeonShooting_Godot/src/framework/map/serialize/room/DungeonRoomGroup.cs b/DungeonShooting_Godot/src/framework/map/serialize/room/DungeonRoomGroup.cs index b6d55f2..5c47f06 100644 --- a/DungeonShooting_Godot/src/framework/map/serialize/room/DungeonRoomGroup.cs +++ b/DungeonShooting_Godot/src/framework/map/serialize/room/DungeonRoomGroup.cs @@ -151,6 +151,11 @@ { if (_init) { + foreach (var keyValuePair in _weightRandomMap) + { + keyValuePair.Value.SetSeedRandom(random); + } + return; } diff --git a/DungeonShooting_Godot/src/game/manager/EditorPlayManager.cs b/DungeonShooting_Godot/src/game/manager/EditorPlayManager.cs index 92581f6..79d0bf8 100644 --- a/DungeonShooting_Godot/src/game/manager/EditorPlayManager.cs +++ b/DungeonShooting_Godot/src/game/manager/EditorPlayManager.cs @@ -25,7 +25,39 @@ _config.GroupName = EditorTileMapManager.SelectDungeonGroup.GroupName; _config.DesignatedType = EditorTileMapManager.SelectRoom.RoomInfo.RoomType; _config.DesignatedRoom = new List<DungeonRoomSplit>(); - _config.DesignatedRoom.Add(EditorTileMapManager.SelectRoom); + + if (_config.DesignatedType == DungeonRoomType.Inlet) //初始房间 + { + var dungeonRoomGroup = GameApplication.Instance.RoomConfig[_config.GroupName]; + _config.DesignatedRoom.Add(EditorTileMapManager.SelectRoom); + //随机选择战斗房间 + for (var i = 0; i < 5; i++) + { + _config.DesignatedRoom.Add(Utils.Random.RandomChoose(dungeonRoomGroup.BattleList)); + } + //结束房间 + _config.DesignatedRoom.Add(Utils.Random.RandomChoose(dungeonRoomGroup.OutletList)); + } + else if (_config.DesignatedType == DungeonRoomType.Outlet) //结束房间 + { + var dungeonRoomGroup = GameApplication.Instance.RoomConfig[_config.GroupName]; + //随机选择初始房间 + _config.DesignatedRoom.Add(Utils.Random.RandomChoose(dungeonRoomGroup.InletList)); + _config.DesignatedRoom.Add(EditorTileMapManager.SelectRoom); + } + else //其他类型房间 + { + var dungeonRoomGroup = GameApplication.Instance.RoomConfig[_config.GroupName]; + //随机选择初始房间 + _config.DesignatedRoom.Add(Utils.Random.RandomChoose(dungeonRoomGroup.InletList)); + for (var i = 0; i < 5; i++) + { + _config.DesignatedRoom.Add(EditorTileMapManager.SelectRoom); + } + //结束房间 + _config.DesignatedRoom.Add(Utils.Random.RandomChoose(dungeonRoomGroup.OutletList)); + } + UiManager.Open_Loading(); GameApplication.Instance.DungeonManager.EditorPlayDungeon(prevUi, _config, () => {