Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class BattleManager : MonoBehaviour
- {
- public static BattleManager instance;
- public bool battleActive;
- public GameObject battleScene;
- public Transform[] playerPositions;
- public Transform[] enemyPositions;
- public BattleChar[] playerPrefabs;
- public BattleChar[] enemyPrefabs;
- public List<BattleChar> activebattlers = new List<BattleChar>();
- public int currentturn;
- public bool turnWait;
- public GameObject uiButtonHolder;
- public BattleMove[] battleMoves;
- public GameObject attackEffect;
- public DmgNumber damageNumber;
- public Text[] playerName, playerHP, playerMP;
- public GameObject targetMenu;
- public BattleTargetButton[] theButtons;
- public GameObject magicMenu;
- public BattleMagicSelect[] magicButtons;
- public BattleNotification battleNotice;
- public int fleeChance = 35;
- public GameObject itemPanel;
- public GameObject itemslotContainers;
- public Item selectedItem;
- public Text itemName, itemDesc;
- public string gameOverScene;
- private bool fleeing;
- public bool cantFlee;
- public int rewardExp;
- public string[] rewardItems;
- // Start is called before the first frame update
- void Start()
- {
- instance = this;
- DontDestroyOnLoad(gameObject);
- }
- // Update is called once per frame
- void Update()
- {
- if (battleActive)
- {
- if (turnWait)
- {
- if (activebattlers[currentturn].isPlayer)
- {
- uiButtonHolder.SetActive(true);
- }
- else
- {
- uiButtonHolder.SetActive(false);
- //enemies should attack
- StartCoroutine(EnemyMoveCo());
- }
- }
- if (Input.GetKeyDown(KeyCode.N))
- {
- NextTurn();
- }
- }
- }
- public void BattleStart(string[] enemiesToSpawn, bool setcantFlee)
- {
- if (!battleActive)
- {
- cantFlee = setcantFlee;
- battleActive = true;
- Gamemanager.instance.battleActive = true;
- transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, transform.position.z);
- battleScene.SetActive(true);
- // AudioManager.instance.PlayBGM(0);
- for (int i = 0; i < playerPositions.Length; i++)
- {
- if (Gamemanager.instance.playerStats[i].gameObject.activeInHierarchy)
- {
- for (int j = 0; j < playerPrefabs.Length; j++)
- {
- if (playerPrefabs[j].charName == Gamemanager.instance.playerStats[i].charName)
- {
- BattleChar newPlayer = Instantiate(playerPrefabs[j], playerPositions[i].position, playerPositions[i].rotation);
- newPlayer.transform.parent = playerPositions[i];
- activebattlers.Add(newPlayer);
- CharStats thePlayer = Gamemanager.instance.playerStats[i];
- activebattlers[i].currentHP = thePlayer.currentHP;
- activebattlers[i].maxHP = thePlayer.maxHp;
- activebattlers[i].currentMP = thePlayer.currentMP;
- activebattlers[i].maxMP = thePlayer.maxMP;
- activebattlers[i].strength = thePlayer.strength;
- activebattlers[i].defense = thePlayer.defense;
- activebattlers[i].wpnPower = thePlayer.weaponPwr;
- activebattlers[i].armrPower = thePlayer.armorPwr;
- }
- }
- }
- }
- for (int i = 0; i < enemiesToSpawn.Length; i++)
- {
- if (enemiesToSpawn[i] != "")
- {
- for (int j = 0; j < enemyPrefabs.Length; j++)
- {
- if (enemyPrefabs[j].charName == enemiesToSpawn[i])
- {
- BattleChar newEnemy = Instantiate(enemyPrefabs[j], enemyPositions[i].position, enemyPositions[i].rotation);
- newEnemy.transform.parent = enemyPositions[i];
- activebattlers.Add(newEnemy);
- }
- }
- }
- }
- turnWait = true;
- //use .COunt because it is a list. .Lenght for arrays, and .Count for lists. It ranges from first inclusive to last (last non inclusive)
- currentturn = Random.Range(0, activebattlers.Count);
- UpdateUIStats();
- }
- }
- public void NextTurn()
- {
- currentturn++;
- if (currentturn >= activebattlers.Count)
- {
- currentturn = 0;
- }
- turnWait = true;
- UpdateBattle();
- UpdateUIStats();
- }
- public void UpdateBattle()
- {
- bool allEnemiesDead = true;
- bool allPlayersDead = true;
- for (int i = 0; i < activebattlers.Count; i++)
- {
- if (activebattlers[i].currentHP < 0)
- {
- activebattlers[i].currentHP = 0;
- }
- if (activebattlers[i].currentHP == 0)
- {
- //handle dead battler
- if(activebattlers[i].isPlayer)
- {
- activebattlers[i].theSprite.sprite = activebattlers[i].deadSprite;
- } else
- {
- activebattlers[i].enemyFade();
- }
- }
- else
- {
- if (activebattlers[i].isPlayer)
- {
- allPlayersDead = false;
- activebattlers[i].theSprite.sprite = activebattlers[i].AliveSprite;
- }
- else
- {
- allEnemiesDead = false;
- }
- }
- }
- if (allEnemiesDead || allPlayersDead)
- {
- if (allEnemiesDead)
- {
- //end battle in victory
- StartCoroutine(EndBattleCo());
- }
- else
- {
- //end battle in defeat
- StartCoroutine(GameOverCo());
- }
- /* battleScene.SetActive(false);
- Gamemanager.instance.battleActive = false;
- battleActive = false;*/
- }
- else
- {
- while (activebattlers[currentturn].currentHP == 0)
- {
- currentturn++;
- if (currentturn >= activebattlers.Count)
- {
- currentturn = 0;
- }
- }
- }
- }
- public IEnumerator EnemyMoveCo()
- {
- turnWait = false;
- yield return new WaitForSeconds(1f);
- EnemyAttack();
- yield return new WaitForSeconds(1f);
- NextTurn();
- }
- public void EnemyAttack()
- {
- List<int> players = new List<int>();
- for (int i = 0; i < activebattlers.Count; i++)
- {
- if (activebattlers[i].isPlayer && activebattlers[i].currentHP > 0)
- {
- players.Add(i);
- }
- }
- int selectedTarget = players[Random.Range(0, players.Count)];
- //activeBattlers[selectedTarget].currentHp -= 30;
- int selectAttack = Random.Range(0, activebattlers[currentturn].movesAvalible.Length);
- int movePower = 0;
- for (int i = 0; i < battleMoves.Length; i++)
- {
- if (battleMoves[i].moveName == activebattlers[currentturn].movesAvalible[selectAttack])
- {
- Instantiate(battleMoves[i].theEffect, activebattlers[selectedTarget].transform.position, activebattlers[selectedTarget].transform.rotation);
- movePower = battleMoves[i].movePower;
- }
- }
- Instantiate(attackEffect, activebattlers[currentturn].transform.position, activebattlers[currentturn].transform.rotation);
- DealDamage(selectedTarget, movePower);
- }
- public void DealDamage(int target, int movePower)
- {
- float atkPwr = activebattlers[currentturn].strength + activebattlers[currentturn].wpnPower;
- float defPwr = activebattlers[target].defense + activebattlers[target].armrPower;
- float damageCalc = (atkPwr / defPwr) * movePower * Random.Range(0.9f, 1.1f);
- int damageToGive = Mathf.RoundToInt(damageCalc);
- Debug.Log(activebattlers[currentturn].charName + " is dealing " + damageCalc + "(" + damageToGive + ") damage to " + activebattlers[target].charName);
- activebattlers[target].currentHP -= damageToGive;
- Instantiate(damageNumber, activebattlers[target].transform.position, activebattlers[target].transform.rotation).setDmg(damageToGive);
- UpdateUIStats();
- }
- public void UpdateUIStats()
- {
- for (int i = 0; i < playerName.Length; i++)
- {
- if (activebattlers.Count > i)
- {
- if (activebattlers[i].isPlayer)
- {
- BattleChar playerData = activebattlers[i];
- playerName[i].gameObject.SetActive(true);
- playerName[i].text = playerData.charName;
- playerHP[i].text = Mathf.Clamp(playerData.currentHP, 0, int.MaxValue) + "/" + playerData.maxHP;
- playerMP[i].text = Mathf.Clamp(playerData.currentMP, 0, int.MaxValue) + "/" + playerData.maxMP;
- }
- else
- {
- playerName[i].gameObject.SetActive(false);
- }
- }
- else
- {
- playerName[i].gameObject.SetActive(false);
- }
- }
- }
- public void PlayerAttack(string moveName, int selectedTarget)
- {
- int movePower = 0;
- for (int i = 0; i < battleMoves.Length; i++)
- {
- if (battleMoves[i].moveName == moveName)
- {
- Instantiate(battleMoves[i].theEffect, activebattlers[selectedTarget].transform.position, activebattlers[selectedTarget].transform.rotation);
- movePower = battleMoves[i].movePower;
- }
- }
- Instantiate(attackEffect, activebattlers[currentturn].transform.position, activebattlers[currentturn].transform.rotation);
- DealDamage(selectedTarget, movePower);
- uiButtonHolder.SetActive(false);
- targetMenu.SetActive(false);
- NextTurn();
- }
- public void OpenTargetMenu(string moveName)
- {
- targetMenu.SetActive(true);
- List<int> Enemies = new List<int>();
- for (int i = 0; i < activebattlers.Count; i++)
- {
- if (!activebattlers[i].isPlayer)
- {
- Enemies.Add(i);
- }
- }
- for (int i = 0; i < theButtons.Length; i++)
- {
- if (Enemies.Count > i && activebattlers[Enemies[i]].currentHP > 0)
- {
- theButtons[i].gameObject.SetActive(true);
- theButtons[i].moveName = moveName;
- theButtons[i].activeTar = Enemies[i];
- theButtons[i].tarName.text = activebattlers[Enemies[i]].charName;
- }
- else
- {
- theButtons[i].gameObject.SetActive(false);
- }
- }
- }
- public void OpenMagicMenu()
- {
- magicMenu.SetActive(true);
- for (int i = 0; i < magicButtons.Length; i++)
- {
- if (activebattlers[currentturn].movesAvalible.Length > i)
- {
- magicButtons[i].gameObject.SetActive(true);
- magicButtons[i].spellName = activebattlers[currentturn].movesAvalible[i];
- magicButtons[i].nameText.text = magicButtons[i].spellName;
- for (int j = 0; j < battleMoves.Length; j++)
- {
- if (battleMoves[j].moveName == magicButtons[i].spellName)
- {
- magicButtons[i].spellCost = battleMoves[j].moveCost;
- magicButtons[i].costText.text = magicButtons[i].spellCost.ToString();
- }
- }
- }
- else
- {
- magicButtons[i].gameObject.SetActive(false);
- }
- }
- }
- public void Flee()
- {
- if (cantFlee)
- {
- battleNotice.notificationText.text = "Why Are You Running?";
- battleNotice.Activate();
- }
- else
- {
- int fleeSuccess = Random.Range(0, 100);
- if (fleeSuccess <= fleeChance)
- {
- //end the battle
- //battleActive = false;
- // battleScene.SetActive(false);
- StartCoroutine(EndBattleCo());
- }
- else
- {
- NextTurn();
- battleNotice.notificationText.text = "Couldn't Flee!";
- battleNotice.Activate();
- }
- }
- }
- public void EquipWeaponMidBattle()
- {
- battleNotice.notificationText.text = "Can't Equip Weapon Mid Battle!";
- battleNotice.Activate();
- }
- public void EquipArmorMidBattle()
- {
- battleNotice.notificationText.text = "Can't Equip Armor Mid Battle!";
- battleNotice.Activate();
- }
- public IEnumerator EndBattleCo()
- {
- battleActive = false;
- uiButtonHolder.SetActive(false);
- targetMenu.SetActive(false);
- magicMenu.SetActive(false);
- itemPanel.SetActive(false);
- yield return new WaitForSeconds(.5f);
- UIFade.instance.FadeToBlack();
- yield return new WaitForSeconds(1.5f);
- for(int i = 0; i < activebattlers.Count; i++)
- {
- if(activebattlers[i].isPlayer)
- {
- for (int j = 0; j < Gamemanager.instance.playerStats.Length; j++)
- {
- if(activebattlers[i].charName == Gamemanager.instance.playerStats[j].charName)
- {
- Gamemanager.instance.playerStats[j].currentHP = activebattlers[i].currentHP;
- Gamemanager.instance.playerStats[j].currentHP = activebattlers[i].currentMP;
- }
- }
- }
- Destroy(activebattlers[i].gameObject);
- }
- UIFade.instance.FadeFromBlack();
- battleScene.SetActive(false);
- activebattlers.Clear();
- currentturn = 0;
- if(fleeing)
- {
- Gamemanager.instance.battleActive = false;
- fleeing = false;
- } else
- {
- BattleReward.instance.OpenRewardScreen(rewardExp, rewardItems);
- }
- }
- public IEnumerator GameOverCo()
- {
- battleActive = false;
- UIFade.instance.FadeToBlack();
- yield return new WaitForSeconds(1.5f);
- battleScene.SetActive(false);
- SceneManager.LoadScene(gameOverScene);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement