- Home /
Question by
Dylanporter · May 09, 2017 at 06:13 AM ·
script.state machinebattle-system
turn based battle system. how to add a back button to the state machine to go back to a previous list
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;
public class BattleStateMachine : MonoBehaviour{
public enum performAction
{
WAIT,
TAKEACTION,
PERFORMACTION,
CHECKALIVE,
WIN,
LOSE
}
public performAction battleStates;
public List<HandleTurn> PerformList = new List<HandleTurn>();
public List<GameObject> HerosInBattle = new List<GameObject>();
public List<GameObject> EnemysInBattle = new List<GameObject>();
public enum HeroGUI
{
ACTIVATE,
WAITING,
INPUT1,
INPUT2,
DONE
}
public HeroGUI HeroInput;
public List<GameObject> HerosToManage = new List<GameObject>();
private HandleTurn HeroChoise;
public GameObject enemyButton;
public Transform Spacer;
public GameObject AttackPanel;
public GameObject EnemySelectPanel;
public GameObject MagicPanel;
//attack of heros
public Transform actionSpacer;
public Transform magicSpacer;
public GameObject actionButton;
public GameObject magicButton;
public GameObject cancelButton;
private List<GameObject> atkBtns = new List<GameObject>();
//enemy buttons
private List<GameObject> enemyBtns = new List<GameObject>();
//SPAWN POINTS
public List<Transform> spawnPoints = new List<Transform>();
void Awake()
{
for(int i = 0; i< GameManager.instance.enemyAmount; i++)
{
GameObject NewEnemy = Instantiate(GameManager.instance.enemysToBattle[i], spawnPoints[i].position, Quaternion.identity) as GameObject;
NewEnemy.name = NewEnemy.GetComponent<EnemyStateMachine>().enemy.theName + "_" + (i + 1);
NewEnemy.GetComponent<EnemyStateMachine>().enemy.theName = NewEnemy.name;
EnemysInBattle.Add(NewEnemy);
}
}
void Start()
{
battleStates = performAction.WAIT;
//EnemysInBattle.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
HerosInBattle.AddRange(GameObject.FindGameObjectsWithTag("Hero"));
HeroInput = HeroGUI.ACTIVATE;
AttackPanel.SetActive(false);
EnemySelectPanel.SetActive(false);
MagicPanel.SetActive(false);
EnemyButtons();
}
// Update is called once per frame
void Update()
{
switch (battleStates)
{
case (performAction.WAIT):
if (PerformList.Count > 0)
{
battleStates = performAction.TAKEACTION;
}
break;
case (performAction.TAKEACTION):
GameObject performer = GameObject.Find(PerformList[0].Attacker);
if (PerformList[0].Type == "Enemy")
{
EnemyStateMachine ESM = performer.GetComponent<EnemyStateMachine>();
for (int i = 0; i < HerosInBattle.Count; i++)
{
if (PerformList[0].AttackersTarget == HerosInBattle[i])
{
ESM.HeroToAttack = PerformList[0].AttackersTarget;
ESM.currentState = EnemyStateMachine.TurnState.ACTION;
break;
}
else
{
PerformList[0].AttackersTarget = HerosInBattle[Random.Range(0, HerosInBattle.Count)];
ESM.HeroToAttack = PerformList[0].AttackersTarget;
ESM.currentState = EnemyStateMachine.TurnState.ACTION;
}
}
}
if (PerformList[0].Type == "Hero") {
HeroStateMachine HSM = performer.GetComponent<HeroStateMachine>();
HSM.EnemyToAttack = PerformList[0].AttackersTarget;
HSM.currentState = HeroStateMachine.TurnState.ACTION;
}
battleStates = performAction.PERFORMACTION;
break;
case (performAction.PERFORMACTION):
//idle
break;
case (performAction.CHECKALIVE):
if (HerosInBattle.Count < 1)
{
battleStates = performAction.LOSE;
//lose game
}
else if (EnemysInBattle.Count < 1)
{
battleStates = performAction.WIN;
//Win the battle
}
else
{
//call function
ClearAttackPanel();
HeroInput = HeroGUI.ACTIVATE;
}
break;
case (performAction.LOSE):
{
Debug.Log("You Lost the Battle");
}
break;
case (performAction.WIN):
{
Debug.Log("You Win the Battle");
for(int i = 0; i < HerosInBattle.Count; i++)
{
HerosInBattle[i].GetComponent<HeroStateMachine>().currentState = HeroStateMachine.TurnState.WAITING;
}
GameManager.instance.LoadSceneAfterBattle();
GameManager.instance.gameState = GameManager.GameStates.WORLD_STATE;
GameManager.instance.enemysToBattle.Clear();
}
break;
}
switch (HeroInput)
{
case (HeroGUI.ACTIVATE):
if (HerosToManage.Count > 0)
{
HerosToManage[0].transform.FindChild("Selector").gameObject.SetActive(true);
//create new handleturn instance
HeroChoise = new HandleTurn();
AttackPanel.SetActive(true);
//populate action buttons
CreateAttackButtons();
HeroInput = HeroGUI.WAITING;
}
break;
case (HeroGUI.WAITING):
//idle
break;
case (HeroGUI.DONE):
HeroInputDone();
break;
}
}
public void collectActions(HandleTurn input)
{
PerformList.Add(input);
}
public void EnemyButtons()
{
//cleanup
foreach(GameObject enemyBtn in enemyBtns)
{
Destroy(enemyBtn);
}
enemyBtns.Clear();
//create buttons
foreach (GameObject enemy in EnemysInBattle)
{
GameObject newButton = Instantiate(enemyButton) as GameObject;
EnemySelectButton button = newButton.GetComponent<EnemySelectButton>();
EnemyStateMachine cur_enemy = enemy.GetComponent<EnemyStateMachine>();
Text buttonText = newButton.transform.FindChild("Text").gameObject.GetComponent<Text>();
buttonText.text = cur_enemy.enemy.theName;
button.EnemyPrefab = enemy;
newButton.transform.SetParent(Spacer, false);
enemyBtns.Add(newButton);
}
}
public void Input1()//attack button
{
HeroChoise.Attacker = HerosToManage[0].name;
HeroChoise.AttackersGameObject = HerosToManage[0];
HeroChoise.Type = "Hero";
HeroChoise.choosenAttack = HerosToManage[0].GetComponent<HeroStateMachine>().hero.attacks[0];
AttackPanel.SetActive(false);
EnemySelectPanel.SetActive(true);
}
public void Input2(GameObject choosenEnemy)//enemy selection
{
HeroChoise.AttackersTarget = choosenEnemy;
HeroInput = HeroGUI.DONE;
}
void HeroInputDone()
{
PerformList.Add(HeroChoise);
//clean the attack panel
ClearAttackPanel();
HerosToManage[0].transform.FindChild("Selector").gameObject.SetActive(false);
HerosToManage.RemoveAt(0);
HeroInput = HeroGUI.ACTIVATE;
}
void ClearAttackPanel()
{
EnemySelectPanel.SetActive(false);
AttackPanel.SetActive(false);
MagicPanel.SetActive(false);
foreach (GameObject atkBtn in atkBtns)
{
Destroy(atkBtn);
}
atkBtns.Clear();
}
//create actionbuttons
void CreateAttackButtons()
{
GameObject AttackButton = Instantiate(actionButton) as GameObject;
Text AttackButtonText = AttackButton.transform.FindChild("Text").gameObject.GetComponent<Text>();
AttackButtonText.text = "Attack";
AttackButton.GetComponent<Button>().onClick.AddListener(() => Input1());
AttackButton.transform.SetParent(actionSpacer, false);
atkBtns.Add(AttackButton);
GameObject MagicAttackButton = Instantiate(actionButton) as GameObject;
Text MagicAttackButtonText = MagicAttackButton.transform.FindChild("Text").gameObject.GetComponent<Text>();
MagicAttackButtonText.text = "Magic";
MagicAttackButton.GetComponent<Button>().onClick.AddListener(() => Input3());
MagicAttackButton.transform.SetParent(actionSpacer, false);
atkBtns.Add(MagicAttackButton);
if (HerosToManage[0].GetComponent<HeroStateMachine>().hero.MagicAttacks.Count > 0)
{
foreach (BaseAttack magicAtk in HerosToManage[0].GetComponent<HeroStateMachine>().hero.MagicAttacks)
{
GameObject MagicButton = Instantiate(magicButton) as GameObject;
Text MagicButtonText = MagicButton.transform.FindChild("Text").gameObject.GetComponent<Text>();
MagicButtonText.text = magicAtk.attackName;
AttackButton ATB = MagicButton.GetComponent<AttackButton>();
ATB.magicAttackToPerform = magicAtk;
MagicButton.transform.SetParent(magicSpacer, false);
atkBtns.Add(MagicButton);
Debug.Log(HerosToManage[0].GetComponent<HeroStateMachine>().hero.MagicAttacks.Count);
}
}
else
{
MagicAttackButton.GetComponent<Button>().interactable = false;
}
}
public void Input4(BaseAttack choosenMagic)//choosen magic attack
{
HeroChoise.Attacker = HerosToManage[0].name;
HeroChoise.AttackersGameObject = HerosToManage[0];
HeroChoise.Type = "Hero";
HeroChoise.choosenAttack = choosenMagic;
MagicPanel.SetActive(false);
EnemySelectPanel.SetActive(true);
}
public void Input3()//switching to magic attacks
{
AttackPanel.SetActive(false);
MagicPanel.SetActive(true);
}
}
Comment
Your answer
Follow this Question
Related Questions
How do i use StateMachine logic with my code ? 1 Answer
Animation State Machine Script Control Issue 1 Answer
Trying to learn Java Script but having some issues...with this code. 0 Answers
How can i create a string variable name that only the first letter will be big ? 1 Answer
How to place an object on terrain without turning it? 0 Answers