MoveCharacter.cs 10 KB

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