67 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using UnityEngine;
2023-06-19 01:51:08 +02:00
/**
* Einfaches Inventory Script v1.1
* Ein Item muss den Tag 'Item' haben um aufgehoben zu werden
* Schlüssel müssen 'key' irgendwo im Namen haben (case insensitive)
2023-06-19 01:51:08 +02:00
* Türen welche mit Schlüssel geöffnet werden sollen brauchen den Tag 'Openable'
*/
public class InventoryBasic : MonoBehaviour
{
2023-06-12 16:48:23 +02:00
private LinkedList<GameObject> items;
2023-06-24 13:49:28 +02:00
private AudioSource audioSource; // Reference to the AudioSource component
public AudioClip keyTakeSound; // The sound clip to be played
public AudioClip doorOpenSound; // The sound clip to be played
2023-06-19 01:51:08 +02:00
// Start is called before the first frame update
void Start()
{
2023-06-12 16:48:23 +02:00
items = new LinkedList<GameObject>();
2023-06-24 13:49:28 +02:00
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D other)
{
switch (other.gameObject.tag) // switch-case hier damit in zukunft einfach fälle hinzugefügt werden können
{
case "Item":
other.gameObject.SetActive(false);
2023-06-12 16:48:23 +02:00
items.AddFirst(other.gameObject);
2023-06-24 13:49:28 +02:00
audioSource.PlayOneShot(keyTakeSound);
Debug.Log("Pickup: Take " + other.gameObject.name);
break;
case "Openable":
if (removeFirstOccurence("KEY"))
{
2023-06-24 13:49:28 +02:00
audioSource.PlayOneShot(doorOpenSound);
other.gameObject.SetActive(false);
Debug.Log("Pickup: Use Key");
}
break;
}
}
bool removeFirstOccurence(String seq)
{
foreach (var x in items)
{
if (x.name.IndexOf(seq, StringComparison.OrdinalIgnoreCase) > -1)
{
items.Remove(x);
return true;
}
}
return false;
}
}