private Rigidbody RB; //The rigid body of the enemy, used to move the enemy. // Start is called before the first frame update new void Start() { base.Start(); //Starts by setting up the state machine and needed states along with other variables. RB = GetComponent <Rigidbody>(); stateMachine = new StateMachine(); idleState = new BasicMeleeIdleState(gameObject, visionConeAngle, range, idleFloatingCurve); chaseState = new MobileBasicMeleeChaseState(gameObject, movementSpeed, stoppingDist, jumpCheckIterations, dashChance, dashCooldown, dashLength, RB, checkObj); attackState = new BasicMeleeAttackState(damage, beforeAttackDelay, afterAttackDelay, animator, attackAnim, this); //Setting up the transitions AT(idleState, chaseState, PlayerDetected()); //idleState -> chaseState AT(chaseState, idleState, PlayerUnDetected()); //chaseState -> idleState AT(chaseState, attackState, InAttackRange()); //chaseState -> attackState AT(attackState, chaseState, HitPlayer()); //attackState -> chaseState //Assigning the initial state of the state machine. stateMachine.SetState(idleState); //Converting the given angle from degrees to radians and calculating the cosine of that as the dot product produces the cos(angle in radians). visionConeAngle = Mathf.Cos(visionConeAngle * Mathf.Deg2Rad); //A wrapper function to make the transition setup cleaner. void AT(IState from, IState to, Func <bool> condition) => stateMachine.AddTransition(from, to, condition); //The conditions for each state transition. Func <bool> PlayerDetected() => () => (idleState.sawPlayer); Func <bool> PlayerUnDetected() => () => chaseState.playerDistance > range; Func <bool> InAttackRange() => () => Vector3.Distance(PlayerVariables.player.transform.position, transform.position) <= 5f && !attackState.attackFinished; Func <bool> HitPlayer() => () => attackState.attackFinished; }
private Rigidbody RB; //The rigid body of the enemy, used to move the enemy. // Start is called before the first frame update new void Start() { base.Start(); //Starts by setting up the state machine and needed states along with other variables. RB = GetComponent <Rigidbody>(); chargePS.Stop(); stateMachine = new StateMachine(); idleState = new BasicMeleeIdleState(gameObject, visionConeAngle, range, idleFloatingCurve); rotationState = new ChargeRotation(gameObject, rotationSpeed, chargeAngle, RB); chargeAttackState = new ChargeAttack(gameObject, chargePS, chargeMovementSpeed, chargeLength, checkFwdDist, RB); //Setting up the transitions AT(idleState, rotationState, PlayerDetected()); AT(rotationState, idleState, PlayerUnDetected()); AT(rotationState, chargeAttackState, StartCharge()); AT(chargeAttackState, rotationState, EndCharge()); //Assigning the initial state of the state machine. stateMachine.SetState(idleState); //Converting the given angle from degrees to radians and calculating the cosine of that as the dot product produces the cos(angle in radians). visionConeAngle = Mathf.Cos(visionConeAngle * Mathf.Deg2Rad); //A wrapper function to make the transition setup cleaner. void AT(IState from, IState to, Func <bool> condition) => stateMachine.AddTransition(from, to, condition); //The conditions for each state transition. Func <bool> PlayerDetected() => () => (idleState.sawPlayer); Func <bool> PlayerUnDetected() => () => rotationState.playerDistance > range; Func <bool> StartCharge() => () => rotationState.readyToCharge; Func <bool> EndCharge() => () => chargeAttackState.chargeDone; }
private Rigidbody RB; //The rigid body of the enemy, used to move the enemy. // Start is called before the first frame update new public void Start() { base.Start(); //Converting the given angle from degrees to radians and calculating the cosine of that as the dot product produces the cos(angle in radians). visionConeAngle = Mathf.Cos(visionConeAngle * Mathf.Deg2Rad); //Starts by setting up the state machine and needed states along with other variables. RB = GetComponent <Rigidbody>(); stateMachine = new StateMachine(); idleState = new BasicMeleeIdleState(gameObject, visionConeAngle, range, idleFloatingCurve); positionState = new BasicRangedPosition(gameObject, movementSpeed, approachRange, backoffRange, RB); attackState = new BasicRangedAttackState(gameObject, attackLength, eye, projectile, attackSpriteAnimator, attackSpriteAnimation, audioSource, attackAudio, this); //Setting up the transitions AT(idleState, positionState, InPositionRange()); AT(positionState, idleState, ReturnToIdle()); AT(positionState, attackState, InAttackRange()); AT(attackState, positionState, FinishedAttack()); AT(idleState, attackState, PlayerDetected()); //Assigning the initial state of the state machine. stateMachine.SetState(idleState); //A wrapper function to make the transition setup cleaner. void AT(IState from, IState to, Func <bool> condition) => stateMachine.AddTransition(from, to, condition); //The conditions for each state transition. Func <bool> InPositionRange() => () => (idleState.playerDistance <backoffRange || idleState.playerDistance> approachRange) && idleState.sawPlayer; Func <bool> ReturnToIdle() => () => positionState.hDistanceFromPlayer > range; Func <bool> InAttackRange() => () => positionState.inAttackRange; Func <bool> FinishedAttack() => () => attackState.attackDone; Func <bool> PlayerDetected() => () => idleState.sawPlayer && (idleState.playerDistance > backoffRange && idleState.playerDistance < approachRange); }
public override void EnterState(BaseAIMovementController owner) { _chasingState = new BasicMeleeChasingState(); _idleState = new BasicMeleeIdleState(); base.EnterState(owner); }