public override List <Cell> GenerateGrid() { HexGridType hexGridType = Width % 2 == 0 ? HexGridType.even_q : HexGridType.odd_q; List <Cell> hexagons = new List <Cell>(); if (HexagonPrefab.GetComponent <Hexagon>() == null) { Debug.LogError("Invalid hexagon prefab provided"); return(hexagons); } for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { GameObject hexagon = Instantiate(HexagonPrefab); var hexSize = hexagon.GetComponent <Cell>().GetCellDimensions(); hexagon.transform.position = new Vector3((j * hexSize.x * 0.75f), (i * hexSize.y) + (j % 2 == 0 ? 0 : hexSize.y * 0.5f)); hexagon.GetComponent <Hexagon>().OffsetCoord = new Vector2(Width - j - 1, Height - i - 1); hexagon.GetComponent <Hexagon>().HexGridType = hexGridType; hexagon.GetComponent <Hexagon>().MovementCost = 1; hexagons.Add(hexagon.GetComponent <Cell>()); hexagon.transform.parent = CellsParent; } } return(hexagons); }
Vector2 HexCorner(Vector2 center, float size, int i, HexGridType hexGridType) { float angleInDeg = hexGridType == HexGridType.FLAT_TOP ? 60 * i : 60 * i - 30; float angleInRad = angleInDeg * Mathf.Deg2Rad; return(new Vector2(center.x + size * Mathf.Cos(angleInRad), center.y + size * Mathf.Sin(angleInRad))); }
public HexGrid(T[,] data, HexGridShape shape, HexGridType type) { _shape = shape; _type = type; switch (shape) { case HexGridShape.HEXAGON: int width = data.GetLength(0); int length = (data.GetLength(1) - width + 2) / 2; _size = new IntVector2(length, width); break; //case HexagonGridShape.TRIANGlE: // _cells = new T[size.x - (size.y & 1) + 1, (size.y >> 1) + (size.y & 1)]; // int width = data.GetLength(1); // int length = (data.GetLength(0) - width + 2) / 2; // _size = new Int2D(length, width); // break; default: _size = new IntVector2(data.GetLength(1), data.GetLength(0)); break; } _cells = data; }
public void CreateCellGrid(MyHexagonData myHexagonData, int weight, int height) { HexGridType hexGridType = weight % 2 == 0 ? HexGridType.even_q : HexGridType.odd_q; // List<Cell> hexagons = new List<Cell>(); for (int i = 0; i < height; i++) { for (int j = 0; j < weight; j++) { //TODO 待完成 // GameEntry.Entity.ShowMyHexagon(); // GameObject hexagon = Instantiate(HexagonPrefab); // var hexSize = hexagon.GetComponent<Cell>().GetCellDimensions(); // // hexagon.transform.position = new Vector3((j * hexSize.x * 0.75f), 0, (i * hexSize.z) + (j % 2 == 0 ? 0 : hexSize.z * 0.5f)); // hexagon.GetComponent<Hexagon>().OffsetCoord = new Vector2(Width - j - 1, Height - i - 1); // hexagon.GetComponent<Hexagon>().HexGridType = hexGridType; // hexagon.GetComponent<Hexagon>().MovementCost = 1; //// hexagons.Add(hexagon.GetComponent<Cell>()); // // hexagon.transform.parent = CellsParent; } } // return hexagons; }
private static void RectangularHexGridGenerator(this EntityComponent entityComponent, HexagonData data, int height, int width) { HexGridType hexGridType = width % 2 == 0 ? HexGridType.even_q : HexGridType.odd_q; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { data.Position = new Vector3(i, j); entityComponent.ShowEntity(typeof(Hexagon), "Cell", Constant.AssetPriority.AircraftAsset, data); } } Log.Info("RectangularHexGridGenerator"); }
public IEnumerator GenerateGrid(int width, int height, Transform parent, GameObject movablePrefab, GameObject[] heightPrefabes) { result = null; HexGridType hexGridType = width % 2 == 0 ? HexGridType.even_q : HexGridType.odd_q; List <HexCell> hexagons = new List <HexCell>(); if (movablePrefab.GetComponent <HexCell>() == null) { Debug.LogError("Invalid hexagon prefab provided"); yield break; } for (int z = 0; z < height; z++) { for (int x = 0; x < width; x++) { GameObject hexagonObj; if (UnityEngine.Random.Range(0f, 1f) < 0.8f) { hexagonObj = GameObject.Instantiate(movablePrefab) as GameObject; } else { hexagonObj = GameObject.Instantiate(heightPrefabes[UnityEngine.Random.Range(0, heightPrefabes.Length)]) as GameObject; } HexCell hexagon = hexagonObj.GetComponent <HexCell>(); var hexSize = hexagon.GetCellDimensions(); hexagon.transform.position = new Vector3(x * hexSize.x * 0.75f, 0f, (z * hexSize.z) + (x % 2 == 0 ? 0 : hexSize.z * 0.5f)); hexagon.offsetCoord = new Vector2(width - x - 1, height - z - 1); hexagon.hexGridType = hexGridType; hexagon.movementCost = 1; hexagons.Add(hexagon); hexagon.transform.parent = parent; hexagon.gameObject.SetActive(false); yield return(null); } } var hexDimensions = movablePrefab.GetComponent <HexCell>().GetCellDimensions(); var hexSide = hexDimensions.x / 2; result = new GridInfo(); result.Cells = hexagons; result.Dimensions = new Vector3(hexSide * (width - 1) * 1.5f, hexDimensions.y * (height - 0.5f), hexDimensions.z); result.Center = result.Dimensions / 2; }
public HexGrid(IntVector2 size, HexGridShape shape, HexGridType type) { _shape = shape; _type = type; _size = size; switch (shape) { case HexGridShape.HEXAGON: _cells = new T[size.y, 2 * size.x + size.y - 2]; break; //case HexagonGridShape.TRIANGlE: // _cells = new T[size.x - (size.y & 1) + 1, (size.y >> 1) + (size.y & 1)]; // break; default: _cells = new T[size.y, size.x]; break; } }
#pragma warning restore 0649 public override GridInfo GenerateGrid() { HexGridType hexGridType = Width % 2 == 0 ? HexGridType.even_q : HexGridType.odd_q; List <Cell> hexagons = new List <Cell>(); if (HexagonPrefab.GetComponent <Hexagon>() == null) { Debug.LogError("Invalid hexagon prefab provided"); return(null); } for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { GameObject hexagon = PrefabUtility.InstantiatePrefab(HexagonPrefab) as GameObject; var hexSize = hexagon.GetComponent <Cell>().GetCellDimensions(); hexagon.transform.position = new Vector3((j * hexSize.x * 0.75f), (i * hexSize.y) + (j % 2 == 0 ? 0 : hexSize.y * 0.5f), 0); hexagon.GetComponent <Hexagon>().OffsetCoord = new Vector2(Width - j - 1, Height - i - 1); hexagon.GetComponent <Hexagon>().HexGridType = hexGridType; hexagon.GetComponent <Hexagon>().MovementCost = 1; hexagons.Add(hexagon.GetComponent <Cell>()); hexagon.transform.parent = CellsParent; } } var hexDimensions = HexagonPrefab.GetComponent <Cell>().GetCellDimensions(); var hexSide = hexDimensions.x / 2; GridInfo gridInfo = new GridInfo(); gridInfo.Cells = hexagons; gridInfo.Dimensions = new Vector3(hexSide * (Width - 1) * 1.5f, hexDimensions.y * (Height - 0.5f), hexDimensions.z); gridInfo.Center = gridInfo.Dimensions / 2; return(gridInfo); }
float NeighbourCellDistanceInY(float size, HexGridType hexGridType) { float h = Height(size, hexGridType); return(hexGridType == HexGridType.FLAT_TOP ? h * .75f : h); }
float NeighbourCellDistanceInX(float size, HexGridType hexGridType) { float w = Width(size, hexGridType); return(hexGridType == HexGridType.FLAT_TOP ? w : w * .75f); }
float Height(float size, HexGridType hexGridType) { return(hexGridType == HexGridType.FLAT_TOP ? mRootThreeValue * size : 2 * size); }
float Width(float size, HexGridType hexGridType) { return(hexGridType == HexGridType.FLAT_TOP ? 2 * size : mRootThreeValue *size); }
public HexGrid(int length, int width, HexGridShape shape, HexGridType type) : this(new IntVector2(length, width), shape, type) { }