Character.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using TMPro;
  7. using Sirenix.OdinInspector;
  8. //角色状态
  9. public enum CharacterState
  10. {
  11. None = 0,
  12. Idle = 1,
  13. Run = 2,
  14. Rise = 3, //空中上升
  15. Fall = 4, //空中下落
  16. Attack = 6,
  17. KeepAttack = 7,
  18. Summon = 8,
  19. Rush = 9,
  20. Sprint = 10,
  21. Die = 11,
  22. HitStun, //僵直
  23. FramePause, //顿帧
  24. //控制效果
  25. SpecialStatus_Float,
  26. SpecialStatus_ShotDown,
  27. SpecialStatus_BlowUp,
  28. SpecialStatus_Weak,
  29. LockSoul = 25, //在锁魂塔中
  30. Conduct = 26, //在指挥中
  31. }
  32. public class Character : MonoBehaviour
  33. {
  34. [FoldoutGroup("组件", order: -1)] public Rigidbody rb;
  35. [FoldoutGroup("组件")] public Transform bodyTrans;
  36. [FoldoutGroup("组件")] public BeSearchTrigger beSearchTrigger;
  37. [FoldoutGroup("组件")] public SearchTrigger searchTrigger;
  38. [FoldoutGroup("组件")] public GameObject bodyCollider;
  39. [FoldoutGroup("组件")] public UIHP uiHp;
  40. [FoldoutGroup("组件")] public BeHitTrigger beHitTrigger;
  41. [FoldoutGroup("组件")] public AttackController attackController;
  42. [FoldoutGroup("组件")] public Platform platform;
  43. [Space(30)]
  44. [Title("Character属性")]
  45. [LabelText("骨骼")] public SkeletonMecanim mecanim;
  46. public Skeleton skeleton;
  47. [HideInInspector]
  48. public MeshRenderer meshRenderer;
  49. [FoldoutGroup("动画", order: -1)] public Animator ani;
  50. [FoldoutGroup("动画")] public float totalDieKeepTime = 2f;
  51. [Header("跳字开关")]
  52. [FoldoutGroup("伤害跳字")] public bool showInjuryNum;
  53. [Header("跳字样式")]
  54. [FoldoutGroup("伤害跳字", order: -1)] public GameObject injuryNumText; //伤害跳字
  55. [FoldoutGroup("伤害跳字")] public GameObject injuryNumTextSummon; //伤害跳字(起手式)
  56. [Header("跳字位置")]
  57. [FoldoutGroup("伤害跳字")] [LabelText("起手式位置")] public Vector2 injuryNumPos_summon = new Vector2(0, 1);
  58. [FoldoutGroup("伤害跳字")] [LabelText("起手式随机范围")] public Vector2 injuryNumRandom_summon = Vector2.one;
  59. [FoldoutGroup("伤害跳字")] [LabelText("行军式位置")] public Vector2 injuryNumPos_march = new Vector2(0, 1);
  60. [FoldoutGroup("伤害跳字")] [LabelText("行军式随机范围")] public Vector2 injuryNumRandom_march = Vector2.one * 2;
  61. [Header("死亡后多久尸体消失")]
  62. [HideInInspector]
  63. public float dieKeepTime;
  64. [Header("受击距离")]
  65. public float beHitDistance;
  66. [HideInInspector]
  67. public float attackDis;
  68. //当前目标
  69. [DisplayOnly]
  70. public Character targetCharacter;
  71. [HideInInspector]
  72. public Character attackTarget;
  73. [DisplayOnly]
  74. [FoldoutGroup("角色信息", order: -1)] public CharacterState state;
  75. [FoldoutGroup("角色信息")] public int totalHp = 100;
  76. [FoldoutGroup("角色信息")] public int hp;
  77. [Header("角色状态")]
  78. public bool canFly = false;
  79. [LabelText("飞行单位高度可进行修改")]
  80. [Tooltip("本来飞行的怪初始化锁定刚体Y轴,勾选这个可以让飞行单位刚体Y轴解锁(例如攻击状态下需要利用刚体向下冲刺的飞鹤)")]
  81. public bool canControlFly = false;
  82. [HideInInspector] public bool nowCanFly; //当前是否能飞行
  83. [LabelText("不会被击落")][Tooltip("本来不能飞行的怪没法被击落,取消勾选这个可以让那些地面怪也被击落(例如爬上树的水猴子)")] public bool canNotShotDown = true;
  84. [LabelText("不会被击飞")] public bool canNotAddForce;
  85. [LabelText("不会被虚弱")] public bool canNotChangeHurt;
  86. [DisplayOnly]
  87. public bool isDie = false; //已死亡
  88. [DisplayOnly]
  89. public bool isRevive; //从虚弱状态恢复中
  90. [DisplayOnly]
  91. public float invincibleTime; //无敌时间
  92. [Header("状态显示(临时)")]
  93. public GameObject statePre;
  94. private TextMeshProUGUI stateText;
  95. private float pastWordTime;
  96. [Header("锁魂塔")]
  97. public LockSoul ls;
  98. public bool isInSoulTower; //在锁魂塔范围内
  99. [Header("体型增大")]
  100. [HideInInspector]
  101. public bool beLarger = false;
  102. public float toLargeSize = 0; //变大程度
  103. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  104. [Header("特效")]
  105. public GameObject cookEffect; //吃串加血
  106. [Header("传送门")]
  107. public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
  108. //调试开关
  109. [Space]
  110. [LabelText("debug攻击者")] public bool debugAttackFrom;
  111. public virtual void Init()
  112. {
  113. //确保组件不丢失
  114. if (!mecanim)
  115. {
  116. mecanim = GetComponentInChildren<SkeletonMecanim>();
  117. }
  118. if (mecanim && skeleton == null)
  119. {
  120. skeleton = mecanim.skeleton;
  121. }
  122. if (!meshRenderer)
  123. {
  124. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  125. }
  126. if (!ani)
  127. {
  128. ani = GetComponentInChildren<Animator>();
  129. }
  130. //血量重置
  131. hp = totalHp;
  132. nowCanFly = canFly;
  133. if (canFly && !canControlFly && rb != null)
  134. {
  135. rb.useGravity = false;
  136. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
  137. }
  138. else if (canFly && canControlFly && rb != null)
  139. {
  140. rb.useGravity = false;
  141. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  142. }
  143. uiHp.Show(hp, totalHp);
  144. isDie = false;
  145. ChangeState(CharacterState.Idle);
  146. if (attackController != null)
  147. {
  148. attackController.Init();
  149. }
  150. }
  151. public virtual void FixedUpdate()
  152. {
  153. OnState();
  154. }
  155. public void Turn()
  156. {
  157. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  158. if (platform != null)
  159. {
  160. platform.left.transform.localPosition = new Vector3(-0.5f * bodyTrans.localScale.x, 0, 0);
  161. platform.right.transform.localPosition = new Vector3(0.5f * bodyTrans.localScale.x, 0, 0);
  162. }
  163. }
  164. public void CheckTurn(float dirX)
  165. {
  166. if (dirX > 0.1f)
  167. {
  168. if (bodyTrans.localScale.x > 0)
  169. {
  170. Turn();
  171. }
  172. }
  173. else if (dirX < -0.1f)
  174. {
  175. if (bodyTrans.localScale.x < 0)
  176. {
  177. Turn();
  178. }
  179. }
  180. }
  181. public virtual void OnState()
  182. {
  183. if (pastWordTime > 0)
  184. {
  185. pastWordTime -= Time.deltaTime;
  186. if (pastWordTime <= 0)
  187. {
  188. pastWordTime = 0;
  189. }
  190. }
  191. }
  192. public virtual void ChangeState(CharacterState newState)
  193. {
  194. }
  195. public void DebugAttackFrom(string attackFrom, int damage)
  196. {
  197. Debug.Log(attackFrom + "对" + gameObject.name + "使用了" + damage.ToString() + "点伤害的攻击");
  198. }
  199. //仅造成伤害
  200. public virtual void BeHit(int damage)
  201. {
  202. if (invincibleTime > 0)
  203. {
  204. return;
  205. }
  206. hp -= damage;
  207. //伤害跳字
  208. if (showInjuryNum)
  209. {
  210. GameObject injuryNum;
  211. injuryNum = PoolManager.Instantiate(injuryNumText);
  212. injuryNum.transform.position = injuryNum.transform.position = new Vector3(
  213. transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
  214. transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
  215. transform.position.z);
  216. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  217. text.text = damage.ToString();
  218. }
  219. uiHp.Show(hp, totalHp);
  220. if (hp <= 0)
  221. {
  222. ChangeState(CharacterState.Die);
  223. return;
  224. }
  225. }
  226. //造成伤害附加其他效果
  227. public virtual void BeHit(AttackInfo attackInfo, Character attackFrom, int damage = -1)
  228. {
  229. }
  230. public virtual Vector3 GetMoveDir()
  231. {
  232. Vector3 moveDir = Vector3.zero;
  233. return moveDir;
  234. }
  235. public virtual void SetSortingOrder(int order)
  236. {
  237. meshRenderer.sortingOrder = order;
  238. }
  239. //吃串增加血量上限
  240. public void HpUp(float value, float larger)
  241. {
  242. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  243. int add = 0;
  244. value = value / 100;
  245. add = (int)(totalHp * value);
  246. totalHp += add;
  247. hp += add;
  248. uiHp.Show(hp, totalHp);
  249. float cur = transform.localScale.x;
  250. toLargeSize = cur * larger;
  251. beLarger = true;
  252. }
  253. //体型变大
  254. public void Enlarge()
  255. {
  256. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  257. if (transform.localScale.x >= toLargeSize - 0.1f)
  258. {
  259. beLarger = false;
  260. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  261. toLargeSize = 0;
  262. }
  263. }
  264. public virtual bool AdjustHeight()
  265. {
  266. return true;
  267. }
  268. public void ChangeStateText(CharacterState state)
  269. {
  270. if (!statePre)
  271. {
  272. return;
  273. }
  274. GameObject obj = PoolManager.Instantiate(statePre);
  275. stateText = obj.GetComponentInChildren<TextMeshProUGUI>();
  276. obj.transform.position = transform.position + Vector3.up;
  277. pastWordTime += 0.5f;
  278. switch (state)
  279. {
  280. case CharacterState.SpecialStatus_Float:
  281. stateText.text = "漂浮";
  282. break;
  283. case CharacterState.SpecialStatus_ShotDown:
  284. stateText.text = "击落";
  285. break;
  286. case CharacterState.SpecialStatus_BlowUp:
  287. stateText.text = "击飞";
  288. break;
  289. case CharacterState.SpecialStatus_Weak:
  290. stateText.text = "击晕";
  291. break;
  292. case CharacterState.HitStun:
  293. stateText.text = "僵直";
  294. break;
  295. }
  296. obj.SetActive(true);
  297. }
  298. }