quantum-space-buddies/QSB/QuantumSync/WorldObjects/QSBMultiStateQuantumObject.cs

66 lines
1.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2021-03-18 13:35:11 +00:00
using System.Linq;
using QSB.Utility;
using QSB.WorldSync;
2020-12-24 15:57:25 +00:00
using UnityEngine.UI;
2020-12-23 22:43:05 +00:00
namespace QSB.QuantumSync.WorldObjects
2020-12-23 11:26:47 +00:00
{
2021-01-26 14:07:17 +00:00
internal class QSBMultiStateQuantumObject : QSBQuantumObject<MultiStateQuantumObject>
2020-12-23 11:26:47 +00:00
{
2021-03-18 13:35:11 +00:00
public List<QSBQuantumState> QuantumStates { get; private set; }
2020-12-24 15:57:25 +00:00
public Text DebugBoxText;
public int CurrentState => AttachedObject._stateIndex;
2020-12-24 10:06:17 +00:00
public override void OnRemoval()
{
base.OnRemoval();
if (DebugBoxText != null)
{
UnityEngine.Object.Destroy(DebugBoxText.gameObject);
}
}
2020-12-23 22:43:05 +00:00
public override void Init(MultiStateQuantumObject attachedObject, int id)
{
ObjectId = id;
AttachedObject = attachedObject;
2021-11-25 22:44:15 +00:00
if (QSBCore.ShowQuantumDebugBoxes)
2020-12-24 15:57:25 +00:00
{
DebugBoxText = DebugBoxManager.CreateBox(AttachedObject.transform, 0, $"Multistate\r\nid:{id}\r\nstate:{CurrentState}").GetComponent<Text>();
2020-12-24 15:57:25 +00:00
}
2021-06-18 22:38:32 +01:00
2021-01-26 17:08:02 +00:00
base.Init(attachedObject, id);
StartDelayedReady();
QSBCore.UnityEvents.RunWhen(() => WorldObjectManager.AllAdded, () =>
{
FinishDelayedReady();
QuantumStates = AttachedObject._states.Select(QSBWorldSync.GetWorldFromUnity<QSBQuantumState>).ToList();
if (QuantumStates.Any(x => x == null))
{
DebugLog.ToConsole($"Error - {AttachedObject.name} has one or more null QSBQuantumStates assigned!", OWML.Common.MessageType.Error);
}
});
}
2021-03-17 17:04:57 +00:00
public void ChangeState(int newStateIndex)
2021-01-26 14:07:17 +00:00
{
2021-03-17 17:04:57 +00:00
if (CurrentState != -1)
2021-01-26 14:07:17 +00:00
{
2021-03-17 17:04:57 +00:00
QuantumStates[CurrentState].SetVisible(false);
2021-01-26 14:07:17 +00:00
}
2021-06-18 22:38:32 +01:00
2021-03-17 17:04:57 +00:00
QuantumStates[newStateIndex].SetVisible(true);
AttachedObject._stateIndex = newStateIndex;
2021-11-25 22:44:15 +00:00
if (QSBCore.ShowQuantumDebugBoxes)
2021-01-26 14:07:17 +00:00
{
DebugBoxText.text = $"Multistate\r\nid:{ObjectId}\r\nstate:{CurrentState}";
2021-01-26 14:07:17 +00:00
}
2020-12-23 22:43:05 +00:00
}
2020-12-23 11:26:47 +00:00
}
}