PropManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class PropManager
  9. {
  10. private MeidoManager meidoManager;
  11. private static bool cubeActive = true;
  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 List<DragPointDogu> doguList = new List<DragPointDogu>();
  40. public int DoguCount => doguList.Count;
  41. public event EventHandler DoguListChange;
  42. public string[] PropNameList
  43. {
  44. get
  45. {
  46. return doguList.Count == 0
  47. ? new[] { Translation.Get("systemMessage", "noProps") }
  48. : doguList.Select(dogu => dogu.Name).ToArray();
  49. }
  50. }
  51. public PropManager(MeidoManager meidoManager)
  52. {
  53. this.meidoManager = meidoManager;
  54. this.meidoManager.BeginCallMeidos += DetachProps;
  55. this.meidoManager.EndCallMeidos += ReattachProps;
  56. }
  57. public void Activate()
  58. {
  59. CubeSmallChange += OnCubeSmall;
  60. }
  61. public void Deactivate()
  62. {
  63. foreach (DragPointDogu dogu in doguList)
  64. {
  65. dogu.Delete -= DeleteDogu;
  66. GameObject.Destroy(dogu.gameObject);
  67. }
  68. doguList.Clear();
  69. CubeSmallChange -= OnCubeSmall;
  70. }
  71. private GameObject GetDeploymentObject()
  72. {
  73. return GameObject.Find("Deployment Object Parent")
  74. ?? new GameObject("Deployment Object Parent");
  75. }
  76. public void SpawnMyRoomProp(MenuFileUtility.MyRoomItem item)
  77. {
  78. MyRoomCustom.PlacementData.Data data = MyRoomCustom.PlacementData.GetData(item.ID);
  79. GameObject dogu = GameObject.Instantiate(data.GetPrefab());
  80. string name = Translation.Get("myRoomPropNames", item.PrefabName);
  81. if (dogu != null) AttachDragPoint(dogu, name, new Vector3(0f, 0f, 0.5f));
  82. else Debug.Log($"Could not load MyRoomCreative prop '{item.PrefabName}'");
  83. }
  84. public void SpawnBG(string assetName)
  85. {
  86. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  87. ?? Resources.Load<GameObject>("BG/" + assetName)
  88. ?? Resources.Load<GameObject>("BG/2_0/" + assetName);
  89. if (obj != null)
  90. {
  91. GameObject dogu = GameObject.Instantiate(obj);
  92. string name = Translation.Get("bgNames", assetName);
  93. dogu.transform.localScale = Vector3.one * 0.1f;
  94. AttachDragPoint(dogu, name, Vector3.zero);
  95. }
  96. }
  97. public void SpawnObject(string assetName)
  98. {
  99. // TODO: Add a couple more things to ignore list
  100. GameObject dogu = null;
  101. string doguName = Translation.Get("propNames", assetName, false);
  102. Vector3 doguPosition = new Vector3(0f, 0f, 0.5f);
  103. if (assetName.EndsWith(".menu"))
  104. {
  105. dogu = MenuFileUtility.LoadModel(assetName);
  106. string handItem = Utility.HandItemToOdogu(assetName);
  107. if (Translation.Has("propNames", handItem))
  108. {
  109. doguName = Translation.Get("propNames", handItem);
  110. }
  111. }
  112. else if (assetName.StartsWith("mirror"))
  113. {
  114. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  115. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  116. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  117. mirrorRenderer.material = mirrorMaterial;
  118. mirrorRenderer.enabled = true;
  119. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  120. mirrorReflection.m_TextureSize = 2048;
  121. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  122. dogu.transform.Rotate(dogu.transform.right, 90f);
  123. switch (assetName)
  124. {
  125. case "mirror1":
  126. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  127. break;
  128. case "mirror2":
  129. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  130. break;
  131. case "mirror3":
  132. localPosition.y = 0.85f;
  133. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  134. break;
  135. }
  136. dogu.transform.localPosition = localPosition;
  137. }
  138. else if (assetName.IndexOf(':') >= 0)
  139. {
  140. string[] assetParts = assetName.Split(':');
  141. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0])
  142. ?? Resources.Load<GameObject>("BG/" + assetParts[0]);
  143. GameObject bg = GameObject.Instantiate(obj);
  144. int num = int.Parse(assetParts[1]);
  145. dogu = bg.transform.GetChild(num).gameObject;
  146. dogu.transform.SetParent(null);
  147. GameObject.Destroy(bg);
  148. bg.SetActive(false);
  149. }
  150. else
  151. {
  152. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  153. ?? Resources.Load<GameObject>("Prefab/" + assetName);
  154. dogu = GameObject.Instantiate<GameObject>(obj);
  155. dogu.transform.localPosition = Vector3.zero;
  156. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  157. for (int i = 0; i < meshRenderers.Length; i++)
  158. {
  159. if (meshRenderers[i] != null
  160. && meshRenderers[i].gameObject.name.ToLower().IndexOf("castshadow") < 0
  161. )
  162. {
  163. meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  164. }
  165. }
  166. Collider collider = dogu.transform.GetComponent<Collider>();
  167. if (collider != null) collider.enabled = false;
  168. foreach (Transform transform in dogu.transform)
  169. {
  170. collider = transform.GetComponent<Collider>();
  171. if (collider != null)
  172. {
  173. collider.enabled = false;
  174. }
  175. }
  176. #region particle system experiment
  177. // if (asset.StartsWith("Particle/"))
  178. // {
  179. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  180. // if (particleSystem != null)
  181. // {
  182. // ParticleSystem.MainModule main;
  183. // main = particleSystem.main;
  184. // main.loop = true;
  185. // main.duration = Mathf.Infinity;
  186. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  187. // foreach (ParticleSystem part in particleSystems)
  188. // {
  189. // ParticleSystem.EmissionModule emissionModule = part.emission;
  190. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  191. // emissionModule.GetBursts(bursts);
  192. // for (int i = 0; i < bursts.Length; i++)
  193. // {
  194. // bursts[i].cycleCount = Int32.MaxValue;
  195. // }
  196. // emissionModule.SetBursts(bursts);
  197. // main = part.main;
  198. // main.loop = true;
  199. // main.duration = Mathf.Infinity;
  200. // }
  201. // }
  202. // }
  203. #endregion
  204. }
  205. if (dogu != null)
  206. {
  207. AttachDragPoint(dogu, doguName, doguPosition);
  208. }
  209. else
  210. {
  211. Debug.LogError($"Could not spawn object '{assetName}'");
  212. }
  213. }
  214. private void AttachDragPoint(GameObject dogu, string name, Vector3 position)
  215. {
  216. // TODO: Figure out why some props aren't centred properly
  217. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  218. GameObject deploymentObject = GetDeploymentObject();
  219. GameObject finalDogu = new GameObject(name);
  220. dogu.transform.SetParent(finalDogu.transform, true);
  221. finalDogu.transform.SetParent(deploymentObject.transform, false);
  222. finalDogu.transform.position = position;
  223. DragPointDogu dragDogu = DragPoint.Make<DragPointDogu>(
  224. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.LightBlue
  225. );
  226. dragDogu.Initialize(() => finalDogu.transform.position, () => Vector3.zero);
  227. dragDogu.Set(finalDogu.transform);
  228. dragDogu.AddGizmo(scale: 0.45f, mode: CustomGizmo.GizmoMode.World);
  229. dragDogu.ConstantScale = true;
  230. dragDogu.Delete += DeleteDogu;
  231. dragDogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  232. doguList.Add(dragDogu);
  233. OnDoguListChange();
  234. }
  235. public DragPointDogu GetDogu(int doguIndex)
  236. {
  237. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return null;
  238. return doguList[doguIndex];
  239. }
  240. public void AttachProp(
  241. int doguIndex, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  242. )
  243. {
  244. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return;
  245. AttachProp(doguList[doguIndex], attachPoint, meido, worldPositionStays);
  246. }
  247. private void AttachProp(
  248. DragPointDogu dragDogu, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  249. )
  250. {
  251. GameObject dogu = dragDogu.MyGameObject;
  252. Transform attachPointTransform = meido?.GetBoneTransform(attachPoint) ?? GetDeploymentObject().transform;
  253. dragDogu.attachPointInfo = new AttachPointInfo(
  254. attachPoint: meido == null ? AttachPoint.None : attachPoint,
  255. maidGuid: meido == null ? String.Empty : meido.Maid.status.guid,
  256. maidIndex: meido == null ? -1 : meido.ActiveSlot
  257. );
  258. worldPositionStays = meido == null ? true : worldPositionStays;
  259. Vector3 position = dogu.transform.position;
  260. Quaternion rotation = dogu.transform.rotation;
  261. dogu.transform.SetParent(attachPointTransform, worldPositionStays);
  262. if (worldPositionStays)
  263. {
  264. dogu.transform.position = position;
  265. dogu.transform.rotation = rotation;
  266. }
  267. else
  268. {
  269. dogu.transform.localPosition = Vector3.zero;
  270. dogu.transform.rotation = Quaternion.identity;
  271. }
  272. if (meido == null) Utility.FixGameObjectScale(dogu);
  273. }
  274. private void DetachProps(object sender, EventArgs args)
  275. {
  276. foreach (DragPointDogu dogu in doguList)
  277. {
  278. if (dogu.attachPointInfo.AttachPoint != AttachPoint.None)
  279. {
  280. dogu.MyObject.SetParent(GetDeploymentObject().transform, true);
  281. }
  282. }
  283. }
  284. private void ReattachProps(object sender, EventArgs args)
  285. {
  286. foreach (DragPointDogu dragDogu in doguList)
  287. {
  288. Meido meido = this.meidoManager.GetMeido(dragDogu.attachPointInfo.MaidGuid);
  289. bool worldPositionStays = meido == null;
  290. AttachProp(dragDogu, dragDogu.attachPointInfo.AttachPoint, meido, worldPositionStays);
  291. }
  292. }
  293. private void DeleteDogu(object sender, EventArgs args)
  294. {
  295. DragPointDogu dogu = (DragPointDogu)sender;
  296. doguList.RemoveAll(dragDogu =>
  297. {
  298. if (dragDogu == dogu)
  299. {
  300. GameObject.Destroy(dragDogu.gameObject);
  301. return true;
  302. }
  303. return false;
  304. }
  305. );
  306. OnDoguListChange();
  307. }
  308. private void OnCubeSmall(object sender, EventArgs args)
  309. {
  310. foreach (DragPointDogu dogu in doguList)
  311. {
  312. dogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  313. }
  314. }
  315. private void OnDoguListChange()
  316. {
  317. this.DoguListChange?.Invoke(this, EventArgs.Empty);
  318. }
  319. }
  320. }