Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / data / DungeonRoomGroup.cs
@小李xl 小李xl on 23 Jul 2023 4 KB 创建地牢组和地牢房间
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.Json.Serialization;
  5. using Godot;
  6.  
  7. /// <summary>
  8. /// 房间组数据
  9. /// </summary>
  10. public class DungeonRoomGroup
  11. {
  12. /// <summary>
  13. /// 组名称
  14. /// </summary>
  15. [JsonInclude]
  16. public string GroupName;
  17. /// <summary>
  18. /// 普通战斗房间, 进入该房间时会关上门, 并刷出若干波敌人, 消灭所有敌人后开门
  19. /// </summary>
  20. [JsonInclude]
  21. public List<DungeonRoomSplit> BattleList = new List<DungeonRoomSplit>();
  22.  
  23. /// <summary>
  24. /// 起始房间, 由上一层地牢的结束房间进入该房间, 每层包含一个起始房间
  25. /// </summary>
  26. [JsonInclude]
  27. public List<DungeonRoomSplit> InletList = new List<DungeonRoomSplit>();
  28.  
  29. /// <summary>
  30. /// 结束房间, 进入另一层地牢, 每层只是包含一个结束房间
  31. /// </summary>
  32. [JsonInclude]
  33. public List<DungeonRoomSplit> OutletList = new List<DungeonRoomSplit>();
  34.  
  35. /// <summary>
  36. /// boss战房间, 进入房间时会关上没, 刷出boss, 消灭boss后开门
  37. /// </summary>
  38. [JsonInclude]
  39. public List<DungeonRoomSplit> BossList = new List<DungeonRoomSplit>();
  40.  
  41. /// <summary>
  42. /// 奖励房间, 给予玩家武器或者道具奖励的房间
  43. /// </summary>
  44. [JsonInclude]
  45. public List<DungeonRoomSplit> RewardList = new List<DungeonRoomSplit>();
  46.  
  47. /// <summary>
  48. /// 商店, 玩家买卖道具装备的房间
  49. /// </summary>
  50. [JsonInclude]
  51. public List<DungeonRoomSplit> ShopList = new List<DungeonRoomSplit>();
  52.  
  53. /// <summary>
  54. /// 事件房间, 触发剧情或者解锁NPC的房间
  55. /// </summary>
  56. [JsonInclude]
  57. public List<DungeonRoomSplit> EventList = new List<DungeonRoomSplit>();
  58.  
  59. /// <summary>
  60. /// 组包住
  61. /// </summary>
  62. [JsonInclude]
  63. public string Remark;
  64. private bool _init = false;
  65. private Dictionary<DungeonRoomType, WeightRandom> _weightRandomMap;
  66.  
  67. /// <summary>
  68. /// 获取所有房间数据
  69. /// </summary>
  70. public List<DungeonRoomSplit> GetAllRoomList()
  71. {
  72. var list = new List<DungeonRoomSplit>();
  73. list.AddRange(BattleList);
  74. list.AddRange(InletList);
  75. list.AddRange(OutletList);
  76. list.AddRange(BossList);
  77. list.AddRange(ShopList);
  78. list.AddRange(RewardList);
  79. list.AddRange(ShopList);
  80. list.AddRange(EventList);
  81. return list;
  82. }
  83.  
  84. /// <summary>
  85. /// 获取指定类型房间集合
  86. /// </summary>
  87. public List<DungeonRoomSplit> GetRoomList(DungeonRoomType roomType)
  88. {
  89. switch (roomType)
  90. {
  91. case DungeonRoomType.Battle: return BattleList;
  92. case DungeonRoomType.Inlet: return InletList;
  93. case DungeonRoomType.Outlet: return OutletList;
  94. case DungeonRoomType.Boss: return BossList;
  95. case DungeonRoomType.Reward: return RewardList;
  96. case DungeonRoomType.Shop: return ShopList;
  97. case DungeonRoomType.Event: return EventList;
  98. }
  99.  
  100. return null;
  101. }
  102.  
  103. /// <summary>
  104. /// 初始化权重处理
  105. /// </summary>
  106. public void InitWeight(SeedRandom random)
  107. {
  108. if (_init)
  109. {
  110. return;
  111. }
  112.  
  113. _init = true;
  114. _weightRandomMap = new Dictionary<DungeonRoomType, WeightRandom>();
  115. foreach (var roomType in Enum.GetValues<DungeonRoomType>())
  116. {
  117. InitAdRewardWeight(roomType, random);
  118. }
  119. }
  120.  
  121. private void InitAdRewardWeight(DungeonRoomType roomType, SeedRandom random)
  122. {
  123. var dungeonRoomSplits = GetRoomList(roomType);
  124. var weightRandom = new WeightRandom(random);
  125. _weightRandomMap.Add(roomType, weightRandom);
  126.  
  127. var list = new List<int>();
  128. foreach (var roomSplit in dungeonRoomSplits)
  129. {
  130. list.Add(roomSplit.RoomInfo.Weight);
  131. }
  132. weightRandom.InitAdRewardWeight(list.ToArray());
  133. }
  134.  
  135. /// <summary>
  136. /// 根据房间类型和权重获取随机房间
  137. /// </summary>
  138. public DungeonRoomSplit GetRandomRoom(DungeonRoomType roomType)
  139. {
  140. if (!_init)
  141. {
  142. GD.PrintErr("未调用DungeonRoomGroup.InitWeight()来初始化权重!");
  143. return null;
  144. }
  145.  
  146. if (_weightRandomMap.TryGetValue(roomType, out var weightRandom))
  147. {
  148. return GetRoomList(roomType)[weightRandom.GetRandomIndex()];
  149. }
  150.  
  151. return null;
  152. }
  153. }