using System; using System.Collections.Generic; using UnityEngine; /** * 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) * Türen welche mit Schlüssel geöffnet werden sollen brauchen den Tag 'Openable' */ public class InventoryBasic : MonoBehaviour { private LinkedList items; 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 // Start is called before the first frame update void Start() { items = new LinkedList(); audioSource = GetComponent(); } // 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); items.AddFirst(other.gameObject); audioSource.PlayOneShot(keyTakeSound); Debug.Log("Pickup: Take " + other.gameObject.name); break; case "Openable": if (removeFirstOccurence("KEY")) { 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; } }