So im working on a game that has the player use a variety of items, all tied to the same 3 keys (i.e. if itemKey1 has the greatsword state, then pressing itemKey1 will use the greatsword). at the moment i have an input handler class that stores the state and checks for input, it then calls a function in my “UseItem” class, and in that, it checks, in a series of if statements, what item is selected, then calls a different function depending.
public enum ItemType
{
rapier, greatsword, bow
}
public class InputHandler : MonoBehaviour
{
[Header("INPUTS")]
public static KeyCode itemKey1 = KeyCode.J;
public static KeyCode itemKey2 = KeyCode.K;
public static KeyCode itemKey3 = KeyCode.L;
public static KeyCode runKey = KeyCode.LeftShift;
[Header("ENUMS")]
public ItemType item1;
public ItemType item2;
public ItemType item3;
[Header("SCRIPT REFERENCES")]
UseItem use;
private void Start()
{
use = FindObjectOfType<UseItem>();
}
private void Update()
{
InputItem();
}
void InputItem()
{
if (Input.GetKeyDown(itemKey1))
{
use.InputUse(item1);
}
if (Input.GetKeyDown(itemKey2))
{
use.InputUse(item2);
}
if (Input.GetKeyDown(itemKey3))
{
use.InputUse(item3);
}
}
}
public class UseItem : MonoBehaviour
{
RapierAttack rapier;
GreatswordAttack greatsword;
PlayerBow bow;
private void Start()
{
rapier = GetComponent<RapierAttack>();
greatsword = GetComponent<GreatswordAttack>();
bow = GetComponent<PlayerBow>();
}
public void InputUse(ItemType item)
{
if(item == ItemType.rapier)
{
if(rapier.canUse)
rapier.Use();
}
else if(item == ItemType.greatsword)
{
if (greatsword.canUse)
greatsword.Use();
}else if(item == ItemType.bow)
{
bow.Use();
}
}
}
This all works, but my concern is that it isnt very expandable. i would like to call one function that is overidden by the specific Use() function that is applicable. Ive heard about abstract classes, but i dont really understand them. would scriptable objects be of any use?
Sorry for the really long question, any help is welcome!