123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using System;
- using System.Linq;
- using UnityEngine;
- namespace COM3D2.MeidoPhotoStudio.Plugin
- {
- using Input = InputManager;
- public class EnvironmentManager : IManager, ISerializable
- {
- private static readonly BgMgr bgMgr = GameMain.Instance.BgMgr;
- public const string header = "ENVIRONMENT";
- private static bool cubeActive;
- public static bool CubeActive
- {
- get => cubeActive;
- set
- {
- if (value != cubeActive)
- {
- cubeActive = value;
- CubeActiveChange?.Invoke(null, EventArgs.Empty);
- }
- }
- }
- private static bool cubeSmall;
- public static bool CubeSmall
- {
- get => cubeSmall;
- set
- {
- if (value != cubeSmall)
- {
- cubeSmall = value;
- CubeSmallChange?.Invoke(null, EventArgs.Empty);
- }
- }
- }
- private static event EventHandler CubeActiveChange;
- private static event EventHandler CubeSmallChange;
- private UltimateOrbitCamera ultimateOrbitCamera;
- private GameObject cameraObject;
- private Camera subCamera;
- private GameObject bgObject;
- private Transform bg;
- private CameraInfo cameraInfo;
- private DragPointBG bgDragPoint;
- private string currentBGAsset = "Theater";
- private bool bgVisible = true;
- public bool BGVisible
- {
- get => bgVisible;
- set
- {
- bgVisible = value;
- bgObject.SetActive(bgVisible);
- }
- }
- private float defaultCameraMoveSpeed;
- private float defaultCameraZoomSpeed;
- private const float cameraFastMoveSpeed = 0.1f;
- private const float cameraFastZoomSpeed = 2f;
- static EnvironmentManager()
- {
- Input.Register(MpsKey.CameraLayer, KeyCode.Q, "Camera control layer");
- Input.Register(MpsKey.CameraSave, KeyCode.S, "Save camera transform");
- Input.Register(MpsKey.CameraLoad, KeyCode.A, "Load camera transform");
- Input.Register(MpsKey.CameraReset, KeyCode.R, "Reset camera transform");
- }
- public EnvironmentManager()
- {
- DragPointLight.EnvironmentManager = this;
- Activate();
- }
- public void Serialize(System.IO.BinaryWriter binaryWriter) => Serialize(binaryWriter, false);
- public void Serialize(System.IO.BinaryWriter binaryWriter, bool kankyo)
- {
- binaryWriter.Write(header);
- binaryWriter.Write(currentBGAsset);
- binaryWriter.WriteVector3(bg.position);
- binaryWriter.WriteQuaternion(bg.rotation);
- binaryWriter.WriteVector3(bg.localScale);
- binaryWriter.Write(kankyo);
- CameraMain camera = GameMain.Instance.MainCamera;
- binaryWriter.WriteVector3(camera.GetTargetPos());
- binaryWriter.Write(camera.GetDistance());
- binaryWriter.WriteQuaternion(camera.transform.rotation);
- StopCameraSpin();
- }
- public void Deserialize(System.IO.BinaryReader binaryReader)
- {
- string bgAsset = binaryReader.ReadString();
- bool isCreative = Utility.IsGuidString(bgAsset);
- System.Collections.Generic.List<string> bgList = isCreative
- ? Constants.MyRoomCustomBGList.Select(kvp => kvp.Key).ToList()
- : Constants.BGList;
- int assetIndex = bgList.FindIndex(
- asset => asset.Equals(bgAsset, StringComparison.InvariantCultureIgnoreCase)
- );
- if (assetIndex < 0)
- {
- Utility.LogWarning($"Could not load BG '{bgAsset}'");
- isCreative = false;
- bgAsset = "Theater";
- }
- else bgAsset = bgList[assetIndex];
- ChangeBackground(bgAsset, isCreative);
- bg.position = binaryReader.ReadVector3();
- bg.rotation = binaryReader.ReadQuaternion();
- bg.localScale = binaryReader.ReadVector3();
- bool kankyo = binaryReader.ReadBoolean();
- Vector3 cameraPosition = binaryReader.ReadVector3();
- float cameraDistance = binaryReader.ReadSingle();
- Quaternion cameraRotation = binaryReader.ReadQuaternion();
- if (!kankyo)
- {
- CameraMain camera = GameMain.Instance.MainCamera;
- camera.SetTargetPos(cameraPosition);
- camera.SetDistance(cameraDistance);
- camera.transform.rotation = cameraRotation;
- StopCameraSpin();
- }
- }
- public void Activate()
- {
- BgMgrPatcher.ChangeBgBegin += OnChangeBegin;
- BgMgrPatcher.ChangeBgEnd += OnChangeEnd;
- bgObject = bgMgr.Parent;
- cameraObject = new GameObject("subCamera");
- subCamera = cameraObject.AddComponent<Camera>();
- subCamera.CopyFrom(Camera.main);
- subCamera.clearFlags = CameraClearFlags.Depth;
- subCamera.cullingMask = 256;
- subCamera.depth = 1f;
- subCamera.transform.parent = GameMain.Instance.MainCamera.transform;
- cameraObject.SetActive(true);
- bgObject.SetActive(true);
- ultimateOrbitCamera =
- Utility.GetFieldValue<CameraMain, UltimateOrbitCamera>(GameMain.Instance.MainCamera, "m_UOCamera");
- ultimateOrbitCamera.enabled = true;
- defaultCameraMoveSpeed = ultimateOrbitCamera.moveSpeed;
- defaultCameraZoomSpeed = ultimateOrbitCamera.zoomSpeed;
- if (!MeidoPhotoStudio.EditMode)
- {
- ResetCamera();
- ChangeBackground("Theater");
- }
- else UpdateBG();
- SaveCameraInfo();
- CubeSmallChange += OnCubeSmall;
- CubeActiveChange += OnCubeActive;
- }
- public void Deactivate()
- {
- BgMgrPatcher.ChangeBgBegin -= OnChangeBegin;
- BgMgrPatcher.ChangeBgEnd -= OnChangeEnd;
- DestroyDragPoint();
- GameObject.Destroy(cameraObject);
- GameObject.Destroy(subCamera);
- BGVisible = true;
- Camera mainCamera = GameMain.Instance.MainCamera.camera;
- mainCamera.backgroundColor = Color.black;
- ultimateOrbitCamera.moveSpeed = defaultCameraMoveSpeed;
- ultimateOrbitCamera.zoomSpeed = defaultCameraZoomSpeed;
- if (MeidoPhotoStudio.EditMode) bgMgr.ChangeBg("Theater");
- else
- {
- bool isNight = GameMain.Instance.CharacterMgr.status.GetFlag("時間帯") == 3;
- if (isNight) bgMgr.ChangeBg("ShinShitsumu_ChairRot_Night");
- else bgMgr.ChangeBg("ShinShitsumu_ChairRot");
- GameMain.Instance.MainCamera.Reset(CameraMain.CameraType.Target, true);
- GameMain.Instance.MainCamera.SetTargetPos(new Vector3(0.5609447f, 1.380762f, -1.382336f), true);
- GameMain.Instance.MainCamera.SetDistance(1.6f, true);
- GameMain.Instance.MainCamera.SetAroundAngle(new Vector2(245.5691f, 6.273283f), true);
- }
- if (bgMgr.BgObject) bgMgr.BgObject.transform.localScale = Vector3.one;
- CubeSmallChange -= OnCubeSmall;
- CubeActiveChange -= OnCubeActive;
- }
- public void Update()
- {
- if (Input.GetKey(MpsKey.CameraLayer))
- {
- if (Input.GetKeyDown(MpsKey.CameraSave))
- {
- SaveCameraInfo();
- }
- if (Input.GetKeyDown(MpsKey.CameraLoad))
- {
- LoadCameraInfo(cameraInfo);
- }
- if (Input.GetKeyDown(MpsKey.CameraReset))
- {
- ResetCamera();
- }
- }
- bool shift = Input.Shift;
- ultimateOrbitCamera.moveSpeed = shift ? cameraFastMoveSpeed : defaultCameraMoveSpeed;
- ultimateOrbitCamera.zoomSpeed = shift ? cameraFastZoomSpeed : defaultCameraZoomSpeed;
- }
- public void ChangeBackground(string assetName, bool creative = false)
- {
- if (creative) bgMgr.ChangeBgMyRoom(assetName);
- else bgMgr.ChangeBg(assetName);
- }
- private void AttachDragPoint(Transform bgObject)
- {
- bgDragPoint = DragPoint.Make<DragPointBG>(PrimitiveType.Cube, Vector3.one * 0.12f);
- bgDragPoint.Initialize(() => bgObject.position, () => Vector3.zero);
- bgDragPoint.Set(bgObject);
- bgDragPoint.AddGizmo();
- bgDragPoint.ConstantScale = true;
- bgDragPoint.gameObject.SetActive(CubeActive);
- }
- private void OnChangeBegin(object sender, EventArgs args) => DestroyDragPoint();
- private void OnChangeEnd(object sender, EventArgs args) => UpdateBG();
- private void UpdateBG()
- {
- if (!bgMgr.BgObject) return;
- currentBGAsset = bgMgr.GetBGName();
- if (currentBGAsset.StartsWith("マイルーム:"))
- {
- currentBGAsset = currentBGAsset.Replace("マイルーム:", string.Empty);
- }
- bg = bgMgr.BgObject.transform;
- AttachDragPoint(bg);
- }
- private void DestroyDragPoint()
- {
- if (bgDragPoint) GameObject.Destroy(bgDragPoint.gameObject);
- }
- private void SaveCameraInfo()
- {
- cameraInfo = new CameraInfo(GameMain.Instance.MainCamera);
- StopCameraSpin();
- }
- public void LoadCameraInfo(CameraInfo cameraInfo)
- {
- CameraMain camera = GameMain.Instance.MainCamera;
- camera.SetTargetPos(cameraInfo.TargetPos);
- camera.SetPos(cameraInfo.Pos);
- camera.SetDistance(cameraInfo.Distance);
- camera.transform.eulerAngles = cameraInfo.Angle;
- StopCameraSpin();
- }
- private void StopCameraSpin()
- {
- UltimateOrbitCamera uoCamera = Utility.GetFieldValue<CameraMain, UltimateOrbitCamera>(
- GameMain.Instance.MainCamera, "m_UOCamera"
- );
- Utility.SetFieldValue(uoCamera, "xVelocity", 0f);
- Utility.SetFieldValue(uoCamera, "yVelocity", 0f);
- }
- private void ResetCamera()
- {
- CameraMain cameraMain = GameMain.Instance.MainCamera;
- cameraMain.Reset(CameraMain.CameraType.Target, true);
- cameraMain.SetTargetPos(new Vector3(0f, 0.9f, 0f), true);
- cameraMain.SetDistance(3f, true);
- }
- private void OnCubeSmall(object sender, EventArgs args)
- {
- bgDragPoint.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
- }
- private void OnCubeActive(object sender, EventArgs args)
- {
- bgDragPoint.gameObject.SetActive(CubeActive);
- }
- }
- public struct CameraInfo
- {
- public Vector3 TargetPos { get; }
- public Vector3 Pos { get; }
- public Vector3 Angle { get; }
- public float Distance { get; }
- public CameraInfo(CameraMain camera)
- {
- TargetPos = camera.GetTargetPos();
- Pos = camera.GetPos();
- Angle = camera.transform.eulerAngles;
- Distance = camera.GetDistance();
- }
- }
- }
|