Yeji.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. public class Yeji : TargetAnimal
  6. {
  7. //动画播放器
  8. Animator animator;
  9. //寻路代理
  10. NavMeshAgent agent;
  11. //血量
  12. [System.NonSerialized] public int hp = 3;
  13. [System.NonSerialized] public TreeAreaRecorder treeAreaRecorder;
  14. [System.NonSerialized] public float flyPlaneHeight = 5;
  15. float currentHeight = 0;
  16. float toFlyHeight = float.NaN;
  17. public void SetFlyHeight(float value) {
  18. if (toFlyHeight.Equals(float.NaN)) toFlyHeight = value;
  19. currentHeight = value;
  20. float agentBaseOffset = value - flyPlaneHeight;
  21. this.agent.baseOffset = agentBaseOffset / 1.2f;//因为agent的baseoffset会受节点的scale影响
  22. state.flying = currentHeight > 0;
  23. }
  24. void Awake()
  25. {
  26. state = new State();
  27. state.animal = this;
  28. animator = GetComponent<Animator>();
  29. agent = GetComponent<NavMeshAgent>();
  30. this.agent.avoidancePriority = avoidancePriority;
  31. }
  32. static int _avoidancePriority = 0;
  33. static int avoidancePriority {
  34. get {
  35. if (_avoidancePriority < 50) {
  36. _avoidancePriority++;
  37. } else {
  38. _avoidancePriority = 1;
  39. }
  40. return _avoidancePriority;
  41. }
  42. }
  43. void OnDestroy() {
  44. tryReleaseOccupyTree();
  45. ReleasePreHeight(this);
  46. }
  47. void Update()
  48. {
  49. //寻路过程监测
  50. if (HasCloseToDestination()) {
  51. OnReachDestination();
  52. } else {
  53. OnMovingToDestination();
  54. }
  55. UpdateAutoStrategy();
  56. }
  57. #region 被击触发
  58. public override void OnHit(Arrow arrow, Vector3 hitPoint, string partName)
  59. {
  60. arrow.Head().position = hitPoint + arrow.transform.forward * 0.1f;
  61. arrow.Hit();
  62. int hurtValue = 0;
  63. if (partName == "Wing") {
  64. hurtValue = 1;
  65. }
  66. else if (partName == "Body") hurtValue = 2;
  67. else if (partName == "Head") hurtValue = 3;
  68. hp -= hurtValue;
  69. GameEventCenter.ins.onTargetAnimalHurt?.Invoke(this, hurtValue);
  70. if (hp <= 0) {
  71. Die(arrow);
  72. } else {
  73. Hurt();
  74. }
  75. }
  76. void Die(Arrow arrow) {
  77. if (state.dead) return;
  78. arrow.onDoNextShoot += delegate() {
  79. Destroy(this.gameObject);
  80. };
  81. this.animator.speed = 1;
  82. state.dead = true;
  83. this.agent.enabled = false;
  84. ReleasePreHeight(this);
  85. onDie?.Invoke(this);
  86. AudioMgr.ins.PlayAnimalEffect("bird_injured", AudioMgr.GetAudioSource(this.gameObject));
  87. if (currentHeight < 1.5f) {
  88. arrow.arrowCameraComp.arrowCameraTemplate.SendMsg(0, null);
  89. }
  90. }
  91. void Hurt() {
  92. CancelStand();
  93. CancelFlyStay();
  94. AudioMgr.ins.PlayAnimalEffect("bird_injured", AudioMgr.GetAudioSource(this.gameObject));
  95. }
  96. #endregion
  97. #region 寻路导航
  98. bool _moving;
  99. bool moving {
  100. get { return _moving; }
  101. set {
  102. movingTime = 0;
  103. _moving = value;
  104. }
  105. }
  106. float movingTime;
  107. //启动寻路
  108. void SetDestination(Vector3 pos) {
  109. moving = true;
  110. this.agent.destination = pos;
  111. }
  112. //寻路过程
  113. void OnMovingToDestination() {
  114. if (!moving) return;
  115. movingTime += Time.deltaTime;
  116. }
  117. //寻路结束
  118. void OnReachDestination() {
  119. if (!moving) return;
  120. moving = false;
  121. }
  122. //是否已经接近目的地(寻路完成判断)
  123. bool HasCloseToDestination() {
  124. if (!moving) return true;
  125. return movingTime > 0.1 && this.agent.velocity.magnitude < 0.05f;
  126. }
  127. //停止寻路
  128. void StopNavigation() {
  129. if (!moving) return;
  130. this.agent.destination = this.agent.nextPosition;
  131. this.agent.velocity = Vector3.zero;
  132. this.transform.position = this.agent.destination;
  133. OnReachDestination();
  134. }
  135. #endregion
  136. #region 自动逻辑
  137. //自动策略的更新逻辑
  138. void UpdateAutoStrategy() {
  139. if (state.dead) return;
  140. UpdateFlyUpDown();
  141. UpdateBehavior();
  142. }
  143. float canStandTime = 3f;
  144. float canFlyStayTime = 3f;
  145. //能够在前几棵树徘徊
  146. [System.NonSerialized] public int canFlyTreeCount = 4;
  147. const float downSpeed = 2.0f;
  148. //更新逻辑-起飞降落
  149. void UpdateFlyUpDown() {
  150. if (state.up && state.landing) { //起飞阶段
  151. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  152. bool isTakeOff = animatorStateInfo.IsName("FlyFromGround");
  153. if (isTakeOff) {
  154. if (animatorStateInfo.normalizedTime >= 1.0) {
  155. state.landing = false;
  156. state.flying = true;
  157. }
  158. }
  159. return;
  160. }
  161. if (state.up && state.flying) { //上升阶段
  162. float surplusHeight = toFlyHeight - currentHeight;
  163. float smoothValue = surplusHeight / toFlyHeight;
  164. float nextFlyHeight = currentHeight + Time.deltaTime * (1f + 2.5f * smoothValue);
  165. this.animator.speed = 1f + 2f * smoothValue;
  166. if (nextFlyHeight >= toFlyHeight) { //上升完成
  167. nextFlyHeight = toFlyHeight;
  168. this.animator.speed = 1;
  169. state.up = false;
  170. }
  171. SetFlyHeight(nextFlyHeight);
  172. return;
  173. }
  174. if (state.down && state.flying) { //下降阶段
  175. float nextH = currentHeight - Time.deltaTime * downSpeed;
  176. if (nextH <= 0) { //下降完成
  177. nextH = 0;
  178. state.landing = true;
  179. state.flying = false;
  180. StopNavigation();
  181. }
  182. SetFlyHeight(nextH);
  183. return;
  184. }
  185. if (state.down && state.landing) { //着落阶段
  186. AnimatorStateInfo animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
  187. bool isLand = animatorStateInfo.IsName("LandOnGround");
  188. if (isLand) {
  189. if (animatorStateInfo.normalizedTime >= 1.0) {
  190. state.down = false;
  191. //进入站立状态
  192. state.standing = true;
  193. canStandTime = 8f + Random.value * 2f;
  194. animator.CrossFade("IdleOnGround2", 0.2f);
  195. }
  196. }
  197. return;
  198. }
  199. }
  200. TreeAreaRecorder.AreaInfo targetAreaInfo;
  201. bool needCheckFlyDown = false;
  202. bool willStayOnTree = false;
  203. void UpdateBehavior() {
  204. if (state.flyStaying) {
  205. canFlyStayTime -= Time.deltaTime;
  206. if (canFlyStayTime <= 0) {
  207. state.flyStaying = false;
  208. }
  209. return;
  210. }
  211. if (state.standing) {
  212. canStandTime -= Time.deltaTime;
  213. if (canStandTime <= 0) {
  214. CancelStand();
  215. }
  216. return;
  217. }
  218. if (state.landing) return;
  219. if (state.flying && !state.down && !state.up) {
  220. if (needCheckFlyDown) {
  221. float downNeedTime = currentHeight / downSpeed;
  222. float keyDistance = this.agent.speed * downNeedTime; //触发下降的水平距离阈值
  223. Vector3 myP = this.transform.position;
  224. Vector3 desP = this.agent.destination;
  225. desP.y = myP.y;
  226. float distance = Vector3.Distance(myP, desP);
  227. if (distance < keyDistance) {
  228. needCheckFlyDown = false;
  229. if (Random.value < 0.5 && tryOccupyTree()) {
  230. state.down = true;
  231. } else {
  232. willStayOnTree = true;
  233. }
  234. }
  235. }
  236. }
  237. if (moving) return;
  238. if (willStayOnTree) {
  239. willStayOnTree = false;
  240. state.flyStaying = true;
  241. canFlyStayTime = 9f + Random.value * 1f;
  242. return;
  243. }
  244. //飞向别的树
  245. #region
  246. Vector3 myPos = this.transform.position;
  247. TreeAreaRecorder.AreaInfo[] areaInfos = treeAreaRecorder.copyAreaInfos(canFlyTreeCount);
  248. for (int i = 0; i < canFlyTreeCount; i++) {
  249. areaInfos[i].teamCompareValue = areaInfos[i].distanceInHorizontal(myPos);
  250. }
  251. System.Array.Sort(areaInfos, new ComparerAreaInfosByDistance());
  252. TreeAreaRecorder.AreaInfo areaInfo = areaInfos[Random.Range(1, canFlyTreeCount)];
  253. Vector3 newPos = YejiHuntGameMode.CalculateNewPosByTreePos(animalsBaseT, flyPlaneHeight, areaInfo);
  254. SetDestination(newPos);
  255. targetAreaInfo = areaInfo;
  256. needCheckFlyDown = true;
  257. #endregion
  258. }
  259. class ComparerAreaInfosByDistance : IComparer {
  260. public int Compare(object x, object y) {
  261. TreeAreaRecorder.AreaInfo a = (TreeAreaRecorder.AreaInfo)x;
  262. TreeAreaRecorder.AreaInfo b = (TreeAreaRecorder.AreaInfo)y;
  263. if (a.teamCompareValue - b.teamCompareValue > 0) return 1;
  264. if (a.teamCompareValue - b.teamCompareValue < 0) return -1;
  265. else return 0;
  266. }
  267. }
  268. void CancelStand() {
  269. if (!state.standing) return;
  270. state.standing = false;
  271. state.up = true;
  272. animator.CrossFade("FlyFromGround", 0.1f);
  273. tryReleaseOccupyTree();
  274. }
  275. void CancelFlyStay() {
  276. if (!state.flyStaying) return;
  277. canFlyStayTime = 0;
  278. }
  279. bool tryOccupyTree() {
  280. if (targetAreaInfo == null) return false;
  281. if (targetAreaInfo.occupy != null) return false;
  282. else {
  283. targetAreaInfo.occupy = this;
  284. return true;
  285. }
  286. }
  287. void tryReleaseOccupyTree() {
  288. if (targetAreaInfo != null && targetAreaInfo.occupy != null && targetAreaInfo.occupy.Equals(this)) {
  289. targetAreaInfo.occupy = null;
  290. }
  291. }
  292. static float[] preFlyHeights;
  293. static object[] preFlyHeightCools;
  294. public static void InitPreHeights() {
  295. preFlyHeights = new float[]{1.7f, 2.2f};
  296. preFlyHeightCools = new object[]{null, null};
  297. }
  298. public static float RandomOnePreHeight(Yeji yeji) {
  299. for (int i = 0; i < preFlyHeightCools.Length; i++) {
  300. if (preFlyHeightCools[i] == null) {
  301. preFlyHeightCools[i] = yeji;
  302. return preFlyHeights[i];
  303. }
  304. }
  305. return 2.5f;
  306. }
  307. public static void ReleasePreHeight(Yeji yeji) {
  308. for (int i = 0; i < preFlyHeightCools.Length; i++) {
  309. if (yeji.Equals(preFlyHeightCools[i])) {
  310. preFlyHeightCools[i] = null;
  311. }
  312. }
  313. }
  314. #endregion
  315. //状态
  316. [System.Serializable]
  317. public class State {
  318. public bool standing = false;
  319. [SerializeField] private bool _flyStaying = false;
  320. public bool flyStaying {
  321. get { return _flyStaying; }
  322. set {
  323. _flyStaying = value;
  324. animal.animator.SetBool("flyStaying", value);
  325. }
  326. }
  327. [SerializeField] private bool _dead;
  328. public bool dead {
  329. get { return _dead; }
  330. set {
  331. _dead = value;
  332. animal.animator.SetBool("dead", value);
  333. }
  334. }
  335. [SerializeField] private bool _down;
  336. public bool down {
  337. get { return _down; }
  338. set {
  339. _down = value;
  340. animal.animator.SetBool("down", value);
  341. }
  342. }
  343. [SerializeField] private bool _landing;
  344. public bool landing {
  345. get { return _landing; }
  346. set {
  347. _landing = value;
  348. animal.animator.SetBool("landing", value);
  349. }
  350. }
  351. [SerializeField] private bool _up;
  352. public bool up {
  353. get { return _up; }
  354. set {
  355. _up = value;
  356. animal.animator.SetBool("up", value);
  357. }
  358. }
  359. [SerializeField] private bool _flying;
  360. public bool flying {
  361. get { return _flying; }
  362. set {
  363. _flying = value;
  364. animal.animator.SetBool("flying", value);
  365. }
  366. }
  367. public Yeji animal;
  368. }
  369. [SerializeField] public State state;
  370. //委托
  371. public System.Action<Yeji> onDie;
  372. }