EnvironmentManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. using Input = InputManager;
  9. public class EnvironmentManager : IManager, ISerializable
  10. {
  11. private static readonly BgMgr bgMgr = GameMain.Instance.BgMgr;
  12. public const string header = "ENVIRONMENT";
  13. public const string defaultBg = "Theater";
  14. private const string myRoomPrefix = "マイルーム:";
  15. private static bool cubeActive;
  16. public static bool CubeActive
  17. {
  18. get => cubeActive;
  19. set
  20. {
  21. if (value == cubeActive) return;
  22. cubeActive = value;
  23. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  24. }
  25. }
  26. private static bool cubeSmall;
  27. public static bool CubeSmall
  28. {
  29. get => cubeSmall;
  30. set
  31. {
  32. if (value == cubeSmall) return;
  33. cubeSmall = value;
  34. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  35. }
  36. }
  37. private static event EventHandler CubeActiveChange;
  38. private static event EventHandler CubeSmallChange;
  39. private Transform bg;
  40. private GameObject bgObject;
  41. private DragPointBG bgDragPoint;
  42. public string CurrentBgAsset { get; private set; } = defaultBg;
  43. private bool bgVisible = true;
  44. public bool BGVisible
  45. {
  46. get => bgVisible;
  47. set
  48. {
  49. bgVisible = value;
  50. bgObject.SetActive(bgVisible);
  51. }
  52. }
  53. public EnvironmentManager()
  54. {
  55. DragPointLight.EnvironmentManager = this;
  56. Activate();
  57. }
  58. public void Update() { }
  59. public void Activate()
  60. {
  61. BgMgrPatcher.ChangeBgBegin += OnChangeBegin;
  62. BgMgrPatcher.ChangeBgEnd += OnChangeEnd;
  63. bgObject = bgMgr.Parent;
  64. bgObject.SetActive(true);
  65. if (MeidoPhotoStudio.EditMode) UpdateBG();
  66. else ChangeBackground(defaultBg);
  67. CubeSmallChange += OnCubeSmall;
  68. CubeActiveChange += OnCubeActive;
  69. }
  70. public void Deactivate()
  71. {
  72. BgMgrPatcher.ChangeBgBegin -= OnChangeBegin;
  73. BgMgrPatcher.ChangeBgEnd -= OnChangeEnd;
  74. DestroyDragPoint();
  75. BGVisible = true;
  76. if (MeidoPhotoStudio.EditMode) bgMgr.ChangeBg(defaultBg);
  77. else
  78. {
  79. var isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
  80. bgMgr.ChangeBg(isNight ? "ShinShitsumu_ChairRot_Night" : "ShinShitsumu_ChairRot");
  81. }
  82. if (bgMgr.BgObject) bgMgr.BgObject.transform.localScale = Vector3.one;
  83. CubeSmallChange -= OnCubeSmall;
  84. CubeActiveChange -= OnCubeActive;
  85. }
  86. public void Serialize(BinaryWriter binaryWriter)
  87. {
  88. binaryWriter.Write(header);
  89. binaryWriter.Write(CurrentBgAsset);
  90. binaryWriter.WriteVector3(bg.position);
  91. binaryWriter.WriteQuaternion(bg.rotation);
  92. binaryWriter.WriteVector3(bg.localScale);
  93. }
  94. public void Deserialize(BinaryReader binaryReader)
  95. {
  96. var bgAsset = binaryReader.ReadString();
  97. var isCreative = Utility.IsGuidString(bgAsset);
  98. List<string> bgList = isCreative
  99. ? Constants.MyRoomCustomBGList.ConvertAll(kvp => kvp.Key)
  100. : Constants.BGList;
  101. var assetIndex = bgList.FindIndex(
  102. asset => asset.Equals(bgAsset, StringComparison.InvariantCultureIgnoreCase)
  103. );
  104. if (assetIndex < 0)
  105. {
  106. Utility.LogWarning($"Could not load BG '{bgAsset}'");
  107. isCreative = false;
  108. bgAsset = defaultBg;
  109. }
  110. else bgAsset = bgList[assetIndex];
  111. ChangeBackground(bgAsset, isCreative);
  112. bg.position = binaryReader.ReadVector3();
  113. bg.rotation = binaryReader.ReadQuaternion();
  114. bg.localScale = binaryReader.ReadVector3();
  115. }
  116. public void ChangeBackground(string assetName, bool creative = false)
  117. {
  118. if (creative) bgMgr.ChangeBgMyRoom(assetName);
  119. else bgMgr.ChangeBg(assetName);
  120. }
  121. private void AttachDragPoint(Transform bgTransform)
  122. {
  123. bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
  124. bgDragPoint.Initialize(() => bgTransform.position, () => Vector3.zero);
  125. bgDragPoint.Set(bgTransform);
  126. bgDragPoint.AddGizmo();
  127. bgDragPoint.ConstantScale = true;
  128. bgDragPoint.gameObject.SetActive(CubeActive);
  129. }
  130. private void OnChangeBegin(object sender, EventArgs args) => DestroyDragPoint();
  131. private void OnChangeEnd(object sender, EventArgs args) => UpdateBG();
  132. private void UpdateBG()
  133. {
  134. if (!bgMgr.BgObject) return;
  135. CurrentBgAsset = bgMgr.GetBGName();
  136. if (CurrentBgAsset.StartsWith(myRoomPrefix))
  137. CurrentBgAsset = CurrentBgAsset.Replace(myRoomPrefix, string.Empty);
  138. bg = bgMgr.BgObject.transform;
  139. AttachDragPoint(bg);
  140. }
  141. private void DestroyDragPoint()
  142. {
  143. if (bgDragPoint) Object.Destroy(bgDragPoint.gameObject);
  144. }
  145. private void OnCubeSmall(object sender, EventArgs args)
  146. {
  147. bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  148. }
  149. private void OnCubeActive(object sender, EventArgs args)
  150. {
  151. bgDragPoint.gameObject.SetActive(CubeActive);
  152. }
  153. }
  154. }