quantum-space-buddies/QSB/QuantumSync/Patches/ServerQuantumStateChangePatches.cs

79 lines
2.6 KiB
C#
Raw Normal View History

2020-12-22 21:39:53 +00:00
using QSB.Events;
using QSB.Patches;
using QSB.QuantumSync.WorldObjects;
2020-12-24 10:06:17 +00:00
using QSB.WorldSync;
using System;
2021-01-01 10:17:35 +00:00
using System.Collections.Generic;
2020-12-24 10:06:17 +00:00
using System.Linq;
2020-12-22 21:39:53 +00:00
using UnityEngine;
namespace QSB.QuantumSync.Patches
2020-12-22 21:39:53 +00:00
{
public class ServerQuantumStateChangePatches : QSBPatch
{
public override QSBPatchTypes Type => QSBPatchTypes.OnServerClientConnect;
2020-12-24 10:06:17 +00:00
public override void DoPatches()
{
QSBCore.Helper.HarmonyHelper.AddPostfix<SocketedQuantumObject>("MoveToSocket", typeof(ServerQuantumStateChangePatches), nameof(Socketed_MoveToSocket));
QSBCore.Helper.HarmonyHelper.AddPostfix<QuantumState>("SetVisible", typeof(ServerQuantumStateChangePatches), nameof(QuantumState_SetVisible));
2021-01-01 10:17:35 +00:00
QSBCore.Helper.HarmonyHelper.AddPrefix<QuantumShuffleObject>("ChangeQuantumState", typeof(ServerQuantumStateChangePatches), nameof(Shuffle_ChangeQuantumState));
2020-12-24 10:06:17 +00:00
}
2020-12-22 21:39:53 +00:00
public static void Socketed_MoveToSocket(SocketedQuantumObject __instance, QuantumSocket socket)
{
var objId = QuantumManager.Instance.GetId(__instance);
var socketId = QuantumManager.Instance.GetId(socket);
2020-12-24 10:06:17 +00:00
GlobalMessenger<int, int, Quaternion>
2021-01-01 10:17:35 +00:00
.FireEvent(
EventNames.QSBSocketStateChange,
2020-12-24 10:06:17 +00:00
objId,
socketId,
__instance.transform.localRotation);
}
public static void QuantumState_SetVisible(QuantumState __instance, bool visible)
{
if (!visible)
{
return;
}
2020-12-24 10:06:17 +00:00
var allMultiStates = QSBWorldSync.GetWorldObjects<QSBMultiStateQuantumObject>();
var owner = allMultiStates.First(x => x.QuantumStates.Contains(__instance));
GlobalMessenger<int, int>
2021-01-01 10:17:35 +00:00
.FireEvent(
EventNames.QSBMultiStateChange,
2020-12-24 10:06:17 +00:00
QuantumManager.Instance.GetId(owner.AttachedObject),
Array.IndexOf(owner.QuantumStates, __instance));
2020-12-22 21:39:53 +00:00
}
2021-01-01 10:17:35 +00:00
public static bool Shuffle_ChangeQuantumState(
QuantumShuffleObject __instance,
ref List<int> ____indexList,
ref Vector3[] ____localPositions,
ref Transform[] ____shuffledObjects,
ref bool __result)
{
____indexList.Clear();
____indexList = Enumerable.Range(0, ____localPositions.Length).ToList();
for (var i = 0; i < ____indexList.Count; ++i)
{
var random = UnityEngine.Random.Range(i, ____indexList.Count);
var temp = ____indexList[i];
____indexList[i] = ____indexList[random];
____indexList[random] = temp;
}
for (var j = 0; j < ____shuffledObjects.Length; j++)
{
____shuffledObjects[j].localPosition = ____localPositions[____indexList[j]];
}
GlobalMessenger<int, int[]>
.FireEvent(
EventNames.QSBQuantumShuffle,
QuantumManager.Instance.GetId(__instance),
____indexList.ToArray());
__result = true;
return false;
}
2020-12-22 21:39:53 +00:00
}
}