SHARE
TWEET

code

a guest Jan 22nd, 2020 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6.  
  7. public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }
  8.  
  9. public class BattleSystem : MonoBehaviour
  10. {
  11.  
  12.  
  13.     public GameObject playerPrefab;
  14.     public GameObject enemyPrefab;
  15.  
  16.     public Transform playerBattleStation;
  17.     public Transform enemyBattleStation;
  18.  
  19.     Unit playerUnit;
  20.     Unit enemyUnit;
  21.  
  22.     public Text dialogueText;
  23.  
  24.     public BattleHud playerHUD;
  25.     public BattleHud enemyHUD;
  26.  
  27.     public BattleState state;
  28.  
  29.  
  30.     // Start is called before the first frame update
  31.     void Start()
  32.     {
  33.         state = BattleState.START;
  34.         StartCoroutine(SetupBattle());
  35.  
  36.     }
  37.  
  38.     IEnumerator SetupBattle()
  39.     {
  40.         GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
  41.         playerUnit = playerGO.GetComponent<Unit>();
  42.  
  43.         GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
  44.         enemyUnit = enemyGO.GetComponent<Unit>();
  45.  
  46.         dialogueText.text = "A powerful " + enemyUnit.unitName + " fights you";
  47.  
  48.         playerHUD.SetHUD(playerUnit);
  49.         enemyHUD.SetHUD(enemyUnit);
  50.  
  51.         yield return new WaitForSeconds(2f);
  52.  
  53.         state = BattleState.PLAYERTURN;
  54.         PlayerTurn();
  55.     }
  56.  
  57.     IEnumerator PlayerAttack()
  58.     {
  59.         bool isDead = enemyUnit.TakeDamage(playerUnit.damage);
  60.  
  61.         enemyHUD.SetHP(enemyUnit.currentHP);
  62.         dialogueText.text = "You slash..It feels good!";
  63.  
  64.         yield return new WaitForSeconds(2f);
  65.  
  66.         if (isDead)
  67.         {
  68.             state = BattleState.WON;
  69.             EndBattle();
  70.         }
  71.         else
  72.         {
  73.             state = BattleState.ENEMYTURN;
  74.             StartCoroutine(EnemyTurn());
  75.         }
  76.     }
  77.  
  78.     IEnumerator EnemyTurn()
  79.     {
  80.         dialogueText.text = enemyUnit.unitName + " attacks!";
  81.  
  82.         yield return new WaitForSeconds(1f);
  83.  
  84.         bool isDead = playerUnit.TakeDamage(enemyUnit.damage);
  85.  
  86.         playerHUD.SetHP(playerUnit.currentHP);
  87.  
  88.         yield return new WaitForSeconds(1f);
  89.  
  90.         if (isDead)
  91.         {
  92.             state = BattleState.LOST;
  93.             EndBattle();
  94.         }
  95.         else
  96.         {
  97.             state = BattleState.PLAYERTURN;
  98.             PlayerTurn();
  99.         }
  100.  
  101.     }
  102.  
  103.     void EndBattle()
  104.     {
  105.         if (state == BattleState.WON)
  106.         {
  107.             dialogueText.text = "You won the fight!";
  108.             SceneManager.LoadScene("Level2Win");
  109.         }
  110.         else if (state == BattleState.LOST)
  111.         {
  112.             dialogueText.text = "You were defeated.";
  113.             SceneManager.LoadScene("Level2");
  114.         }
  115.     }
  116.  
  117.     void PlayerTurn()
  118.     {
  119.         dialogueText.text = "You feel sleepy...choose an action:";
  120.     }
  121.  
  122.     IEnumerator PlayerHeal()
  123.     {
  124.         playerUnit.Heal(10);
  125.  
  126.         playerHUD.SetHP(playerUnit.currentHP);
  127.         dialogueText.text = "You are filled with dreams! +10HP";
  128.  
  129.         yield return new WaitForSeconds(2f);
  130.  
  131.         state = BattleState.ENEMYTURN;
  132.         StartCoroutine(EnemyTurn());
  133.     }
  134.  
  135.     public void OnAttackButton()
  136.     {
  137.         if (state != BattleState.PLAYERTURN)
  138.             return;
  139.  
  140.         StartCoroutine(PlayerAttack());
  141.     }
  142.  
  143.     public void OnHealButton()
  144.     {
  145.         if (state != BattleState.PLAYERTURN)
  146.             return;
  147.  
  148.         StartCoroutine(PlayerHeal());
  149.     }
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top