| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum CharacterState
- {
- None = 0,
- Idle = 1,
- Run = 2,
- Rise = 3,//¿ÕÖÐÉÏÉý
- Fall = 4,//¿ÕÖÐÏÂÂä
- Hurt = 5,
- Attack = 6,
- Summon = 7,
- Rush = 8,
- Die = 9,
- }
- public class Character : MonoBehaviour
- {
- public SkeletonMecanim mecanim;
- public Skeleton skeleton;
- public MeshRenderer meshRenderer;
- public Animator ani;
- public Animator aniCollider;
- public Rigidbody rb;
- public Transform bodyTrans;
- public UIHP uiHp;
- public CharacterState state;
- [HideInInspector]
- public float attackTime;
- public float totalAttack1Time = 0.5f;
- public float totalAttack2Time = 0.5f;
- public bool isDie = false;
- public int totalHp = 100;
- public int hp;
- public List<AttackInfo> attack1Infos;
- public List<AttackInfo> attack2Infos;
- public List<AttackTrigger> attackTriggers;
- public AttackType attackType;
- public GameObject bulletPrefab;
- public List<Transform> shootPos;
- [HideInInspector]
- public float dieKeepTime;
- public float totalDieKeepTime = 2f;
- public virtual void Init()
- {
- if (!mecanim)
- {
- mecanim = GetComponentInChildren<SkeletonMecanim>();
- }
- if (skeleton == null)
- {
- skeleton = mecanim.skeleton;
- }
- if (!meshRenderer)
- {
- meshRenderer = mecanim.GetComponent<MeshRenderer>();
- }
- if (!ani)
- {
- ani = GetComponentInChildren<Animator>();
- }
- hp = totalHp;
- uiHp.ShowHP(hp, totalHp);
- ChangeState(CharacterState.Idle);
- }
- 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()
- {
- }
- public virtual void ChangeState(CharacterState newState)
- {
- }
- public virtual void BeHit(int damage, Vector3 force)
- {
- hp -= damage;
- uiHp.ShowHP(hp, totalHp);
- rb.AddForce(force);
- if (hp < 0)
- {
- ChangeState(CharacterState.Die);
- }
- else
- {
- ChangeState(CharacterState.Hurt);
- }
- }
- public void AttackShootEvent(int attackId, int shootId)
- {
- AttackInfo attackInfo;
- if (attackId == 1)
- {
- attackInfo = attack1Infos[shootId];
- }
- else
- {
- attackInfo = attack2Infos[shootId];
- }
- GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
- Bullet bullet = bulletObj.GetComponent<Bullet>();
- bullet.damage = attackInfo.damage;
- Vector3 attackDir = attackInfo.attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackDir * attackInfo.force);
- }
- public virtual Vector3 GetMoveDir()
- {
- Vector3 moveDir = Vector3.zero;
- return moveDir;
- }
- public virtual void Attack1()
- {
- 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("attack1", 0, 0);
- aniCollider.Play("Attack1", 1, 0);
- attackTime = totalAttack1Time;
- switch (attackType)
- {
- case AttackType.Melee:
- for (int i = 0; i < attack1Infos.Count; i++)
- {
- attackTriggers[i].damage = attack1Infos[i].damage;
- Vector3 attackDir = attack1Infos[i].attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- attackTriggers[i].force = attackDir * attack1Infos[i].force;
- }
- break;
- case AttackType.Shoot:
- break;
- default:
- break;
- }
- ChangeState(CharacterState.Attack);
- }
- public virtual void Attack2()
- {
- 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("attack2", 0, 0);
- aniCollider.Play("Attack2", 1, 0);
- attackTime = totalAttack2Time;
- switch (attackType)
- {
- case AttackType.Melee:
- for (int i = 0; i < attack2Infos.Count; i++)
- {
- attackTriggers[i].damage = attack2Infos[i].damage;
- Vector3 attackDir = attack2Infos[i].attackDir.normalized;
- if (bodyTrans.localScale.x < 0)
- {
- attackDir.x = -attackDir.x;
- }
- attackTriggers[i].force = attackDir * attack2Infos[i].force;
- }
- break;
- case AttackType.Shoot:
- break;
- default:
- break;
- }
- ChangeState(CharacterState.Attack);
- }
- public void SetSortingOrder(int order)
- {
- meshRenderer.sortingOrder = order;
- }
- }
|