Yeji.cs 12 KB

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