| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class MoveCharacter : Character
- {
- [Header("材质")]
- public float matState = 1;
- public GameObject spinee;
- public MeshRenderer mesh;
- public Material[] mats;
- public Material[] outlineMats;
- public Material[] outline1Mats;
- [Header("组件")]
- public Foot foot;
- private ScreenShake ss;
- [Header("额外重力")]
- public float extraRiseGravity = 0; //上升时额外重力加速度
- public float extraFallGravity = -10; //下落时额外重力加速度
- [Header("属性")]
- public bool canMove = true;
- public float moveSpeed = 5;
- [Header("虚弱状态")]
- public float totalBeRepelValue;
-
- [HideInInspector]
- public float beRepelValue;
- public float weakTime;
- public float totalWeakTime;
- [HideInInspector]
- public float newTotalWeakTime;
- public float weakHitRate = 2;
- [HideInInspector]
- public Vector3 weakForce;
- [Header("易伤减伤")]
- public float easyToGetHit = 0.2f;
- public bool isDamageReduction;
- public float reductionDegree;
- public GameObject reductionEffect;
- [Header("被击飞减速度")]
- public float decelerationRatio = 1f;
- [Header("眩晕参数")]
- public float comaTime = 5;
- public float pastComaTime;
- [Header("漂浮")]
- public int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
- private float rise = 1;
- public float maxTime = 1.5f; //上升最大耗时
- public float minTime = 0.1f; //上升最小耗时
- public float maxHeight = 12; //最大上升高度
- public float minHeight = 7; //最小上升高度
- private float curHeight; //当前所在高度
- private Vector3 origPos; //初始位置
- private float origY;
- private float height; //漂浮高度
- public float floatTime = 20; //漂浮时间
- private float curTime; //漂浮已进行时长
- private float riseTime; //上升时间
- private float pastTime; //上升已用时间
- public float maxRotateSpeed = 20; //最大旋转速度
- public float minRotateSpeed = 5; //最小旋转速度
- private float rotateSpeed; //旋转速度
- private float rotateDir; //旋转方向
- private float backSpeed; //往后退的速度
- public bool isFloat; //正在漂浮中
- public float normalFallSpeed;
- [Header("临时血量")]
- public GameObject effect;
- private int temptHP;
- private int curHP;
- private float continueTime;
- private bool isTempt;
- [Header("变身前记录玩家属性")]
- public SkeletonMecanim playerMe;
- public Animator playerAni;
- public Animator playerCol;
- public Transform playerTran;
- public BeSearchTrigger playerBst;
- public GameObject playerBullet;
- public SearchTrigger playerST;
- public Foot playerFoot;
- public GameObject playerSpinee;
- public MeshRenderer playerMesh;
- public Material[] playerMats;
- public Material[] playerOut;
- [Header("魂")]
- public GameObject soulPrefab;
- public float soulStartSpeed = 1f;
- [Header("隐身")]
- public bool isInvisible;
- public float invisibleTime;
- public float velocityAddition;
- [Header("传送门")]
- public bool haveTransmit;
- [HideInInspector]
- public float transmitTime;
- public PortalsController portalsController;
- [Header("受到持续伤害")]
- public bool isSustainedInjury; //是否正在受到持续伤害
- [HideInInspector] public float sustainedInjuryTime; //存储持续伤害经过的时间
- public float sustainedInjury_IntervalTime; //每次伤害的间隔时间
- public int sustainedInjury_damage; //每次造成的伤害
- [Header("受到重伤")]
- public float heavyDamage;
- private void Awake()
- {
- spinee = bodyTrans.GetChild(0).gameObject;
- mesh = spinee.GetComponent<MeshRenderer>();
- mats = mesh.materials;
- origY = transform.position.y;
- ss = Camera.main.GetComponentInParent<ScreenShake>();
- }
- private void Start()
- {
- playerMe = mecanim;
- playerAni = ani;
- playerTran = bodyTrans;
- playerBst = beSearchTrigger;
- playerBullet = bulletPrefab;
- playerST = searchTrigger;
- playerFoot = foot;
- playerSpinee = spinee;
- playerMesh = mesh;
- playerMats = mats;
- playerOut = outlineMats;
- newTotalWeakTime = totalWeakTime;
- }
- //0:漂浮 1:正常 2:无敌
- public void ChangeMat(int state)
- {
- if((state == 0 && matState == 2) || (state == 2 && matState == 0))
- {
- return;
- }
- if (outline1Mats.Length == 0)
- {
- return;
- }
- if (spinee == null || mesh == null || mats == null)
- {
- spinee = transform.GetChild(0).GetChild(0).gameObject;
- mesh = spinee.GetComponent<MeshRenderer>();
- mats = mesh.materials;
- }
- switch (state)
- {
- case 0:
- mesh.materials = outlineMats;
- break;
- case 1:
- mesh.materials = mats;
- break;
- case 2:
- mesh.materials = outline1Mats;
- break;
- }
- matState = state;
- }
- public void FloatStateOn()
- {
- if (isInvisible || state == CharacterState.BaGua)
- {
- return;
- }
- if (GetComponent<PlayerController>())
- {
- if (GetComponent<PlayerController>().isBaseBtnOut)
- {
- return;
- }
- }
- if (canMove)
- {
- canMove = false;
- isFloat = true;
- ChangeMat(0);
- curTime = 0;
- if (floatState == 0)
- {
- origPos = transform.position;
- origY = origPos.y;
- curHeight = origPos.y;
- }
- origPos.x = transform.position.x;
- ChangeState(CharacterState.Rise);
- floatState = 1;
- riseTime = Random.Range(minTime, maxTime);
- backSpeed = Random.Range(1, 4);
- if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
- {
- backSpeed = -backSpeed;
- }
- rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
- rotateDir = (1.5f - Random.Range(1, 3)) * 2;
- height = Random.Range(minHeight, maxHeight);
- }
- }
- private void RotateSelf()
- {
- transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
- }
- public void CharacterFloat()
- {
- if (floatState == 1)
- {
- RotateSelf();
- curTime += Time.deltaTime;
- curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
- transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- if (curHeight >= height - 0.02f)
- {
- floatState = 2;
- pastTime = curTime;
- height = transform.position.y;
- ChangeState(CharacterState.Float);
- }
- }
- else if (floatState == 2)
- {
- RotateSelf();
- curTime += Time.deltaTime;
- transform.position = new Vector3(origPos.x + backSpeed * (curTime - pastTime), height, origPos.z);
- if (curTime >= floatTime)
- {
- transform.localEulerAngles = new Vector3(0, 0, 0);
- floatState = 3;
- origPos.x = transform.position.x;
- ChangeState(CharacterState.Fall);
- }
- }
- else if (floatState == 3)
- {
- if (transform.position.y >= origY + 0.05f)
- {
- curHeight -= normalFallSpeed * Time.deltaTime;
- transform.position = new Vector3(origPos.x, curHeight, origPos.z);
- }
- else if (foot.TrigGround || curHeight <= origY + 0.05f)
- {
- if (gameObject.layer == 7 && isInSoulTower)
- {
- ChangeState(CharacterState.LockSoul);
- }
- else
- {
- ChangeState(CharacterState.Idle);
- }
- ChangeMat(1);
- floatState = 0;
- isFloat = false;
- canMove = true;
- if (gameObject.tag == "Player")
- {
- if (pc == null)
- {
- pc = GetComponentInParent<PlayerController>();
- }
- pc.soulCollector.enabled = true;
- }
- }
- }
- }
- public void GetTemptHP(int addTemptHP, float time)
- {
- isTempt = true;
- effect.SetActive(true);
- curHP = hp;
- totalHp += addTemptHP;
- hp += addTemptHP;
- temptHP = addTemptHP;
- continueTime = time;
- }
- private void LoseTemptHP()
- {
- isTempt = false;
- effect.SetActive(false);
- totalHp -= temptHP;
- if (hp > curHP)
- {
- hp = curHP;
- }
- }
- public virtual void Update()
- {
- if (isTempt)
- {
- continueTime -= Time.deltaTime;
- if (continueTime <= 0 || hp <= curHP)
- {
- LoseTemptHP();
- }
- }
- if (beLarger)
- {
- Enlarge();
- }
- if (floatState != 0)
- {
- CharacterFloat();
- }
- if (isInvisible)
- {
- invisibleTime -= Time.deltaTime;
- if(invisibleTime <= 0)
- {
- isInvisible = false;
- ChangeMat(1);
- }
- }
- if (haveTransmit)
- {
- transmitTime -= Time.deltaTime;
- if(transmitTime <=0)
- {
- haveTransmit = false;
- portalsController.rbs.Remove(rb);
- }
- }
- //受到持续伤害
- if (isSustainedInjury)
- {
- SustainedInjury();
- }
- }
- //伤害减免状态开启(减免程度,减免时长)
- public void DamageReductionStateOn(float degree, GameObject effect)
- {
- if (reductionEffect == null)
- {
- reductionEffect = Instantiate(effect, transform.position, new Quaternion(0, 0, 0, 0), transform);
- }
- reductionEffect.SetActive(true);
- isDamageReduction = true;
- reductionDegree = degree;
- canNotChangeHurt = true;
- }
- public void DamageReductionStateToOff(float onTime)
- {
- Invoke("DamageReductionStateOff", onTime);
- }
- private void DamageReductionStateOff()
- {
- isDamageReduction = false;
- reductionEffect.SetActive(false);
- canNotChangeHurt = false;
- }
- public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
- {
- if (isInvisible)
- {
- return;
- }
- if (invincibleTime > 0)
- {
- return;
- }
- //漂浮易伤
- if (isFloat)
- {
- damage = (int)((1 + easyToGetHit) * damage);
- }
- //伤害减免
- if (isDamageReduction)
- {
- damage = (int)((1 - reductionDegree) * damage);
- }
- hp -= damage;
- //伤害跳字
- if (showInjuryNum)
- {
- GameObject injuryNum = Instantiate(injuryNumText);
- injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
- TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
- text.text = damage.ToString();
- if (gameObject.CompareTag("Player"))
- {
- text.color = Color.red;
- if (debugAttackFrom)
- {
- Debug.Log("主角受到" + damage.ToString() + "点伤害");
- }
- }
- }
- uiHp.Show(hp, totalHp);
- if (hp <= 0)
- {
- ChangeState(CharacterState.Die);
- if (!canNotAddForce)
- {
- weakForce = force;
- }
- else
- {
- weakForce = Vector3.zero;
- }
- return;
- }
- if (canNotChangeHurt)
- {
- return;
- }
-
- if (changeHurt)
- {
- beRepelValue -= repelValue;
- if (state == CharacterState.Weak)
- {
- if (!canNotAddForce)
- {
- rb.AddForce(force * weakHitRate);
- ChangeState(CharacterState.Weak);
- }
- }
- else if (beRepelValue <= 0)
- {
- if (!canNotAddForce)
- {
- rb.AddForce(force);
- }
- ChangeState(CharacterState.Weak);
- }
- }
- if (gameObject.layer == 6) //屏幕红闪+抖动
- {
- if (ss == null)
- {
- ss = Camera.main.GetComponentInParent<ScreenShake>();
- }
- ss.enabled = true;
- if (isSustainedInjury || damage >= heavyDamage)
- {
- ss.HeavyShakeShine();
- }
- }
- }
- //受到持续伤害
- public void SustainedInjury()
- {
- sustainedInjuryTime += Time.deltaTime;
- if(sustainedInjuryTime >= sustainedInjury_IntervalTime)
- {
- sustainedInjuryTime = 0;
- BeHit(sustainedInjury_damage, Vector3.zero,false,0);
- }
- }
- }
|