PropManager.cs 14 KB

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