EnvironmentManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. using Input = InputManager;
  7. internal class EnvironmentManager : IManager, ISerializable
  8. {
  9. public const string header = "ENVIRONMENT";
  10. private static bool cubeActive;
  11. public static bool CubeActive
  12. {
  13. get => cubeActive;
  14. set
  15. {
  16. if (value != cubeActive)
  17. {
  18. cubeActive = value;
  19. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  20. }
  21. }
  22. }
  23. private static bool cubeSmall;
  24. public static bool CubeSmall
  25. {
  26. get => cubeSmall;
  27. set
  28. {
  29. if (value != cubeSmall)
  30. {
  31. cubeSmall = value;
  32. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  33. }
  34. }
  35. }
  36. private static event EventHandler CubeActiveChange;
  37. private static event EventHandler CubeSmallChange;
  38. private GameObject cameraObject;
  39. private Camera subCamera;
  40. private GameObject bgObject;
  41. private Transform bg;
  42. private CameraInfo cameraInfo;
  43. private DragPointBG bgDragPoint;
  44. private string currentBGAsset = "Theater";
  45. private bool bgVisible = true;
  46. public bool BGVisible
  47. {
  48. get => bgVisible;
  49. set
  50. {
  51. this.bgVisible = value;
  52. bgObject.SetActive(this.bgVisible);
  53. }
  54. }
  55. static EnvironmentManager()
  56. {
  57. Input.Register(MpsKey.CameraLayer, KeyCode.Q, "Camera control layer");
  58. Input.Register(MpsKey.CameraSave, KeyCode.S, "Save camera transform");
  59. Input.Register(MpsKey.CameraLoad, KeyCode.A, "Load camera transform");
  60. Input.Register(MpsKey.CameraReset, KeyCode.R, "Reset camera transform");
  61. }
  62. public EnvironmentManager(MeidoManager meidoManager)
  63. {
  64. DragPointLight.environmentManager = this;
  65. }
  66. public void Serialize(System.IO.BinaryWriter binaryWriter) => Serialize(binaryWriter, false);
  67. public void Serialize(System.IO.BinaryWriter binaryWriter, bool kankyo)
  68. {
  69. binaryWriter.Write(header);
  70. binaryWriter.Write(currentBGAsset);
  71. binaryWriter.WriteVector3(this.bg.position);
  72. binaryWriter.WriteQuaternion(this.bg.rotation);
  73. binaryWriter.WriteVector3(this.bg.localScale);
  74. binaryWriter.Write(kankyo);
  75. CameraMain camera = GameMain.Instance.MainCamera;
  76. binaryWriter.WriteVector3(camera.GetTargetPos());
  77. binaryWriter.Write(camera.GetDistance());
  78. binaryWriter.WriteQuaternion(camera.transform.rotation);
  79. StopCameraSpin();
  80. }
  81. public void Deserialize(System.IO.BinaryReader binaryReader)
  82. {
  83. string bgAsset = binaryReader.ReadString();
  84. bool isCreative = Utility.IsGuidString(bgAsset);
  85. System.Collections.Generic.List<string> bgList = isCreative
  86. ? Constants.MyRoomCustomBGList.Select(kvp => kvp.Key).ToList()
  87. : Constants.BGList;
  88. int assetIndex = bgList.FindIndex(
  89. asset => asset.Equals(bgAsset, StringComparison.InvariantCultureIgnoreCase)
  90. );
  91. if (assetIndex < 0)
  92. {
  93. Utility.LogWarning($"Could not load BG '{bgAsset}'");
  94. isCreative = false;
  95. bgAsset = "Theater";
  96. }
  97. else bgAsset = bgList[assetIndex];
  98. ChangeBackground(bgAsset, isCreative);
  99. this.bg.position = binaryReader.ReadVector3();
  100. this.bg.rotation = binaryReader.ReadQuaternion();
  101. this.bg.localScale = binaryReader.ReadVector3();
  102. bool kankyo = binaryReader.ReadBoolean();
  103. Vector3 cameraPosition = binaryReader.ReadVector3();
  104. float cameraDistance = binaryReader.ReadSingle();
  105. Quaternion cameraRotation = binaryReader.ReadQuaternion();
  106. if (!kankyo)
  107. {
  108. CameraMain camera = GameMain.Instance.MainCamera;
  109. camera.SetTargetPos(cameraPosition);
  110. camera.SetDistance(cameraDistance);
  111. camera.transform.rotation = cameraRotation;
  112. StopCameraSpin();
  113. }
  114. }
  115. public void Activate()
  116. {
  117. bgObject = GameObject.Find("__GameMain__/BG");
  118. bg = bgObject.transform;
  119. bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
  120. bgDragPoint.Initialize(() => bg.position, () => Vector3.zero);
  121. bgDragPoint.Set(bg);
  122. bgDragPoint.AddGizmo();
  123. bgDragPoint.ConstantScale = true;
  124. bgDragPoint.gameObject.SetActive(CubeActive);
  125. cameraObject = new GameObject("subCamera");
  126. subCamera = cameraObject.AddComponent<Camera>();
  127. subCamera.CopyFrom(Camera.main);
  128. subCamera.clearFlags = CameraClearFlags.Depth;
  129. subCamera.cullingMask = 256;
  130. subCamera.depth = 1f;
  131. subCamera.transform.parent = GameMain.Instance.MainCamera.transform;
  132. cameraObject.SetActive(true);
  133. bgObject.SetActive(true);
  134. GameMain.Instance.BgMgr.ChangeBg("Theater");
  135. UltimateOrbitCamera uoCamera =
  136. Utility.GetFieldValue<CameraMain, UltimateOrbitCamera>(GameMain.Instance.MainCamera, "m_UOCamera");
  137. uoCamera.enabled = true;
  138. ResetCamera();
  139. SaveCameraInfo();
  140. CubeSmallChange += OnCubeSmall;
  141. CubeActiveChange += OnCubeActive;
  142. }
  143. public void Deactivate()
  144. {
  145. if (bgDragPoint != null) GameObject.Destroy(bgDragPoint.gameObject);
  146. GameObject.Destroy(cameraObject);
  147. GameObject.Destroy(subCamera);
  148. BGVisible = true;
  149. Camera mainCamera = GameMain.Instance.MainCamera.camera;
  150. mainCamera.backgroundColor = Color.black;
  151. bool isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
  152. if (isNight)
  153. {
  154. GameMain.Instance.BgMgr.ChangeBg("ShinShitsumu_ChairRot_Night");
  155. }
  156. else
  157. {
  158. GameMain.Instance.BgMgr.ChangeBg("ShinShitsumu_ChairRot");
  159. }
  160. GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
  161. GameMain.Instance.MainCamera.SetTargetPos(new Vector3(0.5609447f, 1.380762f, -1.382336f), true);
  162. GameMain.Instance.MainCamera.SetDistance(1.6f, true);
  163. GameMain.Instance.MainCamera.SetAroundAngle(new Vector2(245.5691f, 6.273283f), true);
  164. bg.localScale = Vector3.one;
  165. CubeSmallChange -= OnCubeSmall;
  166. CubeActiveChange -= OnCubeActive;
  167. }
  168. public void Update()
  169. {
  170. if (Input.GetKey(MpsKey.CameraLayer))
  171. {
  172. if (Input.GetKeyDown(MpsKey.CameraSave))
  173. {
  174. SaveCameraInfo();
  175. }
  176. if (Input.GetKeyDown(MpsKey.CameraLoad))
  177. {
  178. LoadCameraInfo(cameraInfo);
  179. }
  180. if (Input.GetKeyDown(MpsKey.CameraReset))
  181. {
  182. ResetCamera();
  183. }
  184. }
  185. }
  186. public void ChangeBackground(string assetName, bool creative = false)
  187. {
  188. currentBGAsset = assetName;
  189. if (creative)
  190. {
  191. GameMain.Instance.BgMgr.ChangeBgMyRoom(assetName);
  192. }
  193. else
  194. {
  195. GameMain.Instance.BgMgr.ChangeBg(assetName);
  196. if (assetName == "KaraokeRoom")
  197. {
  198. bg.transform.position = bgObject.transform.position;
  199. bg.transform.localPosition = new Vector3(1f, 0f, 4f);
  200. bg.transform.localRotation = Quaternion.Euler(new Vector3(0f, 90f, 0f));
  201. }
  202. }
  203. }
  204. private void SaveCameraInfo()
  205. {
  206. this.cameraInfo = new CameraInfo(GameMain.Instance.MainCamera);
  207. StopCameraSpin();
  208. }
  209. public void LoadCameraInfo(CameraInfo cameraInfo)
  210. {
  211. CameraMain camera = GameMain.Instance.MainCamera;
  212. camera.SetTargetPos(cameraInfo.TargetPos);
  213. camera.SetPos(cameraInfo.Pos);
  214. camera.SetDistance(cameraInfo.Distance);
  215. camera.transform.eulerAngles = cameraInfo.Angle;
  216. StopCameraSpin();
  217. }
  218. private void StopCameraSpin()
  219. {
  220. UltimateOrbitCamera uoCamera = Utility.GetFieldValue<CameraMain, UltimateOrbitCamera>(
  221. GameMain.Instance.MainCamera, "m_UOCamera"
  222. );
  223. Utility.SetFieldValue<UltimateOrbitCamera, float>(uoCamera, "xVelocity", 0f);
  224. Utility.SetFieldValue<UltimateOrbitCamera, float>(uoCamera, "yVelocity", 0f);
  225. }
  226. private void ResetCamera()
  227. {
  228. CameraMain cameraMain = GameMain.Instance.MainCamera;
  229. cameraMain.Reset(CameraMain.CameraType.Target, true);
  230. cameraMain.SetTargetPos(new Vector3(0f, 0.9f, 0f), true);
  231. cameraMain.SetDistance(3f, true);
  232. }
  233. private void OnCubeSmall(object sender, EventArgs args)
  234. {
  235. this.bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  236. }
  237. private void OnCubeActive(object sender, EventArgs args)
  238. {
  239. this.bgDragPoint.gameObject.SetActive(CubeActive);
  240. }
  241. }
  242. public struct CameraInfo
  243. {
  244. public Vector3 TargetPos { get; }
  245. public Vector3 Pos { get; }
  246. public Vector3 Angle { get; }
  247. public float Distance { get; }
  248. public CameraInfo(CameraMain camera)
  249. {
  250. this.TargetPos = camera.GetTargetPos();
  251. this.Pos = camera.GetPos();
  252. this.Angle = camera.transform.eulerAngles;
  253. this.Distance = camera.GetDistance();
  254. }
  255. }
  256. }