void createGrid() { List<int> layerList = new List<int>(); for (int x = 0; x < gridSize.X; x++) { for (int y = 0; y < gridSize.Y; y++) { Vector2 coordPosition = new Vector2(x, y); if (coordPosition.X % 2 == 0) { coordPosition.Y = coordPosition.X / 2f + y; } else { coordPosition.Y = (coordPosition.X + 1f) / 2f + y; } int layer = 0; if (x % 2 == 0) { layer = y * 2; } else { layer = (y * 2) + 1; } if (!layerList.Contains(layer)) { layerList.Add(layer); } Entity hexEntity = new Entity(1 + layer, State.ScreenState.SKIRMISH); hexEntityGrid[x, y] = hexEntity; HexComponent hexComp = new HexComponent(coordPosition); hexEntity.AddComponent(hexComp); hexDictionary.Add(coordPosition, hexComp); HexEntityDictionary.Add(coordPosition, hexEntity); //Creating the sprite for the hex entity Vector2 screenPosition; if (x % 2 == 0) { screenPosition.Y = y * gridTexture.Height + gridTexture.Height / 2f; } else { screenPosition.Y = y * gridTexture.Height + gridTexture.Height / 2f + gridTexture.Height / 2f; } screenPosition.X = x * (gridTexture.Width / 4f * 3f) + gridTexture.Width / 2f; SpriteComponent hexSprite = new SpriteComponent(true, screenPosition, gridTexture); hexEntity.AddComponent(hexSprite); EntityManager.AddEntity(hexEntity); GetHex(coordPosition).SetVisibility(Visibility.Unexplored); } } //Giving all the hex's their adjacents foreach (KeyValuePair<Vector2, HexComponent> entry in hexDictionary) { HexComponent hex = entry.Value; Vector2 coords = hex.GetCoordPosition(); //Setting up everyones adjacent, if it's null, it doesnt exist HexComponent n, ne, se, sw, s, nw; n = null; ne = null; se = null; s = null; sw = null; nw = null; if (GetHex(new Vector2(coords.X, coords.Y - 1)) != null) n = GetHex(new Vector2(coords.X, coords.Y - 1)); if (GetHex(new Vector2(coords.X + 1, coords.Y)) != null) ne = GetHex(new Vector2(coords.X + 1, coords.Y)); if (GetHex(new Vector2(coords.X + 1, coords.Y + 1)) != null) se = GetHex(new Vector2(coords.X + 1, coords.Y + 1)); if (GetHex(new Vector2(coords.X, coords.Y + 1)) != null) s = GetHex(new Vector2(coords.X, coords.Y + 1)); if (GetHex(new Vector2(coords.X - 1, coords.Y)) != null) sw = GetHex(new Vector2(coords.X - 1, coords.Y)); if (GetHex(new Vector2(coords.X - 1, coords.Y - 1)) != null) nw = GetHex(new Vector2(coords.X - 1, coords.Y - 1)); hex.SetAdjacent(n, ne, se, s, sw, nw); } layerList.Add(0); }
void createGrid() { for (int x = 0; x < gridSize.X; x++) { for (int y = 0; y < gridSize.Y; y++) { //Each hex will be an entity Entity hexEntity = new Entity(0); hexEntityGrid[x, y] = hexEntity; //Creating the hex comp for the hex entity Vector2 coordPosition = new Vector2(x, y); if (coordPosition.X % 2 == 0) { coordPosition.Y = coordPosition.X / 2f + y; } else { coordPosition.Y = (coordPosition.X + 1f) / 2f + y; } HexComponent hexComp = new HexComponent(hexEntity, coordPosition); hexEntity.AddComponent(hexComp); HexDictionary.Add(coordPosition, hexComp); HexEntityDictionary.Add(coordPosition, hexEntity); //Creating the sprite for the hex entity Vector2 screenPosition; if (x % 2 == 0) { screenPosition.Y = y * gridTexture.Height +gridTexture.Height / 2f; } else { screenPosition.Y = y * gridTexture.Height + gridTexture.Height / 2f +gridTexture.Height / 2f; } screenPosition.X = x * (gridTexture.Width / 4f * 3f) +gridTexture.Width / 2f; SpriteComponent hexSprite = new SpriteComponent(hexEntity, true, screenPosition, gridTexture); hexEntity.AddComponent(hexSprite); hexEntity.AddComponent(new CameraComponent(hexEntity, screenPosition)); EntityManager.AddEntity(hexEntity); //Adding text to label the coordinates of the hex entity Vector2 debugTextPosition = new Vector2(hexSprite.getCenterPosition().X, hexSprite.getCenterPosition().Y); hexEntity.AddComponent(new TextSpriteComponent(hexEntity, false, coordPosition.X.ToString() + "," + coordPosition.Y.ToString(), Color.Black, debugTextPosition, gridFont)); } } //Giving all the hex's their adjacents foreach (KeyValuePair<Vector2, HexComponent> entry in HexDictionary) { HexComponent hex = entry.Value; Vector2 coords = hex.getCoordPosition(); //Setting up everyones adjacent, if it's null, it doesnt exist HexComponent n, ne, se, sw, s, nw; n = null; ne = null; se = null; s = null; sw = null; nw = null; if (getHex(new Vector2(coords.X, coords.Y - 1)) != null) n = getHex(new Vector2(coords.X, coords.Y - 1)); if (getHex(new Vector2(coords.X + 1, coords.Y)) != null) ne = getHex(new Vector2(coords.X + 1, coords.Y)); if (getHex(new Vector2(coords.X + 1, coords.Y+1)) != null) se = getHex(new Vector2(coords.X + 1, coords.Y + 1)); if (getHex(new Vector2(coords.X, coords.Y+1)) != null) s = getHex(new Vector2(coords.X, coords.Y + 1)); if (getHex(new Vector2(coords.X - 1, coords.Y)) != null) sw = getHex(new Vector2(coords.X - 1, coords.Y)); if (getHex(new Vector2(coords.X - 1, coords.Y-1)) != null) nw = getHex(new Vector2(coords.X - 1, coords.Y - 1)); hex.setAdjacent(n, ne, se, s, sw, nw); } }