DogInScene.cs 19 KB

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