using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInventory : MonoBehaviour
{
[Header(“General”)]
public List<itemType> inventoryList;
public int selectedItem;
public float playerReach;
[SerializeField] GameObject throwitem_gameobject;
[Space(20)]
[Header("Keys")]
[SerializeField] KeyCode throwItemKey;
[SerializeField] KeyCode pickItemKey;
[Space(20)]
[Header("Item gameobjects")]
[SerializeField] GameObject Wooden_Sword_item;
[SerializeField] GameObject Iron_Sword_item;
[Space(20)]
[Header("Item Prefabs")]
[SerializeField] GameObject Wooden_Sword_prefab;
[SerializeField] GameObject Iron_Sword_prefab;
[SerializeField] Camera cam;
private Dictionary<itemType, GameObject> itemSetActive = new Dictionary<itemType, GameObject>() { };
private Dictionary<itemType, GameObject> itemInstantiate = new Dictionary<itemType, GameObject>() { };
void Start()
{
itemSetActive.Add(itemType.Wooden_Sword, Wooden_Sword_item);
itemSetActive.Add(itemType.Iron_Sword, Iron_Sword_item);
itemInstantiate.Add(itemType.Wooden_Sword, Wooden_Sword_prefab);
itemInstantiate.Add(itemType.Iron_Sword, Iron_Sword_prefab);
NewItemSelected();
}
void Update()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Input.GetKeyDown(throwItemKey) && inventoryList.Count > 1)
{
Instantiate(itemInstantiate[inventoryList[selectedItem]], position: throwitem_gameobject.transform.position, new Quaternion());
inventoryList.RemoveAt(selectedItem);
if (selectedItem != 0)
{
selectedItem -= 1;
}
NewItemSelected();
}
if(Physics.Raycast(ray, out hitInfo, playerReach) && Input.GetKey(pickItemKey))
{
IPickable item = hitInfo.collider.GetComponent<IPickable>();
if (item != null)
{
inventoryList.Add(hitInfo.collider.GetComponent<ItemPickable>().itemScriptableObject.item_type);
item.PickItem();
}
}
if (Input.GetKeyDown(KeyCode.Alpha1) && inventoryList.Count > 0)
{
selectedItem = 0;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && inventoryList.Count > 0)
{
selectedItem = 1;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && inventoryList.Count > 0)
{
selectedItem = 2;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha4) && inventoryList.Count > 0)
{
selectedItem = 3;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha5) && inventoryList.Count > 0)
{
selectedItem = 4;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha6) && inventoryList.Count > 0)
{
selectedItem = 5;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha7) && inventoryList.Count > 0)
{
selectedItem = 6;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha8) && inventoryList.Count > 0)
{
selectedItem = 7;
NewItemSelected();
}
else if (Input.GetKeyDown(KeyCode.Alpha9) && inventoryList.Count > 0)
{
selectedItem = 8;
NewItemSelected();
}
}
private void NewItemSelected()
{
Wooden_Sword_item.SetActive(false);
Iron_Sword_item.SetActive(false);
GameObject selectedItemGameobject = itemSetActive[inventoryList[selectedItem]];
selectedItemGameobject.SetActive(true);
}
}
public interface IPickable
{
void PickItem();
}