private void changeState(BasicMeleeState nextState) { if (nextState == BasicMeleeState.Meet) { if (aStar.canMove) { aStar.canMove = false; } animator.Play("basic_melee_idle"); } else if (nextState == BasicMeleeState.Aggro) { if (!aStar.canMove) { aStar.canMove = true; } animator.Play("basic_melee_move"); } else if (nextState == BasicMeleeState.Attack) { if (aStar.canMove) { aStar.canMove = false; } animator.Play("basic_melee_attack"); } else if (nextState == BasicMeleeState.Death) { stateCounter = 0; if (aStar.canMove) { aStar.canMove = false; } animator.Play("dead"); } else if (nextState == BasicMeleeState.Hurt) { stateCounter = 0; attackCooldownCounter = 0; if (aStar.canMove) { aStar.canMove = false; } animator.Play("hurt"); } else { if (aStar.canMove) { aStar.canMove = false; } animator.Play("basic_melee_idle"); } currentState = nextState; }
// Start is called before the first frame update void Start() { currentState = BasicMeleeState.Idle; player = GameObject.FindGameObjectWithTag("Player"); playerGridPosition = player.GetComponent <GridPosition>(); playerHealth = player.GetComponent <PlayerHealth>(); gridPosition = GetComponent <GridPosition>(); aStar = GetComponent <AIPath>(); animator = GetComponentInChildren <Animator>(); attackCooldownCounter = 0; attackTimeCounter = 0; canAttack = true; stateCounter = 0; monsterHealth = GetComponent <MonsterHealth>(); attacksfx.Pause(); }
// Start is called before the first frame update void Start() { currentState = BasicMeleeState.Idle; player = GameObject.FindGameObjectWithTag("Player"); playerGridPosition = player.GetComponent <GridPosition>(); playerHealth = player.GetComponent <PlayerHealth>(); gridPosition = GetComponent <GridPosition>(); aStar = GetComponent <AIPath>(); attackCooldownCounter = 0; attackTimeCounter = 0; canAttack = false; spriteRenderer = GetComponent <SpriteRenderer>(); returnPoint = Instantiate(pointer); returnPoint.transform.position = transform.position; destinationSetter = GetComponent <AIDestinationSetter>(); aiController = GetComponent <EnemyAIController>(); }
private void StateMachineRunningPerFrame() { BasicMeleeState nextState = BasicMeleeState.Idle; if (currentState == BasicMeleeState.Meet) { if (aStar.canMove) { aStar.canMove = false; } //spriteRenderer.color = Color.cyan; // Check if aggro if (!IsPlayerInMeetRange()) { nextState = BasicMeleeState.Idle; } else if (IsPlayerInAggroRange()) { nextState = BasicMeleeState.Aggro; } else { nextState = BasicMeleeState.Meet; } } else if (currentState == BasicMeleeState.Aggro) { aiController.targetToMove = player.transform; aiController.targetToTrigger = player.transform; if (!aStar.canMove) { aStar.canMove = true; } if (!IsPlayerInMeetRange()) { nextState = BasicMeleeState.Idle; } else if (IsPlayerInAttackRange() && canAttack) { summonAttackAreaPosition = player.transform.position; nextState = BasicMeleeState.Attack; } else { nextState = BasicMeleeState.Aggro; } } else if (currentState == BasicMeleeState.Attack) { //spriteRenderer.color = Color.red; if (aStar.canMove) { aStar.canMove = false; } if (attackTimeCounter >= attckTimeFrame) { canAttack = false; SummonAttackArea(); attackTimeCounter = 0; nextState = BasicMeleeState.Aggro; } else { attackTimeCounter++; nextState = BasicMeleeState.Attack; } } else { if (IsMoveToReturn()) { aiController.targetToMove = returnPoint.transform; aiController.targetToTrigger = returnPoint.transform; aStar.canMove = true; } else { destinationSetter.target = player.transform; aStar.canMove = false; } //spriteRenderer.color = Color.white; // Check if meet if (IsPlayerInMeetRange()) { nextState = BasicMeleeState.Meet; } else { nextState = BasicMeleeState.Idle; } } currentState = nextState; }
private void StateMachineRunningPerFrame() { BasicMeleeState nextState = BasicMeleeState.Idle; if (currentState == BasicMeleeState.Meet) { // Check if aggro if (!IsPlayerInMeetRange()) { nextState = BasicMeleeState.Idle; } else if (IsPlayerInAggroRange()) { nextState = BasicMeleeState.Aggro; } else { nextState = BasicMeleeState.Meet; } } else if (currentState == BasicMeleeState.Aggro) { if (!IsPlayerInMeetRange()) { nextState = BasicMeleeState.Idle; } else if (IsPlayerInAttackRange() && canAttack) { nextState = BasicMeleeState.Attack; } else { nextState = BasicMeleeState.Aggro; } } else if (currentState == BasicMeleeState.Attack) { if (attackTimeCounter >= attckTimeFrame) { canAttack = false; if (IsPlayerInAttackRange()) { playerHealth.DealDamage(attackDamage); } attackTimeCounter = 0; attacksfx.Play(); nextState = BasicMeleeState.Aggro; } else { attackTimeCounter++; nextState = BasicMeleeState.Attack; } } else if (currentState == BasicMeleeState.Death) { if (stateCounter >= deathFrame) { nextState = BasicMeleeState.Death; monsterHealth.Die(); } else { stateCounter++; nextState = BasicMeleeState.Death; } } else if (currentState == BasicMeleeState.Hurt) { if (stateCounter >= hurtFrame) { nextState = BasicMeleeState.Meet; } else { stateCounter++; nextState = BasicMeleeState.Hurt; } } else { // Check if meet if (IsPlayerInMeetRange()) { nextState = BasicMeleeState.Meet; } else { nextState = BasicMeleeState.Idle; } } if (currentState != BasicMeleeState.Hurt && monsterHealth.IsHurt && currentState != BasicMeleeState.Death) { nextState = BasicMeleeState.Hurt; } if (currentState != BasicMeleeState.Death && monsterHealth.IsDie) { nextState = BasicMeleeState.Death; } if (nextState != currentState) { changeState(nextState); } }