private void SpawnEnemy() { _enemyCount++; var hp = Instantiate(_enemyHpViewPrefab, Vector3.zero, Quaternion.identity) as EnemyHpView; hp.transform.SetParent(_canvas.transform); EnemyEntity enemy = null; var rnd = UnityEngine.Random.Range(0, 2); if (rnd == 0) { enemy = Instantiate(_golemPrefab, _spawnPoint.position, Quaternion.identity) as EnemyEntity; } else { enemy = Instantiate(_gydraPrefab, _spawnPoint.position, Quaternion.identity) as EnemyEntity; } enemy.GetComponent <EnemyMovement>().SetPath(_wayPoints); _enemyes.Add(enemy); EnemyCountChanged(); enemy.Destroyed += OnEnemyDestroyed; hp.SetTarget(enemy); }
void MoveToEnemy() { if (Target.GetComponent <EnemyEntity>().Health > 0) { transform.LookAt(Target.transform.position + Vector3.up); transform.Translate(Vector3.forward * Time.deltaTime * Speed); } else { Destroy(gameObject); } }
/// <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(); }
void AddTargetsAsOther() { if (gameManager.enemyEntities.Count > 0) { foreach (EnemyEntity enemy in gameManager.enemyEntities) { Entity entity = enemy.GetComponent <Entity>(); if (!targets.Contains(entity)) { targets.Add(entity); entity.OnDeathEvent.AddListener(delegate { targets.Remove(entity); }); } } } }