BathroomController.cs 9.2 KB

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