Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD2542-Project02-AdamSkawinski/RPG/Assets/Scripts/BattleSystem.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
162 lines (117 sloc)
3.57 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
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 Animator animator; | |
public Text dialogueText; | |
public BattleHUD playerHUD; | |
public BattleHUD enemyHUD; | |
public BattleState state; | |
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 hostile " + enemyUnit.unitName + " approaches..."; | |
playerHUD.SetHUD(playerUnit); | |
enemyHUD.SetHUD(enemyUnit); | |
yield return new WaitForSeconds(.5f); | |
state = BattleState.PLAYERTURN; | |
StartCoroutine(PlayerTurn()); | |
} | |
IEnumerator PlayerAttack() | |
{ | |
//Damage enemy | |
bool isDead = enemyUnit.TakeDamage(playerUnit.damage); | |
enemyHUD.SetHP(enemyUnit.currentHP); | |
yield return new WaitForSeconds(1.0f); | |
dialogueText.text = "The attack is successful!"; | |
state = BattleState.ENEMYTURN; | |
yield return new WaitForSeconds(2f); | |
//check if enemy dead | |
if (isDead) | |
{ | |
state = BattleState.WON; | |
EndBattle(); | |
} | |
else | |
{ | |
state = BattleState.ENEMYTURN; | |
StartCoroutine(EnemyTurn()); | |
} | |
} | |
IEnumerator EnemyTurn() | |
{ | |
dialogueText.text = enemyUnit.unitName + "'s turn"; | |
yield return new WaitForSeconds(1.5f); | |
dialogueText.text = enemyUnit.unitName + " attacks!"; | |
yield return new WaitForSeconds(2f); | |
bool isDead = playerUnit.TakeDamage(enemyUnit.damage); | |
playerHUD.SetHP(playerUnit.currentHP); | |
yield return new WaitForSeconds(.5f); | |
if (isDead) | |
{ | |
state = BattleState.LOST; | |
} | |
else | |
{ | |
state = BattleState.PLAYERTURN; | |
PlayerTurn(); | |
} | |
} | |
void EndBattle() | |
{ | |
//change state | |
if (state == BattleState.WON) | |
{ | |
dialogueText.text = "You won"; | |
} | |
else if (state == BattleState.LOST) | |
{ | |
dialogueText.text = "You lost"; | |
} | |
} | |
IEnumerator PlayerTurn() | |
{ | |
dialogueText.text = "Choose an action:"; | |
yield return new WaitForSeconds(.5f); | |
} | |
IEnumerator PlayerHeal() | |
{ | |
yield return new WaitForSeconds(.5f); | |
playerUnit.Heal(5); | |
playerHUD.SetHP(playerUnit.currentHP); | |
yield return new WaitForSeconds(.01f); | |
dialogueText.text = "You feel renewed strength!"; | |
state = BattleState.ENEMYTURN; | |
yield return new WaitForSeconds(1f); | |
StartCoroutine(EnemyTurn()); | |
} | |
public void OnAttackButton() | |
{ | |
if (state != BattleState.PLAYERTURN) | |
return; | |
StartCoroutine(PlayerAttack()); | |
} | |
public void OnHealButton() | |
{ | |
if (state != BattleState.PLAYERTURN) | |
return; | |
StartCoroutine(PlayerHeal()); | |
} | |
} |