BathroomController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.InputSystem;
  7. using UnityEngine.InputSystem.Controls;
  8. using UnityEngine.SceneManagement;
  9. using static UnityEngine.ParticleSystem;
  10. /* Bathroom 动作的主要控制模块
  11. * 泡泡阈值达到180个粒子后,认为泡泡阶段结束
  12. * 冲洗10秒后,认为冲洗阶段结束
  13. */
  14. public class BathroomController : MonoBehaviour
  15. {
  16. public Texture2D nozzleTexture, spongeTexture; // 对应鼠标图像喷头和海绵
  17. // private CursorMode cursorMode = CursorMode.Auto; // 鼠标模式
  18. // private Vector2 cursorHotSpot = new Vector2(-1f, -1f); // 鼠标图标的点击点位置
  19. private Vector2 previousPointerPosition = Vector2.zero; // 前一帧鼠标位置
  20. private Texture2D activatedCursorTexture = null;
  21. private float bubbleTotalTime; // 上肥皂的合计时间
  22. private float flushTotalTime; // 冲洗的合计时间
  23. private TouchMethod touchMethod; // 定义触控方式
  24. private string gamePhase; // 目前游戏进度 bubble上肥皂,flush冲洗
  25. // Start is called once before the first execution of Update after the MonoBehaviour is created
  26. void Start()
  27. {
  28. Invoke("StartBubbleMsg", 2f);
  29. }
  30. // Update is called once per frame
  31. void FixedUpdate()
  32. {
  33. // 检测泡泡数量是否达到180,达到180后结束泡泡过程
  34. var bubbleParticleSystem = GameObject.Find("Bubble Particle").GetComponent<ParticleSystem>();
  35. if (bubbleParticleSystem.particleCount > 180)
  36. {
  37. EndBubblePhase();
  38. }
  39. // 检查冲洗时间是否达到规定时间
  40. if (flushTotalTime > 8f)
  41. {
  42. EndFlushPhase();
  43. }
  44. if (gamePhase == "bubble")
  45. {
  46. PointerOnDog(spongeTexture);
  47. }else if (gamePhase == "flush")
  48. {
  49. PointerOnDog(nozzleTexture);
  50. }
  51. }
  52. void StartBubbleMsg()
  53. {
  54. Time.timeScale = 0f;
  55. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "start_bubble", EnviromentSetting.languageCode });
  56. MessageBoxController.ShowMessage(msg, StartBubblePhase);
  57. }
  58. // 检测是否点击在狗上
  59. void PointerOnDog(Texture2D cursorTexture)
  60. {
  61. DetectTouchMethod();
  62. var dog = GameObject.Find("dog");
  63. // 检查当前指针是否有效
  64. if (Pointer.current == null) return;
  65. // 获取当前指针的悬浮位置
  66. Vector2 pointerPosition = Pointer.current.position.ReadValue();
  67. Ray ray = Camera.main.ScreenPointToRay(pointerPosition);
  68. if (Physics.Raycast(ray, out RaycastHit hit))
  69. {
  70. //Debug.Log($"Clicked on: {hit.collider.gameObject.name}");
  71. // 射线检测起始点击是否在狗上
  72. if (hit.collider.gameObject.name == "dog")
  73. {
  74. if (previousPointerPosition != pointerPosition)
  75. {
  76. activatedCursorTexture = cursorTexture;
  77. //Cursor.SetCursor(cursorTexture, cursorHotSpot, cursorMode);
  78. ActivateParticle(cursorTexture, hit);
  79. if (touchMethod == TouchMethod.touch)
  80. {
  81. Cursor.visible = false;
  82. }
  83. }
  84. previousPointerPosition = pointerPosition;
  85. }
  86. else
  87. {
  88. activatedCursorTexture = null ;
  89. //Cursor.SetCursor(null, cursorHotSpot, cursorMode);
  90. Cursor.visible = true;
  91. }
  92. }
  93. }
  94. // 检测控制方式是鼠标还是触控
  95. void DetectTouchMethod()
  96. {
  97. // 检测鼠标输入
  98. if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
  99. {
  100. touchMethod = TouchMethod.mouse;
  101. }
  102. // 检测触控输入
  103. if (Touchscreen.current != null && Touchscreen.current.touches.Count > 0)
  104. {
  105. touchMethod = TouchMethod.touch;
  106. }
  107. }
  108. // 当nozzle鼠标材质被调用时,把bubble particle system位置和鼠标位置绑定
  109. void ActivateParticle(Texture2D selectedCursorTexture, RaycastHit hit)
  110. {
  111. if (selectedCursorTexture == spongeTexture)
  112. {
  113. var bubbleParticle = GameObject.Find("Bubble Particle");
  114. var targetPosition = hit.point;
  115. targetPosition.x = -3.55f;
  116. bubbleParticle.transform.position = targetPosition;
  117. bubbleTotalTime += Time.deltaTime;
  118. //Debug.Log("bubbleTotalTime:" + bubbleTotalTime.ToString());
  119. }else if (selectedCursorTexture == nozzleTexture)
  120. {
  121. var bubbleParticle = GameObject.Find("Flush Particle");
  122. var targetPosition = hit.point;
  123. targetPosition.x = -3.55f;
  124. bubbleParticle.transform.position = targetPosition;
  125. flushTotalTime += Time.deltaTime;
  126. //Debug.Log("Flush TotalTime:" + flushTotalTime.ToString());
  127. }
  128. }
  129. // 开始上肥皂过程
  130. void StartBubblePhase()
  131. {
  132. Time.timeScale = 1.0f;
  133. gamePhase = "bubble";
  134. var bubbleSfx = GameObject.Find("Bubble Particle").GetComponent<AudioSource>();
  135. bubbleSfx.Play();
  136. }
  137. // 监控bubble时间,如果达到10秒,提示结束
  138. void EndBubblePhase()
  139. {
  140. if (gamePhase == "bubble") {
  141. // 如果已经结束泡泡阶段就不能更换洗澡的狗了
  142. var dogSelector = GameObject.Find("Dog Selector");
  143. dogSelector.SetActive(false);
  144. gamePhase = null;
  145. //Cursor.SetCursor(null, cursorHotSpot, cursorMode);
  146. Time.timeScale = 0f;
  147. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "start_flush", EnviromentSetting.languageCode });
  148. MessageBoxController.ShowMessage(msg, StartFlushPhase);
  149. var bubbleSfx = GameObject.Find("Bubble Particle").GetComponent<AudioSource>();
  150. bubbleSfx.Stop();
  151. // 隐藏泡泡粒子
  152. var bubbleParticle = GameObject.Find("Bubble Particle");
  153. bubbleParticle.transform.position = new Vector3(10, 10, 10);
  154. }
  155. }
  156. // 检测bubble如果达到条件后,开始冲洗
  157. void StartFlushPhase()
  158. {
  159. Time.timeScale = 1.0f;
  160. gamePhase = "flush";
  161. var soundSfx = GameObject.Find("Flush Particle").GetComponent<AudioSource>();
  162. soundSfx.Play();
  163. // 喷水后缩短泡沫粒子生命
  164. var bubbleParticle = GameObject.Find("Bubble Particle").GetComponent<ParticleSystem>();
  165. int bubbleParticleCount = bubbleParticle.particleCount; // 获取当前粒子数量
  166. var particles = new ParticleSystem.Particle[bubbleParticleCount]; // 初始化粒子数组
  167. // 获取所有粒子的数据
  168. bubbleParticle.GetParticles(particles);
  169. // 修改每个粒子的生命周期
  170. for (int i = 0; i < bubbleParticleCount; i++)
  171. {
  172. particles[i].remainingLifetime = UnityEngine.Random.Range(0.6f,3f); // 设置新的生命周期
  173. }
  174. // 将修改后的粒子数据应用回粒子系统
  175. bubbleParticle.SetParticles(particles);
  176. }
  177. // 检测flush达到标准后,结束过程
  178. void EndFlushPhase()
  179. {
  180. if (gamePhase == "flush")
  181. {
  182. gamePhase = "end";
  183. //Cursor.SetCursor(null, cursorHotSpot, cursorMode);
  184. Time.timeScale = 0f;
  185. var soundSfx = GameObject.Find("Flush Particle").GetComponent<AudioSource>();
  186. soundSfx.Stop();
  187. var flushParticle = GameObject.Find("Flush Particle");
  188. flushParticle.SetActive(false);
  189. // TODO 上传道具使用和狗的名单
  190. BathFinishReq();
  191. }
  192. }
  193. // 提交洗澡道具使用和受影响狗的列表
  194. void BathFinishReq()
  195. {
  196. Debug.Log("BathFinishReq request");
  197. List<string> dogs = new List<string>();
  198. dogs.Add(UserProperty.dogs[GameData.focusDog].d_id);
  199. string dogsJson = JsonConvert.SerializeObject(dogs);
  200. string url = "/api/item/use/";
  201. WWWForm form = new();
  202. form.AddField("user_id", UserProperty.userId);
  203. form.AddField("item_id", GameData.bathItemId);
  204. form.AddField("dog_list", dogsJson);
  205. StartCoroutine(WebController.PostRequest(url, form, callback: BathFinishReqCallback));
  206. }
  207. void BathFinishReqCallback(string json)
  208. {
  209. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  210. if (data != null && data["status"].ToString() == "success")
  211. {
  212. // TODO 清除然后重新写入用户道具数据
  213. // 清除然后重新写入狗的数据
  214. DogProperty[] dogProperties = JsonConvert.DeserializeObject<DogProperty[]>(data["dogs"].ToString());
  215. UserProperty.dogs.Clear();
  216. foreach (var dog in dogProperties)
  217. {
  218. UserProperty.dogs.Add(dog);
  219. }
  220. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "dog_is_clean", EnviromentSetting.languageCode });
  221. // MessageBoxController.ShowMessage(msg, ()=> SceneManager.LoadScene("Home"));
  222. Time.timeScale = 1.0f; // 恢复时间缩放
  223. MessageBoxController.ShowMessage(msg, () => MaskTransitions.TransitionManager.Instance.LoadLevel("Home"));
  224. }
  225. else
  226. {
  227. Debug.Log(data["message"]);
  228. }
  229. }
  230. }
  231. enum TouchMethod
  232. {
  233. mouse,
  234. touch
  235. }