// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        enemy.Accelerate();
        enemy.ApplyAcceleration();

        enemy.TurnRight();
    }
    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        enemy.Accelerate();
        enemy.ApplyAcceleration();

        if (player)
        {
            Vector2 directionToPlayer = player.transform.position - enemy.transform.position;
            directionToPlayer.Normalize();
            float angleToPlayer = Vector2.SignedAngle(directionToPlayer, enemy.transform.up);

            if (angleToPlayer > float.Epsilon)
            {
                enemy.TurnRight();
            }
            else if (angleToPlayer < -float.Epsilon)
            {
                enemy.TurnLeft();
            }

            if (Mathf.Abs(angleToPlayer) <= shootAngle)
            {
                if (!enemy.IsInvoking("Shoot"))
                {
                    enemy.InvokeRepeating("Shoot", 0.00001f, fireRate);
                }
            }
            else
            {
                enemy.CancelInvoke("Shoot");
            }

            if (Mathf.Abs(angleToPlayer) >= flickAngle)
            {
                animator.SetTrigger("Flick");
            }
        }
        else
        {
            enemy.CancelInvoke("Shoot");
        }
    }
Beispiel #3
0
        } = true;                                      //variable para saber si acabo de aparecer, si es asi no hago maniobra por atascamiento

        public void TakeAction(Enemy e, GameModel gameModel, PhysicsGame nivel)
        {
            if (timerJustAppeared <= 0f)
            {
                justAppeared = false;
            }
            else
            {
                timerJustAppeared -= gameModel.ElapsedTime;
            }

            //seteo el modo por defecto
            if (currentMode == null)
            {
                //currentMode = new SeekPlayer(nivel);
                currentMode = new SeekWeapon(nivel); //empieza buscando power weapon en base enemiga
            }

            //ejecuto la rutina actual si existiera
            if (currentRoutine.Count > 0)
            {
                var currentAction = currentRoutine.Find(a => a.t > 0f);
                if (currentAction != null)
                {
                    currentAction.proc.Invoke();
                    currentAction.t -= gameModel.ElapsedTime;
                }
                else
                {
                    currentRoutine = new List <Action>();
                }
                return;
            }

            //variables
            var sp = e.currentSpeed;
            var t  = nivel.time;

            //movimientos regulares
            //rutina para esquivar obstaculos
            if (sp == 0 && !justAppeared) //si no tengo velocidad (salvo si recien aparece) alterno entre 3 soluciones (deberia ser con hayObstaculo)
            {
                currentRoutine.Add(new Action(() => { e.Reverse(); }, 0.5f));
                if (alternate == 0)
                {
                    currentRoutine.Add(new Action(() => { e.Accelerate(); }, 1f));
                    currentRoutine.Add(new Action(() => { e.Jump(); }, 0.5f));
                    alternate = 1;
                }
                else if (alternate == 1)
                {
                    currentRoutine.Add(new Action(() => { e.Accelerate(); e.TurnRight(); }, 0.5f));
                    alternate = 2;
                }
                else
                {
                    currentRoutine.Add(new Action(() => { e.Accelerate(); e.TurnLeft(); }, 0.5f));
                    alternate = 0;
                }
            }
            //acelerar o frenar para mantener una velocidad entre dos valores
            if (sp < minSpeed)
            {
                e.Accelerate();
            }

            if (sp > maxSpeed)
            {
                e.Brake();
            }

            currentMode.Do(this); // hago lo correspondiente al modo actual

            //disparar armas si corresponde
            if (ShootMachineGun)
            {
                e.FireMachinegun(gameModel, nivel);
            }
            if (ShootSpecialWeapon)
            {
                if (timerToShootSpecial >= specialWeaponShootFrec)
                {
                    e.FireWeapon(gameModel, nivel, e.SelectedWeapon);
                    timerToShootSpecial = 0f;
                }
                else
                {
                    timerToShootSpecial += gameModel.ElapsedTime;
                }
            }
        }