2023-06-01 17:03:48 +02:00
|
|
|
using System;
|
2023-04-18 04:29:21 +02:00
|
|
|
using System.Collections.Generic;
|
2023-06-01 17:03:48 +02:00
|
|
|
using TMPro;
|
2023-04-18 04:29:21 +02:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
[Serializable]
|
2023-04-18 04:29:21 +02:00
|
|
|
public class ItemImageRef
|
|
|
|
{
|
|
|
|
public string name;
|
|
|
|
public GameObject item;
|
|
|
|
public Sprite icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SpecialItemCycler : MonoBehaviour
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
[SerializeField] private List<ItemImageRef> spawnableItems = new();
|
|
|
|
|
|
|
|
[SerializeField] private List<ItemImageRef> shootableItems = new();
|
|
|
|
|
|
|
|
[SerializeField] private PlayerInteractionHandler interactionHandler;
|
|
|
|
|
|
|
|
[SerializeField] private PistolComponent pistol;
|
2023-04-18 04:29:21 +02:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
//private Inventory invent;
|
|
|
|
private TempInventory invent;
|
2023-06-01 17:03:48 +02:00
|
|
|
|
|
|
|
[SerializeField] private Image selectedImage;
|
|
|
|
|
|
|
|
[SerializeField] private TMP_Text selectedQuantityText;
|
|
|
|
|
|
|
|
private int shootableIndex;
|
|
|
|
private int spawnableIndex;
|
2023-04-18 04:29:21 +02:00
|
|
|
private Color sqtInitColor;
|
|
|
|
|
2023-04-21 09:30:43 +02:00
|
|
|
|
2023-04-18 04:29:21 +02:00
|
|
|
// Start is called before the first frame update
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Start()
|
2023-04-18 04:29:21 +02:00
|
|
|
{
|
2023-04-21 09:30:43 +02:00
|
|
|
sqtInitColor = selectedQuantityText.color;
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
2023-06-01 17:03:48 +02:00
|
|
|
private void Update()
|
2023-04-18 04:29:21 +02:00
|
|
|
{
|
|
|
|
if (interactionHandler.GunEnabled)
|
|
|
|
{
|
|
|
|
pistol.projectilePrefab = shootableItems[shootableIndex % shootableItems.Count].item;
|
|
|
|
pistol.projectileName = shootableItems[shootableIndex % shootableItems.Count].name;
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-18 04:29:21 +02:00
|
|
|
SetImage();
|
|
|
|
if (Input.GetButtonDown("CycleItems"))
|
|
|
|
{
|
|
|
|
if (interactionHandler.GunEnabled)
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
shootableIndex = (shootableIndex + 1) % shootableItems.Count;
|
|
|
|
selectedImage.sprite = shootableItems[shootableIndex].icon;
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
spawnableIndex = (spawnableIndex + 1) % spawnableItems.Count;
|
|
|
|
selectedImage.sprite = spawnableItems[spawnableIndex].icon;
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-18 04:29:21 +02:00
|
|
|
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);
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-18 04:29:21 +02:00
|
|
|
if (Input.GetButtonDown("TempPlace"))
|
2023-06-01 17:03:48 +02:00
|
|
|
if (invent.GetQuantityOf(spawnableItems[spawnableIndex].name) > 0)
|
|
|
|
if (!interactionHandler.GunEnabled && spawnableItems[spawnableIndex].item != null)
|
2023-04-18 04:29:21 +02:00
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
var prefab = spawnableItems[spawnableIndex].item;
|
|
|
|
var instance = Instantiate(prefab, interactionHandler.CarryingPos);
|
2023-04-18 04:29:21 +02:00
|
|
|
instance.transform.localPosition = Vector3.zero;
|
|
|
|
instance.transform.parent = null;
|
|
|
|
invent.Remove(spawnableItems[spawnableIndex].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:03:48 +02:00
|
|
|
private void SetImage()
|
2023-04-18 04:29:21 +02:00
|
|
|
{
|
|
|
|
if (interactionHandler.GunEnabled)
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
selectedImage.sprite = shootableItems[shootableIndex].icon;
|
|
|
|
selectedQuantityText.text = invent.GetQuantityOf(shootableItems[shootableIndex].name).ToString();
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-01 17:03:48 +02:00
|
|
|
selectedImage.sprite = spawnableItems[spawnableIndex].icon;
|
|
|
|
selectedQuantityText.text = invent.GetQuantityOf(spawnableItems[spawnableIndex].name).ToString();
|
2023-04-18 04:29:21 +02:00
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
|
2023-04-18 04:29:21 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 17:03:48 +02:00
|
|
|
}
|