Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / serialize / room / DungeonRoomGroup.cs
  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 : IClone<DungeonRoomGroup>
  11. {
  12. /// <summary>
  13. /// 组名称
  14. /// </summary>
  15. [JsonInclude]
  16. public string GroupName;
  17.  
  18. /// <summary>
  19. /// 图块集
  20. /// </summary>
  21. [JsonInclude]
  22. public string TileSet;
  23. /// <summary>
  24. /// 普通战斗房间, 进入该房间时会关上门, 并刷出若干波敌人, 消灭所有敌人后开门
  25. /// </summary>
  26. [JsonInclude]
  27. public List<DungeonRoomSplit> BattleList = new List<DungeonRoomSplit>();
  28.  
  29. /// <summary>
  30. /// 起始房间, 由上一层地牢的结束房间进入该房间, 每层包含一个起始房间
  31. /// </summary>
  32. [JsonInclude]
  33. public List<DungeonRoomSplit> InletList = new List<DungeonRoomSplit>();
  34.  
  35. /// <summary>
  36. /// 结束房间, 进入另一层地牢, 每层只是包含一个结束房间
  37. /// </summary>
  38. [JsonInclude]
  39. public List<DungeonRoomSplit> OutletList = new List<DungeonRoomSplit>();
  40.  
  41. /// <summary>
  42. /// boss战房间, 进入房间时会关上没, 刷出boss, 消灭boss后开门
  43. /// </summary>
  44. [JsonInclude]
  45. public List<DungeonRoomSplit> BossList = new List<DungeonRoomSplit>();
  46.  
  47. /// <summary>
  48. /// 奖励房间, 给予玩家武器或者道具奖励的房间
  49. /// </summary>
  50. [JsonInclude]
  51. public List<DungeonRoomSplit> RewardList = new List<DungeonRoomSplit>();
  52.  
  53. /// <summary>
  54. /// 商店, 玩家买卖道具装备的房间
  55. /// </summary>
  56. [JsonInclude]
  57. public List<DungeonRoomSplit> ShopList = new List<DungeonRoomSplit>();
  58.  
  59. /// <summary>
  60. /// 事件房间, 触发剧情或者解锁NPC的房间
  61. /// </summary>
  62. [JsonInclude]
  63. public List<DungeonRoomSplit> EventList = new List<DungeonRoomSplit>();
  64.  
  65. /// <summary>
  66. /// 组包住
  67. /// </summary>
  68. [JsonInclude]
  69. public string Remark;
  70. private bool _init = false;
  71. private Dictionary<DungeonRoomType, WeightRandom> _weightRandomMap;
  72. /// <summary>
  73. /// 获取所有房间数据
  74. /// </summary>
  75. public List<DungeonRoomSplit> GetAllRoomList()
  76. {
  77. var list = new List<DungeonRoomSplit>();
  78. list.AddRange(BattleList);
  79. list.AddRange(InletList);
  80. list.AddRange(OutletList);
  81. list.AddRange(BossList);
  82. list.AddRange(ShopList);
  83. list.AddRange(RewardList);
  84. list.AddRange(EventList);
  85. return list;
  86. }
  87.  
  88. /// <summary>
  89. /// 获取指定类型房间集合
  90. /// </summary>
  91. public List<DungeonRoomSplit> GetRoomList(DungeonRoomType roomType)
  92. {
  93. switch (roomType)
  94. {
  95. case DungeonRoomType.Battle: return BattleList;
  96. case DungeonRoomType.Inlet: return InletList;
  97. case DungeonRoomType.Outlet: return OutletList;
  98. case DungeonRoomType.Boss: return BossList;
  99. case DungeonRoomType.Reward: return RewardList;
  100. case DungeonRoomType.Shop: return ShopList;
  101. case DungeonRoomType.Event: return EventList;
  102. }
  103.  
  104. return null;
  105. }
  106.  
  107. /// <summary>
  108. /// 移除一个房间, 返回是否移除成功
  109. /// </summary>
  110. public bool RemoveRoom(DungeonRoomSplit roomSplit)
  111. {
  112. if (BattleList.Remove(roomSplit))
  113. {
  114. return true;
  115. }
  116. if (InletList.Remove(roomSplit))
  117. {
  118. return true;
  119. }
  120. if (OutletList.Remove(roomSplit))
  121. {
  122. return true;
  123. }
  124. if (BossList.Remove(roomSplit))
  125. {
  126. return true;
  127. }
  128. if (RewardList.Remove(roomSplit))
  129. {
  130. return true;
  131. }
  132. if (ShopList.Remove(roomSplit))
  133. {
  134. return true;
  135. }
  136. if (EventList.Remove(roomSplit))
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142.  
  143. /// <summary>
  144. /// 初始化权重处理
  145. /// </summary>
  146. public void InitWeight(SeedRandom random)
  147. {
  148. if (_init)
  149. {
  150. foreach (var keyValuePair in _weightRandomMap)
  151. {
  152. keyValuePair.Value.SetSeedRandom(random);
  153. }
  154.  
  155. return;
  156. }
  157.  
  158. _init = true;
  159. _weightRandomMap = new Dictionary<DungeonRoomType, WeightRandom>();
  160. foreach (var roomType in Enum.GetValues<DungeonRoomType>())
  161. {
  162. if (roomType == DungeonRoomType.None) continue;
  163. InitAdRewardWeight(roomType, random);
  164. }
  165. }
  166.  
  167. private void InitAdRewardWeight(DungeonRoomType roomType, SeedRandom random)
  168. {
  169. var dungeonRoomSplits = GetRoomList(roomType);
  170. var weightRandom = new WeightRandom(random);
  171. _weightRandomMap.Add(roomType, weightRandom);
  172.  
  173. var list = new List<int>();
  174. foreach (var roomSplit in dungeonRoomSplits)
  175. {
  176. list.Add(roomSplit.RoomInfo.Weight);
  177. }
  178. weightRandom.InitAdRewardWeight(list.ToArray());
  179. }
  180.  
  181. /// <summary>
  182. /// 根据房间类型和权重获取随机房间
  183. /// </summary>
  184. public DungeonRoomSplit GetRandomRoom(DungeonRoomType roomType)
  185. {
  186. if (!_init)
  187. {
  188. Debug.LogError("未调用DungeonRoomGroup.InitWeight()来初始化权重!");
  189. return null;
  190. }
  191.  
  192. if (_weightRandomMap.TryGetValue(roomType, out var weightRandom))
  193. {
  194. return GetRoomList(roomType)[weightRandom.GetRandomIndex()];
  195. }
  196.  
  197. return null;
  198. }
  199. public DungeonRoomGroup Clone()
  200. {
  201. var inst = new DungeonRoomGroup();
  202. inst.GroupName = GroupName;
  203. inst.TileSet = TileSet;
  204. inst.BattleList.AddRange(BattleList);
  205. inst.InletList.AddRange(InletList);
  206. inst.OutletList.AddRange(OutletList);
  207. inst.BossList.AddRange(BossList);
  208. inst.ShopList.AddRange(ShopList);
  209. inst.RewardList.AddRange(RewardList);
  210. inst.EventList.AddRange(EventList);
  211. return inst;
  212. }
  213. }