The melee item class

The melee item class will have similar properties and functions as the self item class. What is different about the two is that the functions don't affect the player, but other GameObjects. Also, the way we activate the item is different.

To get started, create a new script and name it itemMelee. We'll start our script by adding some variables, similar to the ones we used in the itemSelf class.

Adding our variables

First, we'll add a couple of enum variables:

public enum MeleeAction {BuffDebuff, ChangeHP, ActivateEnv, None};
public enum MeleeType {Weapon, Potion, None};

The MeleeAction enum will decide what the melee item does. Since melee items can interact with various GameObjects, its actions will vary just as much as it can. The MeleeType enum will determine whether the player uses a weapon, potion, or no items. Now, let's add the rest of our variables in:

public int Amount, Value;
public float Weight;
public string Name, Stat;
public MeleeAction meleeAction = MeleeAction.None;
public MeleeType meleeType = MeleeType.None;

As you can see, the variables are similar to the ones we used in our itemSelf class; our only major differences are the different names for our Type and Action enums.

Buff or debuff stats

The first function that we'll add to our melee item will allow melee items to modify the stats of other objects. Add this function to the script:

void BuffDebuffStat(GameObject other)
{
  other.SendMessage("BuffDebuffStat", new KeyValuePair<string, int>(Stat, Amount));
}

This function, when called, receives a GameObject, which will be the GameObject that we are affecting with the melee item. We then send a message to that GameObject to call the function that modifies the stats, and then pass the KeyValuePair to it. The KeyValuePair contains the stat we want to modify as well as the amount that we want to modify it by.

The health changer

The next function we'll add to the script will allow the melee item to change the health of other GameObjects. Add the following function after the BuffDebuffStat function in our script:

void ChangeHealth(GameObject other)
{
  other.SendMessage("ChangeHealth", Amount);
}

When this function is called, it will modify the health of the GameObject that the melee item collides with. This could mean healing or hurting the GameObject, but this function can be used either way.

Let's interact with the environment

The last and final function will allow the player to interact with the environment. Add this function to your script:

void ActivateEnvironment(GameObject other)
{
  other.SendMessage("Activate");
}

This function is called when the melee item collides with an environmental object the player can interact with. We send the object we want to interact with the message to activate. From here, the other GameObject handles the rest of the interaction.

Detecting triggers

In order to call the functions that we just created, we have to create the interaction between the melee item and the other GameObject. Add this final function to the script:

void OnTriggerEnter(Collider col)
{
  switch(col.gameObject.tag)
  {
  case "Enemy":
    if(meleeType != MeleeType.Potion)
    {
      if(meleeAction == MeleeAction.ChangeHP)
        ChangeHealth(col.gameObject);

      if(meleeAction == MeleeAction.BuffDebuff)
        BuffDebuffStat(col.gameObject);

      if(meleeAction == MeleeAction.ActivateEnv)
        ActivateEnvironment(col.gameObject);
    }
    break;
  case "Environment":
    if(meleeType == MeleeType.Potion)
    {
      if(meleeAction == MeleeAction.ChangeHP)
        ChangeHealth(col.gameObject);

      if(meleeAction == MeleeAction.BuffDebuff)
        BuffDebuffStat(col.gameObject);
    }
    break;
  }
    
  if(meleeType == MeleeType.Potion)
    Destroy(gameObject);
}

To detect the contact between the melee item and GameObject that the player hits, we use OnTriggerEnter to activate our functions. When the melee item enters a triggered GameObject, the OnTriggerEnter function is called and it will receive the GameObject that it entered.

From here, we use a switch statement to check the tag of the trigger GameObject. Using a tag is a quick way to check what the player hit with their melee item. Once we find the correctly tagged GameObject, we check the meleeType variable and then the meleeAction variable.

Depending on the type of melee item, we decide what the item can and can't do. In both case statements, we check whether the melee type is a potion or not; this will decide whether to activate environmental objects or not. Also, at the end of the function, we destroy the melee item if it is a potion; this ensures that potions are a single-use item.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.198.174