// Update is called once per frame void Update() { if (HexGM.isBattleRound()) { /* * Check whether to display health bars */ if (unit != null && !barsActive) { activateBars(true); } if (barsActive && unit == null) { activateBars(false); } else if (barsActive && unit != null) { setHealthColor(unit.isAlly); healthBar.value = (float)unit.currentHealth / unit.health; manaBar.value = (float)unit.currentMana / unit.mana; } /* * Combat logic here */ if (unit != null && unit.readyToAttack()) { // figure out which tile to attack List <BaseTileHandler> bthl = battlefield.getClosestEnemy(coordinate, unit.isAlly); if (bthl != null) { BaseTileHandler bth = bthl[0]; int distance = Battlefield.getDistance(this.coordinate, bth.getCoordinate()); if (!(distance <= unit.range)) { // it's out of range, move instead, we already got the shortest path so try to move along the path // the first unit is the target, so we want to start with the furthest possible range from the target for (int i = unit.range; i > 0; i--) { if (bthl[i].getCurrentUnit() == null) { if (bthl[i].setUnit(this.unit)) { this.resetDefault(); return; } } } for (int i = unit.range; i < bthl.Count; i++) { if (bthl[i].getCurrentUnit() == null) { if (bthl[i].setUnit(this.unit)) { this.resetDefault(); return; } } } } else { // Create the bullet, it'll be responsible for it's own destruction var newBullet = Instantiate(AllyBullet, this.transform.position, Quaternion.identity); newBullet.transform.SetParent(this.transform.parent.parent); if (!unit.isAlly) { newBullet.GetComponent <Image>().color = Color.red; } BulletHandler b = newBullet.gameObject.GetComponent <BulletHandler>(); b.setDestination(this.transform.position, bth, unit); } } } } else if (barsActive) { activateBars(false); } }
List <Vector2Int> dijkstra(Vector2Int start, bool isAlly) { Node[,] graph = new Node[MAX_X + 1, MAX_Y + 1]; for (int i = 0; i < MAX_X + 1; i++) { for (int j = 0; j < MAX_Y + 1; j++) { graph[i, j] = new Node(Unit.WEIGHT_MAX, null, new Vector2Int(i, j)); } } graph[start.x, start.y] = new Node(0, null, start); List <Vector2Int> path = new List <Vector2Int>(); // Not actually a priority queue but we can solve that by sorting List <Node> prioQueue = new List <Node>(); HashSet <Vector2Int> visited = new HashSet <Vector2Int>(); // init starting point prioQueue.Add(graph[start.x, start.y]); while (prioQueue.Count > 0) { Node current = prioQueue[0]; prioQueue.RemoveAt(0); List <Vector2Int> neighborVectors = findNeighbors(current.vector.x, current.vector.y); foreach (Vector2Int v in neighborVectors) { BaseTileHandler b = tileMap[v.x + "," + v.y]; if (b.getCurrentUnit() != null && b.getCurrentUnit().isAlly != isAlly) { // found enemy path.Add(b.getCoordinate()); path.Add(current.vector); while (current.parent != null) { path.Add(current.parent.vector); current = current.parent; } return(path); } else { // check if the weight is smaller than the recorded weight int weight = current.cost + b.getNodeWeight(isAlly); if (graph[v.x, v.y].cost > weight) { graph[v.x, v.y].cost = weight; graph[v.x, v.y].parent = current; if (!prioQueue.Contains(graph[v.x, v.y])) { prioQueue.Add(graph[v.x, v.y]); } } } // sort the queue by weight after adding all neighbors prioQueue.Sort((one, two) => one.cost.CompareTo(two.cost)); visited.Add(current.vector); } } return(null); }