66 lines
2.8 KiB
C#
66 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace MSCO
|
|
{
|
|
/// <summary>
|
|
/// Admin: Items spawnen. Kopiert ein vorhandenes Item des Spiels (z.B. "beer case(itemx)" aus dem Laden) und
|
|
/// setzt die Kopie an die gewünschte Stelle. Die Kopie behält den Namen des Originals, damit sich die Spiel-Logik
|
|
/// (Trinken, Essen, Tanken ...) wie beim echten Item verhält und der Objekt-Sync sie bei allen gleich benennt.
|
|
/// </summary>
|
|
public static class ItemSpawner
|
|
{
|
|
/// <summary>Auswahl im Admin-Menü (Name ohne "(itemx)").</summary>
|
|
public static readonly string[] Items =
|
|
{
|
|
"beer case", "sausages", "pizza", "macaron box", "milk", "juice", "ground coffee", "sugar", "yeast", "booze",
|
|
"gasoline", "diesel", "spray can", "fire extinguisher", "flashlight", "lantern", "car jack", "floor jack",
|
|
"spanner set", "sledgehammer", "radio", "camera", "grill charcoal", "fireworks bag", "water bucket"
|
|
};
|
|
|
|
static readonly Dictionary<string, GameObject> templates = new Dictionary<string, GameObject>();
|
|
|
|
static GameObject FindTemplate(string item)
|
|
{
|
|
GameObject cached;
|
|
if (templates.TryGetValue(item, out cached) && cached != null) return cached;
|
|
|
|
string name = item + "(itemx)";
|
|
GameObject best = null;
|
|
foreach (Object o in Resources.FindObjectsOfTypeAll(typeof(GameObject)))
|
|
{
|
|
var go = (GameObject)o;
|
|
if (go.name != name || go.GetComponent<Rigidbody>() == null) continue;
|
|
if (best == null || (go.activeInHierarchy && !best.activeInHierarchy)) best = go; // am liebsten ein echtes in der Welt
|
|
}
|
|
templates[item] = best;
|
|
return best;
|
|
}
|
|
|
|
/// <summary>Item erzeugen. Gibt false zurück, wenn es im Spiel kein solches Item gibt.</summary>
|
|
public static bool Spawn(string item, Vector3 position, Quaternion rotation)
|
|
{
|
|
GameObject template = FindTemplate(item);
|
|
if (template == null)
|
|
{
|
|
MscoLog.Warning("Cannot spawn '" + item + "' - no such item in this game");
|
|
return false;
|
|
}
|
|
var clone = (GameObject)Object.Instantiate(template, position, rotation);
|
|
clone.name = template.name;
|
|
clone.transform.SetParent(null, true);
|
|
clone.SetActive(true);
|
|
Rigidbody rb = clone.GetComponent<Rigidbody>();
|
|
if (rb != null)
|
|
{
|
|
rb.isKinematic = false;
|
|
rb.velocity = Vector3.zero;
|
|
rb.angularVelocity = Vector3.zero;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static void ClearCache() { templates.Clear(); }
|
|
}
|
|
}
|