public static Scene.hexNum[] GetLineBetweenPointsCube(Scene.hexNum start, Scene.hexNum end) { int distance = DistanceBetweenPointsCube(start, end); if (distance == 0 || distance == 1) { return(null); } Scene.hexNum[] line = new Scene.hexNum[distance - 1]; // do not include start & end float x = start.num_x; float z = start.num_z; //float y = -start.num_x-start.num_z; float delta_x = (end.num_x - start.num_x) / (float)distance; float delta_z = (end.num_z - start.num_z) / (float)distance; //float delta_y = (-(end.num_x+end.num_z) + (start.num_x+start.num_z))/(float)distance; for (int i = 1; i < distance; i++) { x += delta_x; //y += delta_y; z += delta_z; line[i - 1] = CubeRound(x, z); } return(line); }
bool DetectWalkable(Scene.hexNum hexN) { if (hexN.num_x == -1 || hexN.num_z == -1) { return(false); } int num = Scene.GetOneDimensionVal(hexN.num_x, hexN.num_z); if (!Scene.hexOccupied[num]) { return(false); } try { if (((IList)perhebited_hex_name).Contains(Scene.map[num].name)) { return(false); } } catch (Exception e) { //Debug.Log("hexN.num_x: "+hexN.num_x); //Debug.Log("hexN.num_z: "+hexN.num_z); return(false); // Scene.map[num] = null } return(true); }
public static void Initialize() { // start.z %2 must be 0 area = new int[height * width]; int num; Scene.hexNum hexN; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { num = GetOneDimensionVal(i, j); hexN = new Scene.hexNum { num_x = i, num_z = j }; if (((IList)jumpArea).Contains(hexN)) { area[num] = 6; } else if (((IList)normalArea).Contains(hexN)) { //area[num] = Random.Range(0, 4); area[num] = 0; } else { area[num] = -1; } } } }
public static Scene.hexNum CubeToOffsetCoord(Scene.hexNum hexN) { Scene.hexNum offset; offset.num_z = hexN.num_z; offset.num_x = hexN.num_x + hexN.num_z / 2; // y = -x-z return(offset); }
public static Scene.hexNum OffsetToCubeCoord(Scene.hexNum hexN) { Scene.hexNum cube; cube.num_z = hexN.num_z; cube.num_x = hexN.num_x - hexN.num_z / 2; // y = -x-z return(cube); }
public static Scene.hexNum[] GetRandomArea(Scene.hexNum hexN, int height, int width) { Scene.hexNum[] area = new Scene.hexNum[0]; Scene.hexNum[] row; Scene.hexNum bottomRowCenter, upRowCenter; int rowLength, randomStart, randomHeight = height; bottomRowCenter = new Scene.hexNum { num_x = hexN.num_x + (height - 1) / 2, num_z = hexN.num_z }; upRowCenter = new Scene.hexNum { // temporary value num_x = -1, num_z = -1 }; for (int i = 1; i <= width; i++) { row = GetRow(hexN, randomHeight); rowLength = row.Length; Utils.Add(ref area, row); if (i == width) { upRowCenter = new Scene.hexNum { num_x = hexN.num_x + (randomHeight - 1) / 2, num_z = hexN.num_z }; } randomStart = UnityEngine.Random.Range(-1 + hexN.num_z % 2, 1 + hexN.num_z % 2); // -1, 0, 1 hexN.num_x += randomStart; hexN.num_z += 1; if (hexN.num_z >= Scene.areaWidth) { break; } if (hexN.num_x < 0) { hexN.num_x = 0; randomStart = 0; } randomHeight = rowLength + UnityEngine.Random.Range(-1 - randomStart, 3); if (randomHeight <= 0) { randomHeight = 1; } if (height >= 3 && randomHeight >= 1.5f * height) { randomHeight = Mathf.RoundToInt(1.5f * height); } } Utils.Add(ref area, bottomRowCenter); Utils.Add(ref area, upRowCenter); return(area); }
public static int DistanceBetweenPointsCube(Scene.hexNum start, Scene.hexNum end) { // calculate under cude coordinate int distance_x = Mathf.Abs(start.num_x - end.num_x); int distance_z = Mathf.Abs(start.num_z - end.num_z); int distance_y = Mathf.Abs(-(start.num_x + start.num_z) + (end.num_x + end.num_z)); return((distance_x + distance_y + distance_z) / 2); }
public static bool FindElementInArray(Scene.hexNum[] array, Scene.hexNum element) { for (int i = 0; i < array.Length; i++) { if (array[i].num_x == element.num_x && array[i].num_z == element.num_z) { return(true); } } return(false); }
// Get 6 neighbour hexagons public static Scene.hexNum[] GetNeighbourHex(Scene.hexNum hexN, bool OutOfBound = true) { Scene.hexNum[] neighbour = new Scene.hexNum[6]; // some values can possibly be null neighbour[0] = GetLeftUpHex(hexN, OutOfBound); neighbour[1] = GetRightUpHex(hexN, OutOfBound); neighbour[2] = GetRightHex(hexN, OutOfBound); neighbour[3] = GetRightDownHex(hexN, OutOfBound); neighbour[4] = GetLeftDownHex(hexN, OutOfBound); neighbour[5] = GetLeftHex(hexN, OutOfBound); return(neighbour); }
Scene.hexNum[] FindWalkableHex(Scene.hexNum hexN) { Scene.hexNum[] walkable = new Scene.hexNum[0]; Scene.hexNum[] neighbour = Hexagon.GetNeighbourHex(hexN); for (int i = 0; i < 6; i++) { if (DetectWalkable(neighbour[i])) { Utils.Add(ref walkable, neighbour[i]); } } return(walkable); }
public static void DebugDrawPoint(Scene.hexNum hexN, int angle, Color color, float y = 0.5f) { Scene.hexAxis hexA = Hexagon.NumToAxis(hexN.num_x, hexN.num_z); Vector3 pos; pos.x = hexA.axis_x; pos.z = hexA.axis_z; pos.y = y; Vector3 dir = Matrix4x4.Rotate(Quaternion.Euler(0, angle, 0)).MultiplyVector(Vector3.forward); Debug.DrawLine(pos, pos + dir * 0.5f, color, 10f); }
public static Scene.hexNum GetRightHex(Scene.hexNum hexN, bool OutOfBound = true) { Scene.hexNum tmp; tmp.num_x = hexN.num_x + 1; tmp.num_z = hexN.num_z; if (OutOfBound && tmp.num_x >= Scene.areaHeight) { return new Scene.hexNum { num_x = -1, num_z = -1 } } ;; // outside boundary return(tmp); }
public static Scene.hexNum[] GetLineBetweenPointsOffset(Scene.hexNum start, Scene.hexNum end) { start = OffsetToCubeCoord(start); end = OffsetToCubeCoord(end); Scene.hexNum[] line = GetLineBetweenPointsCube(start, end); if (line == null) { return(null); } for (int i = 0; i < line.Length; i++) { line[i] = CubeToOffsetCoord(line[i]); } return(line); }
public static Scene.hexNum[] GetRow(Scene.hexNum hexN, int height) { if (hexN.num_x + height >= Scene.areaHeight) { height = Scene.areaHeight - hexN.num_x - 1; } Scene.hexNum[] row = new Scene.hexNum[height]; Scene.hexNum tmp = hexN; for (int i = 0; i < height; i++) { row[i] = tmp; tmp = GetRightHex(tmp); } return(row); }
public static Scene.hexNum GetLeftHex(Scene.hexNum hexN, bool OutOfBound = true) { Scene.hexNum tmp; tmp.num_x = hexN.num_x - 1; tmp.num_z = hexN.num_z; if (OutOfBound && tmp.num_x < 0) { return new Scene.hexNum { num_x = -1, num_z = -1 } } ;; return(tmp); } }
public static Scene.hexNum GetLeftUpHex(Scene.hexNum hexN, bool OutOfBound = true) { Scene.hexNum tmp; if (hexN.num_z % 2 == 0) { tmp.num_x = hexN.num_x - 1; tmp.num_z = hexN.num_z + 1; } else { tmp.num_x = hexN.num_x; tmp.num_z = hexN.num_z + 1; } if (OutOfBound && (tmp.num_x < 0 || tmp.num_z >= Scene.areaWidth)) { return new Scene.hexNum { num_x = -1, num_z = -1 } } ;; // outside boundary return(tmp); }
public static Scene.hexNum GetHexByAngle(Scene.hexNum hexN, int angle, bool OutOfBound = true) { Scene.hexNum result = new Scene.hexNum { num_x = -1, num_z = -1 }; if (angle > 150) { angle -= 360; } if (angle < -150) { angle += 360; } switch (angle) { case 30: result = GetRightUpHex(hexN, OutOfBound); break; case 90: result = GetRightHex(hexN, OutOfBound); break; case 150: result = GetRightDownHex(hexN, OutOfBound); break; case -150: result = GetLeftDownHex(hexN, OutOfBound); break; case -90: result = GetLeftHex(hexN, OutOfBound); break; case -30: result = GetLeftUpHex(hexN, OutOfBound); break; default: break; } return(result); }
public static Scene.hexNum GetLeftDownHex(Scene.hexNum hexN, bool OutOfBound = true) { Scene.hexNum tmp; if (hexN.num_z % 2 == 0) { tmp.num_x = hexN.num_x - 1; tmp.num_z = hexN.num_z - 1; } else { tmp.num_x = hexN.num_x; tmp.num_z = hexN.num_z - 1; } if (OutOfBound && (tmp.num_x < 0 || tmp.num_z < 0)) { return new Scene.hexNum { num_x = -1, num_z = -1 } } ;; return(tmp); }
public static Scene.hexNum[] GetSpiralRing(Scene.hexNum hexN, int radius) { // radius=1: 1 hex Scene.hexNum[] ring = new Scene.hexNum[3 * radius * (radius - 1) + 1]; Scene.hexNum[] ring_tmp; int count = 0; for (int i = radius; i >= 2; i--) { ring_tmp = GetRing(hexN, i - 1); if (ring_tmp == null) { return(null); } Array.Copy(ring_tmp, 0, ring, count, 6 * (i - 1)); count += 6 * (i - 1); } ring[count++] = hexN; return(ring); // 6 vertices: ring[radius-2, 2*radius-3, 3*radius-4, 4*radius-5, 5*radius-6, 6*radius-7] // left down, right down, right, right up, left up, left }
void AddMeshData() { float y = 0; for (int z = 0; z < width; z++) { for (int x = 0; x < height; x++) { y = 0; //for (int i=0; i<8; i++) for (int i = 0; i < 1; i++) { //float y = Random.Range(0, 3.5f); y += Mathf.PerlinNoise(x * PerlinNoiseXZRatio, z * PerlinNoiseXZRatio) * PerlinNoiseYRatio; //PerlinNoiseXZRatio *= 2; //PerlinNoiseYRatio *= 0.5f; } y = Mathf.Max(y, lowest_height); //y = Mathf.Min(y, lowest_height+0.405); Scene.hexNum hexN = Hexagon.AxisToNum(x * ratio + transform.position.x, z * ratio + transform.position.z); Scene.hexNum[] neighbour = new Scene.hexNum[] { hexN }; Utils.Add(ref neighbour, Hexagon.GetNeighbourHex(hexN, OutOfBound: false)); for (int i = 0; i < neighbour.Length; i++) { int num = Scene.GetOneDimensionVal(neighbour[i].num_x, neighbour[i].num_z); if (num >= 0 && num < Scene.hexOccupied.Length && Scene.hexOccupied[num]) { //Utils.DebugDrawPoint(hexN, 0, Color.blue, y:lowest_height+0.35f+transform.position.y); if (Scene.map[num].name == "jump(Clone)") { y = Mathf.Min(y, lowest_height + 0.1f); } else { y = Mathf.Min(y, lowest_height + 0.35f); } break; } } if (y > maxHeight) { maxHeight = y; } if (y < minHeight) { minHeight = y; } Vector3 p = new Vector3(x * ratio, y, z * ratio); verts.Add(p); } } for (int z = 0; z < width; z++) { for (int x = 0; x < height; x++) { int index = z * height + x; y = verts[index].y; float p = (y - minHeight) / (maxHeight - minHeight); Color c = gradient.Evaluate(p); colors.Add(c); } } for (int z = 0; z < width - 1; z++) { for (int x = 0; x < height - 1; x++) { int index = z * height + x; int index1 = (z + 1) * height + x; int index2 = (z + 1) * height + x + 1; int index3 = z * height + x + 1; indices.Add(index); indices.Add(index1); indices.Add(index2); indices.Add(index); indices.Add(index2); indices.Add(index3); } } }
public static Scene.hexNum[] GetTwistedEdgesBetweenPointsOffset(Scene.hexNum start, Scene.hexNum end, float range) { // twist twice Scene.hexNumFloat[] points = new Scene.hexNumFloat[4]; start = OffsetToCubeCoord(start); end = OffsetToCubeCoord(end); Scene.hexNumFloat start_f = Scene.HexNumToFloat(start); Scene.hexNumFloat end_f = Scene.HexNumToFloat(end); points[0] = start_f; points[points.Length - 1] = end_f; Scene.hexNumFloat hexNF_1 = new Scene.hexNumFloat { num_x_float = start.num_x, num_z_float = end.num_z, }; Scene.hexNumFloat hexNF_2 = new Scene.hexNumFloat { num_x_float = end.num_x, num_z_float = start.num_z, }; Scene.hexNumFloat hexNF_3 = Scene.Lerp(start_f, hexNF_1, 0.33f); Scene.hexNumFloat hexNF_4 = Scene.Lerp(start_f, hexNF_2, 0.33f); points[1] = Scene.RandomLerp(hexNF_3, hexNF_4, range); hexNF_3 = Scene.Lerp(end_f, hexNF_1, 0.33f); hexNF_4 = Scene.Lerp(end_f, hexNF_2, 0.33f); points[2] = Scene.RandomLerp(hexNF_3, hexNF_4, range); Scene.hexNum[] edge = new Scene.hexNum[0]; Scene.hexNum[] line; Scene.hexNum[] pointsRounded = new Scene.hexNum[points.Length]; for (int i = 0; i < points.Length; i++) { pointsRounded[i] = CubeRound(points[i].num_x_float, points[i].num_z_float); } for (int i = 0; i < points.Length - 1; i++) { line = GetLineBetweenPointsCube(pointsRounded[i], pointsRounded[i + 1]); Utils.Add(ref edge, pointsRounded[i]); if (line != null) { Utils.Add(ref edge, line); } } Utils.Add(ref edge, pointsRounded[points.Length - 1]); for (int i = 0; i < edge.Length; i++) { edge[i] = CubeToOffsetCoord(edge[i]); if (edge[i].num_x < 0) { edge[i].num_x = 0; } if (edge[i].num_x >= Scene.areaHeight) { edge[i].num_x = Scene.areaHeight - 1; } if (edge[i].num_z < 0) { edge[i].num_z = 0; } if (edge[i].num_z >= Scene.areaWidth) { edge[i].num_z = Scene.areaWidth - 1; } } return(edge); }
public static int DistanceBetweenPointsOffset(Scene.hexNum start, Scene.hexNum end) { start = OffsetToCubeCoord(start); end = OffsetToCubeCoord(end); return(DistanceBetweenPointsCube(start, end)); }
public static Scene.hexNum[] GetNoisyEdgesBetweenPointsOffset(Scene.hexNum start, Scene.hexNum end, int level, float range) { // number of segments = pow(2, level) if (level == 0) { return(GetLineBetweenPointsOffset(start, end)); } int numPoints = (int)Mathf.Pow(2f, level) + 1; // include start & end Scene.hexNumFloat[] points = new Scene.hexNumFloat[numPoints]; // use Scene.hexAxis to store paired float values, but the syntax is close to Scene.hexNum int k1 = (int)Mathf.Pow(2f, level - 1); int k2 = (int)Mathf.Pow(2f, level - 2); int k3 = (int)Mathf.Pow(2f, level - 3); start = OffsetToCubeCoord(start); end = OffsetToCubeCoord(end); Scene.hexNumFloat start_f = Scene.HexNumToFloat(start); Scene.hexNumFloat end_f = Scene.HexNumToFloat(end); points[0] = start_f; points[points.Length - 1] = end_f; while (true) { // level = 1 Scene.hexNumFloat hexNF_1 = new Scene.hexNumFloat { num_x_float = start.num_x, num_z_float = end.num_z, }; Scene.hexNumFloat hexNF_2 = new Scene.hexNumFloat { num_x_float = end.num_x, num_z_float = start.num_z, }; points[k1] = Scene.RandomLerp(hexNF_1, hexNF_2, range); if (level == 1) { break; } // level = 2 Scene.hexNumFloat hexNF_3 = Scene.HalfLerp(start_f, hexNF_1); Scene.hexNumFloat hexNF_4 = Scene.HalfLerp(start_f, hexNF_2); points[k2] = Scene.RandomLerp(hexNF_3, hexNF_4, range); Scene.hexNumFloat hexNF_5 = Scene.HalfLerp(end_f, hexNF_2); Scene.hexNumFloat hexNF_6 = Scene.HalfLerp(end_f, hexNF_1); points[k1 + k2] = Scene.RandomLerp(hexNF_5, hexNF_6, range); if (level == 2) { break; } // level = 3 Scene.hexNumFloat hexNF_7 = Scene.HalfLerp(start_f, hexNF_3); Scene.hexNumFloat hexNF_8 = Scene.HalfLerp(start_f, hexNF_4); points[k3] = Scene.RandomLerp(hexNF_7, hexNF_8, range); hexNF_7 = Scene.HalfLerp(hexNF_3, points[k1]); hexNF_8 = Scene.HalfLerp(hexNF_4, points[k1]); points[k2 + k3] = Scene.RandomLerp(hexNF_7, hexNF_8, range); hexNF_7 = Scene.HalfLerp(hexNF_5, points[k1]); hexNF_8 = Scene.HalfLerp(hexNF_6, points[k1]); points[k1 + k3] = Scene.RandomLerp(hexNF_7, hexNF_8, range); hexNF_7 = Scene.HalfLerp(end_f, hexNF_5); hexNF_8 = Scene.HalfLerp(end_f, hexNF_6); points[k1 + k2 + k3] = Scene.RandomLerp(hexNF_7, hexNF_8, range); break; } Scene.hexNum[] noisyEdge = new Scene.hexNum[0]; Scene.hexNum[] line; Scene.hexNum[] pointsRounded = new Scene.hexNum[points.Length]; for (int i = 0; i < points.Length; i++) { pointsRounded[i] = CubeRound(points[i].num_x_float, points[i].num_z_float); } for (int i = 0; i < points.Length - 1; i++) { line = GetLineBetweenPointsCube(pointsRounded[i], pointsRounded[i + 1]); Utils.Add(ref noisyEdge, pointsRounded[i]); if (line != null) { Utils.Add(ref noisyEdge, line); } } Utils.Add(ref noisyEdge, pointsRounded[points.Length - 1]); for (int i = 0; i < noisyEdge.Length; i++) { noisyEdge[i] = CubeToOffsetCoord(noisyEdge[i]); if (noisyEdge[i].num_x < 0) { noisyEdge[i].num_x = 0; } if (noisyEdge[i].num_x >= Scene.areaHeight) { noisyEdge[i].num_x = Scene.areaHeight - 1; } if (noisyEdge[i].num_z < 0) { noisyEdge[i].num_z = 0; } if (noisyEdge[i].num_z >= Scene.areaWidth) { noisyEdge[i].num_z = Scene.areaWidth - 1; } } return(noisyEdge); }
public static Vector3 CenterVectorBetweenTwoHex(Scene.hexNum start, Scene.hexNum end) { Scene.hexAxis startAxis = NumToAxis(start.num_x, start.num_z); Scene.hexAxis endAxis = NumToAxis(end.num_x, end.num_z); return(new Vector3(endAxis.axis_x - startAxis.axis_x, 0, endAxis.axis_z - startAxis.axis_z)); }
public static Scene.hexNum[] GetRing(Scene.hexNum hexN, int distance) { Scene.hexNum[] ring = new Scene.hexNum[distance * 6]; Scene.hexNum hexN_1 = hexN; for (int i = 1; i <= distance; i++) { hexN_1 = GetLeftHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } } int count = 0; for (int i = 1; i <= distance; i++) { hexN_1 = GetRightDownHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } for (int i = 1; i <= distance; i++) { hexN_1 = GetRightHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } for (int i = 1; i <= distance; i++) { hexN_1 = GetRightUpHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } for (int i = 1; i <= distance; i++) { hexN_1 = GetLeftUpHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } for (int i = 1; i <= distance; i++) { hexN_1 = GetLeftHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } for (int i = 1; i <= distance; i++) { hexN_1 = GetLeftDownHex(hexN_1); if (hexN_1.num_x == -1) { return(null); } ring[count++] = hexN_1; } return(ring); }