HomeController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using Cinemachine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.VisualScripting;
  7. using UnityEngine;
  8. using UnityEngine.Animations;
  9. using UnityEngine.Rendering.PostProcessing;
  10. /* 本代码控制室内场景
  11. * 控制宠物在Home场景动画
  12. * !!!特别注意:Dog Initializer 必须挂载在同一个组件下,并且必须在本组价下方。确保比本组件先执行
  13. * 主要调节参数在FixedUpdate代码段里面
  14. */
  15. public class HomeController : MonoBehaviour
  16. {
  17. public static List<DogInScene> dogsInScene = new List<DogInScene>();
  18. public static bool listenBreak = false; // 当按下说话按键后,所有狗停止行动,立刻切换到监听状态。
  19. private bool isInitialDone = false;
  20. public static CinemachineVirtualCamera playerCam, dogCam;
  21. public static DateTime lastCameraChange;
  22. // Start is called once before the first execution of Update after the MonoBehaviour is created
  23. void Start()
  24. {
  25. lastCameraChange = DateTime.Now;
  26. playerCam = GameObject.Find("VCam Player").GetComponent<CinemachineVirtualCamera>();
  27. dogCam = GameObject.Find("VCam Dog").GetComponent<CinemachineVirtualCamera>();
  28. //InitialScene();
  29. StartCoroutine(InitialScene());
  30. // 判断是否在睡觉时间
  31. DateTime dateTime = DateTime.Now;
  32. foreach (var dog in dogsInScene)
  33. {
  34. if (dateTime.Hour >= 22 || dateTime.Hour <= 5) // 深夜模式,狗默认在睡觉状态
  35. {
  36. dog.Sleep();
  37. }
  38. else
  39. {
  40. dog.StartAnimation();
  41. }
  42. }
  43. isInitialDone = true;
  44. }
  45. // Update is called once per frame
  46. void FixedUpdate()
  47. {
  48. if (!isInitialDone)
  49. {
  50. }
  51. // 随机镜头切换代码
  52. RandomCameraChange();
  53. #region 狗随机动作控制代码
  54. // 生成一个数据数用于随机开启动画,如果和狗的randomFactor相同就开启动画
  55. int randomCheck = UnityEngine.Random.Range(0, 51);
  56. foreach (var dog in dogsInScene)
  57. {
  58. if (listenBreak) // 如果用户按下说话按键,立刻切换到监听状态
  59. {
  60. dog.Listen();
  61. }
  62. else if (dog.isMoving)
  63. {
  64. dog.Move();
  65. }
  66. else if (randomCheck == dog.randomFactor) // 当狗自身的随机数和系统随机数相同时候触发。约100秒触发一次。
  67. {
  68. TimeSpan ts = DateTime.Now - dog.animationStartTime;
  69. if (ts.Seconds >= 30) // 如果距离上一个动作超过30秒就可以开始新的动作
  70. {
  71. float r = UnityEngine.Random.Range(0, 1f);
  72. if (r > 0.6) // 随机选择开始动画,或者移动
  73. {
  74. dog.StartAnimation();
  75. }
  76. else // 狗狗开始步行移动
  77. {
  78. dog.SetMoveSpeed(0);
  79. dog.Move();
  80. }
  81. }
  82. }
  83. }
  84. #endregion
  85. }
  86. //void AniOrWalk(DogInScene dog) // 狗在普通状态下,随机或播放动画,或移动
  87. //{
  88. //}
  89. // 初始化场景,加载所有的狗,并配置components,添加到dogsInScene List里面去
  90. //void InitialScene()
  91. IEnumerator InitialScene()
  92. {
  93. //
  94. yield return null; // 跳过第一帧
  95. Debug.Log("Home InitialScene is called");
  96. Debug.Log(isInitialDone);
  97. foreach (var dog in UserProperty.dogs)
  98. {
  99. DogInScene dogInScene = new DogInScene(dog);
  100. float x = UnityEngine.Random.Range(-1f, 1f); // 随机生成位置,考虑到手机评估宽度限制宽度
  101. float z = UnityEngine.Random.Range(-5f, 5f);
  102. float y = UnityEngine.Random.Range(0, 360f);
  103. var initPosition = new Vector3(x, 0, z);
  104. StartCoroutine(DogComponentAdd(dog)); // 加载狗的其他组件
  105. var dogGameObject = GameObject.Find(dog.name);
  106. dogGameObject.transform.position = initPosition;
  107. dogGameObject.transform.rotation = Quaternion.Euler(0, y, 0);
  108. dogGameObject.transform.localScale = new Vector3(2, 2, 2);
  109. dogInScene.SetGameObject(dogGameObject);
  110. dogsInScene.Add(dogInScene);
  111. }
  112. }
  113. // 加载狗的其他组件
  114. IEnumerator DogComponentAdd(DogProperty dogProperty)
  115. {
  116. // 等待一帧,确保所有 Start() 方法都执行完成
  117. yield return null;
  118. // 第一帧以后开始执行
  119. GameObject dog = GameObject.Find(dogProperty.name);
  120. // 加载指定的Animator controller
  121. Animator animator = dog.GetComponent<Animator>();
  122. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController");
  123. if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); }
  124. animator.runtimeAnimatorController = animatorController;
  125. // 加载bbx collider
  126. BoxCollider boxCollider = dog.AddComponent<BoxCollider>();
  127. boxCollider.isTrigger = true;
  128. boxCollider.center = new Vector3(0, 0.225f, 0);
  129. boxCollider.size = new Vector3(0.2f, 0.45f, 0.6f);
  130. }
  131. // 场景随机切换镜头看向不同的狗
  132. void RandomCameraChange()
  133. {
  134. int delay = 10; // 延迟10秒执行一次
  135. TimeSpan ts = DateTime.Now - lastCameraChange;
  136. if (ts.TotalSeconds < delay) {return;}
  137. int dogCount = dogsInScene.Count;
  138. int r = UnityEngine.Random.Range(0, dogCount+1);
  139. Debug.Log("RandomCameraChange Start. R:" + r);
  140. if (r < dogCount)
  141. {
  142. dogCam.m_LookAt = dogsInScene[r].gameObject.transform;
  143. dogCam.Priority = 10;
  144. playerCam.Priority = 1;
  145. }
  146. else
  147. {
  148. dogCam.Priority = 1;
  149. playerCam.Priority = 10;
  150. }
  151. lastCameraChange = DateTime.Now;
  152. }
  153. }
  154. // 本类用于管理场景中所有狗的状态包括动画状态,随机数分配等
  155. public class DogInScene
  156. {
  157. public DogProperty dogProperty;
  158. //public Vector3 location, rotation, scale;
  159. public int randomFactor = UnityEngine.Random.Range(0, 51); // 生成一个随机整数(0 到 50 之间),用于时间校验
  160. public DateTime animationStartTime; // 记录上一个动画开始时间的,每个动作间隔至少30秒。
  161. private int activeIndex; // 动物的活动指数
  162. public GameObject gameObject { set; get; }
  163. private Animator animator;
  164. private Vector3 moveToLocation;
  165. public bool isMoving;
  166. private float moveSpeed; // 用来控制狗的移动速度 0 0.5跑,0.75快跑,1跳
  167. public void SetGameObject(GameObject gameObject)
  168. {
  169. this.gameObject = gameObject;
  170. this.animator = gameObject.GetComponent<Animator>();
  171. }
  172. //public GameObject GetGameObject() { return gameObject; }
  173. public DogInScene(DogProperty property) {
  174. this.dogProperty = property;
  175. this.activeIndex = (int)Math.Round((property.liveliness + property.intimate) * UnityEngine.Random.Range(0.3f, 0.7f));
  176. this.isMoving = false;
  177. }
  178. public void StartAnimation()
  179. {
  180. this.animationStartTime = DateTime.Now;
  181. this.animator.SetInteger("activeIndex", activeIndex);
  182. Debug.Log("activeIndex:" + this.activeIndex);
  183. float randomIndex = UnityEngine.Random.Range(0, 1f);
  184. this.animator.SetFloat("randomIndex", randomIndex);
  185. Debug.Log("randomIndex:" + randomIndex);
  186. }
  187. public void SetMoveSpeed(float speed)
  188. {
  189. this.moveSpeed = speed;
  190. }
  191. public void Move()
  192. {
  193. if (isMoving == false)
  194. {
  195. animationStartTime = DateTime.Now; // 设置动画开始时间
  196. float x = UnityEngine.Random.Range(-5f, 5f);
  197. float z = UnityEngine.Random.Range(-5f, 5f);
  198. this.moveToLocation = new Vector3(x, 0, z);
  199. //Debug.Log("move to location:" + x + ", " + z);
  200. this.isMoving = true;
  201. this.animator.SetTrigger("move");
  202. this.animator.SetBool("isMoving", true);
  203. this.animator.SetFloat("moveSpeed", this.moveSpeed);
  204. }
  205. this.gameObject.transform.LookAt(moveToLocation);
  206. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * 0.02f * 0.01f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整
  207. //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
  208. // 如果狗距离到达重点就停止跑步动画
  209. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  210. if (distance < 0.1)
  211. {
  212. this.animator.SetBool("isMoving", false);
  213. this.isMoving = false;
  214. StartAnimation();
  215. }
  216. }
  217. public void Sleep()
  218. {
  219. this.animator.SetTrigger("sleep");
  220. this.animator.SetBool("isSleeping", true);
  221. }
  222. public void Listen()
  223. {
  224. //StopCoroutine(movingCoroutine);
  225. this.animator.SetTrigger("listen");
  226. this.animator.SetBool("isListening", true);
  227. this.animator.SetBool("isMoving", false) ;
  228. this.animator.SetBool("isBarking", false);
  229. this.animator.SetBool("isSleeping", false);
  230. this.gameObject.transform.LookAt(new Vector3(0, 0, -6));
  231. HomeController.playerCam.Priority = 10;
  232. HomeController.dogCam.Priority = 1;
  233. HomeController.lastCameraChange = DateTime.Now;
  234. }
  235. // 用来出来音频返回结果
  236. public void PostListen(bool result)
  237. {
  238. if (result)
  239. {
  240. // TODO 成功,狗狗跑过来,
  241. }
  242. else
  243. {
  244. this.animator.SetBool("isListening", false);
  245. }
  246. }
  247. }