Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. public class HexPerimeter
  6. {
  7. //Convert the cell perim to a list of world coords on the hex border around the cells
  8. public static List<Vector3> GetLinePositions(List<HexCell> cellPerimeter){
  9. var ret = new List<Vector3>();
  10.  
  11. HexDirection prevDir;
  12. HexDirection nextDir;
  13. HexCell firstHexCell = cellPerimeter[0];
  14. HexCell lastHexCell = cellPerimeter[cellPerimeter.Count - 1];
  15. HexCell nextHexCell = null;
  16. for(int p = 0; p < cellPerimeter.Count; p++){
  17. var cell = cellPerimeter[p];
  18. if(p > 0){
  19. lastHexCell = cellPerimeter[p - 1];
  20. }
  21.  
  22. prevDir = HexDirectionExtensions.CoordDirection(cell.coordinates, lastHexCell.coordinates);
  23.  
  24. if (p + 1 < cellPerimeter.Count) {
  25. nextHexCell = cellPerimeter[p + 1];
  26. }
  27. else {
  28. nextHexCell = firstHexCell;
  29. }
  30. nextDir = HexDirectionExtensions.CoordDirection(cell.coordinates, nextHexCell.coordinates);
  31.  
  32. var bendType = GetBendType(prevDir, nextDir);
  33.  
  34. var currentCorner = startingCornerMap[prevDir];
  35.  
  36. for(int i = 0; i < (int)bendType; i++){
  37. AddDir(ret, cell, currentCorner);
  38. currentCorner = currentCorner.Next();
  39. }
  40.  
  41. }
  42.  
  43. return ret;
  44. }
  45.  
  46. // the value signify how many vertices are used on the line going around the corner
  47. enum HexBendType{
  48. Inner = 1,
  49. Straight = 2,
  50. Outer = 3,
  51. Acute = 4,
  52. Uturn = 5
  53. }
  54.  
  55. //Maintaining the clockwise motion starting from the top right most cell, translate the previous/next tile pair into a bend direction
  56. static HexBendType GetBendType(HexDirection prevDir, HexDirection nextDir){
  57. if(prevDir == nextDir.Opposite()){
  58. return HexBendType.Straight;
  59. }
  60. if(prevDir == nextDir){
  61. return HexBendType.Uturn;
  62. }
  63.  
  64. //Not sure what the axiom is here that makes this works but it does!
  65. if(nextDir == prevDir.Next2()){
  66. return HexBendType.Inner;
  67. }
  68. if(nextDir == prevDir.Previous2()){
  69. return HexBendType.Outer;
  70. }
  71. if(nextDir == prevDir.Previous()){
  72. return HexBendType.Acute;
  73. }
  74.  
  75. //Shouldn't hit here
  76. Debug.LogWarning("Unknown bend type " + prevDir + " " + nextDir);
  77. return HexBendType.Straight;
  78. }
  79.  
  80. //For the perimeter line what hex corner do we need to start adding on
  81. static Dictionary<HexDirection, HexCornerDirection> startingCornerMap = new Dictionary<HexDirection, HexCornerDirection>(){
  82. {HexDirection.NE, HexCornerDirection.SE},
  83. {HexDirection.E, HexCornerDirection.S},
  84. {HexDirection.SE, HexCornerDirection.SW},
  85. {HexDirection.SW, HexCornerDirection.NW},
  86. {HexDirection.W, HexCornerDirection.N},
  87. {HexDirection.NW, HexCornerDirection.NE},
  88. };
  89.  
  90. static void AddDir(List<Vector3> ret, HexCell cell, HexCornerDirection dir){
  91. ret.Add( HexCoordinates.corners[dir] + cell.gameObject.transform.position );
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement