Character.cs 13 KB

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