Search Unity

Getting Enemy Script Dynamically In RPG Turnbased Battle System

Discussion in 'Scripting' started by ChristmasGT, Sep 21, 2011.

  1. ChristmasGT

    ChristmasGT

    Joined:
    Sep 21, 2011
    Posts:
    11
    Hey Guys! I've been trying to pick up C# as well as learning Unity the last few weeks and everything's been fantastic so far. Currently I'm working on an 2D RPG with a Chrono Trigger esque battlesystem, and I've been able to get the battle to initiate as well as a GUI called as well as various other things.

    Currently though, I'm having a problem where I'd like to access the enemy's battlescript so that I can do my battle calculations and such. I've been able to hardcode in the attack and it works just fine, but the problem is; is that I'd like it to be dynamic since I'm going to be having a ton of different enemies (all with the tag Enemy) but they'll all be using their own attack script with the same variable names (e.g: Health, Defense, Agility,)

    Does anyone by chance have an idea about how I could go about that? I've attached a sample of my player battle code as well as my Enemy code. All the other enemies will be using the same basic battle variables the rest of that code is just me messing around with enemy movement.

    Thanks for any help!

    PlayerCode Example:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class H_Battle : MonoBehaviour
    5. {
    6.  
    7.     #region Private Variables
    8.  
    9.     private float H_NotificationTimer;
    10.     #endregion
    11.  
    12.     #region Public Variables
    13.  
    14.     public int H_Level;
    15.     public int H_Health;
    16.     public int H_Defense;
    17.     public int H_Agility;
    18.     public int H_Charisma;
    19.     public int H_Strength;
    20.     private int H_Experience;
    21.     private int H_ExperienceToLevel;
    22.     private float H_BattleTimer;
    23.     private int H_RunChance;
    24.     private float H_BattleMessageTimer;
    25.     private string H_BattleMessage;
    26.     private bool H_DisplayBattleMessage;
    27.     Enemy enemy;
    28.  
    29.     private string H_Name = "Hale";
    30.     #endregion
    31.  
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.                 H_BattleTimer = 5;
    37.                 H_BattleMessageTimer = 5;
    38.     }
    39.    
    40.     // Update is called once per frame
    41.     void Update ()
    42.     {
    43.     }
    44.  
    45.     void H_Defend()
    46.     {
    47.         //Doubles the players defense for a turn.
    48.         //TODO Make sure to change this rate back to normal somewhere else so that it doesn't keep stacking on turns.
    49.         H_Defense = H_Defense * 2;
    50.         H_BattleTimer = 5;
    51.         H_BattleMessageTimer = 5;
    52.         H_BattleMessage = H_Name + " Defended!" + H_Defense;
    53.         H_DisplayBattleMessage = true;
    54.         print("Hale tries to defend");
    55.     }
    56.  
    57.     void H_Run()
    58.     {
    59.                     H_RunChance = Random.Range(1, 3);
    60.  
    61.                     if (H_RunChance == 1)
    62.                     {
    63.                         H_BattleTimer = 5;
    64.                         H_BattleMessageTimer = 5;
    65.                         H_BattleMessage = "You've run from battle!";
    66.                         H_DisplayBattleMessage = true;
    67.                         H_Movement.BattleState = false;
    68.                         print(H_RunChance);
    69.                     }
    70.  
    71.                    Else if (H_RunChance >= 2)
    72.                     {
    73.                         H_BattleTimer = 5;
    74.                         H_BattleMessageTimer = 5;
    75.                         H_BattleMessage = "You've failed from running!";
    76.                         H_DisplayBattleMessage = true;
    77.                         print(H_RunChance);
    78.                     }
    79.                 }
    80.  
    81.     void H_Attack()
    82.     {
    83.  
    84.         //TODO Select enemy to attack
    85.  
    86.         //TODO Play attack sound.
    87.  
    88.         //TODO Peform basic attack and subtract the health of the enemy.
    89.  
    90.         print("Soon to be coded for attack");
    91.         H_BattleTimer = 5;
    92.         H_BattleMessageTimer = 5;
    93.         H_BattleMessage = H_Name + " Attacked!";
    94.         H_DisplayBattleMessage = true;
    95.     }
    96.  
    97.     void OnGUI()
    98.     {
    99.         if (H_Movement.BattleState)
    100.         {
    101.             //Resets the players defense so as not to stack defense.
    102.  
    103.             // After the command has been issued the battle timer counts down again.
    104.             H_BattleTimer -= Time.deltaTime;
    105.  
    106.             if (H_BattleTimer <= 0)
    107.             {
    108.                 GUI.Box(new Rect(0, Screen.height - 170, Screen.width / 4, 20), H_Name);
    109.                 GUI.Box(new Rect(0, Screen.height - 150, Screen.width / 4, 150), "");
    110.  
    111.  
    112.                 if (GUI.Button(new Rect(40, Screen.height - 145, 80, 40), "Attack"))
    113.                 {
    114.                     // Calls the H_Attack sub to carry code out.
    115.                     H_Attack();
    116.                 }
    117.  
    118.                 if (GUI.Button(new Rect(40, Screen.height - 95, 80, 40), "Defend"))
    119.                 {
    120.                     // Calls the H_Defend sub to carry code out.
    121.                     H_Defend();
    122.                 }
    123.  
    124.                 if (GUI.Button(new Rect(40, Screen.height - 45, 80, 40), "Run"))
    125.                 {
    126.                     // Calls the H_Run sub to carry code out.
    127.                     H_Run();
    128.                 }
    129.             }
    130.             if (H_DisplayBattleMessage)
    131.             {
    132.                 if (H_BattleMessageTimer >= 1)
    133.                 {
    134.                     H_BattleMessageTimer -= Time.deltaTime;
    135.                     GUI.Box(new Rect(0, 0, Screen.width, 40), H_BattleMessage);
    136.                 }
    137.                 if (H_BattleMessageTimer == 0)
    138.                 {
    139.                     H_DisplayBattleMessage = false;
    140.                 }
    141.             }
    142.         }
    143.     }
    144. }
    Enemy Code Example:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Leafer : MonoBehaviour
    5. {
    6.     public string EnemyName;
    7.     public int Health;
    8.     public int Defense;
    9.     public int Agility;
    10.     public int Strength;
    11.     public float MoveSpeed;
    12.     public string BattleMusicType;
    13.     private float AttackTimer;
    14.     private Transform trans;
    15.     private int direct = 1;
    16.     private float EnemyPos;
    17.     private float MobX;
    18.     private float MobY;
    19.     private bool LoadEnemyDetails;
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         AttackTimer = 5;
    24.         LoadEnemyDetails = true;
    25.     }
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (H_Movement.BattleState)
    30.         {
    31.             // Loads up the enemy details to the BattleEngine
    32.             if (LoadEnemyDetails)
    33.             {
    34.                 MoveSpeed = 0;
    35.                 LoadEnemyDetails = false;
    36.                 print(EnemyName + " Details are loaded up");
    37.             }
    38.         }
    39.         // Details are loaded up and now able to start the battle
    40.         if (!LoadEnemyDetails)
    41.         {
    42.             if (Health >= 1)
    43.             {
    44.                 //TODO Enemy does attacks on rotation
    45.             }
    46.             else if (Health <= 0)
    47.             {
    48.                 //TODO Player deatha animation
    49.                 Destroy(gameObject);
    50.  
    51.                 //If the leafer is dead, the battle is over.
    52.                 //TODO this needs to be changed for multiple enemies on the screen.
    53.  
    54.                 LoadEnemyDetails = false;
    55.                 H_Movement.BattleState = false;
    56.             }
    57.         }
    58.         else if (!H_Movement.BattleState)
    59.         {
    60.             MoveSpeed = .05f;
    61.             EnemyPos = transform.localPosition.y;
    62.             trans.Translate(MoveSpeed * direct, MoveSpeed * direct, 0);
    63.             // If the player hasn't collided with the enemy, the enemy keeps moving on his route
    64.             if (!H_Movement.BattleState)
    65.             {
    66.                 if (EnemyPos >= (MobX + MobY) + 2)
    67.                 {
    68.                     direct = -1;
    69.                 }
    70.                 if (EnemyPos <= (MobX + MobY) - 2)
    71.                 {
    72.                     direct = +1;
    73.                 }
    74.             }
    75.         }
    76.     }
    77.  
    78.     public void DealDamage()
    79.     {
    80.         print("We're inside of the enemy");
    81.     }
    82.     void Awake()
    83.     {
    84.         trans = transform;
    85.         MobX = transform.position.x;
    86.         MobY = transform.position.y;
    87.     }
    88.     void OnGUI()
    89.     {
    90.         if (H_Movement.BattleState)
    91.         {
    92.             GUI.Box(new Rect(transform.position.x, transform.position.y, 35, 20), Health.ToString());
    93.         }
    94.  
    95.         if (!H_Movement.BattleState)
    96.         {
    97.             //Keep doing whatcha doing, nothing to see here.
    98.         }
    99.     }
    100.     void OnTriggerEnter(Collider PlayerCollision)
    101.     {
    102.         if (PlayerCollision.tag == "Player_Hale")
    103.         {
    104.             MU_MusicBox.BattleMusicType = BattleMusicType;
    105.         }
    106.     }
    107. }
    108.  
    109.  
    110.