public void Attack() { if (cursor.currSelectedChar.GetComponent <Stats>().equippedWeapon.typeOfWeapon == Weapon.WeaponType.STAFF) { for (int i = 0; i < cursor.currSelectedChar.GetComponent <Stats>().weapons.Count; i++) { if (cursor.currSelectedChar.GetComponent <Stats>().weapons[i].typeOfWeapon != Weapon.WeaponType.STAFF) { cursor.currSelectedChar.GetComponent <Stats>().equippedWeapon = cursor.currSelectedChar.GetComponent <Stats>().weapons[i]; } } } if (cursor.currSelectedChar.GetComponent <Stats>().equippedWeapon.typeOfWeapon != Weapon.WeaponType.STAFF) { cursor.doneCalc = false; tileData.DeselectMovement(); cursor.currSelectedChar.GetComponent <Attack>().indexEnemies = 0; cursor.currSelectedChar.GetComponent <Attack>().CheckAttackRange(false); } }
private void Controls(RaycastHit2D hit) { if (!selectPanel.activeSelf && !attackPanel.activeSelf && !itemPanel.activeSelf) { if (Input.GetButtonDown("Horizontal") && Input.GetAxis("Horizontal") > 0) { transform.position = new Vector3(transform.position.x + 1, transform.position.y); } else if (Input.GetButtonDown("Horizontal") && (Input.GetAxis("Horizontal") < 0)) { transform.position = new Vector3(transform.position.x - 1, transform.position.y); } if (Input.GetButtonDown("Vertical") && Input.GetAxis("Vertical") > 0) { transform.position = new Vector3(transform.position.x, transform.position.y + 1); } else if (Input.GetButtonDown("Vertical") && (Input.GetAxis("Vertical") < 0)) { transform.position = new Vector3(transform.position.x, transform.position.y - 1); } if (Input.GetButtonDown("Submit")) { if (hit.collider != null) { if (currSelectedChar == null) { if (hit.collider.CompareTag("Player") && !hit.collider.GetComponent <PlayerMovement>().wait) { currSelectedChar = hit.collider.gameObject; currSelectedChar.GetComponent <PlayerMovement>().Selected(); currSelectedChar.GetComponent <BoxCollider2D>().enabled = false; } if (hit.collider.CompareTag("Enemy")) { currSelectedChar = hit.collider.gameObject; currSelectedChar.GetComponent <EnemyAI>().Selected(); } } if (currSelectedChar != null && currSelectedChar.GetComponent <PlayerMovement>() != null) { if (hit.collider.CompareTag("MoveTile")) { currSelectedChar.GetComponent <PlayerMovement>().oldPos = currSelectedChar.transform.position; currSelectedChar.transform.position = hit.collider.transform.position; currSelectedChar.transform.position = new Vector3(transform.position.x, Mathf.Round(currSelectedChar.transform.position.y) - 0.5f); CheckHealRange(); selectPanel.SetActive(true); EventSystem.current.SetSelectedGameObject(null); EventSystem.current.SetSelectedGameObject(selectPanel.transform.GetChild(0).GetChild(0).gameObject); } } } } if (Input.GetButtonDown("Cancel") && currSelectedChar != null) { tileData.DeselectMovement(); currSelectedChar.GetComponent <BoxCollider2D>().enabled = true; currSelectedChar = null; } } else if (selectPanel.activeSelf && currSelectedChar != null) { if (Input.GetButtonDown("Cancel")) { currSelectedChar.transform.position = currSelectedChar.GetComponent <PlayerMovement>().oldPos; currSelectedChar.GetComponent <PlayerMovement>().Selected(); selectPanel.SetActive(false); } } else if (attackPanel.activeSelf && currSelectedChar != null) { if (Input.GetButtonDown("Cancel")) { attackPanel.SetActive(false); currSelectedChar.GetComponent <Attack>().target = null; currSelectedChar.GetComponent <Attack>().indexEnemies = 0; selectPanel.SetActive(true); EventSystem.current.SetSelectedGameObject(null); EventSystem.current.SetSelectedGameObject(selectPanel.transform.GetChild(0).GetChild(0).gameObject); } else if (Input.GetButtonDown("Submit") && doneCalc) { attackPanel.SetActive(false); battlePanel.SetActive(true); BM.Battle(currSelectedChar.GetComponent <Stats>(), currSelectedChar.GetComponent <Attack>().target.GetComponent <Stats>(), currSelectedChar.GetComponent <Attack>().distance, true); } } else if (itemPanel.activeSelf && currSelectedChar != null) { if (Input.GetButtonDown("Cancel")) { foreach (Transform child in itemPanel.transform.GetChild(0)) { Destroy(child.gameObject); } itemPanel.SetActive(false); selectPanel.SetActive(true); EventSystem.current.SetSelectedGameObject(null); EventSystem.current.SetSelectedGameObject(selectPanel.transform.GetChild(0).GetChild(0).gameObject); } } }
public void GetTarget() { if (!wait) { Debug.Log("-------------- " + name + " searching target --------------"); bool targetInRange = false; List <GameObject> targets = new List <GameObject>(); int x = Mathf.RoundToInt(transform.position.x - 0.5f); int y = Mathf.RoundToInt(transform.position.y - 0.5f); current = true; tileData.DeselectMovement(); Selected(); //Target priority for (int i = 0; i < tileData.players.Count; i++) { if (tileData.rowsMovement[Mathf.RoundToInt(-tileData.players[i].transform.position.y + 0.5f)].transform.GetChild(Mathf.RoundToInt(tileData.players[i].transform.position.x - 0.5f)).CompareTag("MoveTile") || tileData.rowsMovement[Mathf.RoundToInt(-tileData.players[i].transform.position.y + 0.5f)].transform.GetChild(Mathf.RoundToInt(tileData.players[i].transform.position.x - 0.5f)).CompareTag("MoveTileRed")) { targetInRange = true; targets.Add(tileData.players[i]); Debug.Log(tileData.players[i] + " added to the list"); } } if (!targetInRange) { Debug.Log("-------------- not listening --------------"); target = GetClosestPlayer(tileData.players); } else { float bestAcc = 0; //Check if someone killable with highest hitrate and doesn't die themself for (int i = 0; i < targets.Count; i++) { int killOnHit = 0; int xEnemy = Mathf.RoundToInt(targets[i].transform.position.x - 0.5f); int yEnemy = Mathf.RoundToInt(targets[i].transform.position.y - 0.5f); Attack attack = GetComponent <Attack>(); Stats targetStats = targets[i].GetComponent <Stats>(); Stats stats = GetComponent <Stats>(); //Damage calc float damage = attack.CalcDamage(gameObject, targets[i]); int doubling = attack.CalcSpeed(gameObject, targets[i]); //Hit calc float hit = attack.CalcHit(gameObject); float enemyEvade = attack.CalcEvade(targets[i], xEnemy, yEnemy); float acc = attack.CalcAccuracy(hit, enemyEvade, gameObject); //Player attacks back check float distance = Mathf.Abs(transform.position.x - targets[i].transform.position.x) + Mathf.Abs(transform.position.y - targets[i].transform.position.y); float enemyDamage = 0; int enemyDoubling = 0; float enemyHit = 0; float evade = 0; float enemyAcc = 0; if (distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || distance != 1 && distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& !targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || targets[i].GetComponent <Stats>().equippedWeapon.counterAll) { if (targetStats.equippedWeapon.typeOfWeapon != Weapon.WeaponType.STAFF) { enemyDamage = attack.CalcDamage(targets[i], gameObject); enemyDoubling = attack.CalcSpeed(targets[i], gameObject); enemyHit = attack.CalcHit(targets[i]); evade = attack.CalcEvade(gameObject, x, y); enemyAcc = attack.CalcAccuracy(enemyHit, evade, targets[i]); } } if (targetStats.hp - damage <= 0) { Debug.Log(acc + " double " + doubling + " on " + targets[i].name); Debug.Log(bestAcc + " the best"); killOnHit = 1; if (acc > bestAcc) { bestAcc = acc; target = targets[i].transform; } } if (doubling == 2 && killOnHit == 0) { if (targetStats.hp - damage * 2 <= 0) { Debug.Log(acc + " double " + doubling + " on " + targets[i].name); Debug.Log(bestAcc + " the best"); killOnHit = 2; if (acc > bestAcc && stats.hp - enemyDamage > 0 || enemyAcc < 40) { bestAcc = acc; target = targets[i].transform; } } } else if (doubling == 4 && killOnHit == 0) { if (targetStats.hp - damage * 4 <= 0) { Debug.Log(acc + " double " + doubling + " on " + targets[i].name); Debug.Log(bestAcc + " the best"); killOnHit = 4; if (acc > bestAcc && enemyDoubling != 4 || stats.hp - enemyDamage * 3 > 0 && stats.hp - enemyDamage * 2 > 0 || enemyAcc < 40) { bestAcc = acc; target = targets[i].transform; } } } } //Check if someone almost dies from this attack and above and inclusive 65% if (target == null) { Debug.Log("-------------- Can't kill --------------"); float leastHealth = 9999; for (int i = 0; i < targets.Count; i++) { int xEnemy = Mathf.RoundToInt(targets[i].transform.position.x - 0.5f); int yEnemy = Mathf.RoundToInt(targets[i].transform.position.y - 0.5f); Attack attack = GetComponent <Attack>(); Stats targetStats = targets[i].GetComponent <Stats>(); Stats stats = GetComponent <Stats>(); //Damage calc float damage = attack.CalcDamage(gameObject, targets[i]); int doubling = attack.CalcSpeed(gameObject, targets[i]); //Hit calc float hit = attack.CalcHit(gameObject); float enemyEvade = attack.CalcEvade(targets[i], xEnemy, yEnemy); float acc = attack.CalcAccuracy(hit, enemyEvade, gameObject); //Player attacks back check float distance = Mathf.Abs(transform.position.x - targets[i].transform.position.x) + Mathf.Abs(transform.position.y - targets[i].transform.position.y); float enemyDamage = 0; int enemyDoubling = 0; float enemyHit = 0; float evade = 0; float enemyAcc = 0; if (distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || distance != 1 && distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& !targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || targets[i].GetComponent <Stats>().equippedWeapon.counterAll) { if (targetStats.equippedWeapon.typeOfWeapon != Weapon.WeaponType.STAFF) { enemyDamage = attack.CalcDamage(targets[i], gameObject); enemyDoubling = attack.CalcSpeed(targets[i], gameObject); enemyHit = attack.CalcHit(targets[i]); evade = attack.CalcEvade(gameObject, x, y); enemyAcc = attack.CalcAccuracy(enemyHit, evade, targets[i]); } } if (targetStats.hp - damage * doubling < leastHealth && acc >= 65) { Debug.Log((targetStats.hp - damage * doubling) + " double " + doubling + " on " + targets[i].name); Debug.Log(leastHealth + " the best"); if (stats.hp - enemyDamage * enemyDoubling > 0 || enemyAcc < 40) { leastHealth = targetStats.hp; target = targets[i].transform; } } } } //Get highest hitrate if (target == null) { Debug.Log("-------------- Low hitrate --------------"); float bestAccu = 0; for (int i = 0; i < targets.Count; i++) { int xEnemy = Mathf.RoundToInt(targets[i].transform.position.x - 0.5f); int yEnemy = Mathf.RoundToInt(targets[i].transform.position.y - 0.5f); Attack attack = GetComponent <Attack>(); Stats targetStats = targets[i].GetComponent <Stats>(); Stats stats = GetComponent <Stats>(); //Hit calc float hit = attack.CalcHit(gameObject); float enemyEvade = attack.CalcEvade(targets[i], xEnemy, yEnemy); float acc = attack.CalcAccuracy(hit, enemyEvade, gameObject); //Player attacks back check float distance = Mathf.Abs(transform.position.x - targets[i].transform.position.x) + Mathf.Abs(transform.position.y - targets[i].transform.position.y); float enemyHit = 0; float evade = 0; float enemyAcc = 0; if (distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || distance != 1 && distance <= targets[i].GetComponent <Stats>().equippedWeapon.range&& !targets[i].GetComponent <Stats>().equippedWeapon.rangeOneAndTwo || targets[i].GetComponent <Stats>().equippedWeapon.counterAll) { if (targetStats.equippedWeapon.typeOfWeapon != Weapon.WeaponType.STAFF) { enemyHit = attack.CalcHit(targets[i]); evade = attack.CalcEvade(gameObject, x, y); enemyAcc = attack.CalcAccuracy(enemyHit, evade, targets[i]); } } Debug.Log(acc + " on " + targets[i].name); Debug.Log(bestAccu + " the best"); if (acc > bestAccu) { bestAccu = acc; target = targets[i].transform; } } } } if (target != null) { Debug.Log("TARGET = " + target.name); Debug.Log("-------------- Walking --------------"); WalkingTowardsTarget(x, y, false, false, false, false); } else { tileData.DeselectMovement(); } } }
private IEnumerator AttackOrder(Stats player, Stats enemy, Stats damageHolder, GameObject playerSprite, GameObject enemySprite, float distance, int i, bool twiceFourDoubling) { Debug.Log(playerSprite); Debug.Log(enemySprite); playerSprite.GetComponent <AttackingInBattle>().done = false; enemySprite.GetComponent <AttackingInBattle>().done = false; Attack(player, damageHolder.GetComponent <Attack>().damage, damageHolder.GetComponent <Attack>().acc, damageHolder.GetComponent <Attack>().crit, playerSprite, enemySprite);//1st player while (!playerSprite.GetComponent <AttackingInBattle>().done) { yield return(new WaitForSeconds(0.2f)); } playerSprite.GetComponent <Animator>().Play("Idle"); if (damageHolder.GetComponent <Attack>().enemyDoubling == i && enemy.hp > 0 && distance <= enemy.equippedWeapon.range && enemy.equippedWeapon.rangeOneAndTwo || distance != 1 && distance <= enemy.equippedWeapon.range && !enemy.equippedWeapon.rangeOneAndTwo || enemy.equippedWeapon.counterAll) { enemySprite.GetComponent <AttackingInBattle>().done = false; playerSprite.GetComponent <AttackingInBattle>().done = false; Attack(enemy, damageHolder.GetComponent <Attack>().enemyDamage, damageHolder.GetComponent <Attack>().enemyAcc, damageHolder.GetComponent <Attack>().enemyCrit, enemySprite, playerSprite);//1st enemy while (!enemySprite.GetComponent <AttackingInBattle>().done) { yield return(new WaitForSeconds(0.2f)); } enemySprite.GetComponent <Animator>().Play("Idle"); if (player.hp <= 0) { if (damageHolder == playerBattle) { tileData.players.Remove(player.gameObject); tileData.currMapCharPos[Mathf.RoundToInt(-player.GetComponent <PlayerMovement>().oldPos.y + 0.5f), Mathf.RoundToInt(player.GetComponent <PlayerMovement>().oldPos.x - 0.5f)] = 0; tileData.currMapCharPos[Mathf.RoundToInt(-player.transform.position.y + 0.5f), Mathf.RoundToInt(player.transform.position.x - 0.5f)] = 0; } else { tileData.enemiesInGame.Remove(player.gameObject); tileData.currMapCharPos[Mathf.RoundToInt(-player.transform.position.y + 0.5f), Mathf.RoundToInt(player.transform.position.x - 0.5f)] = 0; if (xp > 0 && playerBattle != null) { leveling = true; StartCoroutine(Leveling()); while (leveling) { yield return(new WaitForSeconds(0.5f)); } } } Destroy(player.gameObject); yield return(new WaitForSeconds(1)); Destroy(playerSprite); Destroy(enemySprite); cursor.battlePanel.SetActive(false); tileData.DeselectMovement(); if (damageHolder == enemyBattle) { tileData.DeselectMovement(); BSM.GiveTurnToAI(); } } } else if (enemy.hp <= 0) { tileData.currMapCharPos[Mathf.RoundToInt(-enemy.transform.position.y + 0.5f), Mathf.RoundToInt(enemy.transform.position.x - 0.5f)] = 0; if (damageHolder == playerBattle) { xp *= 10; tileData.enemiesInGame.Remove(enemy.gameObject); } else { tileData.players.Remove(enemy.gameObject); } Destroy(enemy.gameObject); } yield return(new WaitForSeconds(0.2f)); if (damageHolder != null) { if (player.hp > 0 && enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 2 && i == 1 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 1 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 2 || enemy.hp > 0 && damageHolder.GetComponent <Attack>().doubling == 4 && i == 4 && twiceFourDoubling) { int j = 0; switch (i) { case 1: j = 2; break; case 2: j = 4; twiceFourDoubling = true; break; case 4: twiceFourDoubling = false; break; } StartCoroutine(AttackOrder(player, enemy, damageHolder, playerSprite, enemySprite, distance, j, twiceFourDoubling)); } else { if (xp > 0 && playerBattle != null) { leveling = true; StartCoroutine(Leveling()); while (leveling) { yield return(new WaitForSeconds(0.5f)); } } yield return(new WaitForSeconds(1)); Destroy(playerSprite); Destroy(enemySprite); cursor.battlePanel.SetActive(false); if (damageHolder == playerBattle) { GameObject.FindGameObjectWithTag("Canvas").GetComponent <SelectChoices>().Wait(); } else { enemyBattle.GetComponent <EnemyAI>().wait = true; tileData.DeselectMovement(); BSM.enemyIndex++; BSM.GiveTurnToAI(); } } } }