MoveCharacter.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using TMPro;
  7. public class MoveCharacter : Character
  8. {
  9. [Header("材质")]
  10. public float matState = 1;
  11. public GameObject spinee;
  12. public MeshRenderer mesh;
  13. public Material[] mats;
  14. public Material[] outlineMats;
  15. public Material[] outline1Mats;
  16. [Header("组件")]
  17. public Foot foot;
  18. [HideInInspector]public AttributeStatus attributeStatus;
  19. [Header("额外重力")]
  20. public float extraRiseGravity = 0; //上升时额外重力加速度
  21. public float extraFallGravity = -10; //下落时额外重力加速度
  22. [Header("属性")]
  23. public float moveSpeed = 5;
  24. public bool needToAdjustFlyHeight;
  25. [DisplayOnly]
  26. public float flyHeight;
  27. public float maxFlyHeight;
  28. public float minFlyHeight;
  29. float refspeed = 1;
  30. public float flyUpTime;
  31. [HideInInspector]
  32. public int isAdjustHeight;
  33. [Header("减伤")]
  34. public bool isDamageReduction;
  35. public float reductionDegree;
  36. public GameObject reductionEffect;
  37. [Header("临时血量")]
  38. public GameObject effect;
  39. private int temptHP;
  40. private int curHP;
  41. private float continueTime;
  42. private bool isTempt;
  43. [Header("魂")]
  44. public GameObject soulPrefab;
  45. public float soulStartSpeed = 1f;
  46. [Header("隐身")]
  47. public bool isInvisible;
  48. public float invisibleTime;
  49. public float velocityAddition;
  50. [Header("传送门")]
  51. public bool haveTransmit;
  52. [HideInInspector]
  53. public float transmitTime;
  54. public PortalsController portalsController;
  55. [Header("受到持续伤害")]
  56. public bool isSustainedInjury; //是否正在受到持续伤害
  57. [HideInInspector] public float sustainedInjuryTime; //存储持续伤害经过的时间
  58. public float sustainedInjury_IntervalTime; //每次伤害的间隔时间
  59. public int sustainedInjury_damage; //每次造成的伤害
  60. [Header("受到重伤")]
  61. public float heavyDamage;
  62. public virtual void Awake()
  63. {
  64. spinee = bodyTrans.GetChild(0).gameObject;
  65. mesh = spinee.GetComponent<MeshRenderer>();
  66. mats = mesh.materials;
  67. attributeStatus = GetComponent<AttributeStatus>();
  68. }
  69. private void Start()
  70. {
  71. }
  72. //0:漂浮 1:正常 2:无敌
  73. public void ChangeMat(int state)
  74. {
  75. if((state == 0 && matState == 2) || (state == 2 && matState == 0))
  76. {
  77. return;
  78. }
  79. if (outline1Mats.Length == 0)
  80. {
  81. return;
  82. }
  83. if (spinee == null || mesh == null || mats == null)
  84. {
  85. spinee = transform.GetChild(0).GetChild(0).gameObject;
  86. mesh = spinee.GetComponent<MeshRenderer>();
  87. mats = mesh.materials;
  88. }
  89. switch (state)
  90. {
  91. case 0:
  92. mesh.materials = outlineMats;
  93. break;
  94. case 1:
  95. mesh.materials = mats;
  96. break;
  97. case 2:
  98. mesh.materials = outline1Mats;
  99. break;
  100. }
  101. matState = state;
  102. }
  103. public void GetTemptHP(int addTemptHP, float time)
  104. {
  105. isTempt = true;
  106. effect.SetActive(true);
  107. curHP = hp;
  108. totalHp += addTemptHP;
  109. hp += addTemptHP;
  110. temptHP = addTemptHP;
  111. continueTime = time;
  112. }
  113. private void LoseTemptHP()
  114. {
  115. isTempt = false;
  116. effect.SetActive(false);
  117. totalHp -= temptHP;
  118. if (hp > curHP)
  119. {
  120. hp = curHP;
  121. }
  122. }
  123. public virtual void Update()
  124. {
  125. if (isTempt)
  126. {
  127. continueTime -= Time.deltaTime;
  128. if (continueTime <= 0 || hp <= curHP)
  129. {
  130. LoseTemptHP();
  131. }
  132. }
  133. if (beLarger)
  134. {
  135. Enlarge();
  136. }
  137. if (isInvisible)
  138. {
  139. invisibleTime -= Time.deltaTime;
  140. if(invisibleTime <= 0)
  141. {
  142. isInvisible = false;
  143. ChangeMat(1);
  144. }
  145. }
  146. if (haveTransmit)
  147. {
  148. transmitTime -= Time.deltaTime;
  149. if(transmitTime <=0)
  150. {
  151. haveTransmit = false;
  152. portalsController.rbs.Remove(rb);
  153. }
  154. }
  155. //受到持续伤害
  156. if (isSustainedInjury)
  157. {
  158. SustainedInjury();
  159. }
  160. }
  161. //伤害减免状态开启(减免程度,减免时长)
  162. public void DamageReductionStateOn(float degree, GameObject effect)
  163. {
  164. if (reductionEffect == null)
  165. {
  166. reductionEffect = Instantiate(effect, transform.position, new Quaternion(0, 0, 0, 0), transform);
  167. }
  168. reductionEffect.SetActive(true);
  169. isDamageReduction = true;
  170. reductionDegree = degree;
  171. canNotChangeHurt = true;
  172. }
  173. public void DamageReductionStateToOff(float onTime)
  174. {
  175. Invoke("DamageReductionStateOff", onTime);
  176. }
  177. private void DamageReductionStateOff()
  178. {
  179. isDamageReduction = false;
  180. reductionEffect.SetActive(false);
  181. canNotChangeHurt = false;
  182. }
  183. //仅造成伤害
  184. public override void BeHit(int damage)
  185. {
  186. base.BeHit(damage);
  187. }
  188. //造成伤害附加其他效果
  189. public override void BeHit(AttackInfo attackInfo, float dir)
  190. {
  191. base.BeHit(attackInfo, dir);
  192. int damage = attackInfo.damage;
  193. if (attackInfo.attackEffect.Length > 0)
  194. {
  195. foreach (AttackEffect ae in attackInfo.attackEffect)
  196. {
  197. switch (ae)
  198. {
  199. /*控制*/
  200. //漂浮
  201. case AttackEffect.FloatState:
  202. attributeStatus.AddFloat(attackInfo.floatState);
  203. break;
  204. //击飞
  205. case AttackEffect.BlowUp:
  206. attributeStatus.AddBlowUp(attackInfo.blowUp,dir);
  207. break;
  208. //击落
  209. case AttackEffect.ShotDown:
  210. attributeStatus.AddShotDown(attackInfo.shotDown,dir);
  211. break;
  212. //击晕
  213. case AttackEffect.Stun:
  214. attributeStatus.AddStun(attackInfo.stun);
  215. break;
  216. /*非控制*/
  217. //穿甲
  218. case AttackEffect.Armor:
  219. damage = attributeStatus.AddArmor(attackInfo.armor, damage);
  220. break;
  221. //易伤
  222. case AttackEffect.Vulnerable:
  223. attributeStatus.AddVulnerable(attackInfo.vulnerable);
  224. break;
  225. }
  226. }
  227. }
  228. damage = attributeStatus.DamageCalculation(damage);
  229. //伤害减免
  230. if (isDamageReduction)
  231. {
  232. damage = (int)((1 - reductionDegree) * damage);
  233. }
  234. hp -= damage;
  235. //伤害跳字
  236. if (showInjuryNum)
  237. {
  238. GameObject injuryNum = Instantiate(injuryNumText);
  239. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  240. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  241. text.text = damage.ToString();
  242. if (gameObject.CompareTag("Player"))
  243. {
  244. text.color = Color.red;
  245. if (debugAttackFrom)
  246. {
  247. Debug.Log("主角受到" + damage.ToString() + "点伤害");
  248. }
  249. }
  250. }
  251. uiHp.Show(hp, totalHp);
  252. if (hp <= 0)
  253. {
  254. ChangeState(CharacterState.Die);
  255. return;
  256. }
  257. }
  258. //受到持续伤害
  259. public void SustainedInjury()
  260. {
  261. sustainedInjuryTime += Time.deltaTime;
  262. if(sustainedInjuryTime >= sustainedInjury_IntervalTime)
  263. {
  264. sustainedInjuryTime = 0;
  265. BeHit(sustainedInjury_damage);
  266. }
  267. }
  268. public override bool AdjustHeight()
  269. {
  270. if (canFly)
  271. {
  272. if (transform.position.y - flyHeight > 0.1f)
  273. {
  274. Vector3 pos = transform.position;
  275. pos.y = Mathf.SmoothDamp(pos.y, flyHeight, ref refspeed, flyUpTime);
  276. transform.position = pos;
  277. return false;
  278. }
  279. else if (transform.position.y - flyHeight < -0.1f)
  280. {
  281. Vector3 pos = transform.position;
  282. pos.y = Mathf.SmoothDamp(pos.y, flyHeight, ref refspeed, flyUpTime);
  283. transform.position = pos;
  284. return false;
  285. }
  286. else
  287. {
  288. Vector3 pos = transform.position;
  289. pos.y = flyHeight;
  290. transform.position = pos;
  291. isAdjustHeight = 2;
  292. }
  293. }
  294. return true;
  295. }
  296. }