Advertisement
GoodNoodle

BattleSystem.cs

Jul 11th, 2022 (edited)
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.20 KB | None | 0 0
  1. using DG.Tweening;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using UnityEngine.UI;
  8. using Utils.StateMachine;
  9.  
  10. public enum BattleState
  11. {
  12.     Start,
  13.     ActionSelection,
  14.     MoveSelection,
  15.     RunningTurn,
  16.     Busy,
  17.     PartyScreen,
  18.     BattleOver,
  19.     Inventory
  20. }
  21. public enum BattleAction
  22. {
  23.     Move,
  24.     SwitchCreature,
  25.     UseItem,
  26.     Flee
  27.  
  28. }
  29.  
  30. public enum BattleTrigger { Land, Water }
  31. public class BattleSystem : MonoBehaviour
  32. {
  33.     [SerializeField] Battleunit playerUnit;
  34.     [SerializeField] Battleunit enemyUnit;
  35.     [SerializeField] BattleDialogueBox dialogueBox;
  36.     [SerializeField] PartyScreen partyScreen;
  37.     [SerializeField] GameObject captureCapsuleSprite;
  38.     [SerializeField] InventoryUI inventoryUI;
  39.     [SerializeField] RunTurnState runTurns;
  40.    
  41.     [Tooltip("defult attack")]
  42.     [SerializeField] MoveBase genericMove;
  43.  
  44.  
  45.  
  46.     [SerializeField] Image battleBackround;
  47.     [SerializeField] Sprite grassBackround, waterBackround;
  48.  
  49.  
  50.    
  51.  
  52.     BattleState state;
  53.     int currentAction;
  54.     int currentMove;
  55.    public int EscapeAttempts { get; set; }
  56.  
  57.     public event Action<bool> OnBattleOver;
  58.  
  59.     public int SelectedMove { get;  set; }
  60.  
  61.     public BattleAction SelectedAction { get; set; }
  62.  
  63.     public bool isBattleOver { get; private set; }
  64.  
  65.     public BattleDialogueBox battleDialogueBox { get; private set; }
  66.  
  67.    public Party playerParty { get; private set; }
  68.    public Party trainerParty { get; private set; }
  69.     public Creature PotentialCreature { get; private set; }
  70.  
  71.     public Creature SelectedCreature { get;  set; }
  72.  
  73.     public ItemBase SelectedItem { get; set; }
  74.  
  75.  
  76.     public bool IsTrainerBattle { get; private set;} = false;
  77.  
  78.     PlayerController player;
  79.     TrainerController trainer;
  80.  
  81.     public StateMachine<BattleSystem> StateMachine { get; private set; }
  82.  
  83.  
  84.     [SerializeField] public DamageNumber theDamageNumber { get; private set; }
  85.  
  86.     BattleTrigger battletrigger;
  87.     // Start is called before the first frame update
  88.  
  89.     [HideInInspector]
  90.    public BattleSystem instance;
  91.  
  92.     private void Awake()
  93.     {
  94.      
  95.         instance = this;
  96.     }
  97.     public void StartBattle(Party playerParty, Creature potentialCreature, BattleTrigger trigger = BattleTrigger.Land)
  98.     {
  99.         this.playerParty = playerParty;
  100.         this.PotentialCreature = potentialCreature;
  101.         player = playerParty.GetComponent<PlayerController>();
  102.         IsTrainerBattle = false;
  103.         battletrigger = trigger;
  104.  
  105.         StartCoroutine(SetUpBattle());
  106.     }
  107.  
  108.     public void StartTrainerBattle(Party playerParty, Party trainerParty, BattleTrigger trigger = BattleTrigger.Land)
  109.     {
  110.         this.playerParty = playerParty;
  111.         this.trainerParty = trainerParty;
  112.  
  113.         IsTrainerBattle = true;
  114.  
  115.         player = playerParty.GetComponent<PlayerController>();
  116.         trainer = trainerParty.GetComponent<TrainerController>();
  117.  
  118.         battletrigger = trigger;
  119.         StartCoroutine(SetUpBattle());
  120.     }
  121.  
  122.     public void HandleUpdate()
  123.     {
  124.         StateMachine.Execute();
  125.      
  126.      
  127.         if (state == BattleState.MoveSelection)
  128.         {
  129.             HandleMoveSelection();
  130.         }
  131.         else if (state == BattleState.PartyScreen)
  132.         {
  133.             HandlePartyScreenSelection();
  134.         }
  135.        
  136.     }
  137.     void HandlePartyScreenSelection()
  138.     {
  139.         Action onSelected = () =>
  140.         {
  141.             var selectedMember = partyScreen.SelectedCreature;
  142.             if (selectedMember.HP <= 0)
  143.             {
  144.                 return;
  145.             }
  146.             if (selectedMember == playerUnit.Creature)
  147.             {
  148.                 return;
  149.             }
  150.  
  151.             partyScreen.gameObject.SetActive(false);
  152.  
  153.         };
  154.  
  155.         Action onBack = () =>
  156.         {
  157.             if (playerUnit.Creature.HP <= 0)
  158.             {
  159.                 return;
  160.             }
  161.             partyScreen.gameObject.SetActive(false);
  162.  
  163.  
  164.             ActionSelection();      
  165.         };
  166.  
  167.      
  168.     }
  169.  
  170.     public void BattleOver(bool won)
  171.     {
  172.         isBattleOver = true;
  173.         playerParty.Creatures.ForEach(p => p.OnBattleOver());
  174.         playerUnit.Hud.ClearData();
  175.         enemyUnit.Hud.ClearData();
  176.         OnBattleOver(won);
  177.     }
  178.  
  179.    public IEnumerator SwitchCreature(Creature Newcreature)
  180.     {
  181.  
  182.         if (playerUnit.Creature.HP > 0)
  183.         {
  184.  
  185.             playerUnit.PlayDeathAnim();
  186.             yield return new WaitForSeconds(1f);
  187.         }
  188.         playerUnit.SetUp(Newcreature);
  189.         dialogueBox.SetMoveNames(Newcreature.Moves);
  190.  
  191.         yield return new WaitForSeconds(1f);
  192.  
  193.       //  if(partyScreen.CalledFrom == null)
  194.       //  {
  195.        //     state = BattleState.RunningTurn;
  196.        // }
  197.     }
  198.  
  199.  
  200.    
  201.  
  202.     private void HandleMoveSelection()
  203.     {
  204.         if (Input.GetKeyDown(KeyCode.D))
  205.             ++currentMove;
  206.         else if (Input.GetKeyDown(KeyCode.A))
  207.             --currentMove;
  208.         else if (Input.GetKeyDown(KeyCode.S))
  209.             currentMove += 2;
  210.         else if (Input.GetKeyDown(KeyCode.W))
  211.             currentMove -= 2;
  212.  
  213.         currentMove = Mathf.Clamp(currentMove, 0, playerUnit.Creature.Moves.Count - 1);
  214.  
  215.         dialogueBox.UpdateMoveSelection(currentMove, playerUnit.Creature.Moves[currentMove]);
  216.         dialogueBox.UpdateMP(playerUnit.Creature);
  217.  
  218.         if (Input.GetKeyDown(KeyCode.Z) )
  219.         {
  220.             var move = playerUnit.Creature.Moves[currentMove];
  221.          
  222.             dialogueBox.EnableMoveSelector(false);
  223.             dialogueBox.EnableActionSelector(false);
  224.          //  if (CheckMoveMP(playerUnit, move))
  225.            // {
  226.               //  playerUnit.Creature.MP -= playerUnit.Creature.CurrentMove.MPCost;
  227.               // if (GetComponent<Creature>().MP >= playerUnit.Creature.CurrentMove.MPCost)
  228.              //  {
  229.                    //StartCoroutine(runturns.RunTurns());
  230.             //    }
  231.            // }
  232.            // else
  233.             //{
  234.            //     StartCoroutine(runTurns.NotEnoughMP());
  235.            // }
  236.  
  237.  
  238.         } else if (Input.GetKeyDown(KeyCode.X))
  239.         {
  240.             dialogueBox.EnableMoveSelector(false);
  241.             ActionSelection();
  242.         }
  243.  
  244.     }
  245.  
  246.  
  247.  
  248.     private void OpenPartyScreen()
  249.     {
  250.       //  partyScreen.CalledFrom = state;
  251.         state = BattleState.PartyScreen;
  252.         partyScreen.gameObject.SetActive(true);
  253.     }
  254.  
  255.     public IEnumerator SetUpBattle()
  256.     {
  257.         StateMachine = new StateMachine<BattleSystem>(this);
  258.  
  259.         playerUnit.Clear();
  260.         enemyUnit.Clear();
  261.  
  262.        battleBackround.sprite = (battletrigger == BattleTrigger.Land) ? grassBackround : waterBackround;
  263.  
  264.         if (!IsTrainerBattle)
  265.         {
  266.             playerUnit.SetUp(playerParty.GetUninjuredCreature());
  267.             enemyUnit.SetUp(PotentialCreature);    
  268.         }
  269.         else
  270.         {
  271.             // send enemy creature
  272.             enemyUnit.gameObject.SetActive(true);
  273.             var enemyCreature = trainerParty.GetUninjuredCreature();
  274.             enemyUnit.SetUp(enemyCreature);
  275.  
  276.             // send player creature
  277.             playerUnit.gameObject.SetActive(true);
  278.             var playerCreature = playerParty.GetUninjuredCreature();
  279.             playerUnit.SetUp(playerCreature);
  280.             dialogueBox.SetMoveNames(playerUnit.Creature.Moves);
  281.         }
  282.        
  283.  
  284.         isBattleOver = false;
  285.         EscapeAttempts = 0;
  286.         partyScreen.Init();
  287.         yield return new WaitForSeconds(.1f);
  288.  
  289.         StateMachine.ChangeState(ActionSelectionState.i);
  290.        
  291.     }
  292.  
  293.  
  294.  
  295.     void ActionSelection()
  296.     {
  297.         state = BattleState.ActionSelection;
  298.         dialogueBox.EnableActionSelector(true);
  299.     }
  300.  
  301.     void OpenInventory()
  302.     {
  303.         state = BattleState.Inventory;
  304.         inventoryUI.gameObject.SetActive(true);
  305.     }
  306.  
  307.     void MoveSelection()
  308.     {
  309.         state = BattleState.MoveSelection;
  310.         dialogueBox.EnableActionSelector(false);
  311.         dialogueBox.EnableMoveSelector(true);
  312.     }
  313.  
  314.    
  315.  
  316.  
  317.  
  318.     public bool CheckMoveMP(Battleunit sourceUnit, Move moveToCheck)
  319.     {
  320.         if (sourceUnit.Creature.MP >= moveToCheck.MPCost)
  321.         {    
  322.             sourceUnit.Creature.MP -= moveToCheck.MPCost;
  323.             return true;
  324.         }
  325.  
  326.         else
  327.         {
  328.             return false;
  329.         }
  330.     }
  331.  
  332.          
  333.    
  334.     IEnumerator SendNextTrainerCreature(Creature nextCreature)
  335.     {
  336.         state = BattleState.Busy;
  337.  
  338.         enemyUnit.SetUp(nextCreature);
  339.         yield return new WaitForSeconds(1f);
  340.         state = BattleState.RunningTurn;
  341.     }
  342.     public IEnumerator ThrowCapsule(CapsuleItem capsuleItem)
  343.     {
  344.  
  345.         if(IsTrainerBattle)
  346.         {
  347.            
  348.             state = BattleState.RunningTurn;
  349.             yield return DialogueManager.Instance.ShowDialogText("cant capture.");
  350.             yield break;
  351.         }
  352.         yield return new WaitForSeconds(1f);
  353.         var capsuleObj =  Instantiate(captureCapsuleSprite, playerUnit.transform.position - new Vector3(2, 0), Quaternion.identity);
  354.         var capsule = capsuleObj.GetComponent<SpriteRenderer>();
  355.         capsule.sprite = capsuleItem.Icon;
  356.  
  357.         //animations
  358.         yield return capsule.transform.DOJump(enemyUnit.transform.position + new Vector3(0, 2), 2f, 1, 1f).WaitForCompletion();
  359.  
  360.         yield return enemyUnit.PlayCaptureAnimation();
  361.         capsule.transform.DOMoveY(enemyUnit.transform.position.y - 1.3f, .5f).WaitForCompletion();
  362.  
  363.         int shakeCount = TryToCatchCreature(enemyUnit.Creature, capsuleItem);
  364.  
  365.         for (int i = 0; i < shakeCount; ++i)
  366.         {
  367.             yield return new WaitForSeconds(.5f);
  368.            yield return capsule.transform.DOPunchRotation(new Vector3(0, 0, 10f), .8f).WaitForCompletion();
  369.         }
  370.         if(shakeCount == 4)
  371.         {
  372.             yield return capsule.DOFade(0, 1.5f).WaitForCompletion();
  373.             yield return DialogueManager.Instance.ShowDialogText("creature has been added to your party");
  374.             playerParty.AddCreature(enemyUnit.Creature);
  375.  
  376.             Destroy(capsule);
  377.             BattleOver(true);
  378.         } else
  379.         {
  380.             yield return new WaitForSeconds(1f);
  381.             capsule.DOFade(0, 0.2f);
  382.             yield return enemyUnit.PlayBreakOutAnimation();
  383.  
  384.             Destroy(capsule);
  385.         }
  386.     }
  387.  
  388.  
  389.  
  390.     int TryToCatchCreature(Creature creature, CapsuleItem capsuleItem)
  391.     {
  392.         float a = (3 * creature.MaxHP - 2 * creature.HP) * creature.Base.CatchRate * capsuleItem.CatchRateMod
  393.         * ConditionDB.GetStatusBounus(creature.Status) / (3 * creature.MaxHP);
  394.  
  395.         if (a >= 255)
  396.             return 4;
  397.  
  398.         float b = 1048560 / MathF.Sqrt(MathF.Sqrt(16711680 / a));
  399.  
  400.         int shakeCount = 0;
  401.  
  402.         while(shakeCount < 4)
  403.         {
  404.             if (UnityEngine.Random.Range(0, 65535) >= b)
  405.               break;
  406.  
  407.             ++shakeCount;
  408.         }
  409.  
  410.         return shakeCount;
  411.     }
  412.  
  413.  
  414.     public Battleunit PlayerUnit => playerUnit;
  415.  
  416.     public Battleunit EnemyUnit => enemyUnit;
  417.  
  418.     public PartyScreen PartyScreen => partyScreen;
  419.  
  420.     public BattleDialogueBox BattleDialogueBox => battleDialogueBox;
  421.  
  422. }
  423.  
  424.  
  425.    
  426.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement