Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

battle system rpg

Discussion in 'Scripting' started by gregmoffat22, May 13, 2015.

  1. gregmoffat22

    gregmoffat22

    Joined:
    May 12, 2015
    Posts:
    4
    im having a problem with the battle system for a RPG game when it comes to taking of the health its not working any ideas?

    [ code]

    using System.Collections;

    public class TurnBasedCombatMachine : MonoBehaviour {

    private bool hasAddedXP = false;
    private BattleStateStart battleStateScript = new BattleStateStart();
    public BattleCalculations battleCalcSript = new BattleCalculations();
    public static baseAbility playerUsedAbility;
    public static baseAbility enemyUsedAbility;
    public int totalTurnCount;
    public static bool playerDidCompleteTurn;
    public static bool enemyDidCompleteTurn;
    private BattleStateEnemyChoice battleStateEnemyChoiceScript = new BattleStateEnemyChoice();
    private static BattleStates currentUser; // enemy or player choice

    public enum BattleStates{
    START,
    PLAYERCHOICE,
    ENEMYCHOICE,
    CALCMDAMAGE,
    ENDTURN,
    LOSE,
    WIN
    }

    public static BattleStates currentState;

    // Use this for initialization
    void Start () {
    hasAddedXP = false;
    totalTurnCount = 1;
    currentState = BattleStates.START;
    }

    // Update is called once per frame
    void Update () {
    switch (currentState) {
    case( BattleStates.START):
    battleStateScript.PrepareBattle();
    break;
    case( BattleStates.PLAYERCHOICE):
    currentUser = BattleStates.PLAYERCHOICE;
    playerDidCompleteTurn = true;
    break;
    case( BattleStates.ENEMYCHOICE):
    currentUser = BattleStates.ENEMYCHOICE;
    battleStateEnemyChoiceScript.EnemyCompeleteTurn();
    enemyDidCompleteTurn = true;
    //checkWhoGoesNext();
    break;
    case(BattleStates.CALCMDAMAGE):
    Debug.Log("Calculating Damage");
    if(currentUser == BattleStates.PLAYERCHOICE)
    {
    battleCalcSript.CaculateTotalplayerAbilityDamage(playerUsedAbility);
    }
    if(currentUser == BattleStates.ENEMYCHOICE)
    {
    battleCalcSript.CaculateTotalEnemyAbilityDamage(enemyUsedAbility);
    }
    checkWhoGoesNext();
    break;
    case( BattleStates.ENDTURN):
    totalTurnCount++;
    playerDidCompleteTurn = false;
    enemyDidCompleteTurn = false;
    Debug.Log("turnCounter "+totalTurnCount);
    currentState = BattleStates.PLAYERCHOICE;
    break;
    case( BattleStates.LOSE):
    break;
    case( BattleStates.WIN):
    if(!hasAddedXP){
    IncreasingExperience.AddExperience();
    hasAddedXP = true;
    }
    break;
    }
    }


    private void checkWhoGoesNext()
    {
    if (playerDidCompleteTurn == true && enemyDidCompleteTurn == false)
    {
    //enemy turn
    currentState = BattleStates.ENEMYCHOICE;
    }
    if (!playerDidCompleteTurn == false && enemyDidCompleteTurn == true)
    {
    //player turn
    currentState = BattleStates.PLAYERCHOICE;
    }
    if (playerDidCompleteTurn == true && enemyDidCompleteTurn == true)
    {
    //end turn state
    currentState = BattleStates.ENDTURN;
    }
    }
    }

    [ /code]
     
    Last edited: May 13, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    might want to start with reading the sticky about how to use [ code] [ /code] tags at the top of this forum... :)
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    might also be an idea to include the code that actually does damage in your project. I assume that it's these
    Code (csharp):
    1.  
    2. battleCalcSript.CaculateTotalplayerAbilityDamage(...)
    3. battleCalcSript.CaculateTotalEnemyAbilityDamage(...)
    4.  
    ?

    (is that meant to be "TotalPlayer" in there? to match the other function? capitalization matters in function c#)
     
  4. gregmoffat22

    gregmoffat22

    Joined:
    May 12, 2015
    Posts:
    4
    i cant see where to use the code things xD god im blind and ill add the calc damage
     
  5. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You can add code tags by pressing this small button in toolbar:


    As Lefty said, you can post us the code in:
    Code (CSharp):
    1. battleCalcSript.CaculateTotalplayerAbilityDamage(...)
    You can always put a Debug.Log(" o_O "); inside your calculate damage function in order to see if it is called. But I have to say that your approach using switch cases will cause you a lot of trouble as you develop your game further. It would be best at this point to create BaseBattleState class that you can inherit to create additional states. Something like:

    Code (CSharp):
    1. public abstract class BaseBattleState
    2. {
    3.      public abstract void Logic();
    4. }
    5.  
    6. public class BattleStart : BaseBattleState
    7. {
    8.      public override void Logic()
    9.      {
    10.           PrepareBattle();
    11.           //etc...
    12.      }
    13. }
    14.  
    15. public class BattleDamagePhase : BaseBattleState
    16. {
    17.      public override void Logic()
    18.      {
    19.           CalculateDamage();
    20.           //  etc...
    21.      }
    22. }
    23.  
    Now you can add all your states in a List<BaseBattleState> and obtain current state and run its logic.
    Code (CSharp):
    1. List<BaseBattleState> phases = new List<BaseBattleState>();
    2.  
    3. void Start()
    4. {
    5.      phases.Add( new BattleStart() );
    6.      phases.Add( new BattleDamagePhase() );
    7.     // and add other phases as well
    8. }
    9.  
    10. void Update()
    11. {
    12.      BaseBattleState phase = GetCurrentPhase(); // implement your own way to obtain current state,
    13.  
    14.      phase.Logic();
    15. }
    Best thing is, your Update is now a lot clean and all your game logic is encapsulated within the members of BaseBattleState. You can now use them in another script or even another project. Adding new phases is also easy, just create new subclass.

    Although switch cases have their use, it's best to avoid using them when dealing with complex cases like your game logic.

    Good luck!