quantum-space-buddies/QSB/WorldSync/QSBWorldSync.cs

84 lines
2.3 KiB
C#
Raw Normal View History

2020-09-29 20:34:46 +00:00
using OWML.Common;
using QSB.OrbSync;
2020-09-06 08:07:31 +00:00
using QSB.TransformSync;
2020-09-29 20:34:46 +00:00
using QSB.Utility;
2020-09-06 08:07:31 +00:00
using System;
2020-09-04 19:57:35 +00:00
using System.Collections.Generic;
2020-08-13 17:25:12 +00:00
using System.Linq;
2020-09-06 08:07:31 +00:00
using System.Reflection;
2020-08-13 17:25:12 +00:00
namespace QSB.WorldSync
{
2020-12-11 13:14:58 +00:00
public static class QSBWorldSync
2020-12-02 21:29:53 +00:00
{
private static readonly List<WorldObject> WorldObjects = new List<WorldObject>();
public static List<NomaiOrbTransformSync> OrbSyncList = new List<NomaiOrbTransformSync>();
public static List<NomaiInterfaceOrb> OldOrbList = new List<NomaiInterfaceOrb>();
public static List<CharacterDialogueTree> OldDialogueTrees = new List<CharacterDialogueTree>();
2020-12-11 13:14:58 +00:00
public static Dictionary<string, bool> DialogueConditions { get; } = new Dictionary<string, bool>();
2020-09-22 20:11:29 +00:00
2020-12-11 13:14:58 +00:00
public static void AddWorldObject(WorldObject worldObject)
2020-12-02 21:29:53 +00:00
{
if (WorldObjects.Contains(worldObject))
{
return;
}
WorldObjects.Add(worldObject);
}
2020-12-11 13:14:58 +00:00
public static IEnumerable<T> GetWorldObjects<T>()
2020-12-02 21:29:53 +00:00
{
return WorldObjects.OfType<T>();
}
2020-08-13 17:25:12 +00:00
2020-12-11 13:14:58 +00:00
public static T GetWorldObject<T>(int id) where T : WorldObject
2020-12-02 21:29:53 +00:00
{
2020-12-11 13:14:58 +00:00
return GetWorldObjects<T>().FirstOrDefault(x => x.ObjectId == id);
2020-12-02 21:29:53 +00:00
}
2020-09-06 08:07:31 +00:00
2020-12-11 13:14:58 +00:00
public static void RemoveWorldObjects<T>() where T : WorldObject
2020-12-02 21:29:53 +00:00
{
WorldObjects.RemoveAll(x => x.GetType() == typeof(T));
}
2020-09-17 19:14:55 +00:00
2020-12-02 21:29:53 +00:00
public static void HandleSlotStateChange(NomaiInterfaceSlot slot, NomaiInterfaceOrb affectingOrb, bool state)
{
2020-12-11 13:14:58 +00:00
var slotList = GetWorldObjects<QSBOrbSlot>();
2020-12-02 21:29:53 +00:00
if (slotList.Count() == 0)
{
return;
}
2020-12-11 22:42:21 +00:00
var qsbSlot = slotList.First(x => x.InterfaceSlot == slot);
var orbSync = OrbSyncList.First(x => x.AttachedOrb == affectingOrb);
if (orbSync.HasAuthority)
2020-12-02 21:29:53 +00:00
{
2020-12-11 22:42:21 +00:00
qsbSlot.HandleEvent(state, OldOrbList.IndexOf(affectingOrb));
2020-12-02 21:29:53 +00:00
}
}
2020-09-06 08:07:31 +00:00
2020-12-02 21:29:53 +00:00
public static void RaiseEvent(object instance, string eventName)
{
if (!(instance.GetType()
.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(instance) is MulticastDelegate multiDelegate))
{
return;
}
var delegateList = multiDelegate.GetInvocationList().ToList();
2020-12-06 17:00:23 +00:00
foreach (var del in delegateList)
{
del.DynamicInvoke(instance);
}
2020-12-02 21:29:53 +00:00
}
2020-12-11 13:14:58 +00:00
2020-12-11 22:42:21 +00:00
public static void SetDialogueCondition(string name, bool state)
2020-12-11 13:14:58 +00:00
{
if (!QSB.IsServer)
{
DebugLog.DebugWrite("Warning - Cannot write to condition dict when not server!", MessageType.Warning);
return;
}
DialogueConditions[name] = state;
}
2020-12-02 21:29:53 +00:00
}
2020-12-03 08:28:05 +00:00
}