116 lines
4.2 KiB
C#
116 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Item
|
|
{
|
|
[Serializable]
|
|
public class ItemImageRef
|
|
{
|
|
public string name;
|
|
public GameObject item;
|
|
public Sprite icon;
|
|
}
|
|
|
|
public class SpecialItemCycler : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<ItemImageRef> spawnableItems = new();
|
|
|
|
[SerializeField] private List<ItemImageRef> shootableItems = new();
|
|
|
|
[SerializeField] private Player.PlayerInteractionHandler interactionHandler;
|
|
|
|
[SerializeField] private Item.PistolComponent pistol;
|
|
|
|
[SerializeField]
|
|
//private Inventory invent;
|
|
private TempInventory invent;
|
|
|
|
[SerializeField] private Image selectedImage;
|
|
|
|
[SerializeField] private TMP_Text selectedQuantityText;
|
|
|
|
private int shootableIndex;
|
|
private int spawnableIndex;
|
|
private Color sqtInitColor;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
sqtInitColor = selectedQuantityText.color;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
private void Update()
|
|
{
|
|
if (interactionHandler.GunEnabled)
|
|
{
|
|
pistol.projectilePrefab = shootableItems[shootableIndex % shootableItems.Count].item;
|
|
pistol.projectileName = shootableItems[shootableIndex % shootableItems.Count].name;
|
|
}
|
|
|
|
SetImage();
|
|
if (Input.GetButtonDown("CycleItems"))
|
|
{
|
|
if (interactionHandler.GunEnabled)
|
|
{
|
|
shootableIndex = (shootableIndex + 1) % shootableItems.Count;
|
|
selectedImage.sprite = shootableItems[shootableIndex].icon;
|
|
}
|
|
else
|
|
{
|
|
spawnableIndex = (spawnableIndex + 1) % spawnableItems.Count;
|
|
selectedImage.sprite = spawnableItems[spawnableIndex].icon;
|
|
}
|
|
|
|
if (selectedImage.sprite == null)
|
|
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 0);
|
|
else
|
|
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 1);
|
|
}
|
|
|
|
if (Input.GetButtonDown("TempPlace"))
|
|
if (invent.GetQuantityOf(spawnableItems[spawnableIndex].name) > 0)
|
|
if (!interactionHandler.GunEnabled && spawnableItems[spawnableIndex].item != null)
|
|
{
|
|
var prefab = spawnableItems[spawnableIndex].item;
|
|
var instance = Instantiate(prefab, interactionHandler.CarryingPos);
|
|
instance.transform.localPosition = Vector3.zero;
|
|
instance.transform.parent = null;
|
|
invent.Remove(spawnableItems[spawnableIndex].name);
|
|
}
|
|
}
|
|
|
|
private void SetImage()
|
|
{
|
|
if (interactionHandler.GunEnabled)
|
|
{
|
|
selectedImage.sprite = shootableItems[shootableIndex].icon;
|
|
selectedQuantityText.text = invent.GetQuantityOf(shootableItems[shootableIndex].name).ToString();
|
|
}
|
|
else
|
|
{
|
|
selectedImage.sprite = spawnableItems[spawnableIndex].icon;
|
|
selectedQuantityText.text = invent.GetQuantityOf(spawnableItems[spawnableIndex].name).ToString();
|
|
}
|
|
|
|
if (selectedImage.sprite == null)
|
|
{
|
|
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 0);
|
|
selectedQuantityText.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 1);
|
|
selectedQuantityText.gameObject.SetActive(true);
|
|
if (selectedQuantityText.text == "0")
|
|
selectedQuantityText.color = Color.red;
|
|
else
|
|
selectedQuantityText.color = sqtInitColor;
|
|
}
|
|
}
|
|
}
|
|
} |