Yeji.cs 12 KB

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