Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / map / RandomPool.cs
@小李xl 小李xl on 23 Mar 2024 3 KB 更改宝箱预设初始化流程
  1.  
  2. using System.Collections.Generic;
  3. using Config;
  4. using Godot;
  5.  
  6. public class RandomPool
  7. {
  8. /// <summary>
  9. /// 随机数生成器
  10. /// </summary>
  11. public SeedRandom Random { get; }
  12. /// <summary>
  13. /// 所属世界
  14. /// </summary>
  15. public World World { get; }
  16. public RandomPool(World world)
  17. {
  18. World = world;
  19. Random = world.Random;
  20. }
  21.  
  22. /// <summary>
  23. /// 获取随机武器
  24. /// </summary>
  25. public ExcelConfig.ActivityBase GetRandomWeapon()
  26. {
  27. return Random.RandomChoose(PreinstallMarkManager.GetMarkConfigsByType(ActivityType.Weapon));
  28. }
  29.  
  30. /// <summary>
  31. /// 获取随机敌人
  32. /// </summary>
  33. public ExcelConfig.ActivityBase GetRandomEnemy()
  34. {
  35. return Random.RandomChoose(PreinstallMarkManager.GetMarkConfigsByType(ActivityType.Enemy));
  36. }
  37.  
  38. /// <summary>
  39. /// 获取随机道具
  40. /// </summary>
  41. public ExcelConfig.ActivityBase GetRandomProp()
  42. {
  43. return Random.RandomChoose(PreinstallMarkManager.GetMarkConfigsByType(ActivityType.Prop));
  44. }
  45.  
  46. /// <summary>
  47. /// 填充自动波次数据
  48. /// </summary>
  49. public void FillAutoWave(RoomPreinstall preinstall)
  50. {
  51. if (preinstall.RoomInfo.RoomType == DungeonRoomType.Battle)
  52. {
  53. FillBattleRoom(preinstall);
  54. }
  55. }
  56.  
  57. //填充战斗房间
  58. private void FillBattleRoom(RoomPreinstall preinstall)
  59. {
  60. var count = World.Random.RandomRangeInt(3, 10);
  61. var tileInfo = preinstall.RoomInfo.RoomSplit.TileInfo;
  62. var serializeVector2s = tileInfo.NavigationVertices;
  63. var vertices = new List<Vector2>();
  64. foreach (var sv2 in serializeVector2s)
  65. {
  66. vertices.Add(sv2.AsVector2());
  67. }
  68. var positionArray = World.Random.GetRandomPositionInPolygon(vertices, tileInfo.NavigationPolygon, count);
  69. var arr = new ActivityType[] { ActivityType.Enemy, ActivityType.Weapon, ActivityType.Prop };
  70. var weight = new int[] { 15, 2, 1 };
  71. for (var i = 0; i < count; i++)
  72. {
  73. var tempWave = preinstall.GetOrCreateWave(World.Random.RandomRangeInt(0, 2));
  74. var index = World.Random.RandomWeight(weight);
  75. var activityType = arr[index];
  76. //创建标记
  77. var mark = ActivityMark.CreateMark(activityType, i * 0.3f, preinstall.RoomInfo.ToGlobalPosition(positionArray[i]));
  78. if (activityType == ActivityType.Enemy) //敌人
  79. {
  80. mark.Id = GetRandomEnemy().Id;
  81. mark.Attr.Add("Face", "0");
  82. mark.DerivedAttr = new Dictionary<string, string>();
  83. mark.DerivedAttr.Add("Face", World.Random.RandomChoose((int)FaceDirection.Left, (int)FaceDirection.Right).ToString()); //链朝向
  84. if (World.Random.RandomBoolean(0.8f)) //手持武器
  85. {
  86. var weapon = GetRandomWeapon();
  87. var weaponAttribute = Weapon.GetWeaponAttribute(weapon.Id);
  88. mark.Attr.Add("Weapon", weapon.Id); //武器id
  89. mark.Attr.Add("CurrAmmon", weaponAttribute.AmmoCapacity.ToString()); //弹夹弹药量
  90. mark.Attr.Add("ResidueAmmo", weaponAttribute.AmmoCapacity.ToString()); //剩余弹药量
  91. }
  92. }
  93. else if (activityType == ActivityType.Weapon) //武器
  94. {
  95. mark.Id = GetRandomWeapon().Id;
  96. }
  97. else if (activityType == ActivityType.Prop) //道具
  98. {
  99. mark.Id = GetRandomProp().Id;
  100. }
  101. tempWave.Add(mark);
  102. }
  103. }
  104. }