DogInScene.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. //using UnityEngine.Rendering.PostProcessing;
  5. /* 本类用于管理场景中所有狗的状态包括动画状态,随机数分配等
  6. */
  7. //[CreateAssetMenu(fileName = "DogInScene", menuName = "Scriptable Objects/DogInScene")]
  8. public class DogInScene //: ScriptableObject
  9. {
  10. //通用参数段
  11. public DogProperty dogProperty;
  12. public GameObject gameObject { set; get; }
  13. public Animator animator;
  14. private Vector3 moveToLocation;
  15. public float moveSpeed; // 用来控制狗的移动速度 0 0.5跑,0.75快跑,1跳
  16. //public string dogState; // 狗的状态代码分类通道,包括三大类使用道具(itemConsume),闲置(idle),训练(training),自由活动(interact)等
  17. public DogState dogState = DogState.IDLE;
  18. // 喝水吃饭参数段
  19. public DateTime drinkStartTime, eatStartTime; // 记录吃喝开始时间
  20. public bool itemConsumeProgress = false; // 是否在吃和喝的进程中。如果是的话,跳过常规动画检测
  21. public bool isMovingToBowl;
  22. // 随机动作参数段
  23. public int randomFactor;
  24. public DateTime animationStartTime; // 记录上一个动画开始时间的,每个随机动作间隔至少30秒
  25. private int activeIndex; // 动物的活动指数
  26. public bool isMoving; // 动物正在移动,避免其他随机动作发生
  27. public bool isSleeping; // 动物正在正在睡觉,避免触发其他动画
  28. // 固定参数
  29. private const float moveSpeedAdj = 0.025f;
  30. // interact training 相关的参数
  31. public DateTime interactLastUpdate; // 上次交互指令时间
  32. public bool isMovingToPlayer; // 是否在向玩家移动
  33. public string interactAnimation = ""; // 交互动画
  34. public DateTime interactAnimationStartTime; // 交互动画开始时间
  35. #region 通用函数段
  36. // 这个函数主要是为了在狗的属性发生变化时,重新加载狗的属性
  37. public void ReloadDogProperty()
  38. {
  39. foreach (var dog in UserProperty.dogs)
  40. {
  41. if (dog.d_id == dogProperty.d_id)
  42. {
  43. this.dogProperty = dog;
  44. break;
  45. }
  46. }
  47. }
  48. public void SetGameObject(GameObject gameObject)
  49. {
  50. this.gameObject = gameObject;
  51. this.animator = gameObject.GetComponent<Animator>();
  52. this.randomFactor = UnityEngine.Random.Range(0, 51); // 生成一个随机整数(0 到 50 之间),用于时间校验
  53. Debug.Log(this.gameObject.name + "random factor is:" + randomFactor);
  54. }
  55. public DogInScene(DogProperty property)
  56. {
  57. this.dogProperty = property;
  58. this.activeIndex = (int)Math.Round((property.liveliness + property.intimate) * UnityEngine.Random.Range(0.01f, 0.5f));
  59. this.isMoving = false;
  60. }
  61. public void RemoveZzzParticle()
  62. {
  63. // 关闭狗的睡觉粒子特效
  64. var zzzParticle = gameObject.transform.Find("zzzParticle").gameObject;
  65. if (zzzParticle != null)
  66. {
  67. zzzParticle.SetActive(false);
  68. }
  69. }
  70. // 关闭所有animation动画的状态,回到默认的状态
  71. public void ResetAnimationStatus()
  72. {
  73. // 获取所有参数
  74. AnimatorControllerParameter[] parameters = animator.parameters;
  75. foreach (AnimatorControllerParameter parameter in parameters)
  76. {
  77. if (parameter.type == AnimatorControllerParameterType.Bool)
  78. {
  79. animator.SetBool(parameter.name, false);
  80. }
  81. }
  82. }
  83. #endregion
  84. #region interact交互函数段
  85. // 启动交互行为准备过程
  86. public void SetupInteract()
  87. {
  88. Debug.Log("SetupInteract:" + this.gameObject.name);
  89. DogBarkController.Instance.PlayDogBarkWithDelay(3); // 狗叫相应一下
  90. if (dogProperty.voiceCall == 10)
  91. {
  92. // GameData.isFirstInteraction = false;
  93. PlayerPrefs.SetString("isFirstInteraction", "false");
  94. PlayerPrefs.Save();
  95. GameTool.PauseGameTime();
  96. string msg = GameTool.GetValueAtPath(EnviromentSetting.languageData, new string[] { "game_message", "first_touch_interactive", EnviromentSetting.languageCode });
  97. MessageBoxController.ShowMessage(msg, () => GameTool.ResumeGameTime());
  98. }
  99. // 加载Interact animator controller,避免过于复杂的Animator Controller
  100. Animator animator = gameObject.GetComponent<Animator>();
  101. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController");
  102. if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController"); }
  103. animator.runtimeAnimatorController = animatorController;
  104. float randomIndex = UnityEngine.Random.Range(0, 1f);
  105. animator.SetFloat("randomIndex", randomIndex);
  106. //this.dogState = "interact";
  107. this.dogState = DogState.INTERACT; // 设置狗的状态为交互状态
  108. this.interactLastUpdate = DateTime.Now;
  109. this.moveToLocation = new Vector3(0f, 0f, -3f);
  110. this.isMovingToPlayer = true;
  111. // 如果狗距离超出一定范围才开始移动
  112. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  113. Debug.Log(this.gameObject.name + "interact move to player distance:" + distance);
  114. if (distance > 2.5f)
  115. {
  116. this.animator.SetTrigger("move"); // 切换为走路动画
  117. this.animator.SetBool("isMoving", true); // 保持为走路动画
  118. this.animator.SetFloat("moveSpeed", 0.5f);
  119. }
  120. else
  121. {
  122. this.animator.SetBool("isMoving", false);
  123. this.gameObject.transform.LookAt(new Vector3(0f, 1f, -8f));
  124. this.isMovingToPlayer = false;
  125. }
  126. }
  127. public void MovetoPlayer()
  128. {
  129. // 如果距离目标小于0.5米就把速度调整为零
  130. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  131. {
  132. Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  133. this.SetMoveSpeed(0);
  134. Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  135. }
  136. // 如果狗距离到达重点就停止跑步动画
  137. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  138. //Debug.Log(this.gameObject.name + "interact move to player distance:" + distance);
  139. if (distance > 2.5f) // 一定距离内就开始停下来,否则刹不住
  140. {
  141. this.gameObject.transform.LookAt(moveToLocation);
  142. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  143. }
  144. else
  145. {
  146. this.animator.SetBool("isMoving", false);
  147. this.gameObject.transform.LookAt(new Vector3(0f, 1f, -8f));
  148. this.isMovingToPlayer = false;
  149. }
  150. }
  151. // 交互行为计时器
  152. public bool InteractTimeout()
  153. {
  154. TimeSpan ts = new TimeSpan();
  155. ts = DateTime.Now - this.interactLastUpdate;
  156. if (ts.TotalSeconds > 10)
  157. {
  158. Debug.Log("InteractTimeout:" + this.gameObject.name);
  159. //this.dogState = "idle";
  160. this.dogState = DogState.IDLE;
  161. return true;
  162. }
  163. else { return false; }
  164. }
  165. // 交互动画行为计时器(暂时设定为6秒)
  166. public IEnumerator InteractAnimationCountDown(float seconds = 6.0f)
  167. {
  168. TimeSpan ts = new TimeSpan();
  169. ts = DateTime.Now - this.interactAnimationStartTime;
  170. if (ts.TotalSeconds < 6){
  171. yield return new WaitForSeconds(0.25f);
  172. }
  173. else{
  174. Debug.Log("InteractAnimation count down stop:" + this.gameObject.name);
  175. this.ResetAnimationStatus();
  176. }
  177. }
  178. public void ExitInteract()
  179. {
  180. // 结束交互行为,恢复原有的菜单
  181. this.dogState = DogState.IDLE;
  182. // 恢复场景idle对应的Animator Controller
  183. Animator animator = gameObject.GetComponent<Animator>();
  184. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController");
  185. if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); }
  186. animator.runtimeAnimatorController = animatorController;
  187. this.RandomMove();
  188. }
  189. // 狗播放问号的表情
  190. public void PlayQuestionMark()
  191. {
  192. var questionMark = gameObject.transform.Find("QuestionMark").gameObject;
  193. if (questionMark != null)
  194. {
  195. var particleQuestionMark = questionMark.GetComponent<ParticleSystem>();
  196. if (particleQuestionMark != null)
  197. {
  198. particleQuestionMark.Play();
  199. }
  200. }
  201. }
  202. #endregion
  203. #region 随机动作控制函数
  204. // 随机选择宠物动作
  205. public void IdleAnimation()
  206. {
  207. this.animationStartTime = DateTime.Now;
  208. this.animator.SetInteger("activeIndex", activeIndex);
  209. //Debug.Log("activeIndex:" + this.activeIndex);
  210. float randomIndex = UnityEngine.Random.Range(0, 1f);
  211. this.animator.SetFloat("randomIndex", randomIndex);
  212. //this.dogState = "idle";
  213. this.dogState = DogState.IDLE;
  214. //Debug.Log("randomIndex:" + randomIndex);
  215. }
  216. public void SetMoveSpeed(float speed)
  217. {
  218. this.moveSpeed = speed;
  219. }
  220. // 宠物随机移动
  221. public void RandomMove()
  222. {
  223. // 如果距离目标小于0.5米就把速度调整为零
  224. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  225. {
  226. //Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  227. this.SetMoveSpeed(0);
  228. //Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  229. }
  230. if (isMoving == false)
  231. {
  232. animationStartTime = DateTime.Now; // 设置动画开始时间
  233. float x = UnityEngine.Random.Range(-5f, 5f);
  234. float z = UnityEngine.Random.Range(0f, 5f);
  235. this.moveToLocation = new Vector3(x, 0, z);
  236. //Debug.Log("move to location:" + x + ", " + z);
  237. this.isMoving = true;
  238. this.animator.SetTrigger("move");
  239. this.animator.SetBool("isMoving", true);
  240. this.animator.SetFloat("moveSpeed", this.moveSpeed);
  241. }
  242. this.gameObject.transform.LookAt(moveToLocation);
  243. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  244. //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100 + dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
  245. //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
  246. // 如果狗距离到达重点就停止跑步动画
  247. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  248. if (distance < 0.1)
  249. {
  250. this.animator.SetBool("isMoving", false);
  251. //this.moveSpeed = 0.4f;
  252. this.isMoving = false;
  253. IdleAnimation();
  254. }
  255. }
  256. public void Sleep()
  257. {
  258. this.animator.SetTrigger("sleep");
  259. this.animator.SetBool("isSleeping", true);
  260. this.isSleeping = true;
  261. //this.dogState = "sleep";
  262. this.dogState = DogState.SLEEP;
  263. var zzzParticle = gameObject.transform.Find("zzzParticle").gameObject;
  264. if (zzzParticle != null)
  265. {
  266. zzzParticle.SetActive(true);
  267. }
  268. }
  269. public void Listen()
  270. {
  271. //StopCoroutine(movingCoroutine);
  272. //this.animator.SetTrigger("listen");
  273. this.animator.SetBool("isListening", true);
  274. this.animator.SetBool("isMoving", false);
  275. this.animator.SetBool("isBarking", false);
  276. this.animator.SetBool("isSleeping", false);
  277. this.isSleeping = false; // 主人呼叫可以唤醒狗
  278. this.gameObject.transform.LookAt(new Vector3(0, 0, -6));
  279. HomeController.playerCam.Priority = 10;
  280. HomeController.dogCam.Priority = 1;
  281. HomeController.lastCameraChange = DateTime.Now;
  282. //this.dogState = "listen";
  283. this.dogState = DogState.LISTEN;
  284. }
  285. // 用来出来音频返回结果
  286. //public void PostListenRequest(bool result)
  287. //{
  288. // if (result)
  289. // {
  290. // // TODO 语音信息识别成功,狗狗语音适配得分最高的跑过来
  291. // this.animator.SetBool("isListening", false);
  292. // }
  293. // else
  294. // {
  295. // this.animator.SetBool("isListening", false);
  296. // }
  297. // // TODO 根据返回结果设定focusdog
  298. // // focusdog 开启互动模式
  299. // HomeController.dogsInScene[GameData.focusDog].dogState = "interact";
  300. // HomeController.dogsInScene[GameData.focusDog].SetupInteract();
  301. //}
  302. #endregion
  303. #region 喝水,吃食等道具消费函数
  304. // 开始整个使用道具的流程
  305. public void StartItemConsume(ItemGroup group)
  306. {
  307. GameObject bowl = GameObject.Find("Bowl_water"); // 先指定一个碗,编译通过
  308. this.itemConsumeProgress = true; // 开启使用道具过程
  309. //this.dogState = "itemConsume";
  310. this.dogState = DogState.ITEM_CONSUME;
  311. if (group == ItemGroup.WATER)
  312. {
  313. bowl = GameObject.Find("Bowl_water"); // 开启整个喝水的进程
  314. }
  315. if (group == ItemGroup.FOOD)
  316. {
  317. bowl = GameObject.Find("Bowl_food"); // 开启整个吃食物的进程
  318. }
  319. this.moveToLocation = bowl.transform.position;
  320. this.isMovingToBowl = true;
  321. this.animator.SetTrigger("move"); // 切换为走路动画
  322. this.animator.SetBool("isMoving", true); // 保持为走路动画
  323. this.animator.SetFloat("moveSpeed", this.moveSpeed);
  324. }
  325. public void MovetoBowl()
  326. {
  327. // 如果距离目标小于0.5米就把速度调整为零
  328. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  329. {
  330. Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  331. this.SetMoveSpeed(0);
  332. Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  333. }
  334. var vamUI = GameObject.Find("VoiceAndMenu");
  335. if (vamUI != null)
  336. {
  337. vamUI.SetActive(false);
  338. }
  339. this.gameObject.transform.LookAt(moveToLocation);
  340. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  341. //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100+dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
  342. //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
  343. // 如果狗距离到达重点就停止跑步动画
  344. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  345. if (distance < 0.1)
  346. {
  347. this.animator.SetBool("isMoving", false);
  348. }
  349. }
  350. public IEnumerator DrinkAnimation()
  351. {
  352. this.animator.SetBool("isMoving", false); // 关闭移动动画
  353. TimeSpan ts = new TimeSpan();
  354. var targetBowl = GameObject.Find("Bowl_water");
  355. // 摄像头看向水盆位置
  356. HomeController.dogCam.m_LookAt = targetBowl.transform;
  357. HomeController.dogCam.Priority = 10;
  358. HomeController.playerCam.Priority = 1;
  359. while (ts.TotalSeconds < 10)
  360. {
  361. yield return new WaitForSeconds(0.25f);
  362. ts = DateTime.Now - this.drinkStartTime;
  363. //Debug.Log("结束饮水过程:" + ts.TotalSeconds);
  364. // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
  365. this.gameObject.transform.LookAt(targetBowl.transform.position);
  366. }
  367. // 播放10秒后结束饮水过程
  368. this.animator.SetBool("isDrinking", false);
  369. // 摄像头恢复玩家视角
  370. HomeController.dogCam.Priority = 1;
  371. HomeController.playerCam.Priority = 10;
  372. // 让水物消失
  373. var water = GameObject.Find("Water");
  374. water.transform.localPosition = new Vector3(-1, -10, -1);
  375. //var water = GameObject.Find("Water");
  376. //water.SetActive(false);
  377. // 再等待几秒后让水盆消失
  378. while (ts.TotalSeconds < 18)
  379. {
  380. yield return new WaitForSeconds(0.25f);
  381. ts = DateTime.Now - this.drinkStartTime;
  382. //Debug.Log("让水盆消失:" + ts.TotalSeconds);
  383. }
  384. targetBowl.transform.position = new Vector3(-1, -10, -1); // 将喝水碗回归原位
  385. water.transform.localPosition = Vector3.zero; // 将食物回归原位
  386. this.itemConsumeProgress = false; // 关闭整个道具使用的进程
  387. //this.dogState = "idle";
  388. this.dogState = DogState.IDLE;
  389. QuitItemConsume();
  390. }
  391. public IEnumerator EatAnimation()
  392. {
  393. this.animator.SetBool("isMoving", false); // 关闭移动动画
  394. TimeSpan ts = new TimeSpan();
  395. var targetBowl = GameObject.Find("Bowl_food");
  396. // 摄像头看向盆位置
  397. HomeController.dogCam.m_LookAt = targetBowl.transform;
  398. HomeController.dogCam.Priority = 10;
  399. HomeController.playerCam.Priority = 1;
  400. while (ts.TotalSeconds < 10)
  401. {
  402. yield return new WaitForSeconds(0.25f);
  403. ts = DateTime.Now - this.eatStartTime;
  404. // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
  405. this.gameObject.transform.LookAt(targetBowl.transform.position);
  406. }
  407. // 播放10秒后结束吃狗粮过程
  408. this.animator.SetBool("isEating", false);
  409. //Debug.Log(this.dogProperty.dog_name + "狗吃完了");
  410. // 摄像头恢复玩家视角
  411. HomeController.dogCam.Priority = 1;
  412. HomeController.playerCam.Priority = 10;
  413. // 让食物消失
  414. var food = GameObject.Find("Food");
  415. food.transform.localPosition = new Vector3(-1, -10, -1);
  416. //var water = GameObject.Find("Water");
  417. //water.SetActive(false);
  418. // 再等待几秒后让饭盆消失
  419. while (ts.TotalSeconds < 18)
  420. {
  421. yield return new WaitForSeconds(0.25f);
  422. ts = DateTime.Now - this.eatStartTime;
  423. //Debug.Log("让水盆消失:" + ts.TotalSeconds);
  424. }
  425. //Debug.Log(this.dogProperty.dog_name + "把碗放回去了");
  426. targetBowl.transform.position = new Vector3(-1, -10, -1); // 将饭盆回归原位
  427. food.transform.localPosition = Vector3.zero; // 将食物回归原位
  428. this.itemConsumeProgress = false; // 关闭整个道具使用的进程
  429. //this.dogState = "idle";
  430. this.dogState = DogState.IDLE;
  431. QuitItemConsume();
  432. }
  433. public void QuitItemConsume()
  434. {
  435. var uiPlaceholder = GameObject.Find("UI Placeholder");
  436. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  437. vamUI.SetActive(true);
  438. this.moveSpeed = UnityEngine.Random.Range(0.3f, 0.6f);
  439. RandomMove();
  440. }
  441. #endregion
  442. }
  443. // Change the accessibility of the DogState enum to public to match the accessibility of the field "dog_state".
  444. public enum DogState
  445. {
  446. ITEM_CONSUME,
  447. IDLE,
  448. TRAINING,
  449. INTERACT,
  450. SLEEP,
  451. LISTEN
  452. }