/// <summary> /// method called when a enemy should be spawn, /// -create the enemy with the enemy factory /// -call the method "CreateEnemyBehaviorFSM" for create the enemy state machine with the spawn info data /// -set the FSM to the enemy /// -add the FSM to the list /// -remove the spawn info from the level spawn info /// </summary> private void processEnemyInfo() { // create the enemy EnemyEntity enemyBeingSpawn = FactoryEnemies.CreateEnemy(_enemiesSpawnInfo[0].EnemyPrefab); // create the FSM for the enemy with the spawn data FSMachine machine = CreateEnemyBehaviorFSM(enemyBeingSpawn, _enemiesSpawnInfo[0]); // set the behavior var for the enemy enemyBeingSpawn.GetComponentInChildren <EnemyBehavior>().Behavior = machine; // adding the enemy to the FSM AddNewEnemyFSM(machine); // remove for the info _enemiesSpawnInfo.RemoveAt(0); }
/// <summary> /// Method call when the FSM Start. Creating the behavior for the FSM /// -Setting the plane to the initial position /// -Creating the state to go to the shot position and his transition to know when reach the position ( setting acctually by time) /// -Create the state shot with a always true transition ( converting the shot state in a one frame state ) /// -Create the state to go to the final position and his transition to know when reach the position ( setting acctually by time) /// </summary> public override void Create() { // getting the plane and getting the speed for easy time calculation EnemyEntity plane = (StateMachine as FSMEnemyBehavior).Plane; float planeSpeed = plane.GetComponentInChildren <EntityMovement>().Speed; float timeToReachPosition = 0; // setting the plane in the initial position plane.GetComponent <EntityMovement>().Position = _initialPosition; // calculate the time to reach the shop position timeToReachPosition = (_shotPosition - _initialPosition).magnitude / planeSpeed; // creating the state go to position FSMState stateGoToShotPosition = new FSMStateMoveToScreenPosition(this, _shotPosition, timeToReachPosition); // creating the transition to inform when reach shopPosition FSMTransition onReachShotPositionByTime = new FSMTransitionTime(this, timeToReachPosition); // creating state shot FSMState stateShot = new FSMStateShot(this); // creating one frame transition FSMTransition onShot = new FSMTransitionTrue(this); // calculating the time to reach the final positions timeToReachPosition = (_finalPosition - _shotPosition).magnitude / planeSpeed; // creating the state go to final position FSMState stateGoToFinalPositions = new FSMStateMoveToScreenPosition(this, _finalPosition, timeToReachPosition); // creating the transition when reach final position FSMTransition onReachFinalPositionByTime = new FSMTransitionTime(this, timeToReachPosition); //creating the states flow // fist state go to shot positions SetFirstState(stateGoToShotPosition); // go to shot position with transition on reach position by time stateGoToShotPosition.AddTransition(onReachShotPositionByTime); // on reach position go to state shot onReachShotPositionByTime.SetNextState(stateShot); // set transition on shot for state shot stateShot.AddTransition(onShot); // set next state as go to final position onShot.SetNextState(stateGoToFinalPositions); // set transition for state go to finish positoin as reach final position stateGoToFinalPositions.AddTransition(onReachFinalPositionByTime); base.Create(); }