104 lines
4.6 KiB
C#
104 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using HutongGames.PlayMaker;
|
|
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.
|
|
/// Werkzeuge wie der Ratschenkasten sind im Spiel keine Gegenstände, sondern ein Schalter ("hat der Spieler") -
|
|
/// die werden dem Admin direkt gegeben.
|
|
/// </summary>
|
|
public static class ItemSpawner
|
|
{
|
|
/// <summary>Werkzeug statt Gegenstand: globale Spielvariable, die "gekauft" bedeutet.</summary>
|
|
public const string RatchetSet = "ratchet set";
|
|
|
|
/// <summary>Auswahl im Admin-Menü (Anzeigename).</summary>
|
|
public static readonly string[] Items =
|
|
{
|
|
// Werkzeug / Auto
|
|
RatchetSet, "spanner set", "battery", "car jack", "floor jack", "motor hoist", "flashlight", "lantern",
|
|
"fire extinguisher", "spray can", "sledgehammer", "ax", "digging bar",
|
|
// Flüssigkeiten / Kraftstoff
|
|
"motor oil", "coolant", "brake fluid", "two stroke fuel", "gasoline", "diesel", "n2o bottle",
|
|
// Essen / Trinken / Laden
|
|
"beer case", "beer", "booze", "vodka shot", "milk", "juice", "coffee", "ground coffee", "sugar", "yeast",
|
|
"sausages", "pizza", "macaron box", "potato chips", "cigarettes", "mosquito spray", "grill charcoal",
|
|
// Sonstiges
|
|
"grill", "radio", "camera", "helmet", "bucket", "water bucket", "fireworks bag", "parts magazine"
|
|
};
|
|
|
|
// Items, die im Spiel nicht "(itemx)" heißen
|
|
static readonly Dictionary<string, string> objectNames = new Dictionary<string, string>
|
|
{
|
|
{ "battery", "battery(Clone)" }
|
|
};
|
|
|
|
static readonly Dictionary<string, GameObject> templates = new Dictionary<string, GameObject>();
|
|
|
|
public static bool IsKnown(string item) { return System.Array.IndexOf(Items, item) >= 0; }
|
|
|
|
static string ObjectName(string item)
|
|
{
|
|
string name;
|
|
return objectNames.TryGetValue(item, out name) ? name : item + "(itemx)";
|
|
}
|
|
|
|
static GameObject FindTemplate(string item)
|
|
{
|
|
GameObject cached;
|
|
if (templates.TryGetValue(item, out cached) && cached != null) return cached;
|
|
|
|
string name = ObjectName(item);
|
|
GameObject best = null;
|
|
foreach (Object o in Resources.FindObjectsOfTypeAll(typeof(GameObject)))
|
|
{
|
|
var go = (GameObject)o;
|
|
if (go.name != name || go.GetComponent<Rigidbody>() == null) continue;
|
|
// Keine eingebauten Teile kopieren (z.B. die Batterie im Auto): nicht unter einem anderen Rigidbody
|
|
if (go.transform.parent != null && go.transform.parent.GetComponentInParent<Rigidbody>() != null) continue;
|
|
if (best == null || (go.activeInHierarchy && !best.activeInHierarchy)) best = go; // am liebsten eins aus 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;
|
|
}
|
|
|
|
/// <summary>Ratschenkasten "kaufen": der Spieler hat ihn dann wie nach dem Kauf im Laden.</summary>
|
|
public static bool GiveRatchetSet()
|
|
{
|
|
FsmBool has = FsmVariables.GlobalVariables.FindFsmBool("PlayerHasRatchet");
|
|
if (has == null) return false;
|
|
has.Value = true;
|
|
return true;
|
|
}
|
|
|
|
public static void ClearCache() { templates.Clear(); }
|
|
}
|
|
}
|