| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- //角色状态
- 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,
- Weak = 12,
- Spirits = 14, //召唤英灵
- Float = 15, //空中漂浮
- Coma = 21, //昏迷
- RushAttack = 22, //带攻击的冲刺
- Transfiguration = 23, //变身
- BaGua = 24, //八卦状态
- LockSoul = 25, //在锁魂塔中
- Conduct = 26, //在指挥中
- SpecialStatus,
- }
- //攻击类型
- public enum AttackType
- {
- Melee = 0, //近战
- Shoot = 1, //射击
- Dash = 2, //英灵刺客
- }
- //血量上限增加类型
- public enum HpUpType
- {
- Degree = 0, //按一定比例加血
- Add = 1, //直接加血
- Fixed = 2, //加固定血量
- }
- [System.Serializable]
- public struct SpineAniKey
- {
- public string aniName;
- public List<AttackKeyType> keys;
- }
- public enum KeyType
- {
- AttackStart,
- AttackEnd,
- }
- [System.Serializable]
- public struct AttackKeyType
- {
- public KeyType attackType;
- public string startKeyName;
- public float startKeyTime;
- public KeyType endType;
- public string endKeyName;
- public float endKeyTime;
- }
- public class Character : MonoBehaviour
- {
- [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("所有攻击帧事件及时间")]
- public List<SpineAniKey> attackKeys;
- [Header("死亡后多久尸体消失")]
- [HideInInspector]
- public float dieKeepTime;
-
- [Header("组件")]
- public Rigidbody rb;
- public Transform bodyTrans;
- public BeSearchTrigger beSearchTrigger;
- public SearchTrigger searchTrigger;
- public GameObject bodyCollider;
- public UIHP uiHp;
- public BeHitTrigger beHitTrigger;
- [Header("角色状态")]
- public CharacterState state;
- public int totalHp = 100;
- public int hp;
- public bool isDie = false; //已死亡
- public bool isRevive; //从虚弱状态恢复中
- public bool linked; //被铁链连着
- public bool canNotAddForce; //不会被打飞
- public bool canNotChangeHurt; //不会被打虚弱
- [HideInInspector]
- public float invincibleTime; //无敌时间
- public GameObject injuryNumText;//伤害跳字
- public bool showInjuryNum; //伤害跳字开关
- public bool canFly = false;
- [Header("护甲")]
- public int armor;
- [Header("锁魂塔")]
- public LockSoul ls;
- public bool isInSoulTower; //在锁魂塔范围内
- [Header("普通攻击信息")]
- public bool canHitFly;
- [HideInInspector]
- public bool isNonAttack = false; //无普攻
- public AttackType attackType; //攻击类型
- public GameObject bulletPrefab; //远程攻击的子弹
- public List<Transform> shootPos; //远程攻击的子弹发射位置
- public bool shootTrack = false; //远程攻击是否初始时瞄准目标
- public bool shootAlwaysTrack = false; //远程攻击是否始终追踪
- public List<AttackInfo> Attack_summonInfos; //普攻1信息(出场攻击)
- public List<AttackInfo> Attack_marchInfos; //普攻2信息
- public List<AttackTrigger> attackTriggers; //普攻触发器,敌方进入后播放动画进行攻击
- public GameObject addAttackEffect;
- public int armorPiercing; //穿甲率
- public int[] curDamage1;
- public int[] curDamage2;
- public float attackTime;
- public float attackKeyCount; //攻击进行时间
- public float nextStartKeyTime, nextEndKeyTime; //下一个出现/消失attacktrigger的时间
- public List<float> keyTimes; //所有的帧事件时间
- public int curKeyNum; //当前锁定到第几个帧事件
- public bool isAttackTriggerOn = false; //当前Attack Trigger是否开启
- [Header("目标")]
- public List<TargetType> targetTypes;
- public Character targetCharacter;
- public Character attackTarget;
- public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
- public float getDistanceOffset = 0f;
- [Header("是否为英灵、是否为变身形态英灵")]
- public bool isSpirit;
- public bool isTran;
- [HideInInspector]
- public PlayerController pc;
- [Header("血量上限增加类型")]
- public HpUpType hptp;
- [Header("体型增大")]
- 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;
- 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;
- if (!isNonAttack)
- {
- uiHp.Show(hp, totalHp);
- }
- ChangeState(CharacterState.Idle);
- curDamage1 = new int[Attack_summonInfos.Count];
- for (int i = 0; i < Attack_summonInfos.Count; i++)
- {
- curDamage1[i] = Attack_summonInfos[i].damage;
- }
- curDamage2 = new int[Attack_marchInfos.Count];
- for (int i = 0; i < Attack_marchInfos.Count; i++)
- {
- curDamage2[i] = Attack_marchInfos[i].damage;
- }
- }
- public virtual void FixedUpdate()
- {
- OnState();
- }
- private void OnEnable()
- {
- if (attackTriggers != null)
- {
- for (int i = 0; i < attackTriggers.Count; i++)
- {
- attackTriggers[i].gameObject.SetActive(false);
- }
- }
- }
- public void Turn()
- {
- bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- public virtual void OnState()
- {
- }
- 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, Vector3 force, bool changeHurt, float repelValue)
- {
- //无敌状态下免伤
- if (invincibleTime > 0)
- {
- return;
- }
- //非无敌状态扣血
- 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;
- }
- }
- uiHp.Show(hp, totalHp);
- if (hp <= 0)
- {
- if (!canNotAddForce)
- rb.AddForce(force);
- ChangeState(CharacterState.Die);
- return;
- }
- }
- public virtual void AttackShootEvent(int attackId, int shootId)
- {
- AttackInfo attackInfo;
- if (attackId == 1)
- {
- attackInfo = Attack_summonInfos[shootId];
- }
- else
- {
- attackInfo = Attack_marchInfos[shootId];
- attackInfo.damage = curDamage2[0];
- }
- GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
- Bullet bullet = bulletObj.GetComponent<Bullet>();
- Vector3 attackDir = attackInfo.attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- if (attackId == 1 && Attack_summonShootCanTransmit)
- {
- bullet.canTransmit = true;
- }
- bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack, shootAlwaysTrack, attackTarget ? attackTarget : null);
- }
- public virtual Vector3 GetMoveDir()
- {
- Vector3 moveDir = Vector3.zero;
- return moveDir;
- }
- public void SetNextKeyTimes()
- {
- if (curKeyNum < keyTimes.Count)
- {
- nextStartKeyTime = keyTimes[curKeyNum];
- nextEndKeyTime = keyTimes[curKeyNum + 1];
- curKeyNum += 2;
- }
- }
- public virtual void Attack_summon()
- {
- ani.Play("attack_summon", 0, 0);
- if (!isNonAttack)
- {
- if (attackType == AttackType.Shoot)
- {
- foreach(AttackTrigger at in attackTriggers)
- {
- at.isShoot = true;
- at.GetComponent<Collider>().enabled = false;
- at.type = AttackTrigger.attackTpye.summon;
- }
- }
- attackTime = totalAttack_summonTime;
- attackKeyCount = 0;
- keyTimes = new List<float>();
- foreach(SpineAniKey sak in attackKeys)
- {
- if (sak.aniName == "attack_summon")
- {
- foreach(AttackKeyType akt in sak.keys)
- {
- keyTimes.Add(akt.startKeyTime);
- keyTimes.Add(akt.endKeyTime);
- }
- }
- }
- curKeyNum = 0;
- SetNextKeyTimes();
- switch (attackType)
- {
- case AttackType.Melee:
- for (int i = 0; i < Attack_summonInfos.Count; i++)
- {
- attackTriggers[i].damage = curDamage1[i];
- attackTriggers[i].changeHurt = Attack_summonInfos[i].changeHurt;
- attackTriggers[i].repelValue = Attack_summonInfos[i].repelValue;
- Vector3 attackDir = Attack_summonInfos[i].attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- attackTriggers[i].force = attackDir * Attack_summonInfos[i].force;
- }
- break;
- case AttackType.Shoot:
- case AttackType.Dash:
- break;
- default:
- break;
- }
- ChangeState(CharacterState.Attack);
- }
- }
- public virtual void Attack_march()
- {
- Vector3 leftDir = GetMoveDir();
- if (leftDir.x > 0.3f)
- {
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- ani.Play("attack_march", 0, 0);
- if (attackType == AttackType.Shoot)
- {
- foreach (AttackTrigger at in attackTriggers)
- {
- at.isShoot = true;
- at.GetComponent<Collider>().enabled = false;
- at.type = AttackTrigger.attackTpye.march;
- }
- }
- attackTime = totalAttack_marchTime;
- attackKeyCount = 0;
- keyTimes = new List<float>();
- foreach (SpineAniKey sak in attackKeys)
- {
- if (sak.aniName == "attack_march")
- {
- foreach (AttackKeyType akt in sak.keys)
- {
- keyTimes.Add(akt.startKeyTime);
- keyTimes.Add(akt.endKeyTime);
- }
- }
- }
- curKeyNum = 0;
- SetNextKeyTimes();
- if (attackTriggers.Count > 0)
- {
- for (int i = 0; i < Attack_marchInfos.Count; i++)
- {
- attackTriggers[i].damage = curDamage2[i];
- attackTriggers[i].changeHurt = Attack_marchInfos[i].changeHurt;
- attackTriggers[i].repelValue = Attack_marchInfos[i].repelValue;
- Vector3 attackDir = Attack_marchInfos[i].attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- attackTriggers[i].force = attackDir * Attack_marchInfos[i].force;
- }
- }
- ChangeState(CharacterState.Attack);
- }
- 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;
- switch (hptp)
- {
- case HpUpType.Degree:
- value = value / 100;
- add = (int)(totalHp * value);
- break;
- case HpUpType.Add:
- add = (int)value;
- break;
- case HpUpType.Fixed:
- add = (int)(totalHp - value);
- break;
- }
- 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;
- }
- }
- }
|