110 lines
2.5 KiB
C#
Raw Normal View History

2022-01-28 20:49:07 -08:00
using Cysharp.Threading.Tasks;
using Mirror;
2022-01-21 15:13:16 -08:00
using QSB.Messaging;
using QSB.OrbSync.Messages;
using QSB.OrbSync.TransformSync;
2021-12-14 20:24:02 -08:00
using QSB.Utility;
using QSB.Utility.LinkedWorldObject;
2021-12-14 20:24:02 -08:00
using QSB.WorldSync;
2022-03-10 21:23:06 -08:00
using System;
2022-01-28 20:49:07 -08:00
using System.Threading;
2021-12-14 20:24:02 -08:00
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.OrbSync.WorldObjects;
public class QSBOrb : WorldObject<NomaiInterfaceOrb>, ILinkedWorldObject<NomaiOrbTransformSync>
2021-12-14 20:24:02 -08:00
{
2022-03-02 19:46:33 -08:00
public override bool ShouldDisplayDebug() => false;
public NomaiOrbTransformSync NetworkBehaviour { get; private set; }
public void LinkTo(NetworkBehaviour networkBehaviour) => NetworkBehaviour = (NomaiOrbTransformSync)networkBehaviour;
2022-03-02 19:46:33 -08:00
public override async UniTask Init(CancellationToken ct)
2021-12-14 20:24:02 -08:00
{
2022-03-02 19:46:33 -08:00
if (QSBCore.IsHost)
{
this.SpawnLinked(QSBNetworkManager.singleton.OrbPrefab);
}
else
{
await this.WaitForLink(ct);
2022-03-02 19:46:33 -08:00
}
}
2021-12-14 20:24:02 -08:00
2022-03-02 19:46:33 -08:00
public override void OnRemoval()
{
if (QSBCore.IsHost)
2021-12-14 20:24:02 -08:00
{
NetworkServer.Destroy(NetworkBehaviour.gameObject);
2022-03-02 19:46:33 -08:00
}
}
2022-03-02 19:46:33 -08:00
public override void SendInitialState(uint to)
{
this.SendMessage(new OrbDragMessage(AttachedObject._isBeingDragged) { To = to });
2022-03-10 21:23:06 -08:00
var slotIndex = Array.IndexOf(AttachedObject._slots, AttachedObject._occupiedSlot);
2022-03-02 19:46:33 -08:00
this.SendMessage(new OrbSlotMessage(slotIndex, false) { To = to });
}
public void SetDragging(bool value)
{
if (value == AttachedObject._isBeingDragged)
{
return;
2021-12-14 20:24:02 -08:00
}
2022-03-02 19:46:33 -08:00
if (value)
2021-12-14 20:24:02 -08:00
{
2022-03-02 19:46:33 -08:00
AttachedObject._isBeingDragged = true;
AttachedObject._interactibleCollider.enabled = false;
if (AttachedObject._orbAudio != null)
2021-12-14 20:24:02 -08:00
{
2022-03-02 19:46:33 -08:00
AttachedObject._orbAudio.PlayStartDragClip();
2021-12-14 20:24:02 -08:00
}
}
2022-03-02 19:46:33 -08:00
else
{
AttachedObject._isBeingDragged = false;
AttachedObject._interactibleCollider.enabled = true;
}
}
2021-12-14 20:24:02 -08:00
2022-03-02 19:46:33 -08:00
public void SetSlot(int slotIndex, bool playAudio)
{
var oldSlot = AttachedObject._occupiedSlot;
var newSlot = slotIndex == -1 ? null : AttachedObject._slots[slotIndex];
if (newSlot == oldSlot)
2022-01-21 15:13:16 -08:00
{
2022-03-02 19:46:33 -08:00
return;
2022-01-21 15:13:16 -08:00
}
2022-03-02 19:46:33 -08:00
if (oldSlot)
2021-12-14 20:24:02 -08:00
{
2022-03-02 19:46:33 -08:00
oldSlot._occupyingOrb = null;
oldSlot.RaiseEvent(nameof(oldSlot.OnSlotDeactivated), oldSlot);
2022-03-02 19:46:33 -08:00
AttachedObject._occupiedSlot = null;
}
2022-03-02 19:46:33 -08:00
if (newSlot)
{
2022-03-02 19:46:33 -08:00
newSlot._occupyingOrb = AttachedObject;
if (Time.timeSinceLevelLoad > 1f)
{
2022-03-02 19:46:33 -08:00
newSlot.RaiseEvent(nameof(newSlot.OnSlotActivated), newSlot);
}
2022-03-02 19:46:33 -08:00
AttachedObject._occupiedSlot = newSlot;
AttachedObject._enterSlotTime = Time.time;
if (newSlot.CancelsDragOnCollision())
{
2022-03-02 19:46:33 -08:00
AttachedObject.CancelDrag();
2021-12-17 16:53:23 -08:00
}
2022-03-02 19:46:33 -08:00
if (playAudio && AttachedObject._orbAudio != null && newSlot.GetPlayActivationAudio())
2021-12-17 16:53:23 -08:00
{
2022-03-02 19:46:33 -08:00
AttachedObject._orbAudio.PlaySlotActivatedClip();
2021-12-14 20:24:02 -08:00
}
}
}
}