| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Spine;
- using Spine.Unity;
- using UnityEngine.LowLevel;
- using UnityEngine.Playables;
- using System.ComponentModel;
- using Unity.VisualScripting;
- public enum PlayerState
- {
- Idle = 1,
- Run = 2,
- Rise = 3,//空中上升
- Fall = 4,//空中下落
- Hurt = 5,
- Attack = 6,
- Summon = 7,
- Rush = 8,
- Die = 9,
- }
- public class PlayerController : MonoBehaviour
- {
- public SkeletonMecanim mecanim;
- public Skeleton skeleton;
- public Animator ani;
- public Rigidbody rb;
- public Foot foot;
- public Collider bodyCollider;
- public Collider jumpBodyCollider;
- public List<GameObject> demonicPrefabs;
- public List<Vector3> demonicSummonPos;
- 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 float rushSpeed = 100;
- public PlayerState state = PlayerState.Idle;
- [HideInInspector]
- public float invincibleTime;
- public float totalInvincibleTime = 2f;
- [HideInInspector]
- public float canJumpTime; //离开平台后仍然可以跳跃的时间,用于提升手感
- public float leaveGroundCanJumpTime = 0.1f;
- [HideInInspector]
- public float cacheJumpTime; //即将落地时按下跳跃键不会跳跃,手感不好,缓存几帧,在这几帧内落地会立即跳跃;
- public float totalCacheJumpTime = 0.1f;
- [HideInInspector]
- public float hurtKeepTime;
- public float totalHurtKeepTime = 0.5f;
- [HideInInspector]
- public float attackTime;
- public float totalAttackTime = 0.5f;
- [HideInInspector]
- public float summonTime;
- public float totalSummonTime = 0.5f;
- [HideInInspector]
- public float cacheAttackTime; //无法攻击时按下攻击键不会攻击,手感不好,缓存几帧,在这几帧内落地会立即攻击;
- public float totalCacheAttackTime = 0.1f;
- [HideInInspector]
- public float cacheSummonTime; //无法召唤时按下召唤键不会召唤,手感不好,缓存几帧,在这几帧内落地会立即召唤;
- public float totalCacheSummonTime = 0.1f;
- [HideInInspector]
- public int cacheSummonId;
- [HideInInspector]
- public float rushTime;
- public float totalRushTime = 0.5f;
- [HideInInspector]
- public float cacheRushTime; //无法Rush时按下Rush键不会Rush,手感不好,缓存几帧,在这几帧内落地会立即Rush;
- public float totalCacheRushTime = 0.1f;
- public bool isDie = false;
- public bool btnJumpPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.Space) || isClickBtnJump;
- }
- }
- public bool isClickBtnJump;
- public bool btnRushPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.LeftShift) || isClickBtnRush;
- }
- }
- public bool isClickBtnRush;
- public bool btnSouthPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.K) || isClickBtnSouth;
- }
- }
- public bool isClickBtnSouth;
- public bool btnEastPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.L) || isClickBtnEast;
- }
- }
- public bool isClickBtnEast;
- public bool btnWestPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.J) || isClickBtnWest;
- }
- }
- public bool isClickBtnWest;
- public bool btnNorthPress
- {
- get
- {
- return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- }
- }
- public bool isClickBtnNorth;
- public Vector2 leftDir
- {
- get
- {
- return new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
- }
- }
- private void Awake()
- {
- mecanim = GetComponent<SkeletonMecanim>();
- skeleton = mecanim.skeleton;
- ani = GetComponent<Animator>();
- ChangeState(PlayerState.Idle);
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.LeftShift))
- {
- isClickBtnRush = true;
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- isClickBtnJump = true;
- }
- if (Input.GetKeyDown(KeyCode.J))
- {
- isClickBtnWest = true;
- }
- if (Input.GetKeyDown(KeyCode.K))
- {
- isClickBtnSouth = true;
- }
- if (Input.GetKeyDown(KeyCode.L))
- {
- isClickBtnEast = true;
- }
- if (Input.GetKeyDown(KeyCode.I))
- {
- isClickBtnNorth = true;
- }
- }
- private void FixedUpdate()
- {
- OnState();
- }
- public void Jump()
- {
- SetUpSpeed(jumpSpeed);
- ani.Play("jump", 0, 0);
- }
- public void SetUpSpeed(float speed)
- {
- ChangeState(PlayerState.Rise);
- Vector3 velocity = rb.velocity;
- if (leftDir.x > 0.3f)
- {
- if (transform.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- if (transform.localScale.x < 0)
- {
- Turn();
- }
- }
- velocity.y = speed;
- rb.velocity = velocity;
- //animalAni.SetInteger("state", (int)PlayerState.Rise);
- }
- public void Turn()
- {
- transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
- }
- public void OnState()
- {
- cacheJumpTime -= Time.deltaTime;
- cacheAttackTime -= Time.deltaTime;
- cacheSummonTime -= Time.deltaTime;
- canJumpTime -= Time.deltaTime;
- invincibleTime -= Time.deltaTime;
- hurtKeepTime -= Time.deltaTime;
- attackTime -= Time.deltaTime;
- summonTime -= Time.deltaTime;
- rushTime -= Time.deltaTime;
- cacheRushTime -= Time.deltaTime;
- switch (state)
- {
- case PlayerState.Idle:
- if (btnWestPress || cacheAttackTime > 0)
- {
- ChangeState(PlayerState.Attack);
- break;
- }
- if (btnNorthPress)
- {
- Summon(0);
- break;
- }
- if (btnSouthPress)
- {
- Summon(1);
- break;
- }
- if (btnEastPress)
- {
- Summon(2);
- break;
- }
- if (cacheSummonTime > 0)
- {
- Summon(cacheSummonId);
- break;
- }
- if (btnRushPress || cacheRushTime > 0)
- {
- ChangeState(PlayerState.Rush);
- break;
- }
- if (btnJumpPress || cacheJumpTime > 0)
- {
- Jump();
- ChangeState(PlayerState.Rise);
- break;
- }
- if (!foot.TrigGround)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(PlayerState.Rise);
- break;
- }
- else
- {
- ChangeState(PlayerState.Fall);
- break;
- }
- }
- if (leftDir.x > 0.3f || leftDir.x < -0.3f)
- {
- ChangeState(PlayerState.Run);
- break;
- }
- canJumpTime = leaveGroundCanJumpTime;
- //rb.velocity = Vector3.zero;
- break;
- case PlayerState.Run:
- if (btnWestPress || cacheAttackTime > 0)
- {
- ChangeState(PlayerState.Attack);
- break;
- }
- if (btnNorthPress)
- {
- Summon(0);
- break;
- }
- if (btnSouthPress)
- {
- Summon(1);
- break;
- }
- if (btnEastPress)
- {
- Summon(2);
- break;
- }
- if (cacheSummonTime > 0)
- {
- Summon(cacheSummonId);
- break;
- }
- if (btnRushPress || cacheRushTime > 0)
- {
- ChangeState(PlayerState.Rush);
- break;
- }
- if (btnJumpPress || cacheJumpTime > 0)
- {
- Jump();
- break;
- }
- if (!foot.TrigGround)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(PlayerState.Rise);
- break;
- }
- else
- {
- ChangeState(PlayerState.Fall);
- break;
- }
- }
- if (leftDir.x < 0.3f && leftDir.x > -0.3f)
- {
- ChangeState(PlayerState.Idle);
- break;
- }
- canJumpTime = leaveGroundCanJumpTime;
- 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 PlayerState.Rise:
- if (btnRushPress || cacheRushTime > 0)
- {
- ChangeState(PlayerState.Rush);
- break;
- }
- if (rb.velocity.y <= 0)
- {
- ChangeState(PlayerState.Fall);
- break;
- }
- //if (btnJumpPress || cacheJumpTime > 0)
- //{
- // if (!airJumped && rb.velocity.y < airJumpSpeed)
- // {
- // airJumped = true;
- // AirJump();
- // break;
- // }
- //}
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnWestPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnNorthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
- AirMove();
- break;
- case PlayerState.Fall:
- if (btnRushPress || cacheRushTime > 0)
- {
- ChangeState(PlayerState.Rush);
- break;
- }
- if (foot.TrigGround)
- {
- ChangeState(PlayerState.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;
- // }
- //}
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnWestPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnNorthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- AirMove();
- break;
- case PlayerState.Hurt:
- if (hurtKeepTime <= 0)
- {
- ChangeState(PlayerState.Idle);
- break;
- }
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnWestPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnNorthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- if (!foot.TrigGround)
- {
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- }
- rb.velocity = rb.velocity / 5 * 4;
- break;
- case PlayerState.Attack:
- if (attackTime <= 0)
- {
- ChangeState(PlayerState.Idle);
- break;
- }
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnWestPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnNorthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- break;
- case PlayerState.Summon:
- if (summonTime <= 0)
- {
- ChangeState(PlayerState.Idle);
- break;
- }
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnWestPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnNorthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- break;
- case PlayerState.Rush:
- if (rushTime <= 0)
- {
- ChangeState(PlayerState.Idle);
- break;
- }
- if (transform.localScale.x > 0)
- {
- rb.velocity = Vector3.left * rushSpeed;
- }
- else
- {
- rb.velocity = Vector3.right * rushSpeed;
- }
- break;
- default:
- break;
- }
- isClickBtnRush = false;
- isClickBtnJump = false;
- isClickBtnSouth = false;
- isClickBtnEast = false;
- isClickBtnNorth = false;
- isClickBtnWest = false;
- }
- public void ChangeState(PlayerState newState)
- {
- switch (state)
- {
- case PlayerState.Idle:
- break;
- case PlayerState.Run:
- break;
- case PlayerState.Rise:
- break;
- case PlayerState.Fall:
- break;
- case PlayerState.Hurt:
- break;
- case PlayerState.Attack:
- break;
- case PlayerState.Summon:
- break;
- case PlayerState.Rush:
- break;
- case PlayerState.Die:
- isDie = false;
- break;
- default:
- break;
- }
- PlayerState oldState = state;
- state = newState;
- switch (newState)
- {
- case PlayerState.Idle:
- bodyCollider.enabled = true;
- jumpBodyCollider.enabled = false;
- if (oldState == PlayerState.Fall)
- {
- ani.Play("fall_end", 0, 0);
- }
- else
- {
- ani.Play("idle", 0, 0);
- }
- rb.velocity = Vector3.zero;
- //animalAni.SetInteger("state", (int)PlayerState.Idle);
- break;
- case PlayerState.Run:
- bodyCollider.enabled = true;
- jumpBodyCollider.enabled = false;
- ani.Play("run", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Walk);
- break;
- case PlayerState.Rise:
- bodyCollider.enabled = false;
- jumpBodyCollider.enabled = true;
- canJumpTime = 0;
- break;
- case PlayerState.Fall:
- bodyCollider.enabled = false;
- jumpBodyCollider.enabled = true;
- ani.Play("fall", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Fall);
- break;
- case PlayerState.Hurt:
- bodyCollider.enabled = false;
- jumpBodyCollider.enabled = true;
- ani.Play("hitted", 0, 0);
- hurtKeepTime = totalHurtKeepTime;
- invincibleTime = totalInvincibleTime;
- //ani.Play("Invincible", 2, 0);
- break;
- case PlayerState.Attack:
- bodyCollider.enabled = true;
- jumpBodyCollider.enabled = false;
- ani.Play("attack1", 0, 0);
- attackTime = totalAttackTime;
- break;
- case PlayerState.Summon:
- bodyCollider.enabled = true;
- jumpBodyCollider.enabled = false;
- ani.Play("summon", 0, 0);
- summonTime = totalSummonTime;
- break;
- case PlayerState.Rush:
- bodyCollider.enabled = true;
- jumpBodyCollider.enabled = false;
- ani.Play("rush_loop", 0, 0);
- rushTime = totalRushTime;
- break;
- case PlayerState.Die:
- bodyCollider.enabled = false;
- jumpBodyCollider.enabled = false;
- ani.Play("die", 0, 0);
- isDie = true;
- break;
- default:
- break;
- }
- }
- public void AirMove()
- {
- if (leftDir.x > 0.3f)
- {
- //rb.velocity += Vector3.right * airMoveAcc * 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 * airMoveAcc * 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();
- }
- }
- else
- {
- rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
- }
- }
- public void Summon(int id)
- {
- print("Summon:" + id);
- ChangeState(PlayerState.Summon);
- if (id >= demonicPrefabs.Count)
- {
- Debug.LogError("未配置" + id + "号使魔");
- return;
- }
- if (id >= demonicSummonPos.Count)
- {
- Debug.LogError("未配置" + id + "号使魔召唤位置");
- return;
- }
- GameObject prefab = demonicPrefabs[id];
- GameObject demonicObj = Instantiate(prefab);
- Demonic demonic = demonicObj.GetComponent<Demonic>();
- demonicObj.transform.parent = null;
- demonicObj.transform.localEulerAngles = Vector3.zero;
- Vector3 offset = demonicSummonPos[id];
- if (transform.localScale.x > 0)
- {
- demonicObj.transform.position = transform.position + offset;
- demonicObj.transform.localScale = new Vector3(1, 1, 1);
- }
- else
- {
- demonicObj.transform.position = transform.position + new Vector3(-offset.x, offset.y, offset.z);
- demonicObj.transform.localScale = new Vector3(-1, 1, 1);
- }
- demonic.ChangeState(DemonicState.Attack1);
- }
- }
|