SafetyAreaEightNeighbourHelper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SafetyAreaEightNeighbourHelper
  6. {
  7. private static List<int> edgeVertexIndexList = new List<int>();
  8. private static Dictionary<int, int> eightNeighboursDic = new Dictionary<int, int>()
  9. {
  10. { 0, 6},
  11. { 1, 6},
  12. { 2, 0},
  13. { 3, 0},
  14. { 4, 2},
  15. { 5, 2},
  16. { 6, 4},
  17. { 7, 4}
  18. };
  19. /// <summary>
  20. /// 1 2 3
  21. /// 0 x 4
  22. /// 7 6 5
  23. /// </summary>
  24. private static Dictionary<int, Func<int, int>> eightNeighboursIndexCaculatorDic = new Dictionary<int, Func<int, int>>()
  25. {
  26. { 0, (index) => { return index - 1; } },
  27. { 1, (index) => { return index - 1 + PlayAreaConstant.GRID_SIZE + 1 ; } },
  28. { 2, (index) => { return index + PlayAreaConstant.GRID_SIZE + 1; } },
  29. { 3, (index) => { return index + 1 + PlayAreaConstant.GRID_SIZE + 1; } },
  30. { 4, (index) => { return index + 1; } },
  31. { 5, (index) => { return index + 1 - (PlayAreaConstant.GRID_SIZE + 1); } },
  32. { 6, (index) => { return index - (PlayAreaConstant.GRID_SIZE + 1); } },
  33. { 7, (index) => { return index - 1 - (PlayAreaConstant.GRID_SIZE + 1); } }
  34. };
  35. public static void EightNeighbours(int startCaculateIndex, Func<int, bool> conditionFunc, Action<List<int>> onGetEdgeCallback)
  36. {
  37. int firstRedColorIndex = FindFirstConditionIndex(startCaculateIndex, conditionFunc);
  38. edgeVertexIndexList.Clear();
  39. loopCount = 0;
  40. EightNeighboursRecursion(0, firstRedColorIndex, conditionFunc, onGetEdgeCallback);
  41. }
  42. private static int loopCount = 0;
  43. private static void EightNeighboursRecursion(int neighbourIndex, int vertexIndex, Func<int, bool> condition, Action<List<int>> onGetEdgeCallback)
  44. {
  45. loopCount++;
  46. if (loopCount > 5000)
  47. {
  48. Debug.LogError("Too much loop");
  49. onGetEdgeCallback?.Invoke(edgeVertexIndexList);
  50. return;
  51. }
  52. Func<int, int> checkIndexFunc = eightNeighboursIndexCaculatorDic[neighbourIndex];
  53. int checkIndex = checkIndexFunc(vertexIndex);
  54. if (checkIndex >= 0 && checkIndex <= ((PlayAreaConstant.GRID_SIZE + 1) * (PlayAreaConstant.GRID_SIZE + 1) - 1) && condition(checkIndex))
  55. {
  56. if (edgeVertexIndexList.Contains(checkIndex))
  57. {
  58. //for (int i = 0; i < edgeVertexIndexList.Count; i++)
  59. //{
  60. // colors[edgeVertexIndexList[i]] = Color.yellow;
  61. //}
  62. //mesh.colors = colors;
  63. edgeVertexIndexList.Add(checkIndex);
  64. onGetEdgeCallback?.Invoke(edgeVertexIndexList);
  65. Debug.Log("LoopCount:" + loopCount);
  66. return;
  67. }
  68. else
  69. {
  70. edgeVertexIndexList.Add(checkIndex);
  71. int nextNeighbourIndex = eightNeighboursDic[neighbourIndex];
  72. EightNeighboursRecursion(nextNeighbourIndex, checkIndex, condition, onGetEdgeCallback);
  73. }
  74. }
  75. else
  76. {
  77. EightNeighboursRecursion((neighbourIndex + 1) % 8, vertexIndex, condition, onGetEdgeCallback);
  78. }
  79. }
  80. private static int FindFirstConditionIndex(int index, Func<int, bool> conditionFunc)
  81. {
  82. int rowIndex = index / (PlayAreaConstant.GRID_SIZE + 1);
  83. int startIndex = rowIndex * (PlayAreaConstant.GRID_SIZE + 1);
  84. if (index == startIndex)
  85. {
  86. return startIndex;
  87. }
  88. int currentIndex = 0;
  89. for (int i = 0; i < (PlayAreaConstant.GRID_SIZE + 1); i++)
  90. {
  91. currentIndex = startIndex + i;
  92. if (conditionFunc(currentIndex))
  93. {
  94. break;
  95. }
  96. }
  97. return currentIndex;
  98. }
  99. }