void Start() { if (instance == null) { instance = this; } else if (instance != null) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); unitMoving = false; unitAttacking = false; unitPosArray = new List <List <GameObject> >(); nodeArr = new Node[tilemap.cellBounds.size.x, tilemap.cellBounds.size.y]; for (int i = 0; i < tilemap.cellBounds.size.x; i++) { unitPosArray.Add((List <GameObject>) new List <GameObject>()); for (int j = 0; j < tilemap.cellBounds.size.y; j++) { unitPosArray[i].Add(null); SmartTile tile = accessTile(i, j); nodeArr [i, j] = new Node(i, j, tile.walkable, tile.treadable, tile.sailable, tile.flyable, tile.walkCost, tile.treadCost, tile.sailCost, tile.flyCost); } } selectedUnit = null; spawnUnit(redRecon, 0, 0); spawnUnit(redInfantry, 3, 3); spawnUnit(redCarrier, 10, 6); spawnUnit(redBomber, 5, 4); spawnUnit(blueapc, 4, 2); spawnUnit(rotary, 2, 2); }
void Start() { foreach (Vector3Int pos in tilemap.cellBounds.allPositionsWithin) { NavSurface nav = ScriptableObject.CreateInstance <NavSurface>(); Vector3Int localPlace = new Vector3Int(pos.x, pos.y, pos.z); if (tilemap.HasTile(localPlace)) { SmartTile tile = tilemap.GetTile <SmartTile>(pos); nav.isNavigable = tile.nav.isNavigable; nav.cost = tile.nav.cost; nav.isCover = tile.nav.isCover; nav.coverLocations = new List <Vector3Int>(); nav.coverValue = tile.nav.coverValue; navDict.Add(pos, nav); } } foreach (Vector3Int pos in tilemap.cellBounds.allPositionsWithin) { NavSurface nav; navDict.TryGetValue(pos, out nav); if (nav.isCover) { Debug.Log("Is Cover."); for (int i = -1; i <= 1; i++) { if (i == 0) { continue; } Vector3Int positionX = new Vector3Int(pos.x + i, pos.y, pos.z); Vector3Int positionY = new Vector3Int(pos.x, pos.y + i, pos.z); if (HasSmartTile(tilemap, positionX)) { if (tilemap.GetTile <SmartTile>(positionX).nav.isNavigable) { nav.coverLocations.Add(positionX); Debug.Log("Cover Added: " + positionX); } } if (HasSmartTile(tilemap, positionY)) { if (tilemap.GetTile <SmartTile>(positionY).nav.isNavigable) { nav.coverLocations.Add(positionY); Debug.Log("Cover Added: " + positionY); } } } navDict.Remove(pos); navDict.Add(pos, nav); } } Debug.Log("Done defining nav locations."); }
public void resetColor(int xPos, int yPos, string oldColor) { SmartTile oldTile = accessTile(xPos, yPos); SmartTile newTile = oldTile; if (oldColor == "blue") { if (oldTile == waterTileBlue) { newTile = waterTile; } else if (oldTile == grassTileBlue) { newTile = grassTile; } else if (oldTile == mountainTileBlue) { newTile = mountainTile; } } else if (oldColor == "red") { if (unitPosArray[xPos][yPos] != null) { unitToScript(unitPosArray [xPos] [yPos]).changeNormal(); } if (oldTile == waterTileRed) { newTile = waterTile; } else if (oldTile == grassTileRed) { newTile = grassTile; } else if (oldTile == mountainTileRed) { newTile = mountainTile; } } tilemap.SetTile(gridToActual(xPos, yPos), newTile); }
void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Vector3 worldPoint = ray.GetPoint(-ray.origin.z / ray.direction.z); Vector3Int position = grid.WorldToCell(worldPoint); SmartTile tile = tilemap.GetTile <SmartTile>(position); NavSurface nav; if (navDict.ContainsKey(position)) { if (navDict.TryGetValue(position, out nav)) { Debug.Log("Tile cover amount: " + nav.coverLocations.Count); Debug.Log("Tile cover locations :-"); foreach (Vector3Int pos in nav.coverLocations) { Debug.Log(pos); } } } } }
public void CalculateGraph() { lastUpdate = System.DateTime.Now.ToString(); if (nodes == null) { nodes = new List <NavSurface>(); } // Currently the graph is cleared on every update // Is tied directly into the loop, hence having to loop through all tiles everytime we update // #TODO Need to have it so such that only the edited nodes need to clear if (nodes != null) { if (nodes.Count > 0) { foreach (NavSurface node in nodes) { ScriptableObject.DestroyImmediate(node); } nodes.Clear(); } } foreach (Vector3Int pos in tilemap.cellBounds.allPositionsWithin) { // Spawn a new node for each tile position Vector3Int localPlace = new Vector3Int(pos.x, pos.y, pos.z); if (tilemap.HasTile(localPlace)) { NavSurface nav = ScriptableObject.CreateInstance <NavSurface>(); SmartTile tile = tilemap.GetTile <SmartTile>(pos); nav.gridPos = pos; nav.isNavigable = tile.nav.isNavigable; nav.cost = tile.nav.cost; nav.isCover = tile.nav.isCover; nav.coverLocations = new List <Vector3Int>(); nav.coverValue = tile.nav.coverValue; // Add the new node to the nodes list nodes.Add(nav); } } // Calculating connections for all nodes foreach (NavSurface node in nodes) { if (node.isNavigable) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } int isDiagonal = Mathf.Abs(i) * Mathf.Abs(j); Vector3Int neighbourPos = new Vector3Int(node.gridPos.x + i, node.gridPos.y + j, node.gridPos.z); if (HasSmartTile(tilemap, neighbourPos)) { //int index = nodeIndex.FindLastIndex(pos); NavSurface neighbourNode = nodes.Find(x => x.gridPos == neighbourPos); if (neighbourNode.isNavigable) { Connection connection = ScriptableObject.CreateInstance <Connection>(); connection.fromNode = node; connection.toNode = neighbourNode; if (isDiagonal == 1) { connection.cost = connection.toNode.cost * 2; } else { connection.cost = connection.toNode.cost; } node.connections.Add(connection); } } } } } } }
public void AddTile(int x, int y, SmartTile newTile) { tiles[x, y] = newTile; }