| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using Sirenix.OdinInspector;
- //角色状态
- public enum CharacterState
- {
- None = 0,
- Idle = 1,
- Run = 2,
- Rise = 3, //空中上升
- Fall = 4, //空中下落
- Attack = 6,
- KeepAttack = 7,
- Summon = 8,
- Rush = 9,
- Sprint = 10,
- Die = 11,
- HitStun, //僵直
- //控制效果
- SpecialStatus_Float,
- SpecialStatus_ShotDown,
- SpecialStatus_BlowUp,
- SpecialStatus_Weak,
- LockSoul = 25, //在锁魂塔中
- Conduct = 26, //在指挥中
- }
- public class Character : MonoBehaviour
- {
- [FoldoutGroup("组件")] public Rigidbody rb;
- [FoldoutGroup("组件")] public Transform bodyTrans;
- [FoldoutGroup("组件")] public BeSearchTrigger beSearchTrigger;
- [FoldoutGroup("组件")] public SearchTrigger searchTrigger;
- [FoldoutGroup("组件")] public GameObject bodyCollider;
- [FoldoutGroup("组件")] public UIHP uiHp;
- [FoldoutGroup("组件")] public BeHitTrigger beHitTrigger;
- [FoldoutGroup("组件")] public AttackController attackController;
- [Header("骨骼")]
- public SkeletonMecanim mecanim;
- public Skeleton skeleton;
- [HideInInspector]
- public MeshRenderer meshRenderer;
- [Header("动画控制器")]
- public Animator ani;
-
- [Header("重要动画时间")]
- public float totalDieKeepTime = 2f;
- public float totalAttack_summonTime = 0.5f;
- public float totalAttack_marchTime = 0.5f;
- [Header("死亡后多久尸体消失")]
- [HideInInspector]
- public float dieKeepTime;
-
- [Header("受击距离")]
- public float beHitDistance;
- [HideInInspector]
- public float attackDis;
- [DisplayOnly]
- public Character targetCharacter;
- [HideInInspector]
- public Character attackTarget;
- [Header("角色状态")]
- [DisplayOnly]
- public CharacterState state;
- public int totalHp = 100;
- public int hp;
- [DisplayOnly]
- public bool isDie = false; //已死亡
- [DisplayOnly]
- public bool isRevive; //从虚弱状态恢复中
- public bool canNotAddForce; //不会被打飞
- public bool canNotChangeHurt; //不会被打虚弱
- [DisplayOnly]
- public float invincibleTime; //无敌时间
- public GameObject injuryNumText;//伤害跳字
- public GameObject injuryNumTextSummon;//伤害跳字(起手式)
- [HideInInspector]
- public bool isBeHitBySummonAttack;
- public bool showInjuryNum; //伤害跳字开关
- public bool canFly = false;
- [HideInInspector]public bool nowCanFly; //当前是否能飞行
- [Header("状态显示")]
- public GameObject statePre;
- private GameObject curStateObj;
- private TextMeshProUGUI stateText;
- private float pastWordTime;
- [Header("锁魂塔")]
- public LockSoul ls;
- public bool isInSoulTower; //在锁魂塔范围内
- [Header("体型增大")]
- [HideInInspector]
- public bool beLarger = false;
- public float toLargeSize = 0; //变大程度
- private Vector3 speed = new Vector3(1, 1, 0); //变大速度
- [Header("特效")]
- public GameObject cookEffect; //吃串加血
- [Header("传送门")]
- public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
- //调试开关
- [Header("debug攻击者")] public bool debugAttackFrom;
- [FoldoutGroup("伤害跳字")] [LabelText("起手式位置")] public Vector2 injuryNumPos_summon = new Vector2(0, 1);
- [FoldoutGroup("伤害跳字")] [LabelText("起手式随机范围")] public Vector2 injuryNumRandom_summon = Vector2.one;
- [FoldoutGroup("伤害跳字")] [LabelText("行军式位置")] public Vector2 injuryNumPos_march = new Vector2(0, 1);
- [FoldoutGroup("伤害跳字")] [LabelText("行军式随机范围")] public Vector2 injuryNumRandom_march = Vector2.one * 2;
- public virtual void Init()
- {
- //确保组件不丢失
- if (!mecanim)
- {
- mecanim = GetComponentInChildren<SkeletonMecanim>();
- }
- if (mecanim && skeleton == null)
- {
- skeleton = mecanim.skeleton;
- }
- if (!meshRenderer)
- {
- meshRenderer = mecanim.GetComponent<MeshRenderer>();
- }
- if (!ani)
- {
- ani = GetComponentInChildren<Animator>();
- }
- //血量重置
- hp = totalHp;
- nowCanFly = canFly;
- uiHp.Show(hp, totalHp);
- ChangeState(CharacterState.Idle);
- if (attackController != null)
- {
- attackController.Init();
- }
- }
- public virtual void FixedUpdate()
- {
- OnState();
- }
- public void Turn()
- {
- bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- public virtual void OnState()
- {
- if (pastWordTime > 0)
- {
- pastWordTime -= Time.deltaTime;
- if (pastWordTime <= 0)
- {
- curStateObj.SetActive(false);
- pastWordTime = 0;
- }
- }
- }
- public virtual void ChangeState(CharacterState newState)
- {
- }
-
- public void DebugAttackFrom(string attackFrom, int damage)
- {
- Debug.Log(attackFrom + "对" + gameObject.name + "使用了" + damage.ToString() + "点伤害的攻击");
- }
- //仅造成伤害
- public virtual void BeHit(int damage)
- {
- if (invincibleTime > 0)
- {
- return;
- }
- hp -= damage;
- //伤害跳字
- if (showInjuryNum)
- {
- GameObject injuryNum;
- //是起手式
- if (isBeHitBySummonAttack)
- {
- injuryNum = Instantiate(injuryNumTextSummon);
- injuryNum.transform.position = new Vector3(
- transform.position.x + injuryNumPos_summon.x + Random.Range(-injuryNumRandom_summon.x/2f, injuryNumRandom_summon.x/2f),
- transform.position.y + injuryNumPos_summon.y + Random.Range(-injuryNumRandom_summon.y/2f, injuryNumRandom_summon.y/2f),
- transform.position.z);
- }
- //不是起手式
- else
- {
- injuryNum = Instantiate(injuryNumText);
- injuryNum.transform.position = injuryNum.transform.position = new Vector3(
- transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x/2f, injuryNumRandom_march.x/2f),
- transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y/2f, injuryNumRandom_march.y/2f),
- 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);
- return;
- }
- }
- //造成伤害附加其他效果
- public virtual void BeHit(AttackInfo attackInfo, Character attackFrom)
- {
- }
- public virtual Vector3 GetMoveDir()
- {
- Vector3 moveDir = Vector3.zero;
- return moveDir;
- }
- public virtual void SetSortingOrder(int order)
- {
- meshRenderer.sortingOrder = order;
- }
- //吃串增加血量上限
- public void HpUp(float value, float larger)
- {
- cookEffect.transform.GetChild(0).gameObject.SetActive(true);
- int add = 0;
- value = value / 100;
- add = (int)(totalHp * value);
- totalHp += add;
- hp += add;
- uiHp.Show(hp, totalHp);
- float cur = transform.localScale.x;
- toLargeSize = cur * larger;
- beLarger = true;
- }
- //体型变大
- public void Enlarge()
- {
- transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
- if (transform.localScale.x >= toLargeSize - 0.1f)
- {
- beLarger = false;
- transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
- toLargeSize = 0;
- }
- }
- public virtual bool AdjustHeight()
- {
- return true;
- }
- public void ChangeStateText(CharacterState state)
- {
- if (curStateObj == null)
- {
- curStateObj = Instantiate(statePre, transform);
- stateText = curStateObj.GetComponentInChildren<TextMeshProUGUI>();
- }
- pastWordTime += 0.5f;
- switch (state)
- {
- case CharacterState.SpecialStatus_Float:
- stateText.text = "漂浮";
- break;
- case CharacterState.SpecialStatus_ShotDown:
- stateText.text = "击落";
- break;
- case CharacterState.SpecialStatus_BlowUp:
- stateText.text = "击飞";
- break;
- case CharacterState.SpecialStatus_Weak:
- stateText.text = "击晕";
- break;
- case CharacterState.HitStun:
- stateText.text = "僵直";
- break;
- }
- curStateObj.SetActive(true);
- }
- }
|