Decided to rewrite my inventory for something bit more modular. I just wanted to share what I had and if anyone had input on it and possibly overlook and make sure I’m not over complicating things. Been away from programming for very long time so I just want to reassure myself. Thanks for looking at it.
Note: This just sample setup and not full code.
public enum EWeaponType{
empty,
melee,
gun
}
Base Class for Item:
using UnityEngine;
using System.Collections;
public class Item {
public int id {
get;
set;
}
public string name {
get;
set;
}
public string description {
get;
set;
}
public virtual void Update(){
}
public virtual void Equip(){
}
public virtual void UnEquip(){
}
public virtual void Use(){
}
public virtual void Add(){
}
public virtual void Drop(){
}
}
Weapon Class Inherits Item:
using UnityEngine;
using System.Collections;
public class Weapon : Item {
public EWeaponType weaponType {
get;
set;
}
public virtual bool IsSemiAuto(){
return false;
}
}
Gun Class Inherits Weapon:
using UnityEngine;
using System.Collections;
public class Gun : Weapon{
public virtual void Reload(){
}
}
This is the unarmed class I created to fill the inventory default. Most of the comments in it are just to kinda give you an example of what you would do or could do. I haven’t written any of the code for unarmed so i used some samples from my shotgun class to fill the comments. Sorry if it brings confusion.
using UnityEngine;
using System.Collections;
public class Unarmed : Weapon
{
public int id;
public string name = "Unarmed";
public string description = "Unarmed";
public override void Update ()
{
/*This is called every frame from the inventory system
Example if this was gun:
if (!isReloading && shootTimer > 0) {
shootTimer -= Time.deltaTime;
}
*/
}
public override void Add ()
{
/*This is called when a item(weapon) is added to the inventory
For example if this was a gun
isReloading = false;
currentAmmo = clipSize;
shootTimer = 0;
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController> ();
shotgunMesh = GameObject.FindGameObjectWithTag("ShotgunMesh");
*/
}
public override void Equip ()
{
/* This is called when an item is swap to become the active item(weapaon)
You can do things like:
Enable Shotgun mesh or assign new animator controller to arms, character or whatever needs to be done to show the new weapon on screen.
*/
}
public override void UnEquip ()
{
//This is the opposite of above "Equip()". This is where you disable anything you enabled above.
}
public override void Use ()
{
/* This is where all the fun happens. This can be anything custom code you wish.
This is some sample code from my shotgun class:
if (!isReloading && currentAmmo > 0 && shootTimer <= 0) {
animationHandler.Fire ();
currentAmmo--;
shootTimer += 1 / fireRate;
SpawnBullet (); <--this is a function only the shotgun class has. Or you can create virtual function inside the gun class. Which I have already done.
audioSource.PlayOneShot (ContentLoader.Instance.GetAudio(shootSound));
}else if(currentAmmo <= 0 && shootTimer <= 0){
audioSource.PlayOneShot (ContentLoader.Instance.GetAudio(emptyClickSound));
}
*/
}
public override void Drop ()
{
//Here you can write some code like raycast to the floor and spawn shotgun object.
}
}
Inventory Class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour {
private List<Item> weaponSlots;
private int maxWeaponSlots = 3;
private int activeWeapon;
private int activeMenuWeaponSlot;
float lastTriggerValue;
bool triggerPushed;
string triggerAxis = "Trigger";
// Use this for initialization
void Start () {
weaponSlots = new List<Item> ();
//Initialize Lists
//Weapons
for(int i = 0;i < maxWeaponSlots;i++){
weaponSlots.Add(new Unarmed());
}
}
// Update is called once per frame
void Update () {
if(Input.GetButtonUp("wSlot1")){
activeWeapon = 0;
SwapWeapon (activeWeapon);
}
if(Input.GetButtonUp("wSlot2")){
activeWeapon = 1;
SwapWeapon (activeWeapon);
}
if(Input.GetButtonUp("wSlot3")){
activeWeapon = 2;
SwapWeapon (activeWeapon);
}
triggerPushed = Input.GetAxis(triggerAxis) > lastTriggerValue;
lastTriggerValue = Input.GetAxis(triggerAxis);
Weapon weapon = (Weapon)weaponSlots [activeWeapon];
if ((Input.GetAxis(triggerAxis) > 0))
{
if (!weapon.IsSemiAuto())
{
if (triggerPushed)
{
WeaponUse ();
}
}
else
{
WeaponUse ();
}
}
weaponSlots [activeWeapon].Update ();
}
public void SwapWeapon(int slot){
WeaponUnEquip ();
if (weaponSlots [slot].name == "Unarmed") {
activeWeapon = 0;
}else{
activeWeapon = slot;
}
WeaponEquip ();
}
public bool AddWeapon(Weapon weapon){
for(int i = 1;i < maxWeaponSlots;i++){
if (weaponSlots [i].name == "Unarmed") {
weaponSlots [i] = weapon;
return true;
}
}
return false;
}
public void WeaponReload(){
Weapon weapon = (Weapon)weaponSlots [activeWeapon];
if (weapon.weaponType = EWeaponType.gun) {
Gun gun = (Gun)weapon;
gun.Reload ();
}
}
public void WeaponUse(){
weaponSlots [activeWeapon].Use ();
}
public void WeaponEquip(){
weaponSlots [activeWeapon].Equip ();
}
public void WeaponUnEquip(){
weaponSlots [activeWeapon].UnEquip();
}
private void DropWeapon (int slot){
weaponSlots [activeMenuWeaponSlot].Drop();
weaponSlots [slot] = new Unarmed();
}
}