Advertisement
GoodNoodle

BattleManager

Jul 8th, 2022
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public class BattleManager : MonoBehaviour
  8. {
  9.  
  10.     public static BattleManager instance;
  11.  
  12.     public bool battleActive;
  13.  
  14.     public GameObject battleScene;
  15.  
  16.     public Transform[] playerPositions;
  17.     public Transform[] enemyPositions;
  18.  
  19.     public BattleChar[] playerPrefabs;
  20.     public BattleChar[] enemyPrefabs;
  21.  
  22.     public List<BattleChar> activebattlers = new List<BattleChar>();
  23.  
  24.     public int currentturn;
  25.     public bool turnWait;
  26.  
  27.     public GameObject uiButtonHolder;
  28.  
  29.     public BattleMove[] battleMoves;
  30.  
  31.     public GameObject attackEffect;
  32.  
  33.     public DmgNumber damageNumber;
  34.  
  35.     public Text[] playerName, playerHP, playerMP;
  36.  
  37.     public GameObject targetMenu;
  38.  
  39.     public BattleTargetButton[] theButtons;
  40.  
  41.     public GameObject magicMenu;
  42.  
  43.     public BattleMagicSelect[] magicButtons;
  44.  
  45.     public BattleNotification battleNotice;
  46.  
  47.     public int fleeChance = 35;
  48.  
  49.     public GameObject itemPanel;
  50.     public GameObject itemslotContainers;
  51.    
  52.     public Item selectedItem;
  53.  
  54.     public Text itemName, itemDesc;
  55.  
  56.     public string gameOverScene;
  57.  
  58.     private bool fleeing;
  59.  
  60.     public bool cantFlee;
  61.  
  62.     public int rewardExp;
  63.  
  64.     public string[] rewardItems;
  65.     // Start is called before the first frame update
  66.  
  67.     void Start()
  68.  
  69.     {
  70.  
  71.         instance = this;
  72.  
  73.         DontDestroyOnLoad(gameObject);
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.     // Update is called once per frame
  80.  
  81.     void Update()
  82.  
  83.     {
  84.  
  85.  
  86.  
  87.  
  88.  
  89.         if (battleActive)
  90.  
  91.         {
  92.  
  93.             if (turnWait)
  94.  
  95.             {
  96.  
  97.                 if (activebattlers[currentturn].isPlayer)
  98.  
  99.                 {
  100.  
  101.                     uiButtonHolder.SetActive(true);
  102.  
  103.  
  104.  
  105.                 }
  106.  
  107.                 else
  108.  
  109.                 {
  110.  
  111.                     uiButtonHolder.SetActive(false);
  112.  
  113.  
  114.  
  115.                     //enemies should attack
  116.  
  117.                     StartCoroutine(EnemyMoveCo());
  118.  
  119.                 }
  120.  
  121.             }
  122.  
  123.             if (Input.GetKeyDown(KeyCode.N))
  124.  
  125.             {
  126.  
  127.                 NextTurn();
  128.  
  129.             }
  130.  
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     public void BattleStart(string[] enemiesToSpawn, bool setcantFlee)
  138.  
  139.     {
  140.  
  141.         if (!battleActive)
  142.  
  143.         {
  144.  
  145.             cantFlee = setcantFlee;
  146.  
  147.             battleActive = true;
  148.  
  149.  
  150.  
  151.             Gamemanager.instance.battleActive = true;
  152.  
  153.  
  154.  
  155.             transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, transform.position.z);
  156.  
  157.  
  158.  
  159.             battleScene.SetActive(true);
  160.  
  161.  
  162.  
  163.            // AudioManager.instance.PlayBGM(0);
  164.  
  165.  
  166.  
  167.             for (int i = 0; i < playerPositions.Length; i++)
  168.  
  169.             {
  170.  
  171.                 if (Gamemanager.instance.playerStats[i].gameObject.activeInHierarchy)
  172.  
  173.                 {
  174.  
  175.                     for (int j = 0; j < playerPrefabs.Length; j++)
  176.  
  177.                     {
  178.  
  179.                         if (playerPrefabs[j].charName == Gamemanager.instance.playerStats[i].charName)
  180.  
  181.                         {
  182.  
  183.                             BattleChar newPlayer = Instantiate(playerPrefabs[j], playerPositions[i].position, playerPositions[i].rotation);
  184.  
  185.                             newPlayer.transform.parent = playerPositions[i];
  186.  
  187.                             activebattlers.Add(newPlayer);
  188.  
  189.  
  190.  
  191.  
  192.  
  193.                             CharStats thePlayer = Gamemanager.instance.playerStats[i];
  194.  
  195.                             activebattlers[i].currentHP = thePlayer.currentHP;
  196.  
  197.                             activebattlers[i].maxHP = thePlayer.maxHp;
  198.  
  199.                             activebattlers[i].currentMP = thePlayer.currentMP;
  200.  
  201.                             activebattlers[i].maxMP = thePlayer.maxMP;
  202.  
  203.                             activebattlers[i].strength = thePlayer.strength;
  204.  
  205.                             activebattlers[i].defense = thePlayer.defense;
  206.  
  207.                             activebattlers[i].wpnPower = thePlayer.weaponPwr;
  208.  
  209.                             activebattlers[i].armrPower = thePlayer.armorPwr;
  210.  
  211.  
  212.  
  213.                         }
  214.  
  215.                     }
  216.  
  217.  
  218.  
  219.                 }
  220.  
  221.             }
  222.  
  223.  
  224.  
  225.             for (int i = 0; i < enemiesToSpawn.Length; i++)
  226.  
  227.             {
  228.  
  229.                 if (enemiesToSpawn[i] != "")
  230.  
  231.                 {
  232.  
  233.                     for (int j = 0; j < enemyPrefabs.Length; j++)
  234.  
  235.                     {
  236.  
  237.                         if (enemyPrefabs[j].charName == enemiesToSpawn[i])
  238.  
  239.                         {
  240.  
  241.                             BattleChar newEnemy = Instantiate(enemyPrefabs[j], enemyPositions[i].position, enemyPositions[i].rotation);
  242.  
  243.                             newEnemy.transform.parent = enemyPositions[i];
  244.  
  245.                             activebattlers.Add(newEnemy);
  246.  
  247.                         }
  248.  
  249.  
  250.  
  251.                     }
  252.  
  253.                 }
  254.  
  255.             }
  256.  
  257.  
  258.  
  259.             turnWait = true;
  260.  
  261.             //use .COunt because it is a list. .Lenght for arrays, and .Count for lists. It ranges from first inclusive to last (last non inclusive)
  262.  
  263.             currentturn = Random.Range(0, activebattlers.Count);
  264.  
  265.  
  266.  
  267.             UpdateUIStats();
  268.  
  269.         }
  270.  
  271.     }
  272.  
  273.  
  274.  
  275.     public void NextTurn()
  276.  
  277.     {
  278.  
  279.         currentturn++;
  280.  
  281.         if (currentturn >= activebattlers.Count)
  282.  
  283.         {
  284.  
  285.             currentturn = 0;
  286.  
  287.         }
  288.  
  289.  
  290.  
  291.         turnWait = true;
  292.  
  293.  
  294.  
  295.         UpdateBattle();
  296.  
  297.         UpdateUIStats();
  298.  
  299.     }
  300.  
  301.  
  302.  
  303.     public void UpdateBattle()
  304.  
  305.     {
  306.  
  307.         bool allEnemiesDead = true;
  308.  
  309.         bool allPlayersDead = true;
  310.  
  311.  
  312.  
  313.         for (int i = 0; i < activebattlers.Count; i++)
  314.  
  315.         {
  316.  
  317.             if (activebattlers[i].currentHP < 0)
  318.  
  319.             {
  320.  
  321.                 activebattlers[i].currentHP = 0;
  322.  
  323.             }
  324.  
  325.  
  326.  
  327.             if (activebattlers[i].currentHP == 0)
  328.  
  329.             {
  330.  
  331.                 //handle dead battler
  332.                 if(activebattlers[i].isPlayer)
  333.                 {
  334.                     activebattlers[i].theSprite.sprite = activebattlers[i].deadSprite;
  335.                 } else
  336.                 {
  337.                    
  338.                     activebattlers[i].enemyFade();
  339.                 }
  340.  
  341.             }
  342.  
  343.             else
  344.  
  345.             {
  346.  
  347.                 if (activebattlers[i].isPlayer)
  348.  
  349.                 {
  350.  
  351.                     allPlayersDead = false;
  352.                     activebattlers[i].theSprite.sprite = activebattlers[i].AliveSprite;
  353.  
  354.                 }
  355.  
  356.                 else
  357.  
  358.                 {
  359.  
  360.                     allEnemiesDead = false;
  361.  
  362.                 }
  363.  
  364.             }
  365.  
  366.         }
  367.  
  368.  
  369.  
  370.         if (allEnemiesDead || allPlayersDead)
  371.  
  372.         {
  373.  
  374.             if (allEnemiesDead)
  375.  
  376.             {
  377.  
  378.                 //end battle in victory
  379.                 StartCoroutine(EndBattleCo());
  380.             }
  381.  
  382.             else
  383.  
  384.             {
  385.  
  386.                 //end battle in defeat
  387.                 StartCoroutine(GameOverCo());
  388.  
  389.             }
  390.  
  391.  
  392.  
  393.            /* battleScene.SetActive(false);
  394.  
  395.             Gamemanager.instance.battleActive = false;
  396.  
  397.             battleActive = false;*/
  398.  
  399.         }
  400.  
  401.         else
  402.  
  403.         {
  404.  
  405.             while (activebattlers[currentturn].currentHP == 0)
  406.  
  407.             {
  408.  
  409.                 currentturn++;
  410.  
  411.                 if (currentturn >= activebattlers.Count)
  412.  
  413.                 {
  414.  
  415.                     currentturn = 0;
  416.  
  417.                 }
  418.  
  419.             }
  420.  
  421.         }
  422.  
  423.     }
  424.  
  425.  
  426.  
  427.     public IEnumerator EnemyMoveCo()
  428.  
  429.     {
  430.  
  431.         turnWait = false;
  432.  
  433.         yield return new WaitForSeconds(1f);
  434.  
  435.         EnemyAttack();
  436.  
  437.         yield return new WaitForSeconds(1f);
  438.  
  439.         NextTurn();
  440.  
  441.     }
  442.  
  443.  
  444.  
  445.     public void EnemyAttack()
  446.  
  447.     {
  448.  
  449.         List<int> players = new List<int>();
  450.  
  451.         for (int i = 0; i < activebattlers.Count; i++)
  452.  
  453.         {
  454.  
  455.             if (activebattlers[i].isPlayer && activebattlers[i].currentHP > 0)
  456.  
  457.             {
  458.  
  459.                 players.Add(i);
  460.  
  461.             }
  462.  
  463.         }
  464.  
  465.  
  466.  
  467.         int selectedTarget = players[Random.Range(0, players.Count)];
  468.  
  469.  
  470.  
  471.         //activeBattlers[selectedTarget].currentHp -= 30;
  472.  
  473.  
  474.  
  475.         int selectAttack = Random.Range(0, activebattlers[currentturn].movesAvalible.Length);
  476.  
  477.         int movePower = 0;
  478.  
  479.         for (int i = 0; i < battleMoves.Length; i++)
  480.  
  481.         {
  482.  
  483.             if (battleMoves[i].moveName == activebattlers[currentturn].movesAvalible[selectAttack])
  484.  
  485.             {
  486.  
  487.                 Instantiate(battleMoves[i].theEffect, activebattlers[selectedTarget].transform.position, activebattlers[selectedTarget].transform.rotation);
  488.  
  489.                 movePower = battleMoves[i].movePower;
  490.  
  491.             }
  492.  
  493.         }
  494.  
  495.  
  496.  
  497.         Instantiate(attackEffect, activebattlers[currentturn].transform.position, activebattlers[currentturn].transform.rotation);
  498.  
  499.  
  500.  
  501.         DealDamage(selectedTarget, movePower);
  502.  
  503.     }
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.     public void DealDamage(int target, int movePower)
  512.  
  513.     {
  514.  
  515.         float atkPwr = activebattlers[currentturn].strength + activebattlers[currentturn].wpnPower;
  516.  
  517.         float defPwr = activebattlers[target].defense + activebattlers[target].armrPower;
  518.  
  519.  
  520.  
  521.         float damageCalc = (atkPwr / defPwr) * movePower * Random.Range(0.9f, 1.1f);
  522.  
  523.         int damageToGive = Mathf.RoundToInt(damageCalc);
  524.  
  525.  
  526.  
  527.         Debug.Log(activebattlers[currentturn].charName + " is dealing " + damageCalc + "(" + damageToGive + ") damage to " + activebattlers[target].charName);
  528.  
  529.  
  530.  
  531.         activebattlers[target].currentHP -= damageToGive;
  532.  
  533.  
  534.  
  535.         Instantiate(damageNumber, activebattlers[target].transform.position, activebattlers[target].transform.rotation).setDmg(damageToGive);
  536.  
  537.  
  538.  
  539.         UpdateUIStats();
  540.  
  541.     }
  542.  
  543.  
  544.  
  545.     public void UpdateUIStats()
  546.  
  547.     {
  548.  
  549.         for (int i = 0; i < playerName.Length; i++)
  550.  
  551.         {
  552.  
  553.             if (activebattlers.Count > i)
  554.  
  555.             {
  556.  
  557.                 if (activebattlers[i].isPlayer)
  558.  
  559.                 {
  560.  
  561.                     BattleChar playerData = activebattlers[i];
  562.  
  563.                     playerName[i].gameObject.SetActive(true);
  564.  
  565.  
  566.  
  567.                     playerName[i].text = playerData.charName;
  568.  
  569.                     playerHP[i].text = Mathf.Clamp(playerData.currentHP, 0, int.MaxValue) + "/" + playerData.maxHP;
  570.  
  571.                     playerMP[i].text = Mathf.Clamp(playerData.currentMP, 0, int.MaxValue) + "/" + playerData.maxMP;
  572.  
  573.                 }
  574.  
  575.                 else
  576.  
  577.                 {
  578.  
  579.                     playerName[i].gameObject.SetActive(false);
  580.  
  581.                 }
  582.  
  583.             }
  584.  
  585.             else
  586.  
  587.             {
  588.  
  589.                 playerName[i].gameObject.SetActive(false);
  590.  
  591.             }
  592.  
  593.  
  594.  
  595.         }
  596.  
  597.     }
  598.  
  599.  
  600.  
  601.     public void PlayerAttack(string moveName, int selectedTarget)
  602.  
  603.     {
  604.  
  605.  
  606.  
  607.         int movePower = 0;
  608.  
  609.         for (int i = 0; i < battleMoves.Length; i++)
  610.  
  611.         {
  612.  
  613.             if (battleMoves[i].moveName == moveName)
  614.  
  615.             {
  616.  
  617.                 Instantiate(battleMoves[i].theEffect, activebattlers[selectedTarget].transform.position, activebattlers[selectedTarget].transform.rotation);
  618.  
  619.                 movePower = battleMoves[i].movePower;
  620.  
  621.             }
  622.  
  623.         }
  624.  
  625.         Instantiate(attackEffect, activebattlers[currentturn].transform.position, activebattlers[currentturn].transform.rotation);
  626.  
  627.  
  628.  
  629.         DealDamage(selectedTarget, movePower);
  630.  
  631.  
  632.  
  633.         uiButtonHolder.SetActive(false);
  634.  
  635.         targetMenu.SetActive(false);
  636.  
  637.         NextTurn();
  638.  
  639.     }
  640.  
  641.  
  642.  
  643.     public void OpenTargetMenu(string moveName)
  644.  
  645.     {
  646.  
  647.         targetMenu.SetActive(true);
  648.  
  649.  
  650.  
  651.         List<int> Enemies = new List<int>();
  652.  
  653.         for (int i = 0; i < activebattlers.Count; i++)
  654.  
  655.         {
  656.  
  657.             if (!activebattlers[i].isPlayer)
  658.  
  659.             {
  660.  
  661.                 Enemies.Add(i);
  662.  
  663.             }
  664.  
  665.         }
  666.  
  667.         for (int i = 0; i < theButtons.Length; i++)
  668.  
  669.         {
  670.  
  671.             if (Enemies.Count > i && activebattlers[Enemies[i]].currentHP > 0)
  672.  
  673.             {
  674.  
  675.                 theButtons[i].gameObject.SetActive(true);
  676.  
  677.  
  678.  
  679.                 theButtons[i].moveName = moveName;
  680.  
  681.                 theButtons[i].activeTar = Enemies[i];
  682.  
  683.                 theButtons[i].tarName.text = activebattlers[Enemies[i]].charName;
  684.  
  685.             }
  686.  
  687.             else
  688.  
  689.             {
  690.  
  691.                 theButtons[i].gameObject.SetActive(false);
  692.  
  693.             }
  694.  
  695.         }
  696.  
  697.     }
  698.  
  699.  
  700.  
  701.     public void OpenMagicMenu()
  702.  
  703.     {
  704.  
  705.         magicMenu.SetActive(true);
  706.  
  707.  
  708.  
  709.         for (int i = 0; i < magicButtons.Length; i++)
  710.  
  711.         {
  712.  
  713.             if (activebattlers[currentturn].movesAvalible.Length > i)
  714.  
  715.             {
  716.  
  717.                 magicButtons[i].gameObject.SetActive(true);
  718.  
  719.  
  720.  
  721.                 magicButtons[i].spellName = activebattlers[currentturn].movesAvalible[i];
  722.  
  723.                 magicButtons[i].nameText.text = magicButtons[i].spellName;
  724.  
  725.  
  726.  
  727.                 for (int j = 0; j < battleMoves.Length; j++)
  728.  
  729.                 {
  730.  
  731.                     if (battleMoves[j].moveName == magicButtons[i].spellName)
  732.  
  733.                     {
  734.  
  735.                         magicButtons[i].spellCost = battleMoves[j].moveCost;
  736.  
  737.                         magicButtons[i].costText.text = magicButtons[i].spellCost.ToString();
  738.  
  739.                     }
  740.  
  741.                 }
  742.  
  743.  
  744.  
  745.             }
  746.  
  747.             else
  748.  
  749.             {
  750.  
  751.                 magicButtons[i].gameObject.SetActive(false);
  752.  
  753.             }
  754.  
  755.         }
  756.  
  757.     }
  758.  
  759.  
  760.  
  761.     public void Flee()
  762.  
  763.     {
  764.         if (cantFlee)
  765.         {
  766.             battleNotice.notificationText.text = "Why Are You Running?";
  767.  
  768.             battleNotice.Activate();
  769.         }
  770.         else
  771.         {
  772.  
  773.  
  774.             int fleeSuccess = Random.Range(0, 100);
  775.  
  776.  
  777.  
  778.             if (fleeSuccess <= fleeChance)
  779.  
  780.             {
  781.  
  782.                 //end the battle
  783.  
  784.                 //battleActive = false;
  785.  
  786.                 // battleScene.SetActive(false);
  787.  
  788.                 StartCoroutine(EndBattleCo());
  789.  
  790.  
  791.             }
  792.  
  793.             else
  794.  
  795.             {
  796.  
  797.                 NextTurn();
  798.  
  799.                 battleNotice.notificationText.text = "Couldn't Flee!";
  800.  
  801.                 battleNotice.Activate();
  802.  
  803.             }
  804.         }
  805.  
  806.  
  807.     }
  808.  
  809.  
  810.  
  811.     public void EquipWeaponMidBattle()
  812.  
  813.     {
  814.  
  815.         battleNotice.notificationText.text = "Can't Equip Weapon Mid Battle!";
  816.  
  817.         battleNotice.Activate();
  818.  
  819.     }
  820.  
  821.  
  822.  
  823.     public void EquipArmorMidBattle()
  824.  
  825.     {
  826.  
  827.         battleNotice.notificationText.text = "Can't Equip Armor Mid Battle!";
  828.  
  829.         battleNotice.Activate();
  830.  
  831.     }
  832.  
  833.     public IEnumerator EndBattleCo()
  834.     {
  835.         battleActive = false;
  836.  
  837.         uiButtonHolder.SetActive(false);
  838.         targetMenu.SetActive(false);
  839.         magicMenu.SetActive(false);
  840.         itemPanel.SetActive(false);
  841.  
  842.         yield return new WaitForSeconds(.5f);
  843.  
  844.         UIFade.instance.FadeToBlack();
  845.  
  846.         yield return new WaitForSeconds(1.5f);
  847.  
  848.         for(int i = 0; i < activebattlers.Count; i++)
  849.         {
  850.             if(activebattlers[i].isPlayer)
  851.             {
  852.                 for (int j = 0; j < Gamemanager.instance.playerStats.Length; j++)
  853.                 {
  854.                     if(activebattlers[i].charName == Gamemanager.instance.playerStats[j].charName)
  855.                     {
  856.                         Gamemanager.instance.playerStats[j].currentHP = activebattlers[i].currentHP;
  857.                         Gamemanager.instance.playerStats[j].currentHP = activebattlers[i].currentMP;
  858.                     }
  859.                 }
  860.             }
  861.             Destroy(activebattlers[i].gameObject);  
  862.         }
  863.         UIFade.instance.FadeFromBlack();
  864.         battleScene.SetActive(false);
  865.         activebattlers.Clear();
  866.         currentturn = 0;
  867.        
  868.         if(fleeing)
  869.         {
  870.             Gamemanager.instance.battleActive = false;
  871.             fleeing = false;
  872.         } else
  873.         {
  874.             BattleReward.instance.OpenRewardScreen(rewardExp, rewardItems);
  875.         }
  876.  
  877.     }
  878.  
  879.     public IEnumerator GameOverCo()
  880.     {
  881.         battleActive = false;
  882.         UIFade.instance.FadeToBlack();
  883.  
  884.         yield return new WaitForSeconds(1.5f);
  885.         battleScene.SetActive(false);
  886.         SceneManager.LoadScene(gameOverScene);
  887.     }
  888. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement