DogInScene.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // 喝水吃饭参数段
  18. public DateTime drinkStartTime, eatStartTime; // 记录吃喝开始时间
  19. public bool itemConsumeProgress = false; // 是否在吃和喝的进程中。如果是的话,跳过常规动画检测
  20. public bool isMovingToBowl;
  21. // 随机动作参数段
  22. public int randomFactor;
  23. public DateTime animationStartTime; // 记录上一个动画开始时间的,每个随机动作间隔至少30秒
  24. private int activeIndex; // 动物的活动指数
  25. public bool isMoving; // 动物正在移动,避免其他随机动作发生
  26. public bool isSleeping; // 动物正在正在睡觉,避免触发其他动画
  27. // 固定参数
  28. private const float moveSpeedAdj = 0.025f;
  29. // interact training 相关的参数
  30. public DateTime interactLastUpdate; // 上次交互指令时间
  31. public bool isMovingToPlayer; // 是否在向玩家移动
  32. public string interactAnimation = ""; // 交互动画
  33. public DateTime interactAnimationStartTime; // 交互动画开始时间
  34. #region 通用函数段
  35. public void SetGameObject(GameObject gameObject)
  36. {
  37. this.gameObject = gameObject;
  38. this.animator = gameObject.GetComponent<Animator>();
  39. this.randomFactor = UnityEngine.Random.Range(0, 51); // 生成一个随机整数(0 到 50 之间),用于时间校验
  40. Debug.Log(this.gameObject.name + "random factor is:" + randomFactor);
  41. }
  42. public DogInScene(DogProperty property)
  43. {
  44. this.dogProperty = property;
  45. this.activeIndex = (int)Math.Round((property.liveliness + property.intimate) * UnityEngine.Random.Range(0.3f, 0.7f));
  46. this.isMoving = false;
  47. }
  48. #endregion
  49. #region interact交互函数段
  50. // 启动交互行为准备过程
  51. public void SetupInteract()
  52. {
  53. // 加载Interact animator controller,避免过于复杂的Animator Controller
  54. Animator animator = gameObject.GetComponent<Animator>();
  55. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController");
  56. if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController"); }
  57. animator.runtimeAnimatorController = animatorController;
  58. this.dogState = "interact";
  59. this.interactLastUpdate = DateTime.Now;
  60. this.moveToLocation = new Vector3(0f, 0f, -3f);
  61. this.isMovingToPlayer = true;
  62. // 如果狗距离超出一定范围才开始移动
  63. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  64. if ( distance>2.5f)
  65. {
  66. this.animator.SetTrigger("move"); // 切换为走路动画
  67. this.animator.SetBool("isMoving", true); // 保持为走路动画
  68. this.animator.SetFloat("moveSpeed", 0.5f);
  69. }
  70. else
  71. {
  72. this.animator.SetBool("isMoving", false);
  73. this.gameObject.transform.LookAt(new Vector3(0f, 1f, -8f));
  74. this.isMovingToPlayer = false;
  75. }
  76. }
  77. public void MovetoPlayer()
  78. {
  79. // 如果距离目标小于0.5米就把速度调整为零
  80. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  81. {
  82. Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  83. this.SetMoveSpeed(0);
  84. Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  85. }
  86. // 如果狗距离到达重点就停止跑步动画
  87. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  88. //Debug.Log(this.gameObject.name + "interact move to player distance:" + distance);
  89. if (distance > 2.5f) // 一定距离内就开始停下来,否则刹不住
  90. {
  91. this.gameObject.transform.LookAt(moveToLocation);
  92. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  93. }
  94. else
  95. {
  96. this.animator.SetBool("isMoving", false);
  97. this.gameObject.transform.LookAt(new Vector3(0f, 1f, -8f));
  98. this.isMovingToPlayer = false;
  99. }
  100. }
  101. // 交互行为计时器
  102. public bool InteractTimeout()
  103. {
  104. TimeSpan ts = new TimeSpan();
  105. ts = DateTime.Now - this.interactLastUpdate;
  106. if (ts.TotalSeconds > 10)
  107. {
  108. Debug.Log("InteractTimeout:" + this.gameObject.name);
  109. this.dogState= "idle";
  110. return true;
  111. }
  112. else { return false; }
  113. }
  114. // 交互动画行为计时器(暂时设定为6秒)
  115. // 考虑一下是否需要这个功能,如果不需要就删除
  116. public void InteractAnimationTimeout()
  117. {
  118. TimeSpan ts = new TimeSpan();
  119. ts = DateTime.Now - this.interactAnimationStartTime;
  120. if (ts.TotalSeconds > 6)
  121. {
  122. Debug.Log("InteractAnimationTimeout:" + this.gameObject.name);
  123. var animator = this.gameObject.GetComponent<Animator>();
  124. animator.SetBool(interactAnimation, false);
  125. }
  126. }
  127. public void ExitInteract()
  128. {
  129. // 结束交互行为,恢复原有的菜单
  130. this.dogState = "idle";
  131. // 恢复场景idle对应的Animator Controller
  132. Animator animator = gameObject.GetComponent<Animator>();
  133. RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController");
  134. if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); }
  135. animator.runtimeAnimatorController = animatorController;
  136. this.RandomMove();
  137. }
  138. #endregion
  139. #region 随机动作控制函数
  140. // 随机选择宠物动作
  141. public void IdleAnimation()
  142. {
  143. this.animationStartTime = DateTime.Now;
  144. this.animator.SetInteger("activeIndex", activeIndex);
  145. //Debug.Log("activeIndex:" + this.activeIndex);
  146. float randomIndex = UnityEngine.Random.Range(0, 1f);
  147. this.animator.SetFloat("randomIndex", randomIndex);
  148. this.dogState = "idle";
  149. //Debug.Log("randomIndex:" + randomIndex);
  150. }
  151. public void SetMoveSpeed(float speed)
  152. {
  153. this.moveSpeed = speed;
  154. }
  155. // 宠物随机移动
  156. public void RandomMove()
  157. {
  158. // 如果距离目标小于0.5米就把速度调整为零
  159. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  160. {
  161. //Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  162. this.SetMoveSpeed(0);
  163. //Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  164. }
  165. if (isMoving == false)
  166. {
  167. animationStartTime = DateTime.Now; // 设置动画开始时间
  168. float x = UnityEngine.Random.Range(-5f, 5f);
  169. float z = UnityEngine.Random.Range(0f, 5f);
  170. this.moveToLocation = new Vector3(x, 0, z);
  171. //Debug.Log("move to location:" + x + ", " + z);
  172. this.isMoving = true;
  173. this.animator.SetTrigger("move");
  174. this.animator.SetBool("isMoving", true);
  175. this.animator.SetFloat("moveSpeed", this.moveSpeed);
  176. }
  177. this.gameObject.transform.LookAt(moveToLocation);
  178. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  179. //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100 + dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
  180. //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
  181. // 如果狗距离到达重点就停止跑步动画
  182. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  183. if (distance < 0.1)
  184. {
  185. this.animator.SetBool("isMoving", false);
  186. //this.moveSpeed = 0.4f;
  187. this.isMoving = false;
  188. IdleAnimation();
  189. }
  190. }
  191. public void Sleep()
  192. {
  193. this.animator.SetTrigger("sleep");
  194. this.animator.SetBool("isSleeping", true);
  195. this.isSleeping = true;
  196. this.dogState = "sleep";
  197. }
  198. public void Listen()
  199. {
  200. //StopCoroutine(movingCoroutine);
  201. //this.animator.SetTrigger("listen");
  202. this.animator.SetBool("isListening", true);
  203. this.animator.SetBool("isMoving", false);
  204. this.animator.SetBool("isBarking", false);
  205. this.animator.SetBool("isSleeping", false);
  206. this.isSleeping = false; // 主人呼叫可以唤醒狗
  207. this.gameObject.transform.LookAt(new Vector3(0, 0, -6));
  208. HomeController.playerCam.Priority = 10;
  209. HomeController.dogCam.Priority = 1;
  210. HomeController.lastCameraChange = DateTime.Now;
  211. this.dogState = "listen";
  212. }
  213. // 用来出来音频返回结果
  214. //public void PostListenRequest(bool result)
  215. //{
  216. // if (result)
  217. // {
  218. // // TODO 语音信息识别成功,狗狗语音适配得分最高的跑过来
  219. // this.animator.SetBool("isListening", false);
  220. // }
  221. // else
  222. // {
  223. // this.animator.SetBool("isListening", false);
  224. // }
  225. // // TODO 根据返回结果设定focusdog
  226. // // focusdog 开启互动模式
  227. // HomeController.dogsInScene[GameData.focusDog].dogState = "interact";
  228. // HomeController.dogsInScene[GameData.focusDog].SetupInteract();
  229. //}
  230. #endregion
  231. #region 喝水,吃食等道具消费函数
  232. // 开始整个使用道具的流程
  233. public void StartItemConsume(ItemGroup group)
  234. {
  235. GameObject bowl = GameObject.Find("Bowl_water"); // 先指定一个碗,编译通过
  236. this.itemConsumeProgress = true; // 开启使用道具过程
  237. this.dogState = "itemConsume";
  238. if (group == ItemGroup.water)
  239. {
  240. bowl = GameObject.Find("Bowl_water"); // 开启整个喝水的进程
  241. }
  242. if (group == ItemGroup.food)
  243. {
  244. bowl = GameObject.Find("Bowl_food"); // 开启整个吃食物的进程
  245. }
  246. this.moveToLocation = bowl.transform.position;
  247. this.isMovingToBowl = true;
  248. this.animator.SetTrigger("move"); // 切换为走路动画
  249. this.animator.SetBool("isMoving", true); // 保持为走路动画
  250. this.animator.SetFloat("moveSpeed", this.moveSpeed);
  251. }
  252. public void MovetoBowl()
  253. {
  254. // 如果距离目标小于0.5米就把速度调整为零
  255. if (Vector3.Distance(moveToLocation, this.gameObject.transform.position) < 0.5f)
  256. {
  257. Debug.Log(this.gameObject.name + "current move speed:" + moveSpeed);
  258. this.SetMoveSpeed(0);
  259. Debug.Log(this.gameObject.name + "reduce move speed:" + moveSpeed);
  260. }
  261. var vamUI = GameObject.Find("VoiceAndMenu");
  262. if (vamUI != null)
  263. {
  264. vamUI.SetActive(false);
  265. }
  266. this.gameObject.transform.LookAt(moveToLocation);
  267. this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * (1 + moveSpeed) * 0.02f * moveSpeedAdj); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应RM动画
  268. //this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, (100+dogProperty.runSpeed) * (1 + moveSpeed) * 0.02f * 0.015f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整,对应IF动画
  269. //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z);
  270. // 如果狗距离到达重点就停止跑步动画
  271. float distance = Vector3.Distance(gameObject.transform.position, moveToLocation);
  272. if (distance < 0.1)
  273. {
  274. this.animator.SetBool("isMoving", false);
  275. }
  276. }
  277. public IEnumerator DrinkAnimation()
  278. {
  279. this.animator.SetBool("isMoving", false); // 关闭移动动画
  280. TimeSpan ts = new TimeSpan();
  281. var targetBowl = GameObject.Find("Bowl_water");
  282. // 摄像头看向水盆位置
  283. HomeController.dogCam.m_LookAt = targetBowl.transform;
  284. HomeController.dogCam.Priority = 10;
  285. HomeController.playerCam.Priority = 1;
  286. while (ts.TotalSeconds < 10)
  287. {
  288. yield return new WaitForSeconds(0.25f);
  289. ts = DateTime.Now - this.drinkStartTime;
  290. //Debug.Log("结束饮水过程:" + ts.TotalSeconds);
  291. // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
  292. this.gameObject.transform.LookAt(targetBowl.transform.position);
  293. }
  294. // 播放10秒后结束饮水过程
  295. this.animator.SetBool("isDrinking", false);
  296. // 摄像头恢复玩家视角
  297. HomeController.dogCam.Priority = 1;
  298. HomeController.playerCam.Priority = 10;
  299. // 让水物消失
  300. var water = GameObject.Find("Water");
  301. water.transform.localPosition = new Vector3(-1, -10, -1);
  302. //var water = GameObject.Find("Water");
  303. //water.SetActive(false);
  304. // 再等待几秒后让水盆消失
  305. while (ts.TotalSeconds < 18)
  306. {
  307. yield return new WaitForSeconds(0.25f);
  308. ts = DateTime.Now - this.drinkStartTime;
  309. //Debug.Log("让水盆消失:" + ts.TotalSeconds);
  310. }
  311. targetBowl.transform.position = new Vector3(-1, -10, -1); // 将喝水碗回归原位
  312. water.transform.localPosition = Vector3.zero; // 将食物回归原位
  313. this.itemConsumeProgress = false; // 关闭整个道具使用的进程
  314. this.dogState = "idle";
  315. QuitItemConsume();
  316. }
  317. public IEnumerator EatAnimation()
  318. {
  319. this.animator.SetBool("isMoving", false); // 关闭移动动画
  320. TimeSpan ts = new TimeSpan();
  321. var targetBowl = GameObject.Find("Bowl_food");
  322. // 摄像头看向盆位置
  323. HomeController.dogCam.m_LookAt = targetBowl.transform;
  324. HomeController.dogCam.Priority = 10;
  325. HomeController.playerCam.Priority = 1;
  326. while (ts.TotalSeconds < 10)
  327. {
  328. yield return new WaitForSeconds(0.25f);
  329. ts = DateTime.Now - this.eatStartTime;
  330. // 狗一致看向谁碰,确保就算被撞击后依然看向水盆
  331. this.gameObject.transform.LookAt(targetBowl.transform.position);
  332. }
  333. // 播放10秒后结束吃狗粮过程
  334. this.animator.SetBool("isEating", false);
  335. //Debug.Log(this.dogProperty.dog_name + "狗吃完了");
  336. // 摄像头恢复玩家视角
  337. HomeController.dogCam.Priority = 1;
  338. HomeController.playerCam.Priority = 10;
  339. // 让食物消失
  340. var food = GameObject.Find("Food");
  341. food.transform.localPosition = new Vector3(-1, -10, -1);
  342. //var water = GameObject.Find("Water");
  343. //water.SetActive(false);
  344. // 再等待几秒后让饭盆消失
  345. while (ts.TotalSeconds < 18)
  346. {
  347. yield return new WaitForSeconds(0.25f);
  348. ts = DateTime.Now - this.eatStartTime;
  349. //Debug.Log("让水盆消失:" + ts.TotalSeconds);
  350. }
  351. //Debug.Log(this.dogProperty.dog_name + "把碗放回去了");
  352. targetBowl.transform.position = new Vector3(-1, -10, -1); // 将饭盆回归原位
  353. food.transform.localPosition = Vector3.zero; // 将食物回归原位
  354. this.itemConsumeProgress = false; // 关闭整个道具使用的进程
  355. this.dogState = "idle";
  356. QuitItemConsume();
  357. }
  358. public void QuitItemConsume()
  359. {
  360. var uiPlaceholder = GameObject.Find("UI Placeholder");
  361. var vamUI = uiPlaceholder.transform.Find("VoiceAndMenu").gameObject;
  362. vamUI.SetActive(true);
  363. this.moveSpeed = UnityEngine.Random.Range(0.3f, 0.6f);
  364. RandomMove();
  365. }
  366. #endregion
  367. }