HomeController.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. using Cinemachine;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEngine.InputSystem;
  8. /* 本代码控制室内场景
  9. * 控制宠物在Home场景动画
  10. * !!!特别注意:Dog Initializer 必须挂载在同一个组件下,并且必须在本组价下方。确保比本组件先执行
  11. * 主要调节参数在FixedUpdate代码段里面
  12. * 提示用户注册
  13. * SetDogsIsTrigger 正常模式下为true。交互模式和进食模式下为false。
  14. */
  15. public class HomeController : MonoBehaviour
  16. {
  17. public static HomeController Instance;
  18. public static List<DogInScene> dogsInScene = new List<DogInScene>();
  19. public static bool listenBreak = false; // 当按下说话按键后,所有狗停止行动,立刻切换到监听状态。
  20. public static CinemachineVirtualCamera playerCam, dogCam;
  21. public static DateTime lastCameraChange;
  22. private bool isSleepChecked = false; // 用于检测第一次睡眠检测是否执行完成
  23. // Start is called once before the first execution of Update after the MonoBehaviour is created
  24. private GameObject centerOfDogs;
  25. private SceneMode sceneMode = SceneMode.NORMAL; // 当前场景处在的模式
  26. //private bool isInteractMode = false; // 场景是否在交互状态
  27. private Vector2 previousPointerPosition = Vector2.zero; // 前一帧鼠标位置
  28. private GameObject interactDog; // 交互的狗
  29. float interactTime = 0f; // 交互时间
  30. //private bool isTrainingMode = false; // 是否在训练状态
  31. private string trainingContent = String.Empty; // 训练内容 _xx 对于language.json 0x开始提示 1x成功提示 2x失败提示
  32. private int totalTrainingTimes = 2; // 训练总次数
  33. private int currentTrainingTimes = 0; // 当前训练次数
  34. private string trainingDogId = ""; // 训练的狗id
  35. private bool isTrainingMsgShowed_1 = false; // 第一条是否已经显示训练提示
  36. private bool isTrainingMsgShowed_2 = false; // 第二条是否已经显示训练提示
  37. private bool isTrainingAnimationPlayed = false; // 训练动画是否已经播放
  38. public bool isHideBowlRunning = false; // 是否正在运行隐藏碗的协程
  39. private void Awake()
  40. {
  41. if (Instance == null)
  42. {
  43. Instance = this;
  44. //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行
  45. }
  46. else
  47. {
  48. Destroy(gameObject);
  49. }
  50. }
  51. void Start()
  52. {
  53. Time.timeScale = 1; // 确保时间流逝正常
  54. dogsInScene.Clear(); // dogsInScene 是静态,每次启动要清空
  55. lastCameraChange = DateTime.Now;
  56. playerCam = GameObject.Find("VCam Player").GetComponent<CinemachineVirtualCamera>();
  57. dogCam = GameObject.Find("VCam Dog").GetComponent<CinemachineVirtualCamera>();
  58. centerOfDogs = GameObject.Find("CenterOfDogs");
  59. //InitialScene();
  60. StartCoroutine(InitialScene());
  61. }
  62. // Update is called once per frame
  63. void FixedUpdate()
  64. {
  65. if (SceneInitialCheck()) // 确保狗读取成功后执行代码
  66. {
  67. // 计算多只狗的中心位置,用于主摄像机瞄准
  68. centerOfDogs.transform.position = CenterOfDogs();
  69. if (!isSleepChecked) // 每次启动检测只进行一次是否进入睡眠
  70. {
  71. // 判断是否在睡觉时间
  72. DateTime dateTime = DateTime.Now;
  73. foreach (var dog in dogsInScene)
  74. {
  75. if (dateTime.Hour >= 22 || dateTime.Hour <= 5) // 深夜模式,狗默认在睡觉状态
  76. {
  77. dog.Sleep();
  78. }
  79. else if (dog.dogProperty.stamina <= 10) { dog.Sleep(); } // 狗体力太低了,进入睡觉模式
  80. else
  81. {
  82. dog.IdleAnimation();
  83. }
  84. }
  85. isSleepChecked = true;
  86. }
  87. #region 场景动画主循环
  88. // 检测狗是否被撞翻,如果是,立刻翻回来
  89. foreach (var dog in dogsInScene)
  90. {
  91. Quaternion curRotation = dog.gameObject.transform.rotation;
  92. if (curRotation.x != 0)
  93. {
  94. curRotation.x = 0;
  95. }
  96. if (curRotation.z != 0)
  97. {
  98. curRotation.z = 0;
  99. }
  100. dog.gameObject.transform.rotation = curRotation;
  101. }
  102. // 生成一个数据数用于随机开启动画,如果和狗的randomFactor相同就开启动画
  103. int sceneRandomFactor = UnityEngine.Random.Range(0, 51);
  104. // 检测是否有狗没有通过voiceCall训练,如果有,立刻进入训练模式
  105. if (sceneMode == SceneMode.NORMAL) // 这段代码用于在NORMAL场景下检测是否有狗进入训练模式
  106. {
  107. trainingContent = String.Empty;
  108. foreach (var dog in dogsInScene)
  109. {
  110. if (trainingContent != String.Empty) { break; } // 如果已经有狗进入训练模式,跳出循环
  111. if (!dog.dogProperty.voiceCallEnable)
  112. {
  113. trainingContent = "voiceCall";
  114. }
  115. if (trainingContent != String.Empty)
  116. {
  117. sceneMode = SceneMode.TRAINING;
  118. dog.RemoveZzzParticle();
  119. trainingDogId = dog.dogProperty.d_id;
  120. totalTrainingTimes = 2;
  121. currentTrainingTimes = 0;
  122. GameData.focusDog = dogsInScene.IndexOf(dog);
  123. //GameData.focusDog = UserProperty.GetDogIndexById(dog.dogProperty.d_id);
  124. dogsInScene[GameData.focusDog].SetupInteract();
  125. interactDog = dogsInScene[GameData.focusDog].gameObject;
  126. // GameData.isVoiceTrainingToday = true;
  127. GameData.SetIsVoiceTrainingToday(); // 设置当天已经完成了语音训练
  128. VoiceButtonOnlySwitch(true); // 交互模式下关闭其他菜单
  129. }
  130. }
  131. foreach (var dog in dogsInScene)
  132. {
  133. if (trainingContent != String.Empty) { break; } // 如果已经有狗进入训练模式,跳出循环
  134. if (dog.dogProperty.voiceCallEnable && !GameData.isVoiceTrainingToday)
  135. {
  136. if (dog.dogProperty.voiceCall >= 40 && dog.dogProperty.CommandTrainingPhase() < 4)
  137. {
  138. // 当狗的voiceCall大于等于40,进入第一阶段指令训练模式
  139. int random = UnityEngine.Random.Range(0, 100);
  140. if (GameTool.IntBetween(0, 25, random) && !dog.dogProperty.commandSit)
  141. {
  142. trainingContent = "commandSit";
  143. }
  144. else if (GameTool.IntBetween(25, 50, random) && !dog.dogProperty.commandStand)
  145. {
  146. trainingContent = "commandStand";
  147. }
  148. else if (GameTool.IntBetween(50, 75, random) && !dog.dogProperty.commandBark)
  149. {
  150. trainingContent = "commandBark";
  151. }
  152. else if (GameTool.IntBetween(75, 100, random) && !dog.dogProperty.commandLieDown)
  153. {
  154. trainingContent = "commandLieDown";
  155. }
  156. else
  157. {
  158. // GameData.isVoiceTrainingToday = true;
  159. GameData.SetIsVoiceTrainingToday(); // 设置当天已经完成了语音训练
  160. }
  161. }
  162. else if (dog.dogProperty.voiceCommand >= 40 && dog.dogProperty.CommandTrainingPhase() >= 4)
  163. {
  164. // 当狗的voiceCommand大于等于40并且完成第一阶段所有训练,进入第二阶段指令训练模式
  165. int random = UnityEngine.Random.Range(0, 100);
  166. if (GameTool.IntBetween(0, 20, random) && !dog.dogProperty.commandShake)
  167. {
  168. trainingContent = "commandShake";
  169. }
  170. else if (GameTool.IntBetween(20, 40, random) && !dog.dogProperty.commandTouch)
  171. {
  172. trainingContent = "commandTouch";
  173. }
  174. else if (GameTool.IntBetween(40, 60, random) && !dog.dogProperty.commandDeath)
  175. {
  176. trainingContent = "commandDeath";
  177. }
  178. else if (GameTool.IntBetween(60, 80, random) && !dog.dogProperty.commandTurnL)
  179. {
  180. trainingContent = "commandTurnL";
  181. }
  182. else if (GameTool.IntBetween(80, 100, random) && !dog.dogProperty.commandTurnR)
  183. {
  184. trainingContent = "commandTurnR";
  185. }
  186. }
  187. }
  188. if (trainingContent != String.Empty)
  189. {
  190. sceneMode = SceneMode.TRAINING;
  191. dog.RemoveZzzParticle();
  192. trainingDogId = dog.dogProperty.d_id;
  193. totalTrainingTimes = 2;
  194. currentTrainingTimes = 0;
  195. GameData.focusDog = dogsInScene.IndexOf(dog);
  196. //GameData.focusDog = UserProperty.GetDogIndexById(dog.dogProperty.d_id);
  197. dogsInScene[GameData.focusDog].SetupInteract();
  198. interactDog = dogsInScene[GameData.focusDog].gameObject;
  199. GameData.SetIsVoiceTrainingToday(); // 设置当天已经完成了语音训练
  200. VoiceButtonOnlySwitch(true); // 交互模式下关闭其他菜单
  201. }
  202. }
  203. }
  204. if (this.sceneMode == SceneMode.TRAINING) // 这段代码控制场景进入训练模式
  205. {
  206. foreach (var dog in dogsInScene)
  207. {
  208. if (dog.dogState == DogState.INTERACT || dog.dogState == DogState.TRAINING)
  209. {
  210. dogCam.m_LookAt = dog.gameObject.transform; // 摄像机看向交互的狗
  211. dogCam.Priority = 10;
  212. // 单只狗在交互的状态控制代码
  213. if (dog.isMovingToPlayer)
  214. {
  215. dog.MovetoPlayer();
  216. }
  217. else
  218. {
  219. // 开始训练时让狗播放动画
  220. if (trainingContent != "voiceCall" && !isTrainingAnimationPlayed)
  221. {
  222. string command = trainingContent.Substring(7);
  223. dog.animator.SetTrigger(command);
  224. dog.animator.SetBool(command + "_status", true);
  225. isTrainingAnimationPlayed = true;
  226. }
  227. // 狗完成移动后,开始进入正式训练交互模式
  228. dog.dogState = DogState.TRAINING;
  229. if (!isTrainingMsgShowed_1 && !isTrainingMsgShowed_2 && currentTrainingTimes == 0)
  230. {
  231. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", trainingContent + "_00", EnviromentSetting.languageCode });
  232. if (msg.Contains("<<dog_name>>"))
  233. {
  234. msg = msg.Replace("<<dog_name>>", dog.dogProperty.dog_name);
  235. }
  236. MessageBoxController.ShowMessage(msg);
  237. var BGM = GameObject.Find("BGM");
  238. if (BGM != null)
  239. {
  240. FadeBGM(BGM.GetComponent<AudioSource>(), false);
  241. }
  242. isTrainingMsgShowed_1 = true;
  243. }
  244. else if (!isTrainingMsgShowed_2 && isTrainingMsgShowed_1 && currentTrainingTimes == 1)
  245. {
  246. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", trainingContent + "_01", EnviromentSetting.languageCode });
  247. if (msg.Contains("<<dog_name>>"))
  248. {
  249. msg = msg.Replace("<<dog_name>>", dog.dogProperty.dog_name);
  250. }
  251. MessageBoxController.ShowMessage(msg);
  252. isTrainingMsgShowed_2 = true;
  253. }
  254. }
  255. }
  256. else
  257. {
  258. // 暂时将其他非训练状态的狗game object隐藏
  259. //dog.gameObject.SetActive(false);
  260. }
  261. }
  262. }
  263. else if (sceneMode == SceneMode.INACTIVE) // 这段代码控制场景在交互模式
  264. {
  265. foreach (var dog in dogsInScene)
  266. {
  267. if (dog.dogState == DogState.INTERACT)
  268. {
  269. dogCam.m_LookAt = dog.gameObject.transform; // 摄像机看向交互的狗
  270. dogCam.Priority = 10;
  271. // 单只狗在交互的状态控制代码
  272. if (dog.isMovingToPlayer)
  273. {
  274. dog.MovetoPlayer();
  275. }
  276. else if (dog.InteractTimeout()) // 如果交互时间结束,结束交互状态
  277. {
  278. dog.ExitInteract();
  279. sceneMode = SceneMode.NORMAL; // 交互结束,退出交互状态
  280. //isInteractMode = false; // 交互结束,退出交互状态
  281. VoiceButtonOnlySwitch(false); // 交互结束,打开其他菜单
  282. }
  283. else
  284. {
  285. PointerOnDog(); // 检测是否点击在狗上
  286. }
  287. }
  288. }
  289. }
  290. else
  291. {
  292. // 普通场景下的控制代码
  293. foreach (var dog in dogsInScene)
  294. {
  295. // 如果在eat drink进程结束前不执行随机场景代码
  296. // 恢复因为交互,训练模式隐藏的狗
  297. //if (dog.gameObject.activeSelf == false)
  298. //{
  299. // dog.gameObject.SetActive(true);
  300. //}
  301. if (dog.dogState == DogState.ITEM_CONSUME)
  302. {
  303. if (dog.isMovingToBowl)
  304. {
  305. dog.MovetoBowl();
  306. }
  307. }
  308. else
  309. {
  310. // 随机动作控制控制
  311. RandomCameraChange();
  312. if (listenBreak) // 如果用户按下说话按键,立刻切换到监听状态
  313. {
  314. dog.Listen();
  315. // StartCoroutine(dog.RotationToPlayerAndListen());
  316. }
  317. else if (dog.isMoving)
  318. {
  319. dog.RandomMove();
  320. }
  321. else if (sceneRandomFactor == dog.randomFactor && !dog.isSleeping) // 当狗自身的随机数和系统随机数相同时候触发。约100秒触发一次。
  322. {
  323. TimeSpan ts = DateTime.Now - dog.animationStartTime;
  324. if (ts.Seconds >= 30) // 如果距离上一个动作超过30秒就可以开始新的动作
  325. {
  326. float r = UnityEngine.Random.Range(0, 1f);
  327. if (r > 0.6) // 随机选择开始动画,或者移动
  328. {
  329. dog.IdleAnimation();
  330. }
  331. else // 狗狗开始步行移动
  332. {
  333. dog.SetMoveSpeed(0);
  334. dog.moveSpeed = 0;
  335. dog.RandomMove();
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. #endregion
  343. }
  344. }
  345. private void OnDestroy()
  346. {
  347. Debug.Log("Home scene is destoried.");
  348. }
  349. IEnumerator InitialScene()
  350. {
  351. yield return null; // 跳过三帧,初始化最多三只狗
  352. //Debug.Log(isInitialDone);
  353. foreach (var dog in UserProperty.dogs)
  354. {
  355. DogInScene dogInScene = new DogInScene(dog);
  356. float x = UnityEngine.Random.Range(-1f, 1f); // 随机生成位置,考虑到手机评估宽度限制宽度
  357. float z = UnityEngine.Random.Range(0f, 5f); // 随机生成位置,考虑且保持和用户距离
  358. float y = UnityEngine.Random.Range(90f, 270f); // 随机生成朝向,且面向用户
  359. var initPosition = new Vector3(x, 0, z);
  360. StartCoroutine(DogComponentInstaller(dog)); // 加载狗的其他组件
  361. var dogGameObject = GameObject.Find(dog.dog_name);
  362. if (dogGameObject == null)
  363. {
  364. Debug.Log(dog.dog_name + "is not found in Home Controller");
  365. }
  366. dogGameObject.transform.position = initPosition;
  367. dogGameObject.transform.rotation = Quaternion.Euler(0, y, 0);
  368. dogGameObject.transform.localScale = new Vector3(2, 2, 2);
  369. dogInScene.SetGameObject(dogGameObject);
  370. dogsInScene.Add(dogInScene);
  371. }
  372. }
  373. // 加载狗的其他组件
  374. IEnumerator DogComponentInstaller(DogProperty dogProperty)
  375. {
  376. // 等待一帧,确保所有 Start() 方法都执行完成
  377. yield return null;
  378. // 第一帧以后开始执行
  379. GameObject dog = GameObject.Find(dogProperty.dog_name);
  380. // 加载指定的Animator controller
  381. Animator animator = dog.GetComponent<Animator>();
  382. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "home_default"));
  383. animator.runtimeAnimatorController = animatorController;
  384. // 加载Rigidbody
  385. Rigidbody rigidbody = dog.AddComponent<Rigidbody>();
  386. //rigidbody.isKinematic = true;
  387. rigidbody.mass = 10;
  388. rigidbody.linearDamping = 10;
  389. rigidbody.angularDamping = 10;
  390. //rigidbody.freezeRotation = true;
  391. rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;
  392. rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
  393. rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
  394. // 加载box collider
  395. BoxCollider boxCollider = dog.AddComponent<BoxCollider>();
  396. // boxCollider.isTrigger = false;
  397. boxCollider.center = new Vector3(0, 0.25f, 0);
  398. boxCollider.size = new Vector3(0.12f, 0.45f, 0.54f);
  399. boxCollider.isTrigger = true;
  400. // 加载Particle Question Mark
  401. ParticleSystem questionMarkParticle = Resources.Load<ParticleSystem>("Home/Particle_QuestionMark");
  402. questionMarkParticle = Instantiate(questionMarkParticle);
  403. questionMarkParticle.name = "QuestionMarkParticle";
  404. questionMarkParticle.transform.SetParent(dog.transform);
  405. questionMarkParticle.transform.localPosition = new Vector3(0, 0.6f, 0.4f);
  406. questionMarkParticle.transform.localRotation = Quaternion.Euler(-90, 0, 0);
  407. ParticleSystem ps = questionMarkParticle.GetComponent<ParticleSystem>();
  408. ps.Stop();
  409. // 加载sleep particle
  410. ParticleSystem zzzParticle = Resources.Load<ParticleSystem>("Home/Particle_Z");
  411. zzzParticle = Instantiate(zzzParticle);
  412. zzzParticle.name = "zzzParticle";
  413. zzzParticle.transform.SetParent(dog.gameObject.transform);
  414. zzzParticle.transform.localPosition = new Vector3(0.05f, 0.2f, 0.2f);
  415. zzzParticle.transform.localRotation = Quaternion.Euler(-90, 0, 0);
  416. zzzParticle.gameObject.SetActive(false); // 默认关闭
  417. // 添加DogCollisionController
  418. DogCollisionController dogCollisionController = dog.AddComponent<DogCollisionController>();
  419. //yield return null;
  420. }
  421. #region 语音控制区
  422. // 用户语音呼唤上传,Voice call指令用于呼唤所有的狗,得分最高的过来进入交互模式
  423. public void VoiceCallRequest(string filePath)
  424. {
  425. Debug.Log("Voice Call Post request");
  426. string url = "/api/voice/call/";
  427. WWWForm form = new();
  428. form.AddField("user_id", UserProperty.userId);
  429. StartCoroutine(WebController.PostRequest(url, form, filePath, callback: VoiceCallCallback));
  430. }
  431. // 语音呼唤上传回调函数
  432. void VoiceCallCallback(string json)
  433. {
  434. Debug.Log("Voice call callback");
  435. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  436. if (data != null && data["status"].ToString() == "success")
  437. {
  438. // 刷新狗的数据
  439. string dogJson = data["dogs"].ToString();
  440. UserProperty.RefreshDogInfo(dogJson);
  441. // 根据返回结果设定focusdog
  442. float highestScore = 0;
  443. string highestScoreDogId = String.Empty;
  444. var scores = data["call Score MFCC"].ToString();
  445. // var scores = data["call Score Xunfei"].ToString();
  446. var scoresList = JsonConvert.DeserializeObject<Dictionary<string, float>>(scores);
  447. foreach (var score in scoresList)
  448. {
  449. // 根据狗的数量度修正得分。计算方式为狗的voiceCall属性值/1000
  450. int dogIndex = UserProperty.GetDogIndexById(score.Key);
  451. if (dogIndex < 0)
  452. {
  453. continue; // 如果找不到狗的索引,跳过
  454. }
  455. float scoreFactor = UserProperty.dogs[dogIndex].voiceCall / 1000f; // 狗的voiceCall属性值/1000计算加成
  456. float adjScore = score.Value + scoreFactor;
  457. if (adjScore > 1)
  458. {
  459. adjScore = 1;
  460. }
  461. if (adjScore > highestScore)
  462. {
  463. highestScore = adjScore;
  464. highestScoreDogId = score.Key;
  465. }
  466. }
  467. if (highestScore >= EnviromentSetting.voiceRecognitionScore) // 60分以上才可以进入交互模式
  468. {
  469. GameData.focusDog = UserProperty.GetDogIndexById(highestScoreDogId);
  470. sceneMode = SceneMode.INACTIVE; // 交互模式
  471. VoiceButtonOnlySwitch(true); // 交互模式下关闭其他菜单
  472. foreach (var dog in dogsInScene)
  473. {
  474. if (dog.dogProperty.d_id == highestScoreDogId)
  475. {
  476. // if (GameTool.Random100Check(dog.dogProperty.voiceCall))
  477. // {
  478. // dog.SetupInteract();
  479. interactDog = dog.gameObject;
  480. // focusdog 开启互动模式
  481. // HomeController.dogsInScene[GameData.focusDog].dogState = DogState.INTERACT;
  482. HomeController.dogsInScene[GameData.focusDog].SetupInteract();
  483. // 其他狗进入隐藏模式(先保留代码)
  484. //foreach (var otherDog in dogsInScene)
  485. //{
  486. // if (otherDog.dogProperty.d_id != highestScoreDogId)
  487. // {
  488. // otherDog.gameObject.SetActive(false);
  489. // }
  490. //}
  491. }
  492. }
  493. HomeSoundEffectController.Instance.PlaySoundEffect(5);
  494. }
  495. else
  496. {
  497. HomeSoundEffectController.Instance.PlaySoundEffect(4);
  498. }
  499. }
  500. else
  501. {
  502. Debug.Log(data["message"]);
  503. }
  504. }
  505. // 用户语音呼唤上传,Voice call指令用于呼唤所有的狗,得分最高的过来进入交互模式
  506. public void VoiceCommandRequest(string filePath)
  507. {
  508. //Debug.Log("Voice Command Post request");
  509. if (sceneMode == SceneMode.INACTIVE)
  510. {
  511. Debug.Log("Voice Command Post request");
  512. string url = "/api/voice/command/";
  513. WWWForm form = new();
  514. form.AddField("dog_id", UserProperty.dogs[GameData.focusDog].d_id);
  515. form.AddField("user_id", UserProperty.userId);
  516. StartCoroutine(WebController.PostRequest(url, form, filePath, callback: VoiceCommandCallback));
  517. // 打印时间
  518. Debug.Log("Voice command training request at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  519. }
  520. else if (sceneMode == SceneMode.TRAINING)
  521. {
  522. //Debug.Log("current times before ++:" + this.currentTrainingTimes.ToString());
  523. this.currentTrainingTimes++;
  524. string url = "/api/voice/training/";
  525. WWWForm form = new();
  526. form.AddField("dog_id", UserProperty.dogs[GameData.focusDog].d_id);
  527. form.AddField("user_id", UserProperty.userId);
  528. form.AddField("voice_type", trainingContent);
  529. form.AddField("current_times", this.currentTrainingTimes);
  530. //Debug.Log("current times after ++:" + this.currentTrainingTimes.ToString());
  531. form.AddField("total_times", totalTrainingTimes);
  532. StartCoroutine(WebController.PostRequest(url, form, filePath, callback: VoiceCommandCallback));
  533. }
  534. }
  535. // 语音呼唤上传回调函数
  536. void VoiceCommandCallback(string json)
  537. {
  538. // 打印返回时间
  539. Debug.Log("Voice command callback at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  540. if (sceneMode == SceneMode.INACTIVE)
  541. {
  542. Debug.Log("Voice command callback");
  543. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  544. if (data != null && data["status"].ToString() == "success")
  545. {
  546. // 刷新狗的数据
  547. string dogJson = data["dogs"].ToString();
  548. UserProperty.RefreshDogInfo(dogJson);
  549. // 找到得分最高的指令
  550. float highestScore = 0;
  551. string highestScoreCommand = "";
  552. string scores = data["commandScoreMFCC"].ToString();
  553. // string scores = data["commandScoreXunfei"].ToString();
  554. var scoresList = JsonConvert.DeserializeObject<Dictionary<string, float>>(scores);
  555. foreach (var score in scoresList)
  556. {
  557. if (score.Value > highestScore)
  558. {
  559. highestScore = score.Value;
  560. highestScoreCommand = score.Key;
  561. }
  562. }
  563. // 根据狗的voiceCommand属性值修正得分。计算方式为狗的voiceCommand属性值/1000
  564. highestScore += UserProperty.dogs[GameData.focusDog].voiceCommand / 1000f;
  565. if (highestScore > 1)
  566. {
  567. highestScore = 1;
  568. }
  569. Debug.Log("Highest score:" + highestScore.ToString());
  570. Debug.Log("Highest command:" + highestScoreCommand);
  571. dogsInScene[GameData.focusDog].ResetAnimationStatus(); // 重置狗的动画状态
  572. if (highestScore >= EnviromentSetting.voiceRecognitionScore)
  573. {
  574. string animationTrigger = highestScoreCommand.Substring(7);
  575. string animationBool = animationTrigger + "_status";
  576. var animator = dogsInScene[GameData.focusDog].gameObject.GetComponent<Animator>();
  577. if (highestScoreCommand == "commandBark")
  578. {
  579. DogBarkController.Instance.PlayDogBarkWithDelay(3); // 狗叫相应一下
  580. }
  581. else
  582. {
  583. HomeSoundEffectController.Instance.PlaySoundEffect(5);
  584. }
  585. //animator.SetTrigger(animationTrigger);
  586. animator.Play(animationTrigger);
  587. string[] noStatusCommand = { "turnL", "turnR" };
  588. if (Array.IndexOf(noStatusCommand, animationTrigger) < 0)
  589. {
  590. animator.SetBool(animationBool, true);
  591. Debug.Log(animationBool + " is " + animator.GetBool(animationBool));
  592. }
  593. else
  594. {
  595. // turnL 和 turnR 动画不需要设置状态
  596. }
  597. // animator.SetBool(animationBool, true);
  598. // 交互动画执行一段时间后停止
  599. dogsInScene[GameData.focusDog].interactAnimationStartTime = DateTime.Now;
  600. StartCoroutine(dogsInScene[GameData.focusDog].InteractAnimationCountDown());
  601. }
  602. else
  603. {
  604. HomeSoundEffectController.Instance.PlaySoundEffect(4);
  605. dogsInScene[GameData.focusDog].PlayQuestionMark();
  606. }
  607. }
  608. else
  609. {
  610. Debug.Log(data["message"]);
  611. }
  612. }
  613. else if (sceneMode == SceneMode.TRAINING)
  614. {
  615. Debug.Log("Voice training Callback");
  616. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  617. if (data != null && data["status"].ToString().ToLower() == "success")
  618. {
  619. if (data["message"].ToString().ToLower() == "pass")
  620. {
  621. // 刷新狗的数据,包含dogsInScene
  622. string dogJson = data["dogs"].ToString();
  623. UserProperty.RefreshDogInfo(dogJson);
  624. var trainingDog = dogsInScene[GameData.focusDog];
  625. trainingDog.ReloadDogProperty(); // 刷新狗的数据
  626. // 成功后让狗子播放训练的动画
  627. if (trainingContent != "voiceCall")
  628. {
  629. string command = trainingContent.Substring(7);
  630. trainingDog.animator.SetTrigger(command);
  631. // string[] noStatusCommand = { "apple", "banana", "cherry" };
  632. trainingDog.animator.SetBool(command + "_status", true);
  633. }
  634. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", trainingContent + "_10", EnviromentSetting.languageCode });
  635. if (msg.Contains("<<dog_name>>"))
  636. {
  637. msg = msg.Replace("<<dog_name>>", interactDog.name);
  638. }
  639. GameTool.PauseGameTime();
  640. MessageBoxController.ShowMessage(msg, ExitTrainingMode);
  641. this.sceneMode = SceneMode.NORMAL;
  642. VoiceButtonOnlySwitch(false); // 交互结束,打开其他菜单
  643. GameData.SetIsVoiceTrainingToday(); // 设置当天已经完成了语音训练
  644. }
  645. else if (data["message"].ToString() == "fail")
  646. {
  647. HomeSoundEffectController.Instance.PlaySoundEffect(4);
  648. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", trainingContent + "_20", EnviromentSetting.languageCode });
  649. if (msg.Contains("<<dog_name>>"))
  650. {
  651. msg = msg.Replace("<<dog_name>>", interactDog.name);
  652. }
  653. GameTool.PauseGameTime();
  654. MessageBoxController.ShowMessage(msg, RestartTraining);
  655. }
  656. }
  657. else
  658. {
  659. Debug.Log(data["message"]);
  660. if (EnviromentSetting.runEnv == "Release")
  661. {
  662. HomeSoundEffectController.Instance.PlaySoundEffect(4);
  663. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", trainingContent + "_20", EnviromentSetting.languageCode });
  664. if (msg.Contains("<<dog_name>>"))
  665. {
  666. msg = msg.Replace("<<dog_name>>", interactDog.name);
  667. }
  668. GameTool.PauseGameTime();
  669. MessageBoxController.ShowMessage(msg, RestartTraining);
  670. }
  671. }
  672. SetDogsIsTrigger(true);
  673. }
  674. }
  675. #endregion
  676. #region interact mode
  677. // 重置training相关参数
  678. private void ExitTrainingMode()
  679. {
  680. foreach (var dog in dogsInScene)
  681. {
  682. if (dog.dogProperty.d_id == trainingDogId)
  683. {
  684. dog.ExitInteract();
  685. }
  686. }
  687. trainingContent = String.Empty;
  688. totalTrainingTimes = 2;
  689. currentTrainingTimes = 0;
  690. trainingDogId = "";
  691. isTrainingMsgShowed_1 = false;
  692. isTrainingMsgShowed_2 = false;
  693. isTrainingAnimationPlayed = false;
  694. sceneMode = SceneMode.NORMAL; // 交互模式
  695. Debug.Log("Reset Training Mode Parameters");
  696. GameTool.ResumeGameTime();
  697. GameData.SetIsVoiceTrainingToday(); // 训练完成,设置为true
  698. var BGM = GameObject.Find("BGM");
  699. if (BGM != null)
  700. {
  701. FadeBGM(BGM.GetComponent<AudioSource>(), true);
  702. }
  703. }
  704. private void RestartTraining()
  705. {
  706. currentTrainingTimes = 0;
  707. isTrainingMsgShowed_1 = false;
  708. isTrainingMsgShowed_2 = false;
  709. Time.timeScale = 1f;
  710. }
  711. // 改变Voice And Menu 菜单形态
  712. public void VoiceButtonOnlySwitch(bool state)
  713. {
  714. // 交互时候关闭其他菜单
  715. var vamUI = GameObject.Find("VoiceAndMenu");
  716. if (vamUI != null)
  717. {
  718. var UIdocument = vamUI.transform.Find("UIDocument").gameObject;
  719. var voiceController = UIdocument.GetComponent<VoiceController>();
  720. voiceController.isCommandMode = state;
  721. }
  722. }
  723. // 检测是否点击在狗上
  724. void PointerOnDog()
  725. {
  726. //DetectTouchMethod();
  727. // 检查当前指针是否有效
  728. if (Pointer.current == null) return;
  729. // 获取当前指针的悬浮位置
  730. Vector2 pointerPosition = Pointer.current.position.ReadValue();
  731. var mainCamera = GameObject.Find("Camera").GetComponent<Camera>();
  732. Ray ray = mainCamera.ScreenPointToRay(pointerPosition);
  733. if (Physics.Raycast(ray, out RaycastHit hit))
  734. {
  735. //Debug.Log($"Clicked on: {hit.collider.gameObject.name}");
  736. // 射线检测起始点击是否在狗上
  737. if (hit.collider.gameObject == interactDog)
  738. {
  739. if (previousPointerPosition != pointerPosition)
  740. {
  741. interactTime += Time.deltaTime;
  742. Debug.Log("interactTime:" + interactTime);
  743. foreach (var dog in dogsInScene)
  744. {
  745. if (dog.gameObject == interactDog)
  746. {
  747. dog.interactLastUpdate = DateTime.Now;
  748. }
  749. }
  750. previousPointerPosition = pointerPosition;
  751. }
  752. }
  753. if (interactTime > 2.5) // 如果交互时间超过1秒,播放心形粒子效果
  754. {
  755. HeartParticlePlay();
  756. DogBarkController.Instance.PlayDogBarkWithDelay(1); // 狗叫相应一下
  757. }
  758. }
  759. }
  760. void HeartParticlePlay()
  761. {
  762. // 播放心形粒子效果
  763. var heartParticle = GameObject.Find("Particle Heart");
  764. heartParticle.GetComponent<ParticleSystem>().Play();
  765. interactTime = 0;
  766. }
  767. public void SetInteractDog(GameObject dog)
  768. {
  769. interactDog = dog;
  770. dogCam.m_LookAt = dog.gameObject.transform; // 摄像机看向交互的狗
  771. dogCam.Priority = 10;
  772. }
  773. #endregion
  774. #region 场景环境控制
  775. // 淡入或淡出背景音乐
  776. private void FadeBGM(AudioSource bgmSource, bool fadeIn, float duration = 2f)
  777. {
  778. // fadeIn: true表示淡入 false表示淡出
  779. if (bgmSource == null) return; // 如果没有 AudioSource,则直接返回
  780. StartCoroutine(FadeBGMCoroutine(bgmSource, fadeIn, duration));
  781. }
  782. private IEnumerator FadeBGMCoroutine(AudioSource bgmSource, bool fadeIn, float duration)
  783. {
  784. float elapsedTime = 0f;
  785. float startVolume = bgmSource.volume;
  786. float targetVolume = fadeIn ? 0.4f : 0f; // 淡入目标音量为1,淡出目标音量为0
  787. while (elapsedTime < duration)
  788. {
  789. bgmSource.volume = Mathf.Lerp(startVolume, targetVolume, elapsedTime / duration);
  790. elapsedTime += Time.deltaTime;
  791. yield return null;
  792. }
  793. bgmSource.volume = targetVolume; // 确保音量达到目标值
  794. if (!fadeIn)
  795. {
  796. bgmSource.Stop(); // 如果是淡出,停止播放
  797. }
  798. else
  799. {
  800. bgmSource.Play(); // 如果是淡入,开始播放
  801. }
  802. }
  803. // 设置场景模式
  804. public void SetSceneMode(SceneMode mode)
  805. {
  806. this.sceneMode = mode;
  807. }
  808. // 刷新dogInScene的狗数据
  809. public void RefreshDogInScene()
  810. {
  811. foreach (var dog in dogsInScene)
  812. {
  813. dog.ReloadDogProperty();
  814. }
  815. }
  816. // 场景随机切换镜头看向不同的狗
  817. void RandomCameraChange()
  818. {
  819. int delay = 10; // 延迟10秒执行一次
  820. TimeSpan ts = DateTime.Now - lastCameraChange;
  821. if (ts.TotalSeconds < delay) { return; }
  822. int dogCount = dogsInScene.Count;
  823. int r = UnityEngine.Random.Range(0, dogCount + 1);
  824. if (r < dogCount)
  825. {
  826. dogCam.m_LookAt = dogsInScene[r].gameObject.transform;
  827. dogCam.Priority = 10;
  828. playerCam.Priority = 1;
  829. }
  830. else
  831. {
  832. dogCam.Priority = 1;
  833. playerCam.Priority = 10;
  834. }
  835. lastCameraChange = DateTime.Now;
  836. }
  837. // 检测场景是否初始化完成
  838. bool SceneInitialCheck()
  839. {
  840. bool initDone = true;
  841. if (dogsInScene.Count == UserProperty.dogs.Count) // 检测是否所有狗都被加载
  842. {
  843. foreach (var dog in dogsInScene)
  844. {
  845. if (dog.gameObject.GetComponent<Animator>().runtimeAnimatorController == null)
  846. {
  847. initDone = false;
  848. }
  849. }
  850. }
  851. else
  852. {
  853. initDone = false;
  854. }
  855. //Debug.Log("Home scene initial status:"+initDone);
  856. return initDone;
  857. }
  858. // 计算多只狗的中心位置,用于主摄像机瞄准
  859. private Vector3 CenterOfDogs()
  860. {
  861. if (dogsInScene.Count == 0)
  862. {
  863. return Vector3.zero;
  864. }
  865. Vector3 center = Vector3.zero;
  866. foreach (var dog in dogsInScene)
  867. {
  868. center += dog.gameObject.transform.position;
  869. }
  870. center /= dogsInScene.Count;
  871. return center;
  872. }
  873. // 设置所有狗is trigger属性
  874. public void SetDogsIsTrigger(bool triggerSetting)
  875. {
  876. foreach (var dog in dogsInScene)
  877. {
  878. BoxCollider boxCollider = dog.gameObject.GetComponent<BoxCollider>();
  879. if (boxCollider != null)
  880. {
  881. boxCollider.isTrigger = triggerSetting;
  882. }
  883. }
  884. }
  885. #endregion
  886. #region 道具使用控制
  887. public IEnumerator HideBowlAfterItemConsume(ItemGroup type)
  888. {
  889. isHideBowlRunning = true; // 设置为正在运行状态
  890. string inTheBowl;
  891. string bowlName;
  892. if (type == ItemGroup.FOOD)
  893. {
  894. inTheBowl = "Food";
  895. bowlName = "Bowl_food";
  896. }
  897. else if (type == ItemGroup.WATER)
  898. {
  899. inTheBowl = "Water";
  900. bowlName = "Bowl_water";
  901. }
  902. else
  903. {
  904. yield break; // 如果类型不匹配,直接返回
  905. }
  906. bool allDogsDone = false;
  907. while (!allDogsDone)
  908. {
  909. allDogsDone = true; // 假设所有狗都完成了
  910. foreach (var dog in dogsInScene)
  911. {
  912. if (dog.dogState == DogState.ITEM_CONSUME)
  913. {
  914. allDogsDone = false; // 如果有狗还在消耗状态,则设置为false
  915. break;
  916. }
  917. }
  918. yield return new WaitForSeconds(0.5f); // 每0.5秒检查一次
  919. }
  920. var food = GameObject.Find(inTheBowl);
  921. var targetBowl = GameObject.Find(bowlName);
  922. food.transform.localPosition = new Vector3(-1, -10, -1);
  923. yield return new WaitForSeconds(2f); // 暂停2秒
  924. targetBowl.transform.position = new Vector3(-1, -10, -1); // 将饭盆回归原位
  925. food.transform.localPosition = Vector3.zero; // 将食物回归原位
  926. // 重制摄像头和菜单
  927. HomeController.dogCam.Priority = 1;
  928. HomeController.playerCam.Priority = 10;
  929. var uiPlaceholder = GameObject.Find("UI Placeholder");
  930. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  931. vamUI.SetActive(true);
  932. isHideBowlRunning = false; // 设置为不在运行状态
  933. }
  934. #endregion
  935. }
  936. public enum ItemGroup
  937. {
  938. FOOD,
  939. WATER
  940. }
  941. public enum SceneMode
  942. {
  943. TRAINING,
  944. INACTIVE,
  945. NORMAL,
  946. }