Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using UnityEngine.UI;
- /* Purpose: This script is suppose to handle the entire battle event.
- *
- *
- * SHOULD WE PRESS THE BUTTONS OR USE KEYS LIKE NORMAL POKEMON?
- * Alot easier to press the buttons with the mouse i guess. (code wise)
- *
- * TODO: When making this script its with the intent to handle everything in the battle, like both sides pokemons etc.
- * Why in the world did i make BattlePokemon and WildPokemon? Shouldnt they be in this script aswell?
- * Might just work to have them seperate and have a reference to them here.
- * Made By: Jesper, 22/6-19
- */
- public class BattleScript : MonoBehaviour
- {
- public Player player;
- [Header("Pokemon Info")]
- //Here we create a new list to hold all pokemons present in the battle(on the field and in pokeballs).
- public List<PokemonTemplate> jointPokemon = new List<PokemonTemplate>();
- public GameObject currentTeamPokemonGameObject;
- public GameObject currentEnemyPokemonGameObject;
- public GameObject pokemonPanel;
- //We need to know how many pokemons we and the enemy have, aswelll as what index in jointPokemon are currently battling.
- //TODO: Remove SerializeField
- [SerializeField] private float longWaitTime;
- [SerializeField] private float shortWaitTime;
- public int teamPokemonQuantity;
- [SerializeField] private int teamPokemonAbilityQuantity;
- public int currentTeamPokemonIndex;
- [SerializeField] private int currentTeamPokemonAbilityIndex;
- [SerializeField] private int enemyPokemonQuantity;
- [SerializeField] private int currentEnemyPokemonIndex;
- [SerializeField] private int currentEnemyPokemonAbilityIndex;
- //Used so the faster pokemon can attack first and then the slower pokemon.
- [SerializeField] private int fasterPokemonIndex;
- [SerializeField] private int slowerPokemonIndex;
- [SerializeField] private int fasterPokemonAbilityIndex;
- [SerializeField] private int slowerPokemonAbilityIndex;
- [SerializeField] private bool turnInProgress = false;
- [SerializeField] private bool criticalStrike = false;
- [SerializeField] private bool panelIsOpen = false;
- //Battle Specific stuff we might use
- //TODO: Remove SerializeField
- [SerializeField] private string weather; //Certain weather makes moves hit hard/less.
- //Things that will be written throughout the battle.
- //TODO: Is this how we should make the texts?
- private string wildPokemonAppearMsg;
- private string sendOutTeamPokemon;
- private string teamPokemonAttackMsg;
- private string enemyPokemonAttackMsg;
- private string criticalStrikeMsg;
- private string lvlUpMsg;
- private string superEffectiveMsg;
- private string notVeryEffectiveMsg;
- private string foePokemonFaintedMsg;
- private string teamPokemonFaintedMsg;
- private string awardExpMsg;
- [Header("Buttons and Texts")]
- public TextMeshProUGUI battleText;
- public List<Button> attackButtons = new List<Button>();
- private List<TextMeshProUGUI> attackTexts = new List<TextMeshProUGUI>();
- public Button pokemonButton;
- public Button bagButton;
- public Button runButton;
- public TextMeshProUGUI teamPokemonNameTmpro;
- public TextMeshProUGUI teamPokemonHealthTmpro;
- public TextMeshProUGUI teamPokemonLvlTmpro;
- public Image teamPokemonHealthBar;
- public TextMeshProUGUI enemyPokemonNameTmpro;
- public TextMeshProUGUI enemyPokemonHealthTmpro;
- public TextMeshProUGUI enemyPokemonLvlTmpro;
- public Image enemyPokemonHealthBar;
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape) && panelIsOpen)
- {
- ShowPokemonMenu();
- }
- }
- //When the Battle Script is enabled we need to put the myPokemon and enemyPokemon list into that of jointPokemon.
- void OnEnable()
- {
- //Always close pokemonPanel when we enter a battle.
- pokemonPanel.SetActive(false);
- //Add all myPokemons to the list and set how many pokemons we brought to the battle.
- foreach (PokemonTemplate pokemon in player.myPokemons)
- {
- jointPokemon.Add(pokemon);
- }
- teamPokemonQuantity = jointPokemon.Count;
- //Add all enemyPokemons to the list and set how many pokemons the enemy brought to the battle.
- foreach (PokemonTemplate pokemon in player.enemyPokemon)
- {
- jointPokemon.Add(pokemon);
- }
- enemyPokemonQuantity = jointPokemon.Count - teamPokemonQuantity;
- //Check through all the pokemons we brought and take the first alive pokemon and make it the one we send into battle.
- for (int i = 0; i < teamPokemonQuantity; i++)
- {
- if (jointPokemon[i].stats.Health > 0)
- {
- currentTeamPokemonIndex = i;
- break;
- }
- }
- teamPokemonAbilityQuantity = jointPokemon[currentTeamPokemonIndex].moves.Count;
- //The enemy should always have alive pokemons when we first get into battle, so we set their pokemon to be the first index after ours
- currentEnemyPokemonIndex = teamPokemonQuantity;
- //Find the Texts in the attack buttons
- for (int i = 0; i < 4; i++)
- {
- attackTexts.Add(attackButtons[i].gameObject.GetComponentInChildren<TextMeshProUGUI>());
- }
- int xRand = Random.Range(4, 5);
- int zRand = Random.Range(4, 5);
- //The first thing we need to do is to spawn our Pokemon and the enemy Pokemon.
- //TODO: Make it check for first alive pokemon.
- Debug.Log("Should spawn Pokemons/" + jointPokemon[currentTeamPokemonIndex].PokemonName);
- if (currentTeamPokemonGameObject == null)
- {
- currentTeamPokemonGameObject = (GameObject)Instantiate((Resources.Load("Pokemons/" + jointPokemon[currentTeamPokemonIndex].PokemonName)),
- new Vector3(xRand, 2, zRand) + player.transform.position, new Quaternion(0, 0, 0, 0));
- }
- if (currentEnemyPokemonGameObject == null)
- {
- currentEnemyPokemonGameObject = (GameObject)Instantiate((Resources.Load("Pokemons/" + jointPokemon[currentEnemyPokemonIndex].PokemonName)),
- new Vector3(-xRand, 2, -zRand) + player.transform.position, new Quaternion(0, 0, 0, 0));
- }
- //Make the pokemon face eachother
- currentTeamPokemonGameObject.transform.LookAt(currentEnemyPokemonGameObject.transform);
- currentEnemyPokemonGameObject.transform.LookAt(currentTeamPokemonGameObject.transform);
- UpdateUI();
- EnableButtons();
- StartCoroutine(WildPokemonAppearMsg());
- }
- public void UpdateUI()
- {
- for (int i = 0; i < 4; i++)
- {
- if (i < teamPokemonAbilityQuantity)
- {
- attackTexts[i].text = jointPokemon[currentTeamPokemonIndex].moves[i].abilityName;
- }
- else
- {
- attackTexts[i].text = "";
- }
- }
- enemyPokemonNameTmpro.text = jointPokemon[currentEnemyPokemonIndex].PokemonName;
- enemyPokemonHealthTmpro.text = "Health: " + jointPokemon[currentEnemyPokemonIndex].stats.Health.ToString()+ "/" +
- jointPokemon[currentEnemyPokemonIndex].maxStats.Health.ToString();
- enemyPokemonLvlTmpro.text = "Lvl: " + jointPokemon[currentEnemyPokemonIndex].lvl.ToString();
- enemyPokemonHealthBar.fillAmount = (float)jointPokemon[currentEnemyPokemonIndex].stats.Health / jointPokemon[currentEnemyPokemonIndex].maxStats.Health;
- teamPokemonNameTmpro.text = jointPokemon[currentTeamPokemonIndex].PokemonName;
- teamPokemonHealthTmpro.text = "Health: " + jointPokemon[currentTeamPokemonIndex].stats.Health.ToString() + "/" +
- jointPokemon[currentTeamPokemonIndex].maxStats.Health.ToString();
- teamPokemonLvlTmpro.text = "Lvl: " + jointPokemon[currentTeamPokemonIndex].lvl.ToString();
- teamPokemonHealthBar.fillAmount = (float)jointPokemon[currentTeamPokemonIndex].stats.Health / jointPokemon[currentTeamPokemonIndex].maxStats.Health;
- }
- //sets the index of the chosen ability to the corrent one and then calls PrePerformTurn.
- //These functions are gonna be called from the ui buttons.
- public void Attack1()
- {
- currentTeamPokemonAbilityIndex = 0;
- AttackChosen();
- }
- public void Attack2()
- {
- currentTeamPokemonAbilityIndex = 1;
- AttackChosen();
- }
- public void Attack3()
- {
- currentTeamPokemonAbilityIndex = 2;
- AttackChosen();
- }
- public void Attack4()
- {
- currentTeamPokemonAbilityIndex = 3;
- AttackChosen();
- }
- public void ShowPokemonMenu()
- {
- panelIsOpen = !panelIsOpen;
- pokemonPanel.SetActive(panelIsOpen);
- }
- public void PokemonMenuDuringBattle()
- {
- //Here we need to open the pokemon menu during battle.
- //TODO: Make the pokemon menu...
- //If we change our pokemon we waste our turn like in the pokemon games, or?
- //PerformTurn();
- }
- public void BagMenuDuringBattle()
- {
- //Here we need to open the bag menu during battle.
- //TODO: Make the bag menu...
- //If we use an item we waste our turn like in the pokemon games, or?
- //PerformTurn();
- }
- public void RunAwayFromBattle()
- {
- //Run away from the battle.
- //TODO: We need to restore camera angle to what it was previously
- //TODO: We need to make our character visible again.
- //TODO: We need to destroy our 2 prefabs for BattlePokemon and EnemyPokemon.
- jointPokemon.Clear();
- Destroy(currentTeamPokemonGameObject);
- Destroy(currentEnemyPokemonGameObject);
- StopAllCoroutines();
- GameEvents.PokemonBattleOver(); //TODO: This should probably be made better.
- }
- //We chose to do an attack, make sure everything is fine and then send it to startTurn.
- public void AttackChosen()
- {
- //Check if the chosen move (attack1 = index 0) in our pokemon from the joint list
- if (jointPokemon[currentTeamPokemonIndex].moves[currentTeamPokemonAbilityIndex].PP > 0)
- {
- //Check what form ability1 is and set the flag evaluateVariable accordingly so later functions know.
- if (jointPokemon[currentTeamPokemonIndex].moves[currentTeamPokemonAbilityIndex].abilityForm == "Status")
- {
- //TODO: Fix it so we can do status abilities!
- Debug.Log("We cant do status abilities yet!");
- }
- else
- {
- //Check whatever pokemon is faster and set that index as faster.
- //TODO: We should add a priority move check here, IE Quick Attack!
- if (jointPokemon[currentTeamPokemonIndex].stats.Speed > jointPokemon[currentEnemyPokemonIndex].stats.Speed)
- {
- fasterPokemonIndex = currentTeamPokemonIndex;
- fasterPokemonAbilityIndex = currentTeamPokemonAbilityIndex;
- slowerPokemonIndex = currentEnemyPokemonIndex;
- slowerPokemonAbilityIndex = currentEnemyPokemonAbilityIndex;
- }
- else
- {
- fasterPokemonIndex = currentEnemyPokemonIndex;
- fasterPokemonAbilityIndex = currentEnemyPokemonAbilityIndex;
- slowerPokemonIndex = currentTeamPokemonIndex;
- slowerPokemonAbilityIndex = currentTeamPokemonAbilityIndex;
- }
- //Start the mainBattle script.
- StartCoroutine(startTurn());
- }
- }
- else
- {
- //If we didnt have enough PP for the chosen move, let user know that user have to pick another one.
- Debug.Log("Not enough PP, please chose again");
- }
- }
- //This function is in charge of the entire battle scenario.
- //TODO: Change so we can swap pokemon and come in midway?
- IEnumerator startTurn()
- {
- //Trigger the event that the turn is initiated.
- Debug.Log("BattleTurnInitiated");
- DisableButtons();
- //Faster Pokemon Attacks. Check what Index is the faster pokemon and write the correct msg.
- if (fasterPokemonIndex == currentTeamPokemonIndex)
- {
- yield return StartCoroutine(TeamPokemonAttackMsg());
- }
- else
- {
- yield return StartCoroutine(EnemyPokemonAttackMsg());
- }
- yield return new WaitForSeconds(longWaitTime);
- //Here we should also check if its super effective or not!
- //If the hit was a critical strike, write it.
- if (criticalStrike == true)
- {
- yield return StartCoroutine(CriticalStrikeMsg());
- yield return new WaitForSeconds(longWaitTime);
- criticalStrike = false;
- }
- //This is where the damage is applied.
- jointPokemon[slowerPokemonIndex].stats.Health -= DamageCalculator(fasterPokemonIndex, fasterPokemonAbilityIndex, slowerPokemonIndex); ;
- if (jointPokemon[slowerPokemonIndex].stats.Health < 0)
- {
- jointPokemon[slowerPokemonIndex].stats.Health = 0;
- }
- UpdateUI();
- //Slower Pokemon Attacks.
- if (fasterPokemonIndex != currentTeamPokemonIndex)
- {
- yield return StartCoroutine(TeamPokemonAttackMsg());
- }
- else
- {
- yield return StartCoroutine(EnemyPokemonAttackMsg());
- }
- yield return new WaitForSeconds(longWaitTime);
- //Here we should also check if its super effective or not!
- //Check if crit and write if so.
- if (criticalStrike == true)
- {
- yield return StartCoroutine(CriticalStrikeMsg());
- yield return new WaitForSeconds(longWaitTime);
- criticalStrike = false;
- }
- //This is where the damage is applied.
- jointPokemon[fasterPokemonIndex].stats.Health -= DamageCalculator(slowerPokemonIndex, slowerPokemonAbilityIndex, fasterPokemonIndex);
- if (jointPokemon[fasterPokemonIndex].stats.Health < 0)
- {
- jointPokemon[fasterPokemonIndex].stats.Health = 0;
- }
- UpdateUI();
- yield return new WaitForSeconds(longWaitTime);
- EnableButtons();
- }
- //In this function we see if any of the current active pokemons died.
- public void EvalutatePokemons()
- {
- if (jointPokemon[currentTeamPokemonIndex].stats.Health <= 0)
- {
- }
- }
- //We use this function to write out msgs one letter at the time.
- IEnumerator TypeSentence(string _msg)
- {
- battleText.text = "";
- foreach (char letter in _msg.ToCharArray())
- {
- battleText.text += letter;
- yield return null;
- }
- yield return new WaitForSeconds(longWaitTime);
- }
- //We call this to make the user not able to press the buttons.
- public void DisableButtons()
- {
- for (int i = 0; i < 4; i++)
- {
- attackButtons[i].interactable = false;
- }
- pokemonButton.interactable = false;
- bagButton.interactable = false;
- runButton.interactable = false;
- }
- //We call this to make the user able to press the buttons.
- public void EnableButtons()
- {
- for (int i = 0; i < 4; i++)
- {
- attackButtons[i].interactable = false;
- }
- pokemonButton.interactable = true;
- bagButton.interactable = true;
- runButton.interactable = true;
- if (jointPokemon[currentTeamPokemonIndex].moves.Count >= 1)
- attackButtons[0].interactable = true;
- if (jointPokemon[currentTeamPokemonIndex].moves.Count >= 2)
- attackButtons[1].interactable = true;
- if (jointPokemon[currentTeamPokemonIndex].moves.Count >= 3)
- attackButtons[2].interactable = true;
- if (jointPokemon[currentTeamPokemonIndex].moves.Count >= 4)
- attackButtons[3].interactable = true;
- }
- //Message we display when a wild pokemon appears
- IEnumerator WildPokemonAppearMsg()
- {
- wildPokemonAppearMsg = "A wild " + jointPokemon[currentEnemyPokemonIndex].PokemonName + " appears!";
- yield return StartCoroutine(TypeSentence(wildPokemonAppearMsg));
- }
- //Msg we display when we send out a pokemon, either in the start or when we change pokemon.
- IEnumerator SendOutTeamPokemonMsg()
- {
- sendOutTeamPokemon = "Go! " + jointPokemon[currentTeamPokemonIndex].PokemonName;
- yield return StartCoroutine(TypeSentence(sendOutTeamPokemon));
- }
- //Msg we display when our pokemon attack.
- IEnumerator TeamPokemonAttackMsg()
- {
- teamPokemonAttackMsg = jointPokemon[currentTeamPokemonIndex].PokemonName + " used " +
- jointPokemon[currentTeamPokemonIndex].moves[currentTeamPokemonAbilityIndex].abilityName;
- yield return StartCoroutine(TypeSentence(teamPokemonAttackMsg));
- }
- //Msg we display when our pokemon attack.
- IEnumerator EnemyPokemonAttackMsg()
- {
- enemyPokemonAttackMsg = "Foe " + jointPokemon[currentEnemyPokemonIndex].PokemonName + " used " +
- jointPokemon[currentEnemyPokemonIndex].moves[currentEnemyPokemonAbilityIndex].abilityName;
- yield return StartCoroutine(TypeSentence(enemyPokemonAttackMsg));
- }
- IEnumerator CriticalStrikeMsg()
- {
- criticalStrikeMsg = "It's a critical strike!";
- yield return StartCoroutine(TypeSentence(criticalStrikeMsg));
- }
- //Msg we display when an attack is super effective.
- IEnumerator SuperEffectiveMsg()
- {
- superEffectiveMsg = "It's super effective!";
- yield return StartCoroutine(TypeSentence(superEffectiveMsg));
- }
- //Msg we display when an attack is not very effective.
- IEnumerator NotVeryEffectiveMsg()
- {
- notVeryEffectiveMsg = "It's not very effective";
- yield return StartCoroutine(TypeSentence(notVeryEffectiveMsg));
- }
- //Msg we display when an enemy pokemon faints.
- IEnumerator FoePokemonFaintedMsg()
- {
- foePokemonFaintedMsg = "Foe " + jointPokemon[currentEnemyPokemonIndex].PokemonName + " fainted!";
- yield return StartCoroutine(TypeSentence(foePokemonFaintedMsg));
- }
- //Msg we display when a team pokemon faints.
- IEnumerator TeamPokemonFaintedMsg()
- {
- teamPokemonFaintedMsg = jointPokemon[currentTeamPokemonIndex].PokemonName + " fainted!";
- yield return StartCoroutine(TypeSentence(teamPokemonFaintedMsg));
- }
- //Msg we display when we award exp to a team pokemon.
- IEnumerator AwardExpMsg(int _expAmount)
- {
- awardExpMsg = jointPokemon[currentTeamPokemonIndex].PokemonName + " gained " + _expAmount.ToString() + " Exp. Points!";
- yield return StartCoroutine(TypeSentence(awardExpMsg));
- }
- //Msg we display if a pokemons lvls up.
- IEnumerator LvlUpMsg()
- {
- lvlUpMsg = jointPokemon[currentTeamPokemonIndex].PokemonName + " grew to level " + jointPokemon[currentTeamPokemonIndex].lvl + "!";
- yield return StartCoroutine(TypeSentence(lvlUpMsg));
- }
- //This is where the damage formula is appled.
- //See https://bulbapedia.bulbagarden.net/wiki/Damage for how the equation.
- public int DamageCalculator(int _attackPokemonIndex, int _attackMoveIndex, int _defendPokemonIndex)
- {
- float _weather = 1.0f;
- //Check if the ability is boosted by the weather.
- if (jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].abilityType == weather)
- {
- _weather = 1.5f;
- }
- float _stab = 1.0f;
- //Check if the ability is boosted by stab.
- if (jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].abilityType == jointPokemon[_attackPokemonIndex].type1 ||
- jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].abilityType == jointPokemon[_attackPokemonIndex].type2)
- {
- _stab = 1.5f;
- }
- float _crit = 1.0f;
- //20% chance of crit
- //TODO: What should the chance to crit be?
- if (Random.Range(0, 100) <= 20)
- {
- _crit = 1.5f;
- criticalStrike = true;
- Debug.Log("It is a critical hit!");
- }
- //Random multiplier between 0.85-1;
- float _random = (float)(Random.Range(85, 100)) / 100;
- //TODO: hardcoding _badge to 1.0 for now, dont know if we should use it.
- float _badge = 1.0f;
- //TODO: We need to make a super effective check!
- float _type = 1.0f;
- //TODO: Hardcoding burn to one for now, dont know if we should use it.
- float _burn = 1.0f;
- //TODO: Hardcoding targets to 1 since we probably arent gonna fight 2x pokemon.
- float _targets = 1;
- int _level = jointPokemon[_attackPokemonIndex].lvl;
- int _A;
- int _D;
- if (jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].abilityForm == "Physical")
- {
- _A = jointPokemon[_attackPokemonIndex].stats.Attack;
- _D = jointPokemon[_defendPokemonIndex].stats.Defence;
- }
- else if (jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].abilityForm == "Special")
- {
- _A = jointPokemon[_attackPokemonIndex].stats.Sp_Attack;
- _D = jointPokemon[_defendPokemonIndex].stats.Sp_Defence;
- }
- else
- {
- _A = 1;
- _D = 1;
- Debug.Log("wrong assignment of _A and _D in BattleScripts, DamageCalculation function");
- }
- int _power = jointPokemon[_attackPokemonIndex].moves[_attackMoveIndex].ability.power;
- float _modifier = _targets * _weather * _badge * _crit * _random * _stab * _type * _burn;
- int _damage = (int)(((2.5 * _level + 2) * _power * _A / _D / 50 + 2) * _modifier);
- Debug.Log("Damage dealt: " + _damage);
- return _damage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement