StasisPodScript.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 
  2. using GadgetCore.API;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. namespace StasisPod
  8. {
  9. public class StasisPodScript : MonoBehaviour
  10. {
  11. public static List<StasisPodScript> StasisPods = new List<StasisPodScript>();
  12. public bool inUse = false;
  13. public string id;
  14. private void Start()
  15. {
  16. id = "x" + (Math.Floor(transform.position.x) / 4).ToString() + "y" + (Math.Floor(transform.position.y) / 4).ToString();
  17. StasisPods.Add(this);
  18. }
  19. private void OnDestroy()
  20. {
  21. StasisPods.Remove(this);
  22. }
  23. internal void UpdateInUse(bool inUse) => StartCoroutine(UpdateInUseInternal(inUse));
  24. private IEnumerator UpdateInUseInternal(bool inUse)
  25. {
  26. yield return new WaitForSeconds(0.01f);
  27. GadgetCoreAPI.CallCustomRPC(StasisPod.RPCSetPodInUse, RPCMode.AllBuffered, new object[] { this.id, inUse });
  28. yield break;
  29. }
  30. internal static void CallEventSetRPC(object[] args)
  31. {
  32. foreach (var pod in StasisPods)
  33. {
  34. pod.UpdateInUseFromRPC(args);
  35. }
  36. }
  37. private void UpdateInUseFromRPC(object[] o)
  38. {
  39. if (o[0].ToString() == id)
  40. {
  41. inUse = (bool)o[1];
  42. UpdateTile();
  43. }
  44. }
  45. private void UpdateTile() => gameObject.transform.GetChild(1).gameObject.SetActive(inUse);
  46. internal bool CanUse() => !inUse;
  47. }
  48. }