This repository has been archived on 2023-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
station_obscurum_unity/Assets/Scripts/Item/SpecialItemCycler.cs

128 lines
4.1 KiB
C#
Raw Normal View History

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