DogInScene.cs 18 KB

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