using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Controls; using UnityEngine.SceneManagement; using static UnityEngine.ParticleSystem; /* Bathroom 动作的主要控制模块 * 泡泡阈值达到180个粒子后,认为泡泡阶段结束 * 冲洗10秒后,认为冲洗阶段结束 */ public class BathroomController : MonoBehaviour { public Texture2D nozzleTexture, spongeTexture; // 对应鼠标图像喷头和海绵 // private CursorMode cursorMode = CursorMode.Auto; // 鼠标模式 // private Vector2 cursorHotSpot = new Vector2(-1f, -1f); // 鼠标图标的点击点位置 private Vector2 previousPointerPosition = Vector2.zero; // 前一帧鼠标位置 private Texture2D activatedCursorTexture = null; private float bubbleTotalTime; // 上肥皂的合计时间 private float flushTotalTime; // 冲洗的合计时间 private TouchMethod touchMethod; // 定义触控方式 private string gamePhase; // 目前游戏进度 bubble上肥皂,flush冲洗 // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { Invoke("StartBubbleMsg", 2f); } // Update is called once per frame void FixedUpdate() { // 检测泡泡数量是否达到180,达到180后结束泡泡过程 var bubbleParticleSystem = GameObject.Find("Bubble Particle").GetComponent(); if (bubbleParticleSystem.particleCount > 180) { EndBubblePhase(); } // 检查冲洗时间是否达到规定时间 if (flushTotalTime > 8f) { EndFlushPhase(); } if (gamePhase == "bubble") { PointerOnDog(spongeTexture); }else if (gamePhase == "flush") { PointerOnDog(nozzleTexture); } } void StartBubbleMsg() { Time.timeScale = 0f; string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "start_bubble", EnviromentSetting.languageCode }); MessageBoxController.ShowMessage(msg, StartBubblePhase); } // 检测是否点击在狗上 void PointerOnDog(Texture2D cursorTexture) { DetectTouchMethod(); var dog = GameObject.Find("dog"); // 检查当前指针是否有效 if (Pointer.current == null) return; // 获取当前指针的悬浮位置 Vector2 pointerPosition = Pointer.current.position.ReadValue(); Ray ray = Camera.main.ScreenPointToRay(pointerPosition); if (Physics.Raycast(ray, out RaycastHit hit)) { //Debug.Log($"Clicked on: {hit.collider.gameObject.name}"); // 射线检测起始点击是否在狗上 if (hit.collider.gameObject.name == "dog") { if (previousPointerPosition != pointerPosition) { activatedCursorTexture = cursorTexture; //Cursor.SetCursor(cursorTexture, cursorHotSpot, cursorMode); ActivateParticle(cursorTexture, hit); if (touchMethod == TouchMethod.touch) { Cursor.visible = false; } } previousPointerPosition = pointerPosition; } else { activatedCursorTexture = null ; //Cursor.SetCursor(null, cursorHotSpot, cursorMode); Cursor.visible = true; } } } // 检测控制方式是鼠标还是触控 void DetectTouchMethod() { // 检测鼠标输入 if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame) { touchMethod = TouchMethod.mouse; } // 检测触控输入 if (Touchscreen.current != null && Touchscreen.current.touches.Count > 0) { touchMethod = TouchMethod.touch; } } // 当nozzle鼠标材质被调用时,把bubble particle system位置和鼠标位置绑定 void ActivateParticle(Texture2D selectedCursorTexture, RaycastHit hit) { if (selectedCursorTexture == spongeTexture) { var bubbleParticle = GameObject.Find("Bubble Particle"); var targetPosition = hit.point; targetPosition.x = -3.55f; bubbleParticle.transform.position = targetPosition; bubbleTotalTime += Time.deltaTime; //Debug.Log("bubbleTotalTime:" + bubbleTotalTime.ToString()); }else if (selectedCursorTexture == nozzleTexture) { var bubbleParticle = GameObject.Find("Flush Particle"); var targetPosition = hit.point; targetPosition.x = -3.55f; bubbleParticle.transform.position = targetPosition; flushTotalTime += Time.deltaTime; //Debug.Log("Flush TotalTime:" + flushTotalTime.ToString()); } } // 开始上肥皂过程 void StartBubblePhase() { Time.timeScale = 1.0f; gamePhase = "bubble"; var bubbleSfx = GameObject.Find("Bubble Particle").GetComponent(); bubbleSfx.Play(); } // 监控bubble时间,如果达到10秒,提示结束 void EndBubblePhase() { if (gamePhase == "bubble") { // 如果已经结束泡泡阶段就不能更换洗澡的狗了 var dogSelector = GameObject.Find("Dog Selector"); dogSelector.SetActive(false); gamePhase = null; //Cursor.SetCursor(null, cursorHotSpot, cursorMode); Time.timeScale = 0f; string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "start_flush", EnviromentSetting.languageCode }); MessageBoxController.ShowMessage(msg, StartFlushPhase); var bubbleSfx = GameObject.Find("Bubble Particle").GetComponent(); bubbleSfx.Stop(); // 隐藏泡泡粒子 var bubbleParticle = GameObject.Find("Bubble Particle"); bubbleParticle.transform.position = new Vector3(10, 10, 10); } } // 检测bubble如果达到条件后,开始冲洗 void StartFlushPhase() { Time.timeScale = 1.0f; gamePhase = "flush"; var soundSfx = GameObject.Find("Flush Particle").GetComponent(); soundSfx.Play(); // 喷水后缩短泡沫粒子生命 var bubbleParticle = GameObject.Find("Bubble Particle").GetComponent(); int bubbleParticleCount = bubbleParticle.particleCount; // 获取当前粒子数量 var particles = new ParticleSystem.Particle[bubbleParticleCount]; // 初始化粒子数组 // 获取所有粒子的数据 bubbleParticle.GetParticles(particles); // 修改每个粒子的生命周期 for (int i = 0; i < bubbleParticleCount; i++) { particles[i].remainingLifetime = UnityEngine.Random.Range(0.6f,3f); // 设置新的生命周期 } // 将修改后的粒子数据应用回粒子系统 bubbleParticle.SetParticles(particles); } // 检测flush达到标准后,结束过程 void EndFlushPhase() { if (gamePhase == "flush") { gamePhase = "end"; //Cursor.SetCursor(null, cursorHotSpot, cursorMode); Time.timeScale = 0f; var soundSfx = GameObject.Find("Flush Particle").GetComponent(); soundSfx.Stop(); var flushParticle = GameObject.Find("Flush Particle"); flushParticle.SetActive(false); // TODO 上传道具使用和狗的名单 BathFinishReq(); } } // 提交洗澡道具使用和受影响狗的列表 void BathFinishReq() { Debug.Log("BathFinishReq request"); List dogs = new List(); dogs.Add(UserProperty.dogs[GameData.focusDog].d_id); string dogsJson = JsonConvert.SerializeObject(dogs); string url = "/api/item/use/"; WWWForm form = new(); form.AddField("user_id", UserProperty.userId); form.AddField("item_id", GameData.bathItemId); form.AddField("dog_list", dogsJson); StartCoroutine(WebController.PostRequest(url, form, callback: BathFinishReqCallback)); } void BathFinishReqCallback(string json) { var data = JsonConvert.DeserializeObject>(json); if (data != null && data["status"].ToString() == "success") { // TODO 清除然后重新写入用户道具数据 // 清除然后重新写入狗的数据 // DogProperty[] dogProperties = JsonConvert.DeserializeObject(data["dogs"].ToString()); // UserProperty.dogs.Clear(); // foreach (var dog in dogProperties) // { // UserProperty.dogs.Add(dog); // } string dogs = data["dogs"].ToString(); UserProperty.FreshDogInfo(dogs); string props = data["props"].ToString(); UserProperty.FreshUserItems(props); string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "dog_is_clean", EnviromentSetting.languageCode }); // MessageBoxController.ShowMessage(msg, ()=> SceneManager.LoadScene("Home")); Time.timeScale = 1.0f; // 恢复时间缩放 MessageBoxController.ShowMessage(msg, () => MaskTransitions.TransitionManager.Instance.LoadLevel("Home")); } else { Debug.Log(data["message"]); } } } enum TouchMethod { mouse, touch }