Yeji.cs 12 KB

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