Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i use StateMachine logic with my code ? 1 Answer

Animation State Machine Script Control Issue 1 Answer

E-commerce integration 0 Answers

Why are all forms of the Material constructor hidden in the documentation? 0 Answers

the scripts, physics and others execution time increase for low quality compared to good quality 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges