private void GetNextTile() { //Debug.Log("GetNextTile"); System.Random random = new System.Random(); // get a random tile from the neighboring tiles LinkedNode chosenTile = null; // first look for vacant attack points next to the current tile foreach (Transform attackPoint in PathBoard.attackPoints) { if (attackPoint.GetComponent <AttackPoint>().IsVacant(this)) { float distanceToAttackPoint = Vector2.Distance(currTile.transform.position, attackPoint.position); if (distanceToAttackPoint < 0.6) // todo: attack points could be neighbors of Path Tiles (LinkedNodes), but we would have to set them manually { // if this is the first attack point to be examined, pick it // else, pick it with a 50% chance int randomInt = random.Next(0, 2); if (chosenTile == null || randomInt > 0) { chosenTile = attackPoint.GetComponent <LinkedNode>(); } } } } //if (chosenTile != null) // if found attack point, set it as occupied by this enemy object //{ //AttackPoint attackPoint = chosenTile.GetComponent<AttackPoint>(); //if (attackPoint != null) //{ // attackPoint.SetOccupant(this); // latched = true; //} //} if (chosenTile == null) // if there weren't any attack points, find the next unvisited tile { if (currTile.GetComponent <LinkedNode>() != null) { List <LinkedNode> neighbors = currTile.GetComponent <LinkedNode>().GetNeighbors(); neighbors.RemoveAll(x => visitedTiles.Contains(x)); int nc = neighbors.Count; if (nc > 0) { chosenTile = neighbors[random.Next(0, nc)]; } } } if (chosenTile != null) { nextTile = chosenTile; } else { visitedTiles.Clear(); } }
private void Update() { if (!started) { return; } if (coughing) { CoughEffect(); } // update the hit countdown and the replication countdown each frame if (hitCountdown > 0f) { hitCountdown -= Time.deltaTime; } if (replicationCoundtown > 0f && !IsBeingAttacked() && !coughing) { replicationCoundtown -= Time.deltaTime; } if (replicationCoundtown <= 0f && !IsBeingAttacked() && !coughing) { Replicate(); } // if there's a melee unit attacking, // start attacking it (face it, move towards it, and if in range, hit) if (meleeAttackers.Count > 0) { //Debug.Log("attacking melee attacker"); // if it's latched, stop the latched animation if (latched) { attackPoint.SetOccupantActive(false); anim.SetBool("isAttackedWhileLatched", true); anim.SetBool("isMoving", true); anim.SetBool("isAtAP", false); if (!attackedWhileLatched) { attackedWhileLatched = true; } //Debug.Log("stopping latched animation"); //if (anim != null) //{ //anim.SetBool("isLatched", false); //anim.SetTrigger("isAttackedWhileLatched"); //} } AttackMeleeAttacker(); } else // if there's no melee attacker, continue towards the next tile { if (nextTile != null) { //Debug.Log("nextTile != null"); // if already latched, don't move if (latched) { anim.SetBool("isAttackedWhileLatched", false); // unless it's too far away from the attack point; in that case, move towards it if (attackPoint != null && Vector2.Distance(transform.position, attackPoint.transform.position) > 0.15f) { //Debug.Log("move closer to attack point"); attackPoint.SetOccupantActive(false); moveable.MoveDirectlyTowardsPosition(attackPoint.transform.position); anim.SetBool("isAtAP", false); } else { //Debug.Log("starting latched animation"); if (anim != null) { anim.SetBool("isStunned", Shop.instance.IsCoughing()); } if (attackedWhileLatched) { attackedWhileLatched = false; attackPoint.SetOccupantActive(!attackedWhileLatched); } anim.SetBool("isAtAP", true); anim.SetBool("isMoving", false); } } else { //Debug.Log("MoveDirectlyTowardsPosition"); moveable.MoveDirectlyTowardsPosition(nextTile.transform.position); if (Vector2.Distance(transform.position, nextTile.transform.position) <= 0.3f) { // it's reached the tile; go to the next tile, unless the current tile is an attack point AttackPoint ap = nextTile.GetComponent <AttackPoint>(); if (ap != null) { if (ap.IsVacant()) { //Debug.Log("ap != null && ap.IsVacant"); // if it's an attack point and it's still vacant, occupy it ap.SetOccupant(this); attackPoint = ap; latched = true; //anim.SetTrigger("isLatched"); anim.SetBool("isLatched", true); anim.SetBool("isMoving", false); } else // if nextTile is an AP that is occupied, clear visited list and get next tile { //Debug.Log("NEXTTILE IS OCCUPIED AP!!!!!!!! clearing visitedTiles and getting next tile!"); visitedTiles.Clear(); GetNextTile(); } } else // otherwise, continue on the path { VisitTile(nextTile); GetNextTile(); } } } } else { //Debug.Log("nextTile == null; calling GetNextTile()"); GetNextTile(); } } }