2023-06-19 10:42:56 +02:00

61 lines
1.6 KiB
C#

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<GameObject> items;
// Start is called before the first frame update
void Start()
{
items = new LinkedList<GameObject>();
}
// 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);
Debug.Log("Pickup: Take " + other.gameObject.name);
break;
case "Openable":
if (removeFirstOccurence("KEY"))
{
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;
}
}