Chapter 3. Expandable Item Classes

Items in video games are very important. They can be tools, weapons, healing items, traps, clothing, armor, ammo, keys, and so on. Items are what the player will interact with the most in your game. Since the items are so often used, it is a good practice to create item classes that can be expanded and used in all possible situations without having to rewrite the class.

In this chapter, we will cover the following topics:

  • Creating customizable classes for items
  • Learning how GameObjects can interact with each other through sending messages
  • Creating an Item class that affects the player
  • Creating a Melee item class that will affect environments and enemies
  • Creating a Projectile item class that can be used for items that travel distances
  • Utilizing a classification system for all objects to decide what they do
  • Using trigger-based collisions for the Melee and Projectile item classes
  • Using two types of movement for projectile items

The self item class

The first item class we'll create is for an item that affects the player upon usage. Items that players use typically affects their various stats either by adding or removing them or buffing/debuffing them for a certain amount of time. Now let's start scripting; create a new script and name it itemSelf.

Adding our variables

Our first set of variables will actually be added outside of our class as they are enum variables:

public enum SelfAction {BuffDebuff, ChangeHP, ChangeArmor, None};
public enum SelfType {Armor, Potion, None};

The first enum we created will be used to pick what the item does. We've got a few options for our items, but this can be expanded and customized to your liking. The second enum we use will determine of what type the item is; for now, we're just checking to see whether it's a potion or armor. Now let's add the rest of our variables:

public GameObject Player;
public int Amount, Value, ArmorAmount;
public float Weight;
public string Name, Stat;
public SelfAction selfAction = SelfAction.None;
public SelfType selfType = SelfType.None;

We add a GameObject so that we have a player reference to adjust stats. The rest of the variables we added are for the item stats. Finally, we add our two enums to our list of variables. We make these variables public so that anyone can just drag-and-drop the scripts for easy item creation.

Buff or debuff stats

The first function we'll add to our item script will allow us to add or subtract player stats. Add the following code to your script:

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

When we call this function, we send a message to our player, which will call a function in a script on the player that will add or subtract the stat we specify. In this message, we tell this function which function to call as well as send a KeyValuePair variable. We use a KeyValuePair variable to send both the stat we want to modify as well as the amount that we want to modify it by.

The health changer

Our next function to be added will be one that will affect the player's health. Add the following code to your script:

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

When we call ChangeHealth, we send a message to the player to call a function known as ChangeHealth, and we send Amount as well. As you can see, we use Amount often. Since changing stats is all about amounts, we use a single variable to make it easier for us.

The armor changer

The next and final stat modifying function we'll add will allow us to adjust the armor of our player. Add this function to your script:

void ChangeArmorAmount()
{
  Player.SendMessage("ChangeArmorAmount", ArmorAmount);
}

This function is similar to the ChangeHealth function. We send the player a message to call a function that will change the player's armor amount. Then, we also send it the amount we want to change it by.

The item activator

This last function that we add will be called by other classes to activate the item. Add this last function to your script:

void Activate()
{
  switch(selfAction)
  {
  case SelfAction.BuffDebuff:
    BuffDebuffStat();
    break;
  case SelfAction.ChangeHP:
    ChangeHealth();
    break;
  case SelfAction.ChangeArmor:
    ChangeArmorAmount();
    break;
  }

  if(selfType == SelfType.Potion)
    Destroy(gameObject);
}

When this function is called, we use a switch statement to check the selfAction variable. This is an easy way to see what the item should do when the player uses it. At the end of the Activate function, we check to see what type of item it is. If it is a potion, we destroy the GameObject. Not all items get destroyed upon use, such as armor, so we use the selfType variable to determine what type of item it is.

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

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