| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum DemonicState
- {
- Idle = 0,
- Walk = 2,
- Rise = 3,//空中上升
- Fall = 4,//空中下落
- Hurt = 5,
- Attack1 = 6,
- Attack2 = 7,
- Die = 8,
- }
- public enum DemonicTargetType
- {
- Player = 0,
- Demonic = 1,
- FlyDemonic = 2,
- LowHPDemonic = 3,
- Tower = 4,
- }
- public class Demonic : MonoBehaviour
- {
- public SkeletonMecanim mecanim;
- public Skeleton skeleton;
- public Animator ani;
- public Rigidbody rb;
- public Foot foot;
- public Collider bodyCollider;
- public float extraRiseGravity = 0; //上升时额外重力加速度
- public float extraFallGravity = -10; //下落时额外重力加速度
- //public float jumpSpeed = 10;
- //public float airJumpSpeed = 10;
- public float maxMoveSpeed = 5;
- //public float moveAcc = 5f;
- //public float airMoveAcc = 3f;
- public DemonicState state;
- [HideInInspector]
- public float invincibleTime;
- public float totalInvincibleTime = 2f;
- [HideInInspector]
- public float hurtKeepTime;
- public float totalHurtKeepTime = 0.5f;
- [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 bool canFly = false;
- public float attackDistance;
- private void Awake()
- {
- if (!mecanim)
- {
- mecanim = GetComponentInChildren<SkeletonMecanim>();
- skeleton = mecanim.skeleton;
- }
- if (!ani)
- {
- ani = GetComponentInChildren<Animator>();
- }
- ChangeState(DemonicState.Idle);
- }
- private void FixedUpdate()
- {
- OnState();
- }
- public void Turn()
- {
- transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
- }
- public Vector3 GetMoveDir()
- {
- return Vector3.zero;
- }
- public bool GetAttack()
- {
- return false;
- }
- public void OnState()
- {
- invincibleTime -= Time.deltaTime;
- hurtKeepTime -= Time.deltaTime;
- attackTime -= Time.deltaTime;
- Vector3 leftDir = GetMoveDir();
- bool isAttack = GetAttack();
- switch (state)
- {
- case DemonicState.Idle:
- if (isAttack)
- {
- ChangeState(DemonicState.Attack2);
- break;
- }
- if (!foot.TrigGround)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(DemonicState.Rise);
- break;
- }
- else
- {
- ChangeState(DemonicState.Fall);
- break;
- }
- }
- if (leftDir.x > 0.3f || leftDir.x < -0.3f)
- {
- ChangeState(DemonicState.Walk);
- break;
- }
- //rb.velocity = Vector3.zero;
- break;
- case DemonicState.Walk:
- if (isAttack)
- {
- ChangeState(DemonicState.Attack2);
- break;
- }
- if (!foot.TrigGround)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(DemonicState.Rise);
- break;
- }
- else
- {
- ChangeState(DemonicState.Fall);
- break;
- }
- }
- if (leftDir.x < 0.3f && leftDir.x > -0.3f)
- {
- ChangeState(DemonicState.Idle);
- break;
- }
- if (leftDir.x > 0.3f)
- {
- //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.right * maxMoveSpeed;
- //if (rb.velocity.x > maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (transform.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.left * maxMoveSpeed;
- //if (rb.velocity.x < -maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (transform.localScale.x < 0)
- {
- Turn();
- }
- }
- break;
- case DemonicState.Rise:
- if (rb.velocity.y <= 0)
- {
- ChangeState(DemonicState.Fall);
- break;
- }
- //if (btnJumpPress || cacheJumpTime > 0)
- //{
- // if (!airJumped && rb.velocity.y < airJumpSpeed)
- // {
- // airJumped = true;
- // AirJump();
- // break;
- // }
- //}
-
- rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
- break;
- case DemonicState.Fall:
- if (foot.TrigGround)
- {
- ChangeState(DemonicState.Idle);
- break;
- }
- //if (foot.canStepPlayers.Count > 0)
- //{
- // Jump(jumpSpeed / 2);
- // StepOther();
- // break;
- //}
- //if (foot.canStepEnemyList.Count > 0)
- //{
- // Jump(jumpSpeed / 2);
- // StepEnemy();
- // break;
- //}
- //if (btnJumpPress || cacheJumpTime > 0)
- //{
- // if (!airJumped)
- // {
- // airJumped = true;
- // AirJump();
- // break;
- // }
- // else if (canJumpTick >= runner.Tick)
- // {
- // Jump();
- // break;
- // }
- //}
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- break;
- case DemonicState.Hurt:
- if (hurtKeepTime <= 0)
- {
- ChangeState(DemonicState.Idle);
- break;
- }
- if (!foot.TrigGround)
- {
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- }
- rb.velocity = rb.velocity / 5 * 4;
- break;
- case DemonicState.Attack1:
- if (attackTime <= 0)
- {
- ChangeState(DemonicState.Idle);
- break;
- }
- break;
- case DemonicState.Attack2:
- if (attackTime <= 0)
- {
- ChangeState(DemonicState.Idle);
- break;
- }
- break;
- default:
- break;
- }
- }
- public void ChangeState(DemonicState newState)
- {
- switch (state)
- {
- case DemonicState.Idle:
- break;
- case DemonicState.Walk:
- break;
- case DemonicState.Rise:
- break;
- case DemonicState.Fall:
- break;
- case DemonicState.Hurt:
- break;
- case DemonicState.Attack1:
- break;
- case DemonicState.Attack2:
- break;
- case DemonicState.Die:
- isDie = false;
- break;
- default:
- break;
- }
- DemonicState oldState = state;
- state = newState;
- switch (newState)
- {
- case DemonicState.Idle:
- ani.Play("idle", 0, 0);
- rb.velocity = Vector3.zero;
- //animalAni.SetInteger("state", (int)PlayerState.Idle);
- break;
- case DemonicState.Walk:
- ani.Play("walk", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Walk);
- break;
- case DemonicState.Rise:
- break;
- case DemonicState.Fall:
- //animalAni.SetInteger("state", (int)PlayerState.Fall);
- break;
- case DemonicState.Hurt:
- ani.Play("hitted", 0, 0);
- hurtKeepTime = totalHurtKeepTime;
- invincibleTime = totalInvincibleTime;
- //ani.Play("Invincible", 2, 0);
- break;
- case DemonicState.Attack1:
- ani.Play("attack1", 0, 0);
- attackTime = totalAttack1Time;
- break;
- case DemonicState.Attack2:
- ani.Play("attack2", 0, 0);
- attackTime = totalAttack2Time;
- break;
- case DemonicState.Die:
- ani.Play("die", 0, 0);
- isDie = true;
- break;
- default:
- break;
- }
- }
- }
|