public override void Exit() { if (collisionCtrl != null) { collisionCtrl.OnObstacleHit -= HandleOnObstacleHit; collisionCtrl.OnAgentHit -= HandleOnAgentHit; } if (lifeCtrl != null) { lifeCtrl.OnBossDead -= HandleOnBossDead; } if (bossPhaseCtrl != null) { bossPhaseCtrl.OnSecondPhaseStart -= HandleOnSecondPhaseStart; bossPhaseCtrl.OnThirdPhaseStart -= HandleOnThirdPhaseStart; } if (rb != null) { rb.useGravity = true; } bossCtrl = null; collisionCtrl = null; bossPhaseCtrl = null; lifeCtrl = null; currentStatePhase = StatePhases.Raising; distanceTraveled = 0; distanceToTravel = 0; startHeigth = 0; maxHeigth = 0; }
/// <summary> /// Funzione che esegue il comportamento della fase di Moving /// </summary> private void MovingBehaviour() { oldDistanceTraveled = distanceTraveled; if (distanceTraveled >= distanceToTravel) { float remamingDistance = Mathf.Abs(distanceToTravel - oldDistanceTraveled); Vector3 newPos = Vector3.MoveTowards(bossCtrl.transform.position, bossCtrl.transform.position + bossCtrl.transform.forward, remamingDistance); if (!collisionCtrl.CheckCollision(newPos)) { bossCtrl.transform.position = newPos; } currentStatePhase = StatePhases.Landing; } else { Vector3 oldPos = bossCtrl.transform.position; Vector3 newPos = Vector3.MoveTowards(bossCtrl.transform.position, bossCtrl.transform.position + bossCtrl.transform.forward, movementSpeed * Time.deltaTime); if (collisionCtrl.CheckCollision(newPos)) { HandleOnObstacleHit(null); } else { bossCtrl.transform.position = newPos; distanceTraveled += Vector3.Distance(bossCtrl.transform.position, oldPos); } } }
/// <summary> /// Called when a state exits and need to load the next state /// </summary> private void TransitionToNextState() { if (NextState == null) { return; } CurrentState = NextState; CurrentPhase = StatePhases.ENTER; NextState = null; }
/// <summary> /// Funzione che esegue il comportamento della fase di Raising /// </summary> private void RaisingBehaviour() { if (bossCtrl.transform.position.y >= maxHeigth) { Vector3 landingPosition = groupCtrl.GetGroupCenterPoint(); LookAtPosition(landingPosition); Vector3 fakeBossPos = bossCtrl.transform.position; fakeBossPos.y = landingPosition.y; distanceToTravel = Vector3.Distance(landingPosition, fakeBossPos); currentStatePhase = StatePhases.Moving; } else { bossCtrl.transform.position = Vector3.MoveTowards(bossCtrl.transform.position, bossCtrl.transform.position + Vector3.up, raisingSpeed * Time.deltaTime); } }
private void Update() { // update the current state based on the phase of the state switch (CurrentPhase) { case StatePhases.ENTER: CurrentState.OnEnter(); CurrentPhase = StatePhases.UPDATE; break; case StatePhases.UPDATE: CurrentState.Update(); break; case StatePhases.EXIT: CurrentState.OnExit(); TransitionToNextState(); break; default: throw new ArgumentOutOfRangeException(); } // Get the state's input Vector3 input = CurrentState.Input; // calculate the target velocity Vector3 targetVelocity = input * TargetSpeed; // set the ground velocity _groundVelocity = Vector3.MoveTowards(_groundVelocity, targetVelocity, TargetAcceleration * Time.deltaTime); // Apply gravity if (CharacterController.isGrounded) { _verticalVelocity = Vector3.zero; } _verticalVelocity += Physics.gravity * Time.deltaTime; CharacterController.Move((_groundVelocity + _verticalVelocity) * Time.deltaTime); }
/// <summary> /// Used to transition to another state /// </summary> /// <param name="stateName">The name of the state to transition too</param> /// <param name="args">The arguments that is needed to handle a command, like a point in world space or a transform to follow.</param> public bool TransitionToState(string stateName, params object[] args) { NextState = GetState(stateName); if (NextState == null) { return(false); } // TODO: handle better if the same state. Good for now. if (CurrentState == NextState) { CurrentState.Initialize(this, args); CurrentPhase = StatePhases.ENTER; } else { NextState.Initialize(this, args); CurrentPhase = StatePhases.EXIT; } return(true); }
public override void Enter() { groupCtrl = context.GetLevelManager().GetGroupController(); bossCtrl = context.GetBossController(); lifeCtrl = bossCtrl.GetBossLifeController(); collisionCtrl = bossCtrl.GetBossCollisionController(); rb = collisionCtrl.GetRigidBody(); bossPhaseCtrl = bossCtrl.GetBossPhaseController(); rb.useGravity = false; startHeigth = bossCtrl.transform.position.y; maxHeigth = startHeigth + jumpHeight; distanceTraveled = 0; distanceToTravel = 0; currentStatePhase = StatePhases.Raising; bossPhaseCtrl.OnSecondPhaseStart += HandleOnSecondPhaseStart; bossPhaseCtrl.OnThirdPhaseStart += HandleOnThirdPhaseStart; lifeCtrl.OnBossDead += HandleOnBossDead; collisionCtrl.OnObstacleHit += HandleOnObstacleHit; collisionCtrl.OnAgentHit += HandleOnAgentHit; }
private void Awake() { CharacterController = GetComponent <CharacterController>(); if (calculatePathComponent == null) { Debug.LogError($"<b><color=red>Error:</color></b> Calculate Path Component is null. This class must have access to one", this); return; } State firstState = GetState(defaultState); if (firstState == null) { Debug.LogError($"<b><color=red>Error:</color></b> Could not find the default state, {defaultState}", this); return; } // initialize the first state CurrentState = firstState; CurrentState.Initialize(this); CurrentPhase = StatePhases.ENTER; }