EnvironmentManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. using Input = InputManager;
  7. public class EnvironmentManager : IManager
  8. {
  9. private static readonly BgMgr bgMgr = GameMain.Instance.BgMgr;
  10. public const string header = "ENVIRONMENT";
  11. public const string defaultBg = "Theater";
  12. private const string myRoomPrefix = "マイルーム:";
  13. private static bool cubeActive;
  14. public static bool CubeActive
  15. {
  16. get => cubeActive;
  17. set
  18. {
  19. if (value == cubeActive) return;
  20. cubeActive = value;
  21. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  22. }
  23. }
  24. private static bool cubeSmall;
  25. public static bool CubeSmall
  26. {
  27. get => cubeSmall;
  28. set
  29. {
  30. if (value == cubeSmall) return;
  31. cubeSmall = value;
  32. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  33. }
  34. }
  35. private static event EventHandler CubeActiveChange;
  36. private static event EventHandler CubeSmallChange;
  37. private Transform bg;
  38. private GameObject bgObject;
  39. private DragPointBG bgDragPoint;
  40. public string CurrentBgAsset { get; private set; } = defaultBg;
  41. private bool bgVisible = true;
  42. public bool BGVisible
  43. {
  44. get => bgVisible;
  45. set
  46. {
  47. bgVisible = value;
  48. bgObject.SetActive(bgVisible);
  49. }
  50. }
  51. public EnvironmentManager()
  52. {
  53. DragPointLight.EnvironmentManager = this;
  54. Activate();
  55. }
  56. public void Update() { }
  57. public void Activate()
  58. {
  59. BgMgrPatcher.ChangeBgBegin += OnChangeBegin;
  60. BgMgrPatcher.ChangeBgEnd += OnChangeEnd;
  61. bgObject = bgMgr.Parent;
  62. bgObject.SetActive(true);
  63. if (MeidoPhotoStudio.EditMode) UpdateBG();
  64. else ChangeBackground(defaultBg);
  65. CubeSmallChange += OnCubeSmall;
  66. CubeActiveChange += OnCubeActive;
  67. }
  68. public void Deactivate()
  69. {
  70. BgMgrPatcher.ChangeBgBegin -= OnChangeBegin;
  71. BgMgrPatcher.ChangeBgEnd -= OnChangeEnd;
  72. DestroyDragPoint();
  73. BGVisible = true;
  74. if (MeidoPhotoStudio.EditMode) bgMgr.ChangeBg(defaultBg);
  75. else
  76. {
  77. var isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
  78. bgMgr.ChangeBg(isNight ? "ShinShitsumu_ChairRot_Night" : "ShinShitsumu_ChairRot");
  79. }
  80. if (bgMgr.BgObject) bgMgr.BgObject.transform.localScale = Vector3.one;
  81. CubeSmallChange -= OnCubeSmall;
  82. CubeActiveChange -= OnCubeActive;
  83. }
  84. public void ChangeBackground(string assetName, bool creative = false)
  85. {
  86. if (creative) bgMgr.ChangeBgMyRoom(assetName);
  87. else bgMgr.ChangeBg(assetName);
  88. }
  89. private void AttachDragPoint(Transform bgTransform)
  90. {
  91. bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
  92. bgDragPoint.Initialize(() => bgTransform.position, () => Vector3.zero);
  93. bgDragPoint.Set(bgTransform);
  94. bgDragPoint.AddGizmo();
  95. bgDragPoint.ConstantScale = true;
  96. bgDragPoint.gameObject.SetActive(CubeActive);
  97. }
  98. private void OnChangeBegin(object sender, EventArgs args) => DestroyDragPoint();
  99. private void OnChangeEnd(object sender, EventArgs args) => UpdateBG();
  100. private void UpdateBG()
  101. {
  102. if (!bgMgr.BgObject) return;
  103. CurrentBgAsset = bgMgr.GetBGName();
  104. if (CurrentBgAsset.StartsWith(myRoomPrefix))
  105. CurrentBgAsset = CurrentBgAsset.Replace(myRoomPrefix, string.Empty);
  106. bg = bgMgr.BgObject.transform;
  107. AttachDragPoint(bg);
  108. }
  109. private void DestroyDragPoint()
  110. {
  111. if (bgDragPoint) Object.Destroy(bgDragPoint.gameObject);
  112. }
  113. private void OnCubeSmall(object sender, EventArgs args)
  114. {
  115. bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  116. }
  117. private void OnCubeActive(object sender, EventArgs args)
  118. {
  119. bgDragPoint.gameObject.SetActive(CubeActive);
  120. }
  121. }
  122. }