|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Godot; |
| 4 | +using PJ; |
| 5 | + |
| 6 | +/* |
| 7 | + * RATING: 5 stars |
| 8 | + * Simple types |
| 9 | + * CODE REVIEW: 4/14/22 |
| 10 | + */ |
| 11 | +namespace PJ |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// 8-way direction of movement on a board/map |
| 15 | + /// </summary> |
| 16 | + public enum MapDirection |
| 17 | + { |
| 18 | + Northwest, |
| 19 | + North, |
| 20 | + Northeast, |
| 21 | + East, |
| 22 | + South, |
| 23 | + Southeast, |
| 24 | + Southwest, |
| 25 | + West |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Axis limits for map direction movement |
| 30 | + /// </summary> |
| 31 | + public enum MapDirectionAxisLimit |
| 32 | + { |
| 33 | + FourWay, |
| 34 | + |
| 35 | + EightWay |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// A rectangle in 2D space |
| 40 | + /// </summary> |
| 41 | + public struct Rect2I |
| 42 | + { |
| 43 | + public Vector2I position; |
| 44 | + public Vector2I size; |
| 45 | + |
| 46 | + public Rect2I(Vector2I position, Vector2I size) |
| 47 | + { |
| 48 | + this.position = position; |
| 49 | + this.size = size; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Axial direction of travel |
| 55 | + /// </summary> |
| 56 | + public enum AxialDirection |
| 57 | + { |
| 58 | + Right, |
| 59 | + Left |
| 60 | + } |
| 61 | + |
| 62 | + public enum AxialType |
| 63 | + { |
| 64 | + AxialAny, // Any axial tile that touches the origin tile |
| 65 | + AxialEdge // Any axial tile that has an edge touching the origin tile (no square diagonal) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +namespace PJ |
| 71 | +{ |
| 72 | + public static class Extensions_MapDirection |
| 73 | + { |
| 74 | + /// <summary> |
| 75 | + /// Returns the opposite direction |
| 76 | + /// </summary> |
| 77 | + public static MapDirection Opposite(this MapDirection direction) |
| 78 | + { |
| 79 | + switch (direction) |
| 80 | + { |
| 81 | + case MapDirection.Northwest: |
| 82 | + return MapDirection.Southeast; |
| 83 | + case MapDirection.North: |
| 84 | + return MapDirection.South; |
| 85 | + case MapDirection.Northeast: |
| 86 | + return MapDirection.Southwest; |
| 87 | + case MapDirection.Southeast: |
| 88 | + return MapDirection.Northwest; |
| 89 | + case MapDirection.South: |
| 90 | + return MapDirection.North; |
| 91 | + case MapDirection.Southwest: |
| 92 | + return MapDirection.Northeast; |
| 93 | + } |
| 94 | + |
| 95 | + return MapDirection.North; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Returns offset in matrix space (top-left is 0, 0) |
| 100 | + /// </summary> |
| 101 | + public static Vector2I Offset(this MapDirection state) |
| 102 | + { |
| 103 | + switch (state) |
| 104 | + { |
| 105 | + case MapDirection.Northwest: |
| 106 | + return new Vector2I(-1, -1); |
| 107 | + case MapDirection.North: |
| 108 | + return new Vector2I(0, -1); |
| 109 | + case MapDirection.Northeast: |
| 110 | + return new Vector2I(1, -1); |
| 111 | + case MapDirection.East: |
| 112 | + return new Vector2I(1, 0); |
| 113 | + case MapDirection.Southeast: |
| 114 | + return new Vector2I(1, 1); |
| 115 | + case MapDirection.South: |
| 116 | + return new Vector2I(0, 1); |
| 117 | + case MapDirection.Southwest: |
| 118 | + return new Vector2I(-1, 1); |
| 119 | + case MapDirection.West: |
| 120 | + return new Vector2I(-1, 0); |
| 121 | + } |
| 122 | + |
| 123 | + return new Vector2I(0, 0); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static class MapUtils |
| 128 | + { |
| 129 | + /// <summary> |
| 130 | + /// Translate a degree angle to a map direction |
| 131 | + /// </summary> |
| 132 | + public static MapDirection AngleToMapDirection(Angle angle, MapDirectionAxisLimit axisLimit) |
| 133 | + { |
| 134 | + var degreeAngle = angle.Degrees; |
| 135 | + var axisLimitNumber = 4; |
| 136 | + switch (axisLimit) |
| 137 | + { |
| 138 | + case MapDirectionAxisLimit.EightWay: |
| 139 | + axisLimitNumber = 8; |
| 140 | + break; |
| 141 | + } |
| 142 | + |
| 143 | + var angleAxisLimiter = new AngleAxisLimiter2D(axisLimitNumber); |
| 144 | + var limitedAngle = angleAxisLimiter.LimitAngle(Angle.DegreesAngle(degreeAngle)); |
| 145 | + |
| 146 | + var result = MapDirection.North; |
| 147 | + |
| 148 | + switch (Mathf.RoundToInt(limitedAngle.Degrees)) |
| 149 | + { |
| 150 | + case 0: |
| 151 | + case 360: |
| 152 | + return MapDirection.North; |
| 153 | + case 45: |
| 154 | + return MapDirection.Northeast; |
| 155 | + case 90: |
| 156 | + return MapDirection.East; |
| 157 | + case 135: |
| 158 | + return MapDirection.Southeast; |
| 159 | + case 180: |
| 160 | + return MapDirection.South; |
| 161 | + case 225: |
| 162 | + return MapDirection.Southwest; |
| 163 | + case 270: |
| 164 | + return MapDirection.West; |
| 165 | + case 315: |
| 166 | + return MapDirection.Northwest; |
| 167 | + } |
| 168 | + |
| 169 | + return result; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments