DogInScene.cs 20 KB

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