EnvironmentManager.cs 11 KB

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