pass slot before setting it to null

This commit is contained in:
JohnCorby 2021-12-15 00:10:35 -08:00
parent 1284ce3012
commit 2f648733c2
4 changed files with 19 additions and 15 deletions

View File

@ -9,22 +9,23 @@ namespace QSB.OrbSync.Events
{ {
public override bool RequireWorldObjectsReady => true; public override bool RequireWorldObjectsReady => true;
public override void SetupListener() => GlobalMessenger<QSBOrb, NomaiInterfaceSlot, bool>.AddListener(EventNames.QSBOrbSlot, Handler); public override void SetupListener() => GlobalMessenger<QSBOrb, NomaiInterfaceSlot, bool, bool>.AddListener(EventNames.QSBOrbSlot, Handler);
public override void CloseListener() => GlobalMessenger<QSBOrb, NomaiInterfaceSlot, bool>.RemoveListener(EventNames.QSBOrbSlot, Handler); public override void CloseListener() => GlobalMessenger<QSBOrb, NomaiInterfaceSlot, bool, bool>.RemoveListener(EventNames.QSBOrbSlot, Handler);
private void Handler(QSBOrb qsbOrb, NomaiInterfaceSlot slot, bool playAudio) => SendEvent(CreateMessage(qsbOrb, slot, playAudio)); private void Handler(QSBOrb qsbOrb, NomaiInterfaceSlot slot, bool slotted, bool playAudio) => SendEvent(CreateMessage(qsbOrb, slot, slotted, playAudio));
private OrbSlotMessage CreateMessage(QSBOrb qsbOrb, NomaiInterfaceSlot slot, bool playAudio) => new() private OrbSlotMessage CreateMessage(QSBOrb qsbOrb, NomaiInterfaceSlot slot, bool slotted, bool playAudio) => new()
{ {
ObjectId = qsbOrb.ObjectId, ObjectId = qsbOrb.ObjectId,
SlotIndex = qsbOrb.AttachedObject._slots.IndexOf(slot), SlotIndex = qsbOrb.AttachedObject._slots.IndexOf(slot),
Slotted = slotted,
PlayAudio = playAudio PlayAudio = playAudio
}; };
public override void OnReceiveRemote(bool server, OrbSlotMessage message) public override void OnReceiveRemote(bool server, OrbSlotMessage message)
{ {
var qsbOrb = QSBWorldSync.GetWorldFromId<QSBOrb>(message.ObjectId); var qsbOrb = QSBWorldSync.GetWorldFromId<QSBOrb>(message.ObjectId);
qsbOrb.SetOccupiedSlot(message.SlotIndex == -1 ? null : qsbOrb.AttachedObject._slots[message.SlotIndex], message.PlayAudio); qsbOrb.SetOccupiedSlot(qsbOrb.AttachedObject._slots[message.SlotIndex], message.Slotted, message.PlayAudio);
} }
} }
} }

View File

@ -6,12 +6,14 @@ namespace QSB.OrbSync.Events
public class OrbSlotMessage : WorldObjectMessage public class OrbSlotMessage : WorldObjectMessage
{ {
public int SlotIndex; public int SlotIndex;
public bool Slotted;
public bool PlayAudio; public bool PlayAudio;
public override void Deserialize(QNetworkReader reader) public override void Deserialize(QNetworkReader reader)
{ {
base.Deserialize(reader); base.Deserialize(reader);
SlotIndex = reader.ReadInt32(); SlotIndex = reader.ReadInt32();
Slotted = reader.ReadBoolean();
PlayAudio = reader.ReadBoolean(); PlayAudio = reader.ReadBoolean();
} }
@ -19,6 +21,7 @@ namespace QSB.OrbSync.Events
{ {
base.Serialize(writer); base.Serialize(writer);
writer.Write(SlotIndex); writer.Write(SlotIndex);
writer.Write(Slotted);
writer.Write(PlayAudio); writer.Write(PlayAudio);
} }
} }

View File

@ -81,15 +81,15 @@ namespace QSB.OrbSync.Patches
{ {
__instance._orbAudio.PlaySlotActivatedClip(); __instance._orbAudio.PlaySlotActivatedClip();
} }
QSBEventManager.FireEvent(EventNames.QSBOrbSlot, qsbOrb, __instance._occupiedSlot, playAudio); QSBEventManager.FireEvent(EventNames.QSBOrbSlot, qsbOrb, __instance._occupiedSlot, true, playAudio);
break; break;
} }
} }
} }
else if ((!__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged) && !__instance._occupiedSlot.CheckOrbCollision(__instance)) else if ((!__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged) && !__instance._occupiedSlot.CheckOrbCollision(__instance))
{ {
QSBEventManager.FireEvent(EventNames.QSBOrbSlot, qsbOrb, __instance._occupiedSlot, false, playAudio);
__instance._occupiedSlot = null; __instance._occupiedSlot = null;
QSBEventManager.FireEvent(EventNames.QSBOrbSlot, qsbOrb, __instance._occupiedSlot, playAudio);
} }
__instance._owCollider.SetActivation(__instance._occupiedSlot == null || !__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged); __instance._owCollider.SetActivation(__instance._occupiedSlot == null || !__instance._occupiedSlot.IsAttractive() || __instance._isBeingDragged);

View File

@ -58,31 +58,31 @@ namespace QSB.OrbSync.WorldObjects
public NomaiInterfaceSlot OccupiedSlot => AttachedObject._occupiedSlot; public NomaiInterfaceSlot OccupiedSlot => AttachedObject._occupiedSlot;
public void SetOccupiedSlot(NomaiInterfaceSlot slot, bool playAudio) public void SetOccupiedSlot(NomaiInterfaceSlot slot, bool slotted, bool playAudio)
{ {
if (slot == OccupiedSlot) if (slotted == OccupiedSlot)
{ {
return; return;
} }
if (slot) if (slotted)
{ {
slot._occupyingOrb = AttachedObject;
slot.RaiseEvent(nameof(slot.OnSlotActivated), slot);
AttachedObject._occupiedSlot = slot; AttachedObject._occupiedSlot = slot;
AttachedObject._enterSlotTime = Time.time; AttachedObject._enterSlotTime = Time.time;
if (playAudio && AttachedObject._orbAudio != null && slot.GetPlayActivationAudio()) if (playAudio && AttachedObject._orbAudio != null && slot.GetPlayActivationAudio())
{ {
AttachedObject._orbAudio.PlaySlotActivatedClip(); AttachedObject._orbAudio.PlaySlotActivatedClip();
} }
slot._occupyingOrb = AttachedObject;
slot.RaiseEvent(nameof(slot.OnSlotActivated), slot);
} }
else else
{ {
AttachedObject._occupiedSlot = null;
slot._occupyingOrb = null; slot._occupyingOrb = null;
slot.RaiseEvent(nameof(slot.OnSlotDeactivated), slot); slot.RaiseEvent(nameof(slot.OnSlotDeactivated), slot);
AttachedObject._occupiedSlot = null;
} }
} }
} }