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 enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }
- public class BattleSystem : MonoBehaviour
- {
- public GameObject playerPrefab;
- public GameObject enemyPrefab;
- public Transform playerBattleStation;
- public Transform enemyBattleStation;
- Unit playerUnit;
- Unit enemyUnit;
- public Text dialogueText;
- public BattleHud playerHUD;
- public BattleHud enemyHUD;
- public BattleState state;
- // Start is called before the first frame update
- void Start()
- {
- state = BattleState.START;
- StartCoroutine(SetupBattle());
- }
- IEnumerator SetupBattle()
- {
- GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
- playerUnit = playerGO.GetComponent<Unit>();
- GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
- enemyUnit = enemyGO.GetComponent<Unit>();
- dialogueText.text = "A powerful " + enemyUnit.unitName + " fights you";
- playerHUD.SetHUD(playerUnit);
- enemyHUD.SetHUD(enemyUnit);
- yield return new WaitForSeconds(2f);
- state = BattleState.PLAYERTURN;
- PlayerTurn();
- }
- IEnumerator PlayerAttack()
- {
- bool isDead = enemyUnit.TakeDamage(playerUnit.damage);
- enemyHUD.SetHP(enemyUnit.currentHP);
- dialogueText.text = "You slash..It feels good!";
- yield return new WaitForSeconds(2f);
- if (isDead)
- {
- state = BattleState.WON;
- EndBattle();
- }
- else
- {
- state = BattleState.ENEMYTURN;
- StartCoroutine(EnemyTurn());
- }
- }
- IEnumerator EnemyTurn()
- {
- dialogueText.text = enemyUnit.unitName + " attacks!";
- yield return new WaitForSeconds(1f);
- bool isDead = playerUnit.TakeDamage(enemyUnit.damage);
- playerHUD.SetHP(playerUnit.currentHP);
- yield return new WaitForSeconds(1f);
- if (isDead)
- {
- state = BattleState.LOST;
- EndBattle();
- }
- else
- {
- state = BattleState.PLAYERTURN;
- PlayerTurn();
- }
- }
- void EndBattle()
- {
- if (state == BattleState.WON)
- {
- dialogueText.text = "You won the fight!";
- SceneManager.LoadScene("Level2Win");
- }
- else if (state == BattleState.LOST)
- {
- dialogueText.text = "You were defeated.";
- SceneManager.LoadScene("Level2");
- }
- }
- void PlayerTurn()
- {
- dialogueText.text = "You feel sleepy...choose an action:";
- }
- IEnumerator PlayerHeal()
- {
- playerUnit.Heal(10);
- playerHUD.SetHP(playerUnit.currentHP);
- dialogueText.text = "You are filled with dreams! +10HP";
- yield return new WaitForSeconds(2f);
- state = BattleState.ENEMYTURN;
- StartCoroutine(EnemyTurn());
- }
- public void OnAttackButton()
- {
- if (state != BattleState.PLAYERTURN)
- return;
- StartCoroutine(PlayerAttack());
- }
- public void OnHealButton()
- {
- if (state != BattleState.PLAYERTURN)
- return;
- StartCoroutine(PlayerHeal());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement