private void InitializeCell(Hexagon prefab, Vector3 position, HexagonData data, int index = -1) { Hexagon cell = Instantiate(prefab); if (index >= 0) { cells[index] = cell; } else { cells.Add(cell); } cell.transform.SetParent(transform, false); cell.transform.localPosition = position; cell.HexagonData = data; //Need to recalculate entire grid, if inserting a new cell in the middle. if (index >= 0) { cells.ForEach(CalculateNeighbours); } else { CalculateNeighbours(cell); } }
public List <Vector2> EnemySpawns = new List <Vector2>(); //Spawn point hexagons for enemy team #if UNITY_EDITOR /// <summary> /// Setup the hexagons of this map using the HexagonData class /// </summary> public void SetHexagons(Hexagon[] newHexagons) { map = new HexagonData[GridWidth * GridHeight]; for (int x = 0; x < GridWidth; x++) { for (int y = 0; y < GridHeight; y++) { // HexagonData h = new HexagonData(newHexagons[y * GridWidth + x]); HexagonData h = ScriptableObject.CreateInstance <HexagonData>(); h.CreateHexagonData(newHexagons[y * GridWidth + x]); map[y * GridWidth + x] = h; if (h.CurrentSpawnType == Hexagon.SpawnType.Player) { PlayerSpawns.Add(new Vector2(newHexagons[y * GridWidth + x].HexRow, newHexagons[y * GridWidth + x].HexColumn)); } else if (h.CurrentSpawnType == Hexagon.SpawnType.Enemy) { EnemySpawns.Add(new Vector2(newHexagons[y * GridWidth + x].HexRow, newHexagons[y * GridWidth + x].HexColumn)); } EditorUtility.SetDirty(this); } } if (PlayerSpawns.Count < 4) { Debug.LogWarning("Less than 4 Player Spawns on map, add more"); } if (EnemySpawns.Count < 4) { Debug.LogWarning("Less than 4 Enemy Spawn on map, add more"); } }
/// <summary> /// Constructor given HexagonData /// </summary> public Hexagon(HexagonData h) { HexRow = h.HexRow; HexHeight = h.HexHeight; CurrentHexType = h.CurrentHexType; CurrentSpawnType = h.CurrentSpawnType; CurrentBrushTexture = h.CurrentBrushTexture; typeMaterials = h.typeMaterials; HexHeight = h.HexHeight; }
/// <summary> /// Setup a hexagon using hexagon data manually /// </summary> public void SetupHexagon(HexagonData h) { HexRow = h.HexRow; HexHeight = h.HexHeight; CurrentHexType = h.CurrentHexType; CurrentSpawnType = h.CurrentSpawnType; CurrentBrushTexture = h.CurrentBrushTexture; typeMaterials = h.typeMaterials; HexHeight = h.HexHeight; SetToType(); //TODO: needs to set to texture whenever that happens }
private void CreateCell(int x, int z, Hexagon.TileType type = Hexagon.TileType.Grass) { Vector3 position; position.x = (x + z * 0.5f - z / 2) * (GridSettings.INNER_RADIUS * 2f); position.y = 0f; position.z = z * (GridSettings.OUTER_RADIUS * 1.5f); HexagonData hexagonData = new HexagonData(HexCoordinates.FromOffsetCoordinates(x, z), type, x, z); InitializeCell(GetPrefabFromType(type), position, hexagonData); }
public void ChangeCellType(Vector3 position, Hexagon.TileType type) { position = transform.InverseTransformPoint(position); HexCoordinates coordinates = HexCoordinates.FromPosition(position); int index = Hexagon.Index(coordinates, width); Hexagon cell = cells[index]; if (cell.HexagonData.Type == type) { return; } HexagonData data = new HexagonData(cell.HexagonData.Coordinates, type, cell.HexagonData.XIndex, cell.HexagonData.ZIndex); InitializeCell(GetPrefabFromType(type), cell.transform.localPosition, data, index); Destroy(cell.gameObject); }
void OnEnable() { stats = this.gameObject.GetComponent <HexagonData>(); }