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

116 lines
4.2 KiB
C#
Raw Normal View History

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-02 06:30:58 +02:00
namespace Item
2023-04-18 04:29:21 +02:00
{
2023-06-02 06:30:58 +02:00
[Serializable]
public class ItemImageRef
{
public string name;
public GameObject item;
public Sprite icon;
}
2023-04-18 04:29:21 +02:00
2023-06-02 06:30:58 +02:00
public class SpecialItemCycler : MonoBehaviour
{
[SerializeField] private List<ItemImageRef> spawnableItems = new();
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private List<ItemImageRef> shootableItems = new();
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Player.PlayerInteractionHandler interactionHandler;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Item.PistolComponent pistol;
2023-04-18 04:29:21 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField]
//private Inventory invent;
private TempInventory invent;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private Image selectedImage;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
[SerializeField] private TMP_Text selectedQuantityText;
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +02:00
private int shootableIndex;
private int spawnableIndex;
private Color sqtInitColor;
2023-04-18 04:29:21 +02:00
2023-04-21 09:30:43 +02:00
2023-06-02 06:30:58 +02:00
// Start is called before the first frame update
private void Start()
{
sqtInitColor = selectedQuantityText.color;
}
2023-04-18 04:29:21 +02:00
2023-06-02 06:30:58 +02:00
// Update is called once per frame
private void Update()
2023-04-18 04:29:21 +02:00
{
2023-06-02 06:30:58 +02:00
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);
}
2023-04-18 04:29:21 +02:00
}
2023-06-01 17:03:48 +02:00
2023-06-02 06:30:58 +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;
2023-06-02 06:30:58 +02:00
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;
2023-06-02 06:30:58 +02:00
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)
2023-06-02 06:30:58 +02:00
{
2023-04-18 04:29:21 +02:00
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 0);
2023-06-02 06:30:58 +02:00
selectedQuantityText.gameObject.SetActive(false);
}
2023-04-18 04:29:21 +02:00
else
2023-06-02 06:30:58 +02:00
{
2023-04-18 04:29:21 +02:00
selectedImage.color = new Color(selectedImage.color.r, selectedImage.color.g, selectedImage.color.b, 1);
2023-06-02 06:30:58 +02:00
selectedQuantityText.gameObject.SetActive(true);
if (selectedQuantityText.text == "0")
selectedQuantityText.color = Color.red;
else
selectedQuantityText.color = sqtInitColor;
}
2023-04-18 04:29:21 +02:00
}
}
2023-06-01 17:03:48 +02:00
}