StellarXipher/Assets/Scripts/PickUpKeyCard.cs
nlevin6 013f1dcd1d
All checks were successful
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Successful in 5m7s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Successful in 5m15s
Build project / Publish to itch.io (StandaloneLinux64) (push) Successful in 10s
Build project / Publish to itch.io (StandaloneWindows64) (push) Successful in 10s
bug fix: a bunch of shit
2025-04-16 21:45:32 -04:00

82 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using TMPro;
public class PickUpKeyCard : MonoBehaviour
{
[Header("PerObject Settings")]
[SerializeField] private bool isKeyCard = false;
[SerializeField] private string keyCardName = "Deck D key card";
[Header("Refs & Settings")]
public TMP_Text interactionPrompt;
private Outline outline;
[SerializeField] private Animator objectAnimator;
[SerializeField] private string animationName = "PickupAnimation";
[SerializeField] private AudioClip pickupSound;
[SerializeField] private KeyCardPlayer keyCardPlayer;
[SerializeField] private float interactionDistance = 5f;
[SerializeField] private Item keyCardItem;
[SerializeField] private Inventory playerInventory;
private bool isPickedUp;
void Awake()
{
outline = GetComponent<Outline>();
if (outline) outline.enabled = true;
if (interactionPrompt) interactionPrompt.enabled = false;
}
void Update()
{
if (isPickedUp) return;
var ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2));
if (!Physics.Raycast(ray, out var hit, interactionDistance))
{
if (interactionPrompt) interactionPrompt.enabled = false;
return;
}
if (hit.collider.gameObject != gameObject)
{
if (interactionPrompt) interactionPrompt.enabled = false;
return;
}
if (interactionPrompt)
{
interactionPrompt.text = isKeyCard
? $"Left click to pick up {keyCardName}"
: "Left click to pick up item";
interactionPrompt.enabled = true;
}
if (!Input.GetMouseButtonDown(0)) return;
bool hasSpace = false;
foreach (var slot in playerInventory.slots)
if (slot == null) { hasSpace = true; break; }
if (!hasSpace)
{
Debug.Log("Inventory is full!");
if (interactionPrompt) interactionPrompt.text = "Inventory full!";
return;
}
isPickedUp = true;
if (isKeyCard && keyCardPlayer != null)
keyCardPlayer.hasKeyCard = true;
playerInventory.AddItem(keyCardItem);
FindObjectOfType<InventoryUI>()?.RefreshUI();
Destroy(gameObject);
outline.enabled = false;
interactionPrompt.enabled = false;
objectAnimator?.Play(animationName, 0, 0);
if (pickupSound) SoundFXManager.instance.PlaySound(pickupSound, transform, 1.5f);
}
}