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:
Item
class that affects the playerMelee
item class that will affect environments and enemiesProjectile
item class that can be used for items that travel distancesMelee
and Projectile
item classesThe 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
.
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.
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.
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 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.
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.
3.22.71.28