Search Unity

Help With Turn Based RPG code

Discussion in 'Scripting' started by maxsterfinn, Mar 13, 2019.

  1. maxsterfinn

    maxsterfinn

    Joined:
    Mar 13, 2019
    Posts:
    3
    I made this code based on some things here and there but i mostly winged it from my imagination which is why it doesn't have whatever a states machine is. The problem is that the program works but after my first turn, the damage dealt will loop even if i choose a not attack option in the next turn. Also sorry if this is wrong thread its my first post.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class BattleManager : MonoBehaviour {
    7.  
    8.     public bool playerTurn;
    9.     public bool enemyTurn;
    10.  
    11.     public int CursorPositionX;
    12.     public int CursorPositionY;
    13.     private float x;
    14.     private float y;
    15.     private int playerHP;
    16.     private int enemyHP;
    17.     private int playerMP;
    18.     private int enemyMP;
    19.     private int playerATK;
    20.     private int enemyATK;
    21.     private int playerDEF;
    22.     private int enemyDEF;
    23.     private int playerEVA;
    24.     private int enemyEVA;
    25.     private int playerINT;
    26.     private int enemyINT;
    27.     private int playerCRIT;
    28.     private int enemyCRIT;
    29.     private int randomNumber;
    30.     private int enemyDMG;
    31.     private int playerDMG;
    32.     private Animator anim;
    33.     private GameObject Player;
    34.     private static bool battle;
    35.     public AudioClip newTrack;
    36.     private GameObject hitmark;
    37.  
    38.     private Music theAM;
    39.  
    40.     public bool turnUsed;
    41.  
    42.     private int StressLev;
    43.     private int PeaceLev;
    44.  
    45.     float waittime;
    46.     private PlayerController thePlayer;
    47.  
    48.  
    49.  
    50.     // Use this for initialization
    51.     void Start () {
    52.        
    53.         hitmark = GameObject.Find("Atk");
    54.         StressLev = 0;
    55.         PeaceLev = 0;
    56.         playerTurn = true;
    57.         enemyTurn = false;
    58.         turnUsed = false;
    59.         x = -6.44f;
    60.         y = -4.11f;
    61.         GameObject Player = GameObject.Find("Player");
    62.         PlayerStats Playerstat = Player.GetComponent<PlayerStats>();
    63.         GameObject Enemy = GameObject.Find("Enemy");
    64.         EnemyStats Enemystat = Enemy.GetComponent<EnemyStats>();
    65.         enemyHP = Enemystat.enemyCurrentHealth;
    66.         playerHP = Playerstat.playerCurrentHealth;
    67.         playerMP = Playerstat.playerCurrentMagic;
    68.         enemyMP = Enemystat.enemyCurrentMagic;
    69.         playerATK = Playerstat.playerATK;
    70.         enemyATK = Enemystat.enemyATK;
    71.         playerDEF = Playerstat.playerDEF;
    72.         enemyDEF = Enemystat.enemyDEF;
    73.         playerEVA = Playerstat.playerEVA;
    74.         enemyEVA = Enemystat.enemyEVA;
    75.         playerINT = Playerstat.playerINT;
    76.         enemyINT = Enemystat.enemyINT;
    77.         playerCRIT = Playerstat.playerCRIT;
    78.         enemyCRIT = Enemystat.enemyCRIT;
    79.         System.Random rnd = new System.Random();
    80.         randomNumber = rnd.Next(1, 100);
    81.         theAM = FindObjectOfType<Music>();
    82.         Player = GameObject.Find("Player");
    83.         anim = Player.GetComponent<Animator>();
    84.         CursorPositionX = 1;
    85.         CursorPositionY = 1;
    86.         thePlayer = FindObjectOfType<PlayerController>();
    87.     }
    88.  
    89.     // Update is called once per frame
    90.     IEnumerator Waiter()
    91.     {
    92.             if (enemyHP <= 0)
    93.             {
    94.  
    95.                 theAM.ChangeBGM(newTrack);
    96.                 yield return new WaitForSeconds(45);
    97.             Battle1.battle = false;
    98.             SceneManager.LoadScene("Starting");
    99.             thePlayer.startPoint = "BattleExit";
    100.  
    101.         }
    102.        
    103.     }
    104.     void Update () {
    105.        
    106.         if (playerTurn && !turnUsed)
    107.         {
    108.             playerChoose();
    109.         }
    110.         if (playerTurn && turnUsed)
    111.         {
    112.             playerDo();
    113.         }
    114.         if (enemyTurn)
    115.         {
    116.             enemyDo();
    117.         }
    118.         if (!playerTurn && !enemyTurn)
    119.         {
    120.             endSequence();
    121.         }
    122.         if (enemyHP <= 0)
    123.         {
    124.             StartCoroutine(Waiter());
    125.         }
    126.  
    127.  
    128.     }
    129.  
    130.     void playerChoose()
    131.     {
    132.             if (Input.GetAxisRaw("Horizontal") > 0.5 && CursorPositionX != 2)
    133.             {
    134.                 CursorPositionX++;
    135.             System.Threading.Thread.Sleep(100);
    136.             }
    137.             if (Input.GetAxisRaw("Horizontal") < -0.5 && CursorPositionX != 1)
    138.             {
    139.                 CursorPositionX--;
    140.             System.Threading.Thread.Sleep(100);
    141.             }
    142.             if (Input.GetAxisRaw("Vertical") > 0.5 && CursorPositionY != 1)
    143.             {
    144.                 CursorPositionY--;
    145.             System.Threading.Thread.Sleep(100);
    146.         }
    147.             if (Input.GetAxisRaw("Vertical") < -0.5 && CursorPositionY != 3)
    148.             {
    149.                 CursorPositionY++;
    150.             System.Threading.Thread.Sleep(100);
    151.         }
    152.             if (CursorPositionX == 1)
    153.                 x = -3.27f;
    154.             if (CursorPositionX == 2)
    155.                 x = -1.09f;
    156.             if (CursorPositionY == 1)
    157.                 y = -2.46f;
    158.             if (CursorPositionY == 2)
    159.                 y = -3.42f;
    160.             if (CursorPositionY == 3)
    161.                 y = -4.19f;
    162.             transform.position = new Vector2(x, y);
    163.         if(Input.GetAxisRaw("Confirm") != 0)
    164.         {
    165.             turnUsed = true;
    166.         }
    167.     }
    168.     void playerDo()
    169.     {
    170.         if (CursorPositionX == 1 && CursorPositionY == 1)
    171.         {
    172.             //Attack
    173.             enemyDMG = playerATK - enemyDEF / 2;
    174.         }
    175.         if (CursorPositionX == 1 && CursorPositionY == 2)
    176.         {
    177.             //Magic
    178.             enemyDMG = playerINT - enemyDEF / 2;
    179.         }
    180.         if (CursorPositionX == 2 && CursorPositionY == 3)
    181.         {
    182.             GameObject Player = GameObject.Find("Player");
    183.             PlayerStats Playerstat = Player.GetComponent<PlayerStats>();
    184.             //Stress
    185.             if (StressLev < 300)
    186.             {
    187.                 StressLev = StressLev + 25;
    188.                 Playerstat.Stress = StressLev;
    189.             }
    190.             else
    191.                 enemyDMG = 300 + playerATK;
    192.         }
    193.         if (CursorPositionX == 2 && CursorPositionY == 1)
    194.         {
    195.             //Break
    196.             if (StressLev >= 100 && StressLev < 200)
    197.             {
    198.                 enemyDMG = 100 + playerATK;
    199.                 StressLev = 0;
    200.             }
    201.             if (StressLev >= 200 && StressLev < 300)
    202.             {
    203.                 enemyDMG = 200 + playerATK;
    204.                 StressLev = 0;
    205.             }
    206.             if (StressLev == 300)
    207.             {
    208.                 enemyDMG = 300 + playerATK;
    209.                 StressLev = 0;
    210.             }
    211.             if (PeaceLev >= 100 && PeaceLev < 200)
    212.             {
    213.                 enemyDMG = 125 + playerATK;
    214.                 PeaceLev = 0;
    215.             }
    216.             if (PeaceLev >= 200 && PeaceLev < 300)
    217.             {
    218.                 enemyDMG = 250 + playerATK;
    219.                 PeaceLev = 0;
    220.             }
    221.             if (PeaceLev == 300)
    222.             {
    223.                 enemyDMG = 375 + playerATK;
    224.                 PeaceLev = 0;
    225.             }
    226.             if (StressLev >= 100 && StressLev < 200 && PeaceLev >= 100 && PeaceLev < 200)
    227.             {
    228.                 enemyDMG = 225 + playerATK;
    229.                 StressLev = 0;
    230.                 PeaceLev = 0;
    231.             }
    232.  
    233.             if (StressLev >= 200 && StressLev < 300 && PeaceLev >= 200 && PeaceLev < 300)
    234.             {
    235.                 enemyDMG = 450 + playerATK;
    236.                 StressLev = 0;
    237.                 PeaceLev = 0;
    238.             }
    239.             if (StressLev == 300 && PeaceLev == 300)
    240.             {
    241.                 StressLev = 0;
    242.                 PeaceLev = 0;
    243.                 enemyDMG = 675 + playerATK;
    244.  
    245.             }
    246.             if (StressLev < 100 && PeaceLev < 100)
    247.                 enemyDMG = playerATK - enemyDEF / 2;
    248.         }
    249.         if (CursorPositionX == 2 && CursorPositionY == 2)
    250.         {
    251.             //Items
    252.             if(playerHP <= 90)
    253.             playerHP = playerHP + 10;
    254.         }
    255.         if (CursorPositionX == 1 && CursorPositionY == 3)
    256.         {
    257.             GameObject Player = GameObject.Find("Player");
    258.             PlayerStats Playerstat = Player.GetComponent<PlayerStats>();
    259.             //Peace
    260.             if (PeaceLev < 300)
    261.             {
    262.                 PeaceLev = PeaceLev + 20;
    263.             Playerstat.Peace = PeaceLev;
    264.         }
    265.         else
    266.             enemyDMG = 375 + playerATK;
    267.         }
    268.         enemyTurn = true;
    269.         playerTurn = false;
    270.         enemyHP = enemyHP - enemyDMG;
    271.     }
    272.     void enemyChoose()
    273.     {
    274.  
    275.     }
    276.     void enemyDo()
    277.     {
    278.         playerDMG = enemyATK - playerDEF / 2;
    279.         if (playerDMG < 0)
    280.             playerDMG = 0;
    281.         playerHP = playerHP - playerDMG;
    282.         enemyTurn = false;
    283.         playerTurn = false;
    284.     }
    285.     void endSequence()
    286.     {
    287.         enemyTurn = false;
    288.         playerTurn = true;
    289.         GameObject Player = GameObject.Find("Player");
    290.         PlayerStats Playerstat = Player.GetComponent<PlayerStats>();
    291.         GameObject Enemy = GameObject.Find("Enemy");
    292.         EnemyStats Enemystat = Enemy.GetComponent<EnemyStats>();
    293.         enemyTurn = false;
    294.         playerTurn = true;
    295.         hitmark.transform.position = new Vector2(-7.07f,-3.44f);
    296.         Playerstat.HurtPlayer(playerDMG);
    297.        
    298.  
    299.         Enemystat.Hurtenemy(enemyDMG);
    300.         hitmark.transform.position = new Vector2(0f, 2.5f);
    301.         turnUsed = false;
    302.         System.Threading.Thread.Sleep(500);
    303.         hitmark.transform.position = new Vector2(0f, 112.5f);
    304.  
    305.     }
    306. }
    307.  
    there's some stuff in there that doesn't do anything BTW. here are the codes that are reference for hurting
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerStats : MonoBehaviour {
    6.     public string playername;
    7.     public int Stress;
    8.     public int Peace;
    9.     public int playerMaxHealth;
    10.     public int playerCurrentHealth;
    11.     public int playerMaxMagic;
    12.     public int playerCurrentMagic;
    13.     public int playerATK;
    14.     public int playerDEF;
    15.     public int playerEVA;
    16.     public int playerINT;
    17.     public int playerCRIT;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         playerCurrentHealth = playerMaxHealth;
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         if (playerCurrentHealth <= 0)
    28.             {
    29.             gameObject.SetActive(false);
    30.         }
    31.     }
    32.  
    33.     public void HurtPlayer(int damageTogive)
    34.     {
    35.         playerCurrentHealth -= damageTogive;
    36.     }
    37. }
    38.  
    this code is the same for the enemy BTW.
     
  2. maxsterfinn

    maxsterfinn

    Joined:
    Mar 13, 2019
    Posts:
    3
    Nevermind
    fixed one thing, I needed to put a damage resetter at beggining of update now i just need to fix the darn Peace/Stress system, the Break command doesnt deal damage even when peace/stress is over 100