| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490 |
- 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;
- using System;
- using Base.Common;
- using cfg;
- using static UnityEngine.EventSystems.EventTrigger;
- using UnityEngine.InputSystem;
- [Serializable]
- public struct AttackInfo
- {
- public int damage;
- public Vector3 attackDir;
- public float force;
- public bool changeHurt;
- public float repelValue;
- }
- public enum PlayerAttackState
- {
- Idle = 0,
- WalkForward = 1,
- WalkBack = 2,
- }
- public class PlayerController : MoveCharacter
- {
- //public static PlayerController instance;
- public List<GameObject> changePrefabs;
- private GameObject spiritObj; //当前变身的对象
- private int endChange = 0;
- public float changeTime; //变身时长
- private float pastChangeTime;
- public List<GameObject> demonicPrefabs;
- public List<Vector3> demonicSummonPos;
- public Dictionary<int, List<Demonic>> demonicDic;
- public List<int> demonicId;
- public UIHP uiMp;
- public PlayerRope playerRope;
- public EventTrigger eventTrigger;
- public SprintLinkTrigger sprintLinkTrigger;
- public float jumpSpeed = 10;
- public float airJumpSpeed = 10;
- //public float moveAcc = 5f;
- //public float airMoveAcc = 3f;
- public float rushSpeed = 100;
- public float mp;
- public float totalMp;
- public float mpReplySpeed = 1;
- public float rushCostMp = 5;
- public float sprintCostMp = 5;
- //尸体资源
- public int corpses = 0;
- public float rushInvincibleTime = 0.2f;
- [HideInInspector]
- public float canJumpTime; //离开平台后仍然可以跳跃的时间,用于提升手感
- public float leaveGroundCanJumpTime = 0.1f;
- [HideInInspector]
- public float cacheJumpTime; //即将落地时按下跳跃键不会跳跃,手感不好,缓存几帧,在这几帧内落地会立即跳跃;
- public float totalCacheJumpTime = 0.1f;
- [HideInInspector]
- public float summonTime;
- [HideInInspector]
- public float cacheAttackTime; //无法攻击时按下攻击键不会攻击,手感不好,缓存几帧,在这几帧内落地会立即攻击;
- public float totalCacheAttackTime = 0.1f;
- [HideInInspector]
- public float cachePullRopeTime;
- public float totalCachePullRopeTime = 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;
- [HideInInspector]
- public bool airJumped;
- public PlayerAttackState attackState;
- public float attackMoveSpeed = 5f;
- public Vector3 rushDir;
- private int currentSpirit; //当前将要召唤的英灵种类
- private Spirits spirits;
- public float lostMp;
- public GameObject soul;
- private float addMp = 10;
- public Collider soulCollector;
- private bool isTransfiguration = false; //已变身
- public bool btnJumpPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.Space) || isClickBtnJump;
- return isClickBtnJump;
- }
- }
- [HideInInspector]
- public bool isClickBtnJump;
- public bool btnRushPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.LeftShift) || isClickBtnRush;
- return isClickBtnRush;
- }
- }
- [HideInInspector]
- public bool isClickBtnRush;
- public bool btnRushKeep
- {
- get
- {
- //return Input.GetKey(KeyCode.LeftShift) || isKeepBtnRush;
- return LBisHold || isKeepBtnRush;
- }
- }
- [HideInInspector]
- public bool isKeepBtnRush;
- public bool btnSouthPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.K) || isClickBtnSouth;
- return isClickBtnSouth;
- }
- }
- [HideInInspector]
- public bool isClickBtnSouth;
- public bool btnEastPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.L) || isClickBtnEast;
- return isClickBtnEast;
- }
- }
- [HideInInspector]
- public bool isClickBtnEast;
- public bool btnWestPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.J) || isClickBtnWest;
- return isClickBtnWest;
- }
- }
- [HideInInspector]
- public bool isClickBtnWest;
- public bool btnNorthPress
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- return isClickBtnNorth;
- }
- }
- [HideInInspector]
- public bool isClickBtnNorth;
- public bool btnSpiritSummon
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- return isSpiritSummon;
- }
- }
- [HideInInspector]
- public bool isSpiritSummon;
- public bool btnSpiritSummon1
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- return isSpiritSummon1;
- }
- }
- [HideInInspector]
- public bool isSpiritSummon1;
- public bool btnSpiritSummon2
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- return isSpiritSummon2;
- }
- }
- [HideInInspector]
- public bool isSpiritSummon2;
- public bool btnSpiritSummon3
- {
- get
- {
- //return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
- return isSpiritSummon3;
- }
- }
- [HideInInspector]
- public bool isSpiritSummon3;
- public bool btnNorthKeep
- {
- get
- {
- //return Input.GetKey(KeyCode.I) || isKeepBtnNorth;
- return isKeepBtnNorth;
- }
- }
- [HideInInspector]
- public bool isKeepBtnNorth;
- [HideInInspector]
- public Vector2 leftDir;
- public int playerId;
- public SkeletonMecanim skeletonMecanim;
- public SkeletonDataAsset[] playerSpine;
- public bool isInvisible;
- //public Vector2 leftDir
- //{
- // get
- // {
- // int x = 0;
- // int y = 0;
- // if (Input.GetKey(KeyCode.A))
- // {
- // x--;
- // }
- // if (Input.GetKey(KeyCode.D))
- // {
- // x++;
- // }
- // if (Input.GetKey(KeyCode.S))
- // {
- // y--;
- // }
- // if (Input.GetKey(KeyCode.W))
- // {
- // y++;
- // }
- // return new Vector2(x, y);
- // }
- //}
- public override void Init()
- {
- base.Init();
- mp = totalMp;
- uiMp.Show(mp, totalMp);
- }
- private void Awake()
- {
- PlayerInput playerInput = transform.GetComponent<PlayerInput>();
- spirits = GetComponent<Spirits>();
- playerId = playerInput.playerIndex;
- transform.position = new Vector3(142 + 4 * playerId, 0, 0);
- playerRope.playerId = playerId;
- sprintLinkTrigger.playerID = playerId;
- if (PlayersInput.instance[1] == PlayersInput.instance[0])
- {
- PlayersInput.instance[1] = this;
- }
- PlayersInput.instance[playerId] = this;
- skeletonMecanim.skeletonDataAsset = playerSpine[playerId];
-
- //else
- //{
- // DestroyImmediate(gameObject);
- // return;
- //}
- demonicDic = new Dictionary<int, List<Demonic>>();
- demonicId = new List<int>() { 0, 0, 0, 0, 0, 0, 0 };
- Init();
- }
- private void Update()
- {
- //if (Input.GetKeyDown(KeyCode.LeftShift))
- //{
- // isClickBtnRush = true;
- //}
- //if (Input.GetKey(KeyCode.LeftShift))
- //{
- // isKeepBtnRush = 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;
- //}
- if (LBisHold)
- {
- isKeepBtnRush = true;
- }
- if (floatState != 0)
- {
- CharacterFloat();
- }
- if (endChange != 0)
- {
- pastChangeTime += Time.deltaTime;
- if (pastChangeTime >= changeTime)
- {
- Transfiguration(endChange);
- endChange = 0;
- pastChangeTime = 0;
- }
- }
- }
- void OnSprintingPress()
- {
- LBisHold = true;
- isClickBtnRush = true;
- isKeepBtnRush = true;
- }
- void OnSprintingRelease()
- {
- LBisHold = false;
- }
- //手柄按下LB
- [HideInInspector]
- public bool LBisHold;
- //读取手柄参数
- private void OnMove(InputValue value)
- {
- leftDir = value.Get<Vector2>();
- }
- void OnJump()
- {
- isClickBtnJump = true;
- }
- void OnSummon0()
- {
- isClickBtnWest = true;
- }
- void OnSummon1()
- {
- isClickBtnSouth = true;
- }
- void OnSummon2()
- {
- isClickBtnEast = true;
- }
- void OnSummonSpirit()
- {
- isSpiritSummon = true;
- }
- void OnSummonSpirit1()
- {
- isSpiritSummon1 = true;
- }
- void OnSummonSpirit2()
- {
- isSpiritSummon2 = true;
- }
- void OnSummonSpirit3()
- {
- isSpiritSummon3 = true;
- }
- public void Jump()
- {
- SetUpSpeed(jumpSpeed);
- ani.Play("jump", 0, 0);
- }
- public void AirJump()
- {
- SetUpSpeed(airJumpSpeed);
- ani.Play("jump", 0, 0);
- }
- public void SetUpSpeed(float speed)
- {
- ChangeState(CharacterState.Rise);
- Vector3 velocity = rb.velocity;
- CheckTurn();
- velocity.y = speed;
- rb.velocity = velocity;
- //animalAni.SetInteger("state", (int)PlayerState.Rise);
- }
- public bool CheckSummon()
- {
- if (cacheSummonTime > 0)
- {
- Summon(cacheSummonId);
- return true;
- }
- if (btnWestPress)
- {
- Summon(0);
- return true;
- }
- if (btnSouthPress)
- {
- Summon(1);
- return true;
- }
- if (btnEastPress)
- {
- Summon(2);
- return true;
- }
- if (isSpiritSummon)
- {
- Transfiguration(3);
- return true;
- }
- if (isSpiritSummon1)
- {
- Transfiguration(4);
- return true;
- }
- if (isSpiritSummon2)
- {
- Transfiguration(5);
- return true;
- }
- if (isSpiritSummon3)
- {
- Transfiguration(6);
- return true;
- }
- return false;
- }
- //角色处于可自由活动状态时的通用切换状态逻辑,如Idle、Run状态,以及别的状态结束时准备回到Idle状态前
- public bool CheckPlayerChangeState(CharacterState excludeState = CharacterState.None)
- {
- if (!foot.TrigGround)
- {
- if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
- {
- if (excludeState != CharacterState.Rush)
- {
- ChangeState(CharacterState.Rush);
- return true;
- }
- }
- if (rb.velocity.y > 0)
- {
- if (excludeState != CharacterState.Rise)
- {
- ChangeState(CharacterState.Rise);
- return true;
- }
- }
- else
- {
- if (excludeState != CharacterState.Fall)
- {
- ChangeState(CharacterState.Fall);
- return true;
- }
- }
- }
- else
- {
- airJumped = false;
- //if (btnNorthPress || cacheAttackTime > 0)
- //{
- // if (excludeState != CharacterState.Attack)
- // {
- // Attack1();
- // return true;
- // }
- //}
- if (eventTrigger.triggedRivet && (btnNorthPress || cachePullRopeTime > 0))
- {
- if (sprintLinkTrigger.linkedEnemy.Count > 0)
- {
- eventTrigger.triggedRivet.BindingRopes(sprintLinkTrigger.linkedEnemy);
- }
- //ChangeState(CharacterState.PullRope);
- }
- if (excludeState != CharacterState.Summon)
- {
- if (CheckSummon())
- {
- return true;
- }
- }
- if (excludeState != CharacterState.Transfiguration)
- {
- if (CheckSummon())
- {
- return true;
- }
- }
- if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
- {
- if (excludeState != CharacterState.Rush)
- {
- ChangeState(CharacterState.Rush);
- return true;
- }
- }
- if (btnJumpPress || cacheJumpTime > 0)
- {
- if (excludeState != CharacterState.Rise)
- {
- Jump();
- ChangeState(CharacterState.Rise);
- return true;
- }
- }
- if (leftDir.x > 0.3f || leftDir.x < -0.3f)
- {
- if (excludeState != CharacterState.Run)
- {
- ChangeState(CharacterState.Run);
- return true;
- }
- }
- else
- {
- if (excludeState != CharacterState.Idle)
- {
- ChangeState(CharacterState.Idle);
- return true;
- }
- }
- }
- return false;
- }
- public override Vector3 GetMoveDir()
- {
- return leftDir;
- }
- public void CachedPlayerInput()
- {
- if (btnRushPress)
- {
- cacheRushTime = totalCacheRushTime;
- }
- if (btnJumpPress)
- {
- cacheJumpTime = totalCacheJumpTime;
- }
- if (btnNorthPress)
- {
- cacheAttackTime = totalCacheAttackTime;
- }
- if (btnWestPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 0;
- }
- if (btnSouthPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 1;
- }
- if (btnEastPress)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 2;
- }
- if (btnSpiritSummon)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 3;
- }
- if (btnSpiritSummon1)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 4;
- }
- if (btnSpiritSummon2)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 5;
- }
- if (btnSpiritSummon3)
- {
- cacheSummonTime = totalCacheSummonTime;
- cacheSummonId = 6;
- }
- }
- public override void OnState()
- {
- base.OnState();
- hurtKeepTime -= Time.deltaTime;
- cacheJumpTime -= Time.deltaTime;
- cacheAttackTime -= Time.deltaTime;
- cacheSummonTime -= Time.deltaTime;
- canJumpTime -= Time.deltaTime;
- invincibleTime -= Time.deltaTime;
- attackTime -= Time.deltaTime;
- summonTime -= Time.deltaTime;
- rushTime -= Time.deltaTime;
- cacheRushTime -= Time.deltaTime;
- dieKeepTime -= Time.deltaTime;
- weakTime -= Time.deltaTime;
- Vector3 velocity = rb.velocity;
- switch (state)
- {
- case CharacterState.Idle:
- if (CheckPlayerChangeState(CharacterState.Idle))
- {
- break;
- }
- break;
- case CharacterState.Run:
- if (CheckPlayerChangeState(CharacterState.Run))
- {
- break;
- }
- CheckTurn();
- if (leftDir.x > 0.3f)
- {
- rb.velocity = Vector3.right * moveSpeed;
- }
- else if (leftDir.x < -0.3f)
- {
- rb.velocity = Vector3.left * moveSpeed;
- }
- break;
- case CharacterState.Rise:
- if (CheckSummon())
- {
- break;
- }
- if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
- {
- ChangeState(CharacterState.Rush);
- break;
- }
- if (rb.velocity.y <= 0)
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- if (btnJumpPress || cacheJumpTime > 0)
- {
- if (!airJumped && rb.velocity.y < airJumpSpeed)
- {
- airJumped = true;
- AirJump();
- break;
- }
- }
- CachedPlayerInput();
- rb.velocity = AirMove(rb.velocity);
- break;
- case CharacterState.Fall:
- if (CheckSummon())
- {
- break;
- }
- if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
- {
- ChangeState(CharacterState.Rush);
- break;
- }
- if (foot.TrigGround)
- {
- if (CheckPlayerChangeState())
- {
- 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 (canJumpTime > 0)
- {
- Jump();
- break;
- }
- else if (!airJumped)
- {
- airJumped = true;
- AirJump();
- break;
- }
- }
- CachedPlayerInput();
- rb.velocity = AirMove(rb.velocity);
- break;
- case CharacterState.Hurt:
- if (hurtKeepTime <= 0 && rb.velocity.magnitude < hurtChangeVelocity)
- {
- if (CheckPlayerChangeState())
- {
- break;
- }
- }
- if (!foot.TrigGround)
- {
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- }
- Vector3 vel = rb.velocity;
- vel.x = vel.x * (1 - decelerationRatio * Time.deltaTime);
- rb.velocity = vel;
- CachedPlayerInput();
- break;
- case CharacterState.Coma:
- break;
- case CharacterState.Attack:
- if (attackTime <= 0)
- {
- if (btnNorthKeep)
- {
- ChangeState(CharacterState.KeepAttack);
- break;
- }
- if (CheckPlayerChangeState())
- {
- break;
- }
- }
- CachedPlayerInput();
- break;
- case CharacterState.KeepAttack:
- if ((btnRushPress) && mp >= rushCostMp)
- {
- ChangeState(CharacterState.Rush);
- break;
- }
- if (btnJumpPress && canJumpTime > 0)
- {
- Jump();
- break;
- }
- if (!btnNorthKeep)
- {
- if (CheckPlayerChangeState(CharacterState.Attack))
- {
- break;
- }
- }
- switch (attackState)
- {
- case PlayerAttackState.Idle:
- if (bodyTrans.localScale.x > 0)
- {
- if (leftDir.x > 0.3f)
- {
- SetAttackState(PlayerAttackState.WalkBack);
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x < -0.3f)
- {
- SetAttackState(PlayerAttackState.WalkForward);
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- }
- else
- {
- if (leftDir.x > 0.3f)
- {
- SetAttackState(PlayerAttackState.WalkForward);
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x < -0.3f)
- {
- SetAttackState(PlayerAttackState.WalkBack);
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- }
- velocity.x = 0;
- rb.velocity = velocity;
- break;
- case PlayerAttackState.WalkForward:
- if (bodyTrans.localScale.x > 0)
- {
- if (leftDir.x > 0.3f)
- {
- SetAttackState(PlayerAttackState.WalkBack);
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
- {
- SetAttackState(PlayerAttackState.Idle);
- velocity.x = 0;
- rb.velocity = velocity;
- break;
- }
- else
- {
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- }
- }
- else
- {
- if (leftDir.x < -0.3f)
- {
- SetAttackState(PlayerAttackState.WalkBack);
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
- {
- SetAttackState(PlayerAttackState.Idle);
- velocity.x = 0;
- rb.velocity = velocity;
- break;
- }
- else
- {
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- }
- }
- break;
- case PlayerAttackState.WalkBack:
- if (bodyTrans.localScale.x > 0)
- {
- if (leftDir.x < -0.3f)
- {
- SetAttackState(PlayerAttackState.WalkForward);
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
- {
- SetAttackState(PlayerAttackState.Idle);
- velocity.x = 0;
- rb.velocity = velocity;
- break;
- }
- else
- {
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- }
- }
- else
- {
- if (leftDir.x > 0.3f)
- {
- SetAttackState(PlayerAttackState.WalkForward);
- velocity.x = attackMoveSpeed;
- rb.velocity = velocity;
- break;
- }
- else if (leftDir.x > -0.3f && leftDir.x > 0.3f)
- {
- SetAttackState(PlayerAttackState.Idle);
- velocity.x = 0;
- rb.velocity = velocity;
- break;
- }
- else
- {
- velocity.x = -attackMoveSpeed;
- rb.velocity = velocity;
- }
- }
- break;
- default:
- break;
- }
- break;
- case CharacterState.Summon:
- if (summonTime <= 0)
- {
- if (CheckPlayerChangeState())
- {
- break;
- }
- }
- break;
- case CharacterState.Rush:
- if (rushTime <= 0)
- {
- if (btnRushKeep)
- {
- ChangeState(CharacterState.Sprint);
- break;
- }
- if (CheckPlayerChangeState())
- {
- break;
- }
- }
- CachedPlayerInput();
- //if (leftDir.magnitude < 0.3f)
- //{
- // if (bodyTrans.localScale.x > 0)
- // {
- // rushDir = Vector3.left;
- // }
- // else
- // {
- // rushDir = Vector3.right;
- // }
- //}
- //else
- //{
- // rushDir = leftDir.normalized;
- //}
- rb.velocity = rushDir * rushSpeed;
- break;
- case CharacterState.Sprint:
- if (!btnRushKeep)
- {
- if (CheckPlayerChangeState(CharacterState.Rush))
- {
- break;
- }
- }
- if (mp < sprintCostMp * Time.deltaTime)
- {
- if (CheckPlayerChangeState(CharacterState.Rush))
- {
- break;
- }
- }
- mp -= sprintCostMp * Time.deltaTime;
- uiMp.Show(mp, totalMp);
- CachedPlayerInput();
- //CheckTurn();
- //if (leftDir.magnitude < 0.3f)
- //{
- // if (bodyTrans.localScale.x > 0)
- // {
- // rushDir = Vector3.left;
- // }
- // else
- // {
- // rushDir = Vector3.right;
- // }
- //}
- //else
- //{
- // rushDir = leftDir.normalized;
- //}
- rb.velocity = rushDir * rushSpeed;
- break;
- case CharacterState.Die:
- if (dieKeepTime <= 0)
- {
- gameObject.SetActive(false);
- break;
- }
- break;
- case CharacterState.Weak:
- if (weakTime <= 0)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- break;
- case CharacterState.PullRope:
- break;
- default:
- break;
- }
- if (!foot.TrigGround)
- {
- if (rb.velocity.y > 0)
- {
- rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
- }
- else
- {
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- }
- }
- isClickBtnRush = false;
- isKeepBtnRush = false;
- isClickBtnJump = false;
- isClickBtnSouth = false;
- isClickBtnEast = false;
- isClickBtnNorth = false;
- isClickBtnWest = false;
- isSpiritSummon = false;
- isSpiritSummon1 = false;
- isSpiritSummon2 = false;
- isSpiritSummon3 = false;
- if (foot.TrigGround)
- {
- canJumpTime = leaveGroundCanJumpTime;
- }
- SearchTarget();
- attackTarget = targetCharacter;
- if (floatState == 0)
- {
- if (mp < totalMp)
- {
- mp += mpReplySpeed * Time.deltaTime;
- }
- if (mp > totalMp)
- {
- mp = totalMp;
- }
- }
- else
- {
- if (mp > 0)
- {
- lostMp += mpReplySpeed * Time.deltaTime;
- mp -= mpReplySpeed * Time.deltaTime;
- }
- if (mp < 0)
- {
- mp = 0;
- }
- if (lostMp >= addMp)
- {
- Instantiate(soul, transform.position, new Quaternion(0, 0, 0, 0), null);
- lostMp = 0;
- }
- }
- uiMp.Show(mp, totalMp);
- }
- public override void ChangeState(CharacterState newState)
- {
- Vector3 velocity = rb.velocity;
- switch (state)
- {
- case CharacterState.Idle:
- break;
- case CharacterState.Run:
- velocity.x = 0;
- break;
- case CharacterState.Rise:
- break;
- case CharacterState.Fall:
- break;
- case CharacterState.Hurt:
- break;
- case CharacterState.Coma:
- foreach (GameObject i in HitCols)
- {
- i.SetActive(true);
- }
- break;
- case CharacterState.Attack:
- aniCollider.Play("NotAttack", 1, 0);
- break;
- case CharacterState.KeepAttack:
- aniCollider.Play("NotAttack", 1, 0);
- break;
- case CharacterState.Summon:
- rb.isKinematic = false;
- break;
- case CharacterState.Transfiguration:
- rb.isKinematic = false;
- break;
- case CharacterState.Rush:
- velocity = Vector3.zero;
- break;
- case CharacterState.Sprint:
- velocity = Vector3.zero;
- break;
- case CharacterState.Die:
- isDie = false;
- break;
- case CharacterState.Weak:
- break;
- default:
- break;
- }
- CharacterState oldState = state;
- state = newState;
- switch (newState)
- {
- case CharacterState.Idle:
- aniCollider.Play("Idle", 0, 0);
- if (oldState == CharacterState.Fall)
- {
- ani.Play("fall_end", 0, 0);
- }
- else
- {
- ani.Play("idle", 0, 0);
- }
- velocity = Vector3.zero;
- //animalAni.SetInteger("state", (int)PlayerState.Idle);
- break;
- case CharacterState.Run:
- aniCollider.Play("Run", 0, 0);
- ani.Play("run_start", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Walk);
- break;
- case CharacterState.Rise:
- aniCollider.Play("Rise", 0, 0);
- canJumpTime = 0;
- break;
- case CharacterState.Fall:
- aniCollider.Play("Fall", 0, 0);
- ani.Play("fall", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Fall);
- break;
- case CharacterState.Hurt:
- aniCollider.Play("Hurt", 0, 0);
- ani.Play("hitted", 0, 0);
- invincibleTime = totalInvincibleTime;
- hurtKeepTime = minHurtKeepTime;
- //ani.Play("Invincible", 2, 0);
- break;
- case CharacterState.Coma:
- //ani.Play("Coma", 0, 0);
- ani.Play("idle", 0, 0);
- aniCollider.Play("Idle", 0, 0);
- rb.velocity = Vector3.zero;
- foreach (GameObject i in HitCols)
- {
- i.SetActive(false);
- }
- break;
- case CharacterState.Attack:
- attackTime = totalAttack1Time;
- break;
- case CharacterState.KeepAttack:
- aniCollider.Play("Attack1Keep", 1, 0);
- break;
- case CharacterState.Summon:
- aniCollider.Play("Summon", 0, 0);
- ani.Play("summon", 0, 0);
- velocity = Vector3.zero;
- rb.isKinematic = true;
- break;
- case CharacterState.Transfiguration:
- isTransfiguration = true;
- aniCollider.Play("Transfiguration", 0, 0);
- ani.Play("transfiguration", 0, 0);
- velocity = Vector3.zero;
- rb.isKinematic = true;
- break;
- case CharacterState.Rush:
- aniCollider.Play("Rush", 0, 0);
- ani.Play("rush_loop", 0, 0);
- rushTime = totalRushTime;
- invincibleTime = rushInvincibleTime;
- //if (leftDir.magnitude < 0.3f)
- //{
- // if (bodyTrans.localScale.x > 0)
- // {
- // rushDir = Vector3.left;
- // }
- // else
- // {
- // rushDir = Vector3.right;
- // }
- //}
- //else
- //{
- // rushDir = leftDir.normalized;
- //}
- if (bodyTrans.localScale.x > 0)
- {
- rushDir = Vector3.left;
- }
- else
- {
- rushDir = Vector3.right;
- }
- velocity = rushDir * rushSpeed;
- mp -= rushCostMp;
- uiMp.Show(mp, totalMp);
- break;
- case CharacterState.Sprint:
- aniCollider.Play("Sprint", 0, 0);
- ani.Play("rush_loop", 0, 0);
- velocity = rushDir * rushSpeed;
- break;
- case CharacterState.Die:
- aniCollider.Play("Die", 0, 0);
- ani.Play("die", 0, 0);
- isDie = true;
- dieKeepTime = totalDieKeepTime;
- break;
- case CharacterState.Weak:
- aniCollider.Play("Weak", 0, 0);
- ani.Play("weak", 0, 0);
- velocity.y = weakUpSpeed;
- weakTime = totalWeakTime;
- break;
- default:
- break;
- }
- rb.velocity = velocity;
- }
- public void CheckTurn()
- {
- if (leftDir.x > 0.3f && bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- else if (leftDir.x < -0.3f && bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- public Vector3 AirMove(Vector3 velocity)
- {
- CheckTurn();
- if (leftDir.x > 0.3f)
- {
- velocity = new Vector3(moveSpeed, velocity.y, velocity.z);
- }
- else if (leftDir.x < -0.3f)
- {
- velocity = new Vector3(-moveSpeed, velocity.y, velocity.z);
- }
- else
- {
- velocity = new Vector3(0, velocity.y, velocity.z);
- }
- return velocity;
- }
- public void Transfiguration(int id) //变身
- {
- id = id - 3;
- if (id >= changePrefabs.Count)
- {
- Debug.LogError("未配置" + id + "号变身");
- return;
- }
- if (id == 6 && isInvisible)
- {
- isInvisible = false;
- return;
- }
- GameObject prefab = changePrefabs[id];
- if (!CheckCanSummon(id))
- {
- return;
- }
- ChangeState(CharacterState.Transfiguration);
- summonTime = prefab.GetComponent<Demonic>().totalSummonTime;
- float costMp = prefab.GetComponent<Demonic>().costMp;
- mp -= costMp;
- uiMp.Show(mp, totalMp);
- bodyTrans.gameObject.SetActive(false);
- spiritObj = PoolManager.Instantiate(prefab);
- Demonic dem = spiritObj.GetComponent<Demonic>();
- dem.id = id;
- dem.playerID = playerId;
- spiritObj.transform.parent = transform;
- spiritObj.transform.localEulerAngles = Vector3.zero;
- spiritObj.transform.localScale = new Vector3(1, 1, 1);
- if (bodyTrans.localScale.x > 0)
- {
- spiritObj.transform.position = transform.position;
- if (dem.bodyTrans.localScale.x < 0)
- {
- dem.Turn();
- }
- }
- else
- {
- spiritObj.transform.position = transform.position ;
- if (dem.bodyTrans.localScale.x > 0)
- {
- dem.Turn();
- }
- }
- /* 法师出场释放浮空场
- if (id == 3)
- {
- if ((int)spirits.currentSpirit == 0)
- {
- if (!spiritObj.GetComponent<Demonic>().hasEffect)
- {
- spiritObj.GetComponent<Demonic>().hasEffect = true;
- Instantiate(spirits.floatEffect, spiritObj.transform.position, new Quaternion(0, 0, 0, 0), spiritObj.transform);
- }
- }
- }
- */
- endChange = id + 3;
- }
- public void EndTransfiguration(int id)
- {
- bodyTrans.gameObject.SetActive(true);
- spiritObj.transform.parent = null;
- spiritObj.SetActive(false);
- Summon(id);
- }
- public void Summon(int id)
- {
- if (id >= demonicPrefabs.Count)
- {
- Debug.LogError("未配置" + id + "号使魔");
- return;
- }
- if (id >= demonicSummonPos.Count)
- {
- Debug.LogError("未配置" + id + "号使魔召唤位置");
- return;
- }
- if (id == 6 && isInvisible)
- {
- isInvisible = false;
- return;
- }
- GameObject prefab = demonicPrefabs[id];
- if (!CheckCanSummon(id))
- {
- return;
- }
- ChangeState(CharacterState.Summon);
- summonTime = prefab.GetComponent<Demonic>().totalSummonTime;
- float costMp = prefab.GetComponent<Demonic>().costMp;
- mp -= costMp;
- uiMp.Show(mp, totalMp);
- GameObject demonicObj = PoolManager.Instantiate(prefab);
- Demonic demonic = demonicObj.GetComponent<Demonic>();
- demonic.id = id;
- demonic.playerID = playerId;
- if (!demonicDic.ContainsKey(id))
- {
- demonicDic.Add(id, new List<Demonic>());
- }
- demonicDic[id].Add(demonic);
- demonicObj.transform.parent = null;
- demonicObj.transform.localEulerAngles = Vector3.zero;
- demonicObj.transform.localScale = new Vector3(1, 1, 1);
- Vector3 offset = demonicSummonPos[id];
- if (bodyTrans.localScale.x > 0)
- {
- demonicObj.transform.position = transform.position + offset;
- if (demonic.bodyTrans.localScale.x < 0)
- {
- demonic.Turn();
- }
- }
- else
- {
- demonicObj.transform.position = transform.position + new Vector3(-offset.x, offset.y, offset.z);
- if (demonic.bodyTrans.localScale.x > 0)
- {
- demonic.Turn();
- }
- }
- if (demonic.canFly)
- {
- demonic.flyHeight = demonic.transform.position.y;
- }
- if (id == 3)
- {
- if ((int)spirits.currentSpirit == 0)
- {
- if (!demonicObj.GetComponent<Demonic>().hasEffect)
- {
- demonicObj.GetComponent<Demonic>().hasEffect = true;
- Instantiate(spirits.floatEffect, demonicObj.transform.position, new Quaternion(0, 0, 0, 0), demonicObj.transform);
- }
- }
- }
- demonic.Init();
- demonic.SetSortingOrder(demonic.sortingOrder + demonicId[id]);
- demonicId[id]++;
- demonic.Attack1();
- }
- public void OnDemonicRecycle(Demonic demonic)
- {
- if (!demonicDic.ContainsKey(demonic.id))
- {
- return;
- }
- demonicDic[demonic.id].Remove(demonic);
- for (int i = 0; i < demonicDic[demonic.id].Count; i++)
- {
- demonicDic[demonic.id][i].SetSortingOrder(demonic.id * 1000 + i);
- }
- }
- public bool CheckCanSummon(int id)
- {
- GameObject prefab = demonicPrefabs[id];
- float costMp = prefab.GetComponent<Demonic>().costMp;
- if (mp < costMp)
- {
- Debug.Log("mp不足召唤失败, 还得加个动画或者音效啥的");
- return false;
- }
- return true;
- }
- public override void Attack1()
- {
- base.Attack1();
- if (leftDir.x > 0.3f)
- {
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- SetAttackState(PlayerAttackState.WalkForward);
- }
- else if (leftDir.x < -0.3f)
- {
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- SetAttackState(PlayerAttackState.WalkForward);
- }
- else
- {
- SetAttackState(PlayerAttackState.Idle);
- }
- }
- public void SetAttackState(PlayerAttackState value)
- {
- attackState = value;
- ani.SetInteger("attackState", (int)value);
- aniCollider.Play("Attack1Keep", 1, 0);
- }
- public void SearchTarget()
- {
- targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
- }
- }
|