| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- 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, //僵直
- FramePause, //顿帧
- //控制效果
- SpecialStatus_Float,
- SpecialStatus_ShotDown,
- SpecialStatus_BlowUp,
- SpecialStatus_Weak,
- LockSoul = 25, //在锁魂塔中
- Conduct = 26, //在指挥中
- }
- public class Character : MonoBehaviour
- {
- [FoldoutGroup("组件", order: -1)] 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;
- [FoldoutGroup("组件")] public Platform platform;
- [Space(30)]
- [Title("Character属性")]
- [LabelText("骨骼")] public SkeletonMecanim mecanim;
- public Skeleton skeleton;
- [HideInInspector]
- public MeshRenderer meshRenderer;
- [FoldoutGroup("动画", order: -1)] public Animator ani;
- [FoldoutGroup("动画")] public float totalDieKeepTime = 2f;
- [Header("跳字开关")]
- [FoldoutGroup("伤害跳字")] public bool showInjuryNum;
- [Header("跳字样式")]
- [FoldoutGroup("伤害跳字", order: -1)] public GameObject injuryNumText; //伤害跳字
- [FoldoutGroup("伤害跳字")] public GameObject injuryNumTextSummon; //伤害跳字(起手式)
- [Header("跳字位置")]
- [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;
- [Header("死亡后多久尸体消失")]
- [HideInInspector]
- public float dieKeepTime;
- [Header("受击距离")]
- public float beHitDistance;
- [HideInInspector]
- public float attackDis;
- //当前目标
- [DisplayOnly]
- public Character targetCharacter;
- [HideInInspector]
- public Character attackTarget;
- [DisplayOnly]
- [FoldoutGroup("角色信息", order: -1)] public CharacterState state;
- [FoldoutGroup("角色信息")] public int totalHp = 100;
- [FoldoutGroup("角色信息")] public int hp;
- [Header("角色状态")]
- public bool canFly = false;
- [LabelText("飞行单位高度可进行修改")]
- [Tooltip("本来飞行的怪初始化锁定刚体Y轴,勾选这个可以让飞行单位刚体Y轴解锁(例如攻击状态下需要利用刚体向下冲刺的飞鹤)")]
- public bool canControlFly = false;
- [HideInInspector] public bool nowCanFly; //当前是否能飞行
- [LabelText("不会被击落")][Tooltip("本来不能飞行的怪没法被击落,取消勾选这个可以让那些地面怪也被击落(例如爬上树的水猴子)")] public bool canNotShotDown = true;
- [LabelText("不会被击飞")] public bool canNotAddForce;
- [LabelText("不会被虚弱")] public bool canNotChangeHurt;
- [DisplayOnly]
- public bool isDie = false; //已死亡
- [DisplayOnly]
- public bool isRevive; //从虚弱状态恢复中
- [DisplayOnly]
- public float invincibleTime; //无敌时间
- [Header("状态显示(临时)")]
- public GameObject statePre;
- 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弓箭可以被传送
- //调试开关
- [Space]
- [LabelText("debug攻击者")] public bool debugAttackFrom;
- 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;
- if (canFly && !canControlFly && rb != null)
- {
- rb.useGravity = false;
- rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
- }
- else if (canFly && canControlFly && rb != null)
- {
- rb.useGravity = false;
- rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
- }
- uiHp.Show(hp, totalHp);
- isDie = false;
- 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);
- if (platform != null)
- {
- platform.left.transform.localPosition = new Vector3(-0.5f * bodyTrans.localScale.x, 0, 0);
- platform.right.transform.localPosition = new Vector3(0.5f * bodyTrans.localScale.x, 0, 0);
- }
- }
- public void CheckTurn(float dirX)
- {
- if (dirX > 0.1f)
- {
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (dirX < -0.1f)
- {
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- }
- public virtual void OnState()
- {
- if (pastWordTime > 0)
- {
- pastWordTime -= Time.deltaTime;
- if (pastWordTime <= 0)
- {
- 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;
- injuryNum = PoolManager.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();
- }
- uiHp.Show(hp, totalHp);
- if (hp <= 0)
- {
- ChangeState(CharacterState.Die);
- return;
- }
- }
- //造成伤害附加其他效果
- public virtual void BeHit(AttackInfo attackInfo, Character attackFrom, int damage = -1)
- {
- }
- 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 (!statePre)
- {
- return;
- }
- GameObject obj = PoolManager.Instantiate(statePre);
- stateText = obj.GetComponentInChildren<TextMeshProUGUI>();
- obj.transform.position = transform.position + Vector3.up;
- 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;
- }
- obj.SetActive(true);
- }
- }
|