Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / serialize / DungeonRoomGroup.cs
@小李xl 小李xl on 2 Sep 2023 5 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 : IClone<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. private List<DungeonRoomSplit> _readyBattleList;
  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(EventList);
  80. return list;
  81. }
  82.  
  83. /// <summary>
  84. /// 获取指定类型房间集合
  85. /// </summary>
  86. public List<DungeonRoomSplit> GetRoomList(DungeonRoomType roomType)
  87. {
  88. switch (roomType)
  89. {
  90. case DungeonRoomType.Battle: return BattleList;
  91. case DungeonRoomType.Inlet: return InletList;
  92. case DungeonRoomType.Outlet: return OutletList;
  93. case DungeonRoomType.Boss: return BossList;
  94. case DungeonRoomType.Reward: return RewardList;
  95. case DungeonRoomType.Shop: return ShopList;
  96. case DungeonRoomType.Event: return EventList;
  97. }
  98.  
  99. return null;
  100. }
  101.  
  102. /// <summary>
  103. /// 移除一个房间, 返回是否移除成功
  104. /// </summary>
  105. public bool RemoveRoom(DungeonRoomSplit roomSplit)
  106. {
  107. if (BattleList.Remove(roomSplit))
  108. {
  109. return true;
  110. }
  111. if (InletList.Remove(roomSplit))
  112. {
  113. return true;
  114. }
  115. if (OutletList.Remove(roomSplit))
  116. {
  117. return true;
  118. }
  119. if (BossList.Remove(roomSplit))
  120. {
  121. return true;
  122. }
  123. if (RewardList.Remove(roomSplit))
  124. {
  125. return true;
  126. }
  127. if (ShopList.Remove(roomSplit))
  128. {
  129. return true;
  130. }
  131. if (EventList.Remove(roomSplit))
  132. {
  133. return true;
  134. }
  135. return false;
  136. }
  137.  
  138. /// <summary>
  139. /// 初始化权重处理
  140. /// </summary>
  141. public void InitWeight(SeedRandom random)
  142. {
  143. if (_init)
  144. {
  145. return;
  146. }
  147.  
  148. _init = true;
  149. _weightRandomMap = new Dictionary<DungeonRoomType, WeightRandom>();
  150. foreach (var roomType in Enum.GetValues<DungeonRoomType>())
  151. {
  152. InitAdRewardWeight(roomType, random);
  153. }
  154. }
  155.  
  156. private void InitAdRewardWeight(DungeonRoomType roomType, SeedRandom random)
  157. {
  158. var dungeonRoomSplits = GetRoomList(roomType);
  159. var weightRandom = new WeightRandom(random);
  160. _weightRandomMap.Add(roomType, weightRandom);
  161.  
  162. var list = new List<int>();
  163. foreach (var roomSplit in dungeonRoomSplits)
  164. {
  165. list.Add(roomSplit.RoomInfo.Weight);
  166. }
  167. weightRandom.InitAdRewardWeight(list.ToArray());
  168. }
  169.  
  170. /// <summary>
  171. /// 根据房间类型和权重获取随机房间
  172. /// </summary>
  173. public DungeonRoomSplit GetRandomRoom(DungeonRoomType roomType)
  174. {
  175. if (!_init)
  176. {
  177. GD.PrintErr("未调用DungeonRoomGroup.InitWeight()来初始化权重!");
  178. return null;
  179. }
  180.  
  181. if (_weightRandomMap.TryGetValue(roomType, out var weightRandom))
  182. {
  183. return GetRoomList(roomType)[weightRandom.GetRandomIndex()];
  184. }
  185.  
  186. return null;
  187. }
  188. public DungeonRoomGroup Clone()
  189. {
  190. var inst = new DungeonRoomGroup();
  191. inst.GroupName = GroupName;
  192. inst.BattleList.AddRange(BattleList);
  193. inst.InletList.AddRange(InletList);
  194. inst.OutletList.AddRange(OutletList);
  195. inst.BossList.AddRange(BossList);
  196. inst.ShopList.AddRange(ShopList);
  197. inst.RewardList.AddRange(RewardList);
  198. inst.EventList.AddRange(EventList);
  199. return inst;
  200. }
  201. }