BathroomController.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. var bubbleParticle = GameObject.Find("Bubble Particle");
  149. bubbleParticle.transform.position = new Vector3(10, 10, 10);
  150. }
  151. }
  152. // 检测bubble如果达到条件后,开始冲洗
  153. void StartFlushPhase()
  154. {
  155. Time.timeScale = 1.0f;
  156. gamePhase = "flush";
  157. var soundSfx = GameObject.Find("Flush Particle").GetComponent<AudioSource>();
  158. soundSfx.Play();
  159. // 喷水后缩短泡沫粒子生命
  160. var bubbleParticle = GameObject.Find("Bubble Particle").GetComponent<ParticleSystem>();
  161. int bubbleParticleCount = bubbleParticle.particleCount; // 获取当前粒子数量
  162. var particles = new ParticleSystem.Particle[bubbleParticleCount]; // 初始化粒子数组
  163. // 获取所有粒子的数据
  164. bubbleParticle.GetParticles(particles);
  165. // 修改每个粒子的生命周期
  166. for (int i = 0; i < bubbleParticleCount; i++)
  167. {
  168. particles[i].remainingLifetime = UnityEngine.Random.Range(0.6f,3f); // 设置新的生命周期
  169. }
  170. // 将修改后的粒子数据应用回粒子系统
  171. bubbleParticle.SetParticles(particles);
  172. }
  173. // 检测flush达到标准后,结束过程
  174. void EndFlushPhase()
  175. {
  176. if (gamePhase == "flush")
  177. {
  178. gamePhase = "end";
  179. //Cursor.SetCursor(null, cursorHotSpot, cursorMode);
  180. Time.timeScale = 0f;
  181. var soundSfx = GameObject.Find("Flush Particle").GetComponent<AudioSource>();
  182. soundSfx.Stop();
  183. var flushParticle = GameObject.Find("Flush Particle");
  184. flushParticle.SetActive(false);
  185. // TODO 上传道具使用和狗的名单
  186. BathFinishReq();
  187. }
  188. }
  189. // 提交洗澡道具使用和受影响狗的列表
  190. void BathFinishReq()
  191. {
  192. Debug.Log("BathFinishReq request");
  193. List<string> dogs = new List<string>();
  194. dogs.Add(UserProperty.dogs[GameData.focusDog].d_id);
  195. string dogsJson = JsonConvert.SerializeObject(dogs);
  196. string url = "/api/item/use/";
  197. WWWForm form = new();
  198. form.AddField("user_id", UserProperty.userId);
  199. form.AddField("item_id", GameData.bathItemId);
  200. form.AddField("dog_list", dogsJson);
  201. StartCoroutine(WebController.PostRequest(url, form, callback: BathFinishReqCallback));
  202. }
  203. void BathFinishReqCallback(string json)
  204. {
  205. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  206. if (data != null && data["status"].ToString() == "success")
  207. {
  208. // TODO 清除然后重新写入用户道具数据
  209. // 清除然后重新写入狗的数据
  210. DogProperty[] dogProperties = JsonConvert.DeserializeObject<DogProperty[]>(data["dogs"].ToString());
  211. UserProperty.dogs.Clear();
  212. foreach (var dog in dogProperties)
  213. {
  214. UserProperty.dogs.Add(dog);
  215. }
  216. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "dog_is_clean", EnviromentSetting.languageCode });
  217. MessageBoxController.ShowMessage(msg, ()=> SceneManager.LoadScene("Home"));
  218. }
  219. else
  220. {
  221. Debug.Log(data["message"]);
  222. }
  223. }
  224. }
  225. enum TouchMethod
  226. {
  227. mouse,
  228. touch
  229. }