|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using TMPro; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +public class AIBrain : MonoBehaviour |
| 7 | +{ |
| 8 | + [SerializeField] private float waitBeforeActing; |
| 9 | + [SerializeField] private float waitAfterActing; |
| 10 | + [Range(0f, 100f)] |
| 11 | + [SerializeField] private float ignoreShootChance = 20f; |
| 12 | + [SerializeField] private float ignoreHealChance = 20f; |
| 13 | + |
| 14 | + private CharacterControl charCon; |
| 15 | + |
| 16 | + private void Awake() |
| 17 | + { |
| 18 | + charCon = this.GetComponent<CharacterControl>(); |
| 19 | + } |
| 20 | + |
| 21 | + public void ChooseAction() |
| 22 | + { |
| 23 | + StartCoroutine(ChooseCo()); |
| 24 | + } |
| 25 | + |
| 26 | + public IEnumerator ChooseCo() //Make Choices Heres |
| 27 | + { |
| 28 | + Debug.Log(name + " is choosing an action"); |
| 29 | + |
| 30 | + yield return new WaitForSeconds(waitBeforeActing); |
| 31 | + |
| 32 | + bool actionTaken = false; |
| 33 | + |
| 34 | + charCon.GetMeleeTargets(); //Getting the melee targets |
| 35 | + |
| 36 | + if(charCon.meleeTargets.Count > 0) //IF THERE ARE ENEMIES WITHIN MELEE RANGE |
| 37 | + { |
| 38 | + Debug.Log(name + "Chosen Melee"); |
| 39 | + |
| 40 | + charCon.currentMeleeTarget = Random.Range(0, charCon.meleeTargets.Count); |
| 41 | + |
| 42 | + GameManager.instance.currentActionCost = 1; |
| 43 | + |
| 44 | + StartCoroutine(WaitToEndAction(waitAfterActing)); |
| 45 | + |
| 46 | + charCon.animator.SetBool("isAttacking", true); |
| 47 | + charCon.DoMelee(); |
| 48 | + yield return new WaitForSeconds(1.5f); |
| 49 | + charCon.animator.SetBool("isAttacking", false); |
| 50 | + actionTaken = true; |
| 51 | + } |
| 52 | + |
| 53 | + if(!charCon.isPlayer && charCon.rangedEnemy || !charCon.isPlayer && charCon.magicalEnemy) //IF THE ENEMY IS RANGED & MAGICAL |
| 54 | + { |
| 55 | + charCon.GetRangedTargets(); |
| 56 | + if(actionTaken == false && charCon.rangedTargets.Count > 0) |
| 57 | + { |
| 58 | + if(Random.Range(0f, 100f) > ignoreShootChance) |
| 59 | + { |
| 60 | + Debug.Log(name + "Chosen Ranged"); |
| 61 | + |
| 62 | + charCon.currentRangedTarget = Random.Range(0, charCon.rangedTargets.Count); |
| 63 | + |
| 64 | + GameManager.instance.currentActionCost = 1; |
| 65 | + |
| 66 | + StartCoroutine(WaitToEndAction(waitAfterActing)); |
| 67 | + |
| 68 | + if (charCon.rangedEnemy) |
| 69 | + { |
| 70 | + charCon.animator.SetBool("isRangedAttacking", true); |
| 71 | + charCon.ShootTheTarget(); |
| 72 | + yield return new WaitForSeconds(1.5f); |
| 73 | + |
| 74 | + charCon.animator.SetBool("isRangedAttacking", false); |
| 75 | + actionTaken = true; |
| 76 | + } |
| 77 | + |
| 78 | + if (charCon.magicalEnemy) |
| 79 | + { |
| 80 | + charCon.animator.SetBool("isSpellCasting", true); |
| 81 | + charCon.ShootTheTarget(); |
| 82 | + yield return new WaitForSeconds(1.5f); |
| 83 | + |
| 84 | + charCon.animator.SetBool("isSpellCasting", false); |
| 85 | + actionTaken = true; |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if(!charCon.isPlayer && charCon.healerEnemy) //IF THE ENEMY IS A HEALER |
| 93 | + { |
| 94 | + charCon.GetTargetsToHeal(); |
| 95 | + charCon.currentHealingTarget = Random.Range(0, charCon.healingTargets.Count); |
| 96 | + if (charCon.healingTargets.Count > 0 && charCon.healingTargets[charCon.currentHealingTarget].currentHealth |
| 97 | + < charCon.healingTargets[charCon.currentHealingTarget].maxHealth) |
| 98 | + { |
| 99 | + |
| 100 | + if (actionTaken == false) |
| 101 | + { |
| 102 | + if (Random.Range(0f, 100f) > ignoreHealChance) |
| 103 | + { |
| 104 | + Debug.Log(name + "Choose to heal"); |
| 105 | + |
| 106 | + GameManager.instance.ActivePlayer.transform.LookAt(charCon.healingTargets[charCon.currentHealingTarget].transform); |
| 107 | + |
| 108 | + GameManager.instance.currentActionCost = 1; |
| 109 | + |
| 110 | + StartCoroutine(WaitToEndAction(waitAfterActing)); |
| 111 | + |
| 112 | + charCon.animator.SetBool("isHealing", true); |
| 113 | + charCon.healingTargets[charCon.currentHealingTarget].HealTarget(charCon.healingAmount); |
| 114 | + yield return new WaitForSeconds(1.5f); |
| 115 | + |
| 116 | + charCon.animator.SetBool("isHealing", false); |
| 117 | + actionTaken = true; |
| 118 | + |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + if (!actionTaken) //IF NO ACTION TAKEN |
| 126 | + { |
| 127 | + var potentialMovePoints = MoveGrid.instance.GetMovePointsInRange(charCon.moveRange, transform.position); //Get the move points in range |
| 128 | + |
| 129 | + if (potentialMovePoints.Count > 0) |
| 130 | + { |
| 131 | + int nearestPlayer = 0; |
| 132 | + for (int i = 1; i < GameManager.instance.PlayerTeam.Count; i++) //Find the nearest player character |
| 133 | + { |
| 134 | + if (Vector3.Distance(transform.position, GameManager.instance.PlayerTeam[nearestPlayer].transform.position) |
| 135 | + > Vector3.Distance(transform.position, GameManager.instance.PlayerTeam[i].transform.position)) |
| 136 | + { |
| 137 | + nearestPlayer = i; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + int selectedPoint = 0; //Select one |
| 142 | + float closestDistance = 1000f; //Never gonna need this far but just to safe keeping |
| 143 | + for (int i = 0; i < potentialMovePoints.Count; i++) |
| 144 | + { |
| 145 | + if (Vector3.Distance(GameManager.instance.PlayerTeam[nearestPlayer].transform.position, potentialMovePoints[i]) < closestDistance) |
| 146 | + { |
| 147 | + closestDistance = Vector3.Distance(GameManager.instance.PlayerTeam[nearestPlayer].transform.position, potentialMovePoints[i]); |
| 148 | + selectedPoint = i; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + GameManager.instance.currentActionCost = 1; //ACTION COST FOR WALKING |
| 153 | + |
| 154 | + charCon.MoveToPoint(potentialMovePoints[selectedPoint]); //MOVE TO THAT POINT |
| 155 | + |
| 156 | + actionTaken = true; //ACTION TAKEN |
| 157 | + } |
| 158 | + |
| 159 | + if (!actionTaken) |
| 160 | + { |
| 161 | + //skip turn |
| 162 | + GameManager.instance.EndTurn(); |
| 163 | + |
| 164 | + Debug.Log(name + "Skipped Turn"); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + IEnumerator WaitToEndAction(float timeToWait) |
| 171 | + { |
| 172 | + Debug.Log("Waiting to end action"); |
| 173 | + yield return new WaitForSeconds(timeToWait); |
| 174 | + GameManager.instance.SpendTurnPoints(); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments