| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using Sirenix.OdinInspector;
- //各个状态
- public enum SpecialState
- {
- Null = -1,
- FloatState = 0,
- BlownUp = 1,
- ShotDown = 2,
- Weak = 3,
- }
- public class AttributeStatus : MonoBehaviour
- {
- //组件
- private MoveCharacter character;
- private Rigidbody rb;
- private Foot foot;
- private HitFeedbackSystem hitFeedbackSystem;
- //behit参数
- [HideInInspector] public bool haveNewSpecialStates;
- [HideInInspector] public AttackInfo attackInfo;
- [HideInInspector] public Character attackFrom;
- [DisplayOnly] public SpecialState curSpecialStates = SpecialState.Null;
- [LabelText("控制时间")] [DisplayOnly] public float attributeTime;
- [TabGroup("漂浮")]
- [DisplayOnly] public int floatingState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
- private Vector3 origPos; //初始位置
- private float curHeight; //当前所在高度
- private float riseTime; //上升时间
- private float backSpeed; //返回速度
- private float rotateSpeed; //旋转速度
- private float rotateDir; //旋转方向
- private float height; //漂浮高度
- private float rise = 1;
- private float normalFallSpeed = 15f;
- [TabGroup("击飞击落")] [DisplayOnly] public int hitState;
- [TabGroup("击飞击落")] [DisplayOnly] public bool isFly;
- [TabGroup("击飞击落")] [LabelText("X方向阻力")] public float decelerationRatioX = 2f;
- [TabGroup("击飞击落")] [LabelText("Y方向阻力")] public float decelerationRatioY = 15f;
- [TabGroup("易伤")]
- [DisplayOnly] public bool haveVulnerable;
- [TabGroup("易伤")]
- [DisplayOnly] public float vulnerableTime;
- private float vulnerableRate;
- //抗性
- [Serializable]
- public struct Resistances
- {
- //控制效果抗性
- [Range(0, 1)]
- [LabelText("漂浮抗性")]
- public float Float;
- [Range(0, 1)]
- [LabelText("击飞抗性")]
- public float BlowUp;
- [Range(0, 1)]
- [LabelText("击落抗性")]
- public float ShotDown;
- [Range(0, 1)]
- [LabelText("击晕抗性")]
- public float Weak;
- [Space]
- //非控制效果抗性
- [LabelText("护甲值")] public int armor;
- }
- [LabelText("抗性")] public Resistances resistances;
- private void Awake()
- {
- character = GetComponentInParent<MoveCharacter>();
- rb = character.rb;
- foot = character.foot;
- hitFeedbackSystem = GetComponent<HitFeedbackSystem>();
- }
- public void Update()
- {
- //易伤
- vulnerableTime -= Time.deltaTime;
- if (vulnerableTime <= 0)
- {
- haveVulnerable = false;
- }
- }
- public void AddSpecialState()
- {
- if (!haveNewSpecialStates)
- {
- hitFeedbackSystem.EnterHitStun();
- return;
- }
- hitFeedbackSystem.canHitStun = false;
- haveNewSpecialStates = false;
- switch (curSpecialStates)
- {
- case SpecialState.FloatState:
- AddFloat(attackInfo.floatState);
- break;
- case SpecialState.BlownUp:
- if (rb.useGravity)
- {
- AddBlowUp(attackInfo.blowUp, attackFrom.transform.position.x < character.transform.position.x ? -1 : 1);
- }
- break;
- case SpecialState.ShotDown:
- if (!rb.useGravity)
- {
- AddShotDown(attackInfo.shotDown, attackFrom.transform.position.x < character.transform.position.x ? -1 : 1);
- }
- break;
- case SpecialState.Weak:
- AddWeak(attackInfo.weak);
- break;
- }
- }
- //CharacterState为SpecialStatus时调用此函数
- public void SpecialStateEffect(SpecialState specialState)
- {
- switch (specialState)
- {
- //漂浮
- case SpecialState.FloatState:
- switch (floatingState)
- {
- case 1:
- character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
- curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
- character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- if (curHeight >= height - 0.02f)
- {
- floatingState = 2;
- height = character.transform.position.y;
- }
- break;
- case 2:
- character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
- break;
- case 3:
- if (character.transform.position.y >= origPos.y + 0.05f)
- {
- curHeight -= normalFallSpeed * Time.deltaTime;
- character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- }
- else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
- {
- floatingState = 0;
- character.isAdjustHeight = 1;
- OutSpecialState();
- return;
- }
- break;
- }
- PlayerController playerController = GetComponent<PlayerController>();
- if (playerController != null)
- {
- if (playerController.mp > 0)
- {
- playerController.lostMp += playerController.mpReplySpeed * Time.deltaTime;
- playerController.mp -= playerController.mpReplySpeed * Time.deltaTime;
- }
- if (playerController.lostMp >= playerController.addMp)
- {
- Instantiate(playerController.soul, character.transform.position, new Quaternion(0, 0, 0, 0), null);
- playerController.lostMp = 0;
- }
- }
- attributeTime -= Time.deltaTime;
- if (attributeTime <= 0)
- {
- character.transform.localEulerAngles = Vector3.zero;
- floatingState = 3;
- rb.useGravity = true;
- }
- break;
- //击飞
- case SpecialState.BlownUp:
- //击落
- case SpecialState.ShotDown:
- switch (hitState)
- {
- case 0:
- Vector3 vel = rb.velocity;
- if (isFly && foot.TrigGround && vel.y <= 0.01f)
- {
- vel = Vector3.zero;
- character.ani.Play("weak", 0, 0);
- hitState = 1;
- }
- else
- {
- vel.x -= decelerationRatioX * Time.deltaTime;
- vel.y -= decelerationRatioY * Time.deltaTime;
- isFly = true;
- }
- rb.velocity = vel;
- break;
- case 1:
- //眩晕状态
- if (attributeTime <= 0)
- {
- character.isAdjustHeight = 1;
- OutSpecialState();
- }
- else
- {
- rb.velocity = Vector3.zero;
- attributeTime -= Time.deltaTime;
- }
- break;
- }
- break;
- //眩晕
- case SpecialState.Weak:
- if (attributeTime <= 0)
- {
- OutSpecialState();
- }
- else
- {
- rb.velocity = Vector3.zero;
- attributeTime -= Time.deltaTime;
- }
- break;
- }
- }
- public void OutSpecialState()
- {
- curSpecialStates = SpecialState.Null;
- if (character.canFly)
- {
- rb.useGravity = false;
- rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
- }
- character.ChangeState(CharacterState.Idle);
- }
- public int DamageCalculation(int damage)
- {
- damage = (int)(damage * (1 + vulnerableRate) + 0.5f);
- return damage;
- }
- //判断优先级,ture为优先级高于当前控制
- public bool PriorityOrder(SpecialState specialState, AttackInfo attackInfo, Character attackFrom)
- {
- if (curSpecialStates != SpecialState.Null && curSpecialStates >= specialState)
- {
- curSpecialStates = specialState;
- this.attackInfo = attackInfo;
- this.attackFrom = attackFrom;
- haveNewSpecialStates = true;
- return true;
- }
- if (curSpecialStates == SpecialState.Null)
- {
- curSpecialStates = specialState;
- this.attackInfo = attackInfo;
- this.attackFrom = attackFrom;
- haveNewSpecialStates = true;
- return true;
- }
- return true;
- }
- //受到漂浮
- public void AddFloat(AttackInfo.FloatState floatState)
- {
- rb.useGravity = false;
- floatingState = 1;
- origPos = character.transform.position;
- curHeight = origPos.y;
- riseTime = UnityEngine.Random.Range(floatState.upTime.x, floatState.upTime.y);
- backSpeed = UnityEngine.Random.Range(floatState.backSpeed.x, floatState.backSpeed.y);
- if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
- {
- backSpeed = -backSpeed;
- }
- rotateSpeed = UnityEngine.Random.Range(floatState.rotateSpeed.x, floatState.rotateSpeed.y);
- rotateDir = (1.5f - UnityEngine.Random.Range(1, 3)) * 2;
- height = UnityEngine.Random.Range(floatState.height.x, floatState.height.y);
- attributeTime = floatState.time * (1 - resistances.Float);
- character.ChangeState(CharacterState.SpecialStatus_Float);
- character.ChangeStateText(CharacterState.SpecialStatus_Float);
- }
- //受到击飞
- public void AddBlowUp(AttackInfo.BlowUp blowUp, float dir)
- {
- attributeTime = blowUp.time * (1 - resistances.BlowUp);
- Vector3 vec3 = blowUp.dir.normalized;
- if (dir < 0)
- {
- vec3.x = -vec3.x;
- }
- rb.useGravity = true;
- rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
- rb.velocity = Vector3.zero;
- rb.AddForce(vec3 * blowUp.force * (1 - resistances.BlowUp), ForceMode.Impulse);
- character.ani.Play("hitted", 0, 0);
- hitState = 0;
- isFly = false;
- character.ChangeState(CharacterState.SpecialStatus_BlowUp);
- character.ChangeStateText(CharacterState.SpecialStatus_BlowUp);
- }
- //受到击落
- public void AddShotDown(AttackInfo.ShotDown shotDown, float dir)
- {
- attributeTime = shotDown.time * (1 - resistances.ShotDown);
- Vector3 vec3 = shotDown.dir.normalized;
- if (dir < 0)
- {
- vec3.x = -vec3.x;
- }
- rb.useGravity = true;
- rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
- rb.velocity = Vector3.zero;
- rb.AddForce(vec3 * shotDown.force * (1 - resistances.ShotDown), ForceMode.Impulse);
- character.ani.Play("hitted", 0, 0);
- hitState = 0;
- isFly = false;
- character.ChangeState(CharacterState.SpecialStatus_ShotDown);
- character.ChangeStateText(CharacterState.SpecialStatus_ShotDown);
- }
- //受到击晕
- public void AddWeak(AttackInfo.Weak weak)
- {
- attributeTime = weak.time * (1 - resistances.Weak);
- character.ani.Play("weak", 0, 0);
- character.ChangeState(CharacterState.SpecialStatus_Weak);
- character.ChangeStateText(CharacterState.SpecialStatus_Weak);
- }
- //受到穿甲
- public int AddArmor(AttackInfo.Armor armor, int damage)
- {
- //计算护甲减免
- int am = resistances.armor;
- if (am > 0)
- {
- int ap = armor.rate;
- int c = am - ap;
- if (c < 0)
- {
- c = 0;
- }
- damage = (int)(damage * (100f / (100 + c)) + 0.5f);
- }
- return damage;
- }
- //受到易伤
- public void AddVulnerable(AttackInfo.Vulnerable vulnerable)
- {
- vulnerableTime = vulnerable.time;
- vulnerableRate = vulnerable.rate;
- haveVulnerable = true;
- }
- }
|