Esempio n. 1
0
    void Start()
    {
        var walking     = new State <humanInput>("walking");
        var runningAway = new State <humanInput>("runningAway");

        StateConfigurer.Create(walking)
        .SetTransition(humanInput.Wander, runningAway);

        StateConfigurer.Create(runningAway)
        .SetTransition(humanInput.RunAway, walking);

        runningAway.OnEnter += OnUpdateRunning;
        walking.OnEnter     += OnUpdateWalking;

        _fsm = new EventFSM <humanInput>(walking);
    }
Esempio n. 2
0
    void Start()
    {
        base.BaseStart();

        //cambiar ia2.state por state cuando se borre la otra clase de state
        var idle    = new IA2.State <MeleeInput>("IDLE");
        var prep    = new IA2.State <MeleeInput>("PREP");
        var pursuit = new IA2.State <MeleeInput>("PURSUIT");
        var attack  = new IA2.State <MeleeInput>("ATTACK");
        var die     = new IA2.State <MeleeInput>("DIE");

        StateConfigurer.Create(idle).SetTransition(MeleeInput.PREP, prep).SetTransition(MeleeInput.PURSUIT, pursuit).SetTransition(MeleeInput.DIE, die).Done();
        StateConfigurer.Create(prep).SetTransition(MeleeInput.ATTACK, attack).SetTransition(MeleeInput.DIE, die).SetTransition(MeleeInput.IDLE, idle).Done();
        StateConfigurer.Create(pursuit).SetTransition(MeleeInput.PREP, prep).SetTransition(MeleeInput.IDLE, idle).SetTransition(MeleeInput.DIE, die).Done();
        StateConfigurer.Create(attack).SetTransition(MeleeInput.IDLE, idle).SetTransition(MeleeInput.DIE, die).Done();
        StateConfigurer.Create(die).Done();



        _myFsm = new EventFSM <MeleeInput>(idle);
    }
Esempio n. 3
0
    void Start()
    {
        _rb = GetComponent <Rigidbody>();
        var enemies = GameObject.FindGameObjectsWithTag("Enemy").Where(x => x.gameObject != gameObject);

        //IA2-P3
        var idle      = new State <Feed>("IDLE");
        var jumping   = new State <Feed>("JUMPING");
        var searching = new State <Feed>("SEARCHING");
        var shooting  = new State <Feed>("SHOOTING");
        var following = new State <Feed>("FOLLOWING");
        var dying     = new State <Feed>("DYING");


        StateConfigurer.Create(idle)
        .SetTransition(Feed.SEARCH, searching)
        .SetTransition(Feed.DIE, dying)
        .SetTransition(Feed.JUMP, jumping)
        .Done();

        StateConfigurer.Create(searching)
        .SetTransition(Feed.JUMP, jumping)
        .SetTransition(Feed.SEARCH, searching)
        .SetTransition(Feed.SHOOT, shooting)
        .SetTransition(Feed.IDLE, idle)
        .SetTransition(Feed.DIE, dying)
        .Done();

        StateConfigurer.Create(jumping)
        .SetTransition(Feed.SEARCH, searching)
        .SetTransition(Feed.FOLLOW, following)
        .SetTransition(Feed.DIE, dying)
        .Done();

        StateConfigurer.Create(following)
        .SetTransition(Feed.JUMP, jumping)
        .SetTransition(Feed.SHOOT, shooting)
        .SetTransition(Feed.IDLE, idle)
        .SetTransition(Feed.DIE, dying)
        .Done();

        StateConfigurer.Create(shooting)
        .SetTransition(Feed.SEARCH, searching)
        .SetTransition(Feed.JUMP, jumping)
        .SetTransition(Feed.FOLLOW, following)
        .SetTransition(Feed.IDLE, idle)
        .SetTransition(Feed.DIE, dying)
        .Done();

        StateConfigurer.Create(dying).Done();

        //IDLE
        idle.OnUpdate += () =>
        {
            if (enemyList.Count() > 0)
            {
                FeedFSM(Feed.SEARCH);
            }
            else
            {
                enemyList = enemies.Where(x => x != null).Select(x => x.GetComponent <Enemy>()).ToList();
            }
        };


        //SEARCHING
        searching.OnEnter += h =>
        {
            enemyList = enemyList.Where(x => x != null).ToList();
            if (enemyList.Count() < 1)
            {
                FeedFSM(Feed.IDLE);
                return;
            }

            shootingEnemy = enemyList.OrderBy(x => Vector3.Distance(transform.position, x.transform.position)).First();

            (GridEntity first, GridEntity last) = GetFirstAndLastNode();

            if (first != null && last != null)
            {
                _last = last;
                _path = AStar.Run(first, Satisfies, Expand, Heuristic);
            }
            else
            {
                FeedFSM(Feed.SEARCH);
            }
        };
        searching.OnUpdate += () =>
        {
            if (shootingEnemy == null)
            {
                FeedFSM(Feed.IDLE);
                return;
            }

            if (Vector3.Distance(transform.position, shootingEnemy.transform.position) < 5)
            {
                FeedFSM(Feed.SHOOT);
                return;
            }

            if (_path != null)
            {
                if (_searchPoint != null && Vector3.Distance(_searchPoint, transform.position) < 0.5f)
                {
                    _path = _path.Skip(1);
                }
                if (_path.Count() > 0)
                {
                    _searchPoint      = _path.First().transform.position;
                    _searchPoint.y    = transform.position.y;
                    transform.forward = _searchPoint - transform.position;
                    Move(searchSpeed);
                }
                else
                {
                    FeedFSM(Feed.SEARCH);
                }
            }
        };

        //JUMPING
        jumping.OnEnter += x =>
        {
            Jump();
        };
        jumping.OnUpdate += () =>
        {
            if (Physics.Raycast(transform.position, Vector3.down, 1.5f) || _rb.velocity.y == 0)
            {
                if (shootingEnemy == null)
                {
                    FeedFSM(Feed.SEARCH);
                }
                else
                {
                    FeedFSM(Feed.FOLLOW);
                }
            }
        };

        //SHOOTING
        shooting.OnUpdate += () =>
        {
            if (shootingEnemy == null)
            {
                FeedFSM(Feed.IDLE);
                return;
            }

            if (Vector3.Distance(shootingEnemy.transform.position, transform.position) > enemyRadius)
            {
                FeedFSM(Feed.FOLLOW);
                return;
            }

            Debug.DrawRay(transform.position, shootingEnemy.transform.position - transform.position, Color.green);

            if (_timeStamp <= Time.time)
            {
                _timeStamp = Time.time + weapon.coolDown;

                Vector3 targetPostition = new Vector3(shootingEnemy.transform.position.x, transform.position.y, shootingEnemy.transform.position.z);
                transform.LookAt(targetPostition);

                _bullets -= weapon.bulletsUsedPerShot;
                var bul = Instantiate(bullet);
                bul.transform.position = transform.position + transform.forward;
                bul.transform.forward  = transform.forward;
                var bulComp = bul.GetComponent <Bullet>();
                bulComp.numberOne = this;
                bulComp.damage    = weapon.damage;
            }
        };

        //FOLLOWING
        following.OnUpdate += () =>
        {
            if (shootingEnemy == null)
            {
                FeedFSM(Feed.IDLE);
                return;
            }

            if (Vector3.Distance(shootingEnemy.transform.position, transform.position) <= enemyRadius)
            {
                FeedFSM(Feed.SHOOT);
                return;
            }

            transform.forward = shootingEnemy.transform.position - transform.position;
            Move(followSpeed);
        };

        //DYING
        dying.OnEnter += x =>
        {
            UIManager.Instance.deadOnes.Add((transform.name, kills, weapon.name, life, false));
            _lastHit.SetKill();
            Destroy(gameObject);
        };


        _lifePercentage = (life * 100) / maxLife;

        //////////////////////////
        //IA2-P1 (Operación 1) -> Busqueda de enemigos
        /* Se utilizó Select, Where, SkipWhile (+ Take), OrderBy (+ OrderByDescending) */
        if (_lifePercentage > 80)
        {
            enemyList = enemies.Select(x => x.GetComponent <Enemy>()).Where(x => x != null && _lifePercentage > 30).SkipWhile(x => x.weapon.name == "Pistol").OrderBy(x => x.life).ToList();
        }
        else if (_lifePercentage > 30)
        {
            enemyList = enemies.Select(x => x.GetComponent <Enemy>()).Where(x => x != null && _lifePercentage < 50).Take(Random.Range(0, 10)).OrderByDescending(x => x.life).ToList();
        }
        else
        {
            enemyList = enemies.Select(x => x.GetComponent <Enemy>()).ToList();
        }
        //////////////////////////

        _FSM = new EventFSM <Feed>(idle);
    }
Esempio n. 4
0
    protected virtual void StateMachine()
    {
        #region STATE CONFIGS
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        var idle      = new State <Inputs>(CommonState.IDLE);
        var onSigth   = new State <Inputs>(CommonState.ONSIGHT);
        var pursuit   = new State <Inputs>(CommonState.PURSUIRT);
        var searching = new State <Inputs>(CommonState.SEARCHING);
        var attack    = new State <Inputs>(CommonState.ATTACKING);
        var die       = new State <Inputs>(CommonState.DIE);

        StateConfigurer.Create(idle)
        .SetTransition(Inputs.ON_LINE_OF_SIGHT, onSigth)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(onSigth)
        .SetTransition(Inputs.PROBOCATED, pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.IN_RANGE_TO_ATTACK, attack)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(searching)
        .SetTransition(Inputs.TIME_OUT, idle)
        .SetTransition(Inputs.ON_LINE_OF_SIGHT, pursuit)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(attack)
        .SetTransition(Inputs.OUT_RANGE_TO_ATTACK, pursuit)
        .SetTransition(Inputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(Inputs.DIE, die)
        .Done();

        StateConfigurer.Create(die).Done();


        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion
        #region STATES
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////

        //******************
        //*** IDLE
        //******************
        idle.OnEnter += x =>
        {
            myRb.velocity = Vector2.zero;
            anim.Idle();
        };
        idle.OnUpdate += () =>
        {
            Deb_Estado = "IDLE";
            if (LineOfSight())
            {
                Debug.Log("Line of sigth");
                SendInputToFSM(Inputs.ON_LINE_OF_SIGHT);
            }

            CheckBullet();
        };

        //******************
        //*** ON SIGHT
        //******************
        onSigth.OnEnter += x =>
        {
            feedbackstate.SetStateGraphic(preset.sprite_OnSight);
        };
        onSigth.OnUpdate += () =>
        {
            Deb_Estado = "ON SIGTH";
            if (LineOfSight())
            {
                LookSmooth();
                OnSight_CountDownForProbocate();
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };
        onSigth.OnExit += x =>
        {
            feedbackstate.SetStateGraphic();
        };

        //******************
        //*** PURSUIT
        //******************
        pursuit.OnEnter += x =>
        {
            anim.Run();
        };

        pursuit.OnUpdate += () =>
        {
            Deb_Estado = "PURSUIT";
            if (LineOfSight())
            {
                FollowPlayer();

                if (IsInDistanceToAttack())
                {
                    SendInputToFSM(Inputs.IN_RANGE_TO_ATTACK);
                }
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };

        pursuit.OnExit += x =>
        {
            myRb.velocity = Vector2.zero;
        };

        //******************
        //*** SEARCH
        //******************
        searching.OnEnter += x =>
        {
            anim.Walk();
            feedbackstate.SetStateGraphic(preset.sprite_Search);
        };

        searching.OnUpdate += () =>
        {
            CheckBullet();

            Deb_Estado = "SEARCH";
            if (LineOfSight())
            {
                SendInputToFSM(Inputs.ON_LINE_OF_SIGHT);
            }
            else
            {
                transform.Rotate(0, 0, 2);
                OutSight_CountDownForIgnore();
            }
        };

        searching.OnExit += x =>
        {
            feedbackstate.SetStateGraphic();
        };

        //******************
        //*** ATTACK
        //******************
        attack.OnUpdate += () =>
        {
            Deb_Estado = "ATTACK";
            if (LineOfSight())
            {
                LookSmooth();

                if (IsInDistanceToAttack())
                {
                    anim.Attack();
                    Attack();
                }
                else
                {
                    SendInputToFSM(Inputs.OUT_RANGE_TO_ATTACK);
                }
            }
            else
            {
                SendInputToFSM(Inputs.OUT_LINE_OF_SIGHT);
            }
        };

        attack.OnExit += x =>
        {
            timer = 0;
        };

        //******************
        //*** DEATH
        //******************
        die.OnEnter += x =>
        {
            Deb_Estado = "DEATH";
            canMove    = false;
            gameObject.GetComponent <Collider2D>().enabled = false;
            anim.Die();
            lifebar.Off();
            del_to_remove(this);
        };

        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion

        _myFsm = new EventFSM <Inputs>(idle);
    }
Esempio n. 5
0
    void Start()
    {
        _ui                = FindObjectOfType <UIManager>();
        _nav               = new Navigation(transform, speed, threshold, nodeLayerMask);
        _inGameItems       = GetInGameItems();
        _goToActions       = GoapGoToAction();
        _afterWalkActions  = GoapAfterWalkActions();
        _afterThrowActions = GoapAfterThrowActions();
        _currentWorldState = new WorldState();
        _owner             = _inGameItems["Owner"].GetComponent <Owner>();

        _rb   = GetComponent <Rigidbody>();
        _anim = GetComponent <Animator>();
        _as   = GetComponent <AudioSource>();

        _ui.OnRunButtonPressed += OnRunButtonPressed;

        var plan         = new State <PlayerAction>("Plan");
        var walk         = new State <PlayerAction>("Walk");
        var pickup       = new State <PlayerAction>("PickUp");
        var throwObject  = new State <PlayerAction>("ThrowObject");
        var success      = new State <PlayerAction>("Success");
        var die          = new State <PlayerAction>("Die");
        var playBullsEye = new State <PlayerAction>("PlayBullsEye");
        var any          = new State <PlayerAction>("<any>");

        plan.OnEnter += a =>
        {
            _anim.Play("Idle");
            _currentWorldState = null;
        };

        plan.OnUpdate += () =>
        {
            if (_currentWorldState == null)
            {
                return;
            }

            var action = _currentWorldState.generatingAction;
            if (_goToActions.ContainsKey(action))
            {
                _goToActions[action]();
            }
        };

        walk.OnEnter += a =>
        {
            _anim.Play("Walking");
            StartCoroutine(_nav.GetRouteTo(_gameObjectObjective));
            StartCoroutine(_nav.NavCoroutine());
        };

        walk.OnUpdate += () =>
        {
            if (_nav.Path == null)
            {
                return;
            }

            if (!_nav.PathEnded)
            {
                _nav.Update();
            }
            else
            {
                _afterWalkActions[_currentWorldState.generatingAction]();
            }
        };

        walk.OnExit += a =>
        {
            _currentIndex = 0;
        };

        pickup.OnEnter += a =>
        {
            _anim.Play("Taking Item");
            var objPosition = _gameObjectObjective.transform.position;
            transform.LookAt(new Vector3(objPosition.x, transform.position.y, objPosition.z));
            StartCoroutine(PickupObject());
            OnCoroutineEnd += FsmFeedPlan;
        };

        pickup.OnExit += a => OnCoroutineEnd -= FsmFeedPlan;

        throwObject.OnEnter += a =>
        {
            _anim.Play("Throw");
            _afterThrowActions[_currentWorldState.generatingAction]();
            OnCoroutineEnd += FsmFeedPlan;
        };

        throwObject.OnExit += a => OnCoroutineEnd -= FsmFeedPlan;

        success.OnEnter += a =>
        {
            StartCoroutine(Escape());
        };

        die.OnEnter += a =>
        {
            _anim.Play("Death");
            _ui.ShowRestart();
        };

        playBullsEye.OnEnter += a =>
        {
            MoveAside();
            _anim.Play("Idle");
            _owner.OnOwnerFinishedPlaying += CanThrowDarts;
        };

        playBullsEye.OnExit += a =>
        {
            _owner.OnOwnerFinishedPlaying -= CanThrowDarts;
        };

        StateConfigurer.Create(any)
        .SetTransition(PlayerAction.Plan, plan)
        .SetTransition(PlayerAction.Success, success)
        .SetTransition(PlayerAction.Die, die)
        .Done();

        StateConfigurer.Create(plan)
        .SetTransition(PlayerAction.Walk, walk)
        .SetTransition(PlayerAction.PickUp, pickup)
        .Done();

        StateConfigurer.Create(walk)
        .SetTransition(PlayerAction.PickUp, pickup)
        .SetTransition(PlayerAction.ThrowObject, throwObject)
        .SetTransition(PlayerAction.PlayBullsEye, playBullsEye)
        .Done();

        _fsm = new EventFSM <PlayerAction>(plan, any);
    }
Esempio n. 6
0
    void StateMachine()
    {
        #region STATE MACHINE CONFIGS
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        var idle      = new State <PlayerInputs>(CommonState.IDLE);
        var onSigth   = new State <PlayerInputs>(CommonState.ONSIGHT);
        var pursuit   = new State <PlayerInputs>(CommonState.PURSUIRT);
        var searching = new State <PlayerInputs>(CommonState.SEARCHING);
        var attack    = new State <PlayerInputs>(CommonState.ATTACKING);
        var freeze    = new State <PlayerInputs>(CommonState.FREEZE);
        var die       = new State <PlayerInputs>(CommonState.DIE);

        StateConfigurer.Create(idle)
        .SetTransition(PlayerInputs.ON_LINE_OF_SIGHT, onSigth)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.FREEZE, freeze)
        .Done();

        StateConfigurer.Create(onSigth)
        .SetTransition(PlayerInputs.PROBOCATED, pursuit)
        .SetTransition(PlayerInputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.FREEZE, freeze)
        .Done();

        StateConfigurer.Create(pursuit)
        .SetTransition(PlayerInputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(PlayerInputs.IN_RANGE_TO_ATTACK, attack)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.FREEZE, freeze)
        .Done();

        StateConfigurer.Create(searching)
        .SetTransition(PlayerInputs.TIME_OUT, idle)
        .SetTransition(PlayerInputs.ON_LINE_OF_SIGHT, pursuit)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.FREEZE, freeze)
        .Done();

        StateConfigurer.Create(attack)
        .SetTransition(PlayerInputs.OUT_RANGE_TO_ATTACK, pursuit)
        .SetTransition(PlayerInputs.OUT_LINE_OF_SIGHT, searching)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.FREEZE, freeze)
        .Done();

        StateConfigurer.Create(freeze)
        .SetTransition(PlayerInputs.DIE, die)
        .Done();

        StateConfigurer.Create(die).Done();


        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion

        #region STATE MACHINE DO SOMETING
        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////

        //******************
        //*** IDLE
        //******************
        idle.OnUpdate += () => {
            Deb_Estado = "IDLE";
            if (LineOfSight())
            {
                Debug.Log("Line of sigth");
                SendInputToFSM(PlayerInputs.ON_LINE_OF_SIGHT);
            }
        };

        //******************
        //*** ON SIGHT
        //******************
        onSigth.OnUpdate += () => {
            Deb_Estado = "ON SIGTH";
            if (LineOfSight())
            {
                OnSight_CountDownForProbocate();
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(PlayerInputs.OUT_LINE_OF_SIGHT);
            }
        };

        //******************
        //*** PURSUIT
        //******************
        pursuit.OnUpdate += () => {
            Deb_Estado = "PURSUIT";
            if (LineOfSight())
            {
                FollowPlayer();

                if (IsInDistanceToAttack())
                {
                    SendInputToFSM(PlayerInputs.IN_RANGE_TO_ATTACK);
                }
            }
            else
            {
                timer_to_probocate = 0; SendInputToFSM(PlayerInputs.OUT_LINE_OF_SIGHT);
            }
        };

        //******************
        //*** SEARCH
        //******************
        searching.OnUpdate += () => {
            Deb_Estado = "SEARCH";
            if (LineOfSight())
            {
                SendInputToFSM(PlayerInputs.ON_LINE_OF_SIGHT);
            }
            else
            {
                OutSight_CountDownForIgnore();
            }
        };

        //******************
        //*** ATTACK
        //******************
        attack.OnUpdate += () => {
            Deb_Estado = "ATTACK";
            if (LineOfSight())
            {
                if (IsInDistanceToAttack())
                {
                    Attack();
                }
                else
                {
                    SendInputToFSM(PlayerInputs.OUT_RANGE_TO_ATTACK);
                }
            }
            else
            {
                SendInputToFSM(PlayerInputs.OUT_LINE_OF_SIGHT);
            }
        };

        //******************
        //*** FREEZE
        //******************
        freeze.OnEnter += x => {
            Deb_Estado = "FREEZE";
            myRender.material.color = Color.grey;
            canMove = false;
        };

        //******************
        //*** DEATH
        //******************
        die.OnEnter += x => {
            Deb_Estado = "DEATH";
            canMove    = false;
            if (myRender.material.color == Color.black)
            {
                return;
            }
            myRender.material.color = Color.black;
            transform.localScale    = transform.localScale / 2;
            StopUpdating();
        };

        ///////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////
        #endregion

        _myFsm = new EventFSM <PlayerInputs>(idle);
    }
Esempio n. 7
0
    private void Awake()
    {
        _myRb = gameObject.GetComponent <Rigidbody>();

        //PARTE 1: SETEO INICIAL

        //Creo los estados
        var idle    = new State <PlayerInputs>(CommonState.IDLE);
        var moving  = new State <PlayerInputs>(CommonState.MOVING);
        var jumping = new State <PlayerInputs>(CommonState.JUMPING);
        var die     = new State <PlayerInputs>(CommonState.DIE);

        //creo las transiciones
        StateConfigurer.Create(idle)
        .SetTransition(PlayerInputs.MOVE, moving)
        .SetTransition(PlayerInputs.JUMP, jumping)
        .SetTransition(PlayerInputs.DIE, die)
        .Done();     //aplico y asigno

        StateConfigurer.Create(moving)
        .SetTransition(PlayerInputs.IDLE, idle)
        .SetTransition(PlayerInputs.JUMP, jumping)
        .SetTransition(PlayerInputs.DIE, die)
        .Done();

        StateConfigurer.Create(jumping)
        .SetTransition(PlayerInputs.IDLE, idle)
        .SetTransition(PlayerInputs.JUMP, jumping)
        .Done();

        //die no va a tener ninguna transición HACIA nada (uno puede morirse, pero no puede pasar de morirse a caminar)
        //entonces solo lo creo e inmediatamente lo aplico asi el diccionario de transiciones no es nulo y no se rompe nada.
        StateConfigurer.Create(die).Done();

        //PARTE 2: SETEO DE LOS ESTADOS

        #region IDLE
        idle.OnUpdate += () =>
        {
            if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
            {
                SendInputToFSM(PlayerInputs.MOVE);
            }
            else if (Input.GetKeyDown(KeyCode.Space))
            {
                SendInputToFSM(PlayerInputs.JUMP);
            }
        };
        #endregion
        #region MOVING
        moving.OnUpdate += () =>
        {
            if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
            {
                SendInputToFSM(PlayerInputs.IDLE);
            }
            else if (Input.GetKeyDown(KeyCode.Space))
            {
                SendInputToFSM(PlayerInputs.JUMP);
            }
        };
        moving.OnFixedUpdate += () =>
        {
            _myRb.velocity += (transform.forward * Input.GetAxis("Vertical") * 20f + transform.right * Input.GetAxis("Horizontal") * 20f) * Time.deltaTime;
        };
        moving.OnExit += x =>
        {
            //x es el input que recibí, por lo que puedo modificar el comportamiento según a donde estoy llendo
            if (x != PlayerInputs.JUMP)
            {
                _myRb.velocity = Vector3.zero;
            }
        };
        #endregion
        #region JUMPING
        jumping.OnEnter += x =>
        {
            //tambien uso el rigidbody, pero en vez de tener una variable en cada estado, tengo una sola referencia compartida...
            _myRb.AddForce(transform.up * 10f, ForceMode.Impulse);
        };
        jumping.OnUpdate += () =>
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                SendInputToFSM(PlayerInputs.JUMP);
            }
        };
        jumping.GetTransition(PlayerInputs.JUMP).OnTransition += x =>
        {
            _myRen.material.color = Color.red;
        };
        jumping.GetTransition(PlayerInputs.IDLE).OnTransition += x =>
        {
            _myRen.material.color = Color.white;
        };
        #endregion

        //con todo ya creado, creo la FSM y le asigno el primer estado
        _myFsm = new EventFSM <PlayerInputs>(idle);
    }
Esempio n. 8
0
    void Awake()
    {
        var idle    = new State <TankieInputs>("Idle");
        var pursuit = new State <TankieInputs>("Pursuit");
        var shoot   = new State <TankieInputs>("Shoot");
        var disable = new State <TankieInputs>("Disable");

        #region StateConfiguration
        StateConfigurer.Create(idle)
        .SetTransition(TankieInputs.PURSUIT, pursuit)
        .SetTransition(TankieInputs.SHOOT, shoot)
        .SetTransition(TankieInputs.DISABLE, disable)
        .Done();

        StateConfigurer.Create(pursuit)
        .SetTransition(TankieInputs.IDLE, idle)
        .SetTransition(TankieInputs.SHOOT, shoot)
        .SetTransition(TankieInputs.DISABLE, disable)
        .Done();

        StateConfigurer.Create(shoot)
        .SetTransition(TankieInputs.IDLE, idle)
        .SetTransition(TankieInputs.PURSUIT, pursuit)
        .SetTransition(TankieInputs.DISABLE, disable)
        .Done();

        StateConfigurer.Create(disable)
        .SetTransition(TankieInputs.IDLE, idle)
        .SetTransition(TankieInputs.PURSUIT, pursuit)
        .SetTransition(TankieInputs.SHOOT, shoot)
        .Done();
        #endregion

        #region IdleSettings

        idle.OnUpdate += () =>
        {
            if (Vector3.Distance(transform.position, heroe.transform.position) < minDist)
            {
                Ray        ray     = new Ray(gun.transform.position, heroe.transform.position - gun.transform.position);
                RaycastHit rayInfo = new RaycastHit();
                if (Physics.Raycast(ray, out rayInfo, minDist))
                {
                    if (rayInfo.transform.gameObject == heroe)
                    {
                        SendInputToFSM(TankieInputs.PURSUIT);
                    }
                }
            }
            if (health <= 0)
            {
                SendInputToFSM(TankieInputs.DISABLE);
            }
            if (alarmTrigger)
            {
                SendInputToFSM(TankieInputs.PURSUIT);
            }
        };

        /*idle.GetTransition(TankieInputs.DISABLE).OnTransition += x =>
         * {
         *  //Activar Particula
         * };*/
        idle.GetTransition(TankieInputs.PURSUIT).OnTransition += x =>
        {
            //Hacer Ruido
            PlaySound("StartPursuit");
        };

        #endregion

        #region PursuitSettings

        pursuit.OnUpdate += () =>
        {
            if (navMeshA.velocity.x <= 0 && navMeshA.velocity.z == 0)
            {
                SendInputToFSM(TankieInputs.IDLE);
            }

            if (Vector3.Distance(transform.position, heroe.transform.position) > minDist && !alarmTrigger)
            {
                SendInputToFSM(TankieInputs.IDLE);
            }

            if (Vector3.Distance(transform.position, heroe.transform.position) < minDist)
            {
                Ray        ray     = new Ray(gun.transform.position, heroe.transform.position - gun.transform.position);
                RaycastHit rayInfo = new RaycastHit();
                if (Physics.Raycast(ray, out rayInfo))
                {
                    if (rayInfo.transform.gameObject == heroe)
                    {
                        SendInputToFSM(TankieInputs.SHOOT);
                    }
                }
            }

            OnMove(this);

            if (health <= 0)
            {
                SendInputToFSM(TankieInputs.DISABLE);
            }
        };
        pursuit.OnFixedUpdate += () =>
        {
            if (Vector3.Distance(transform.position, heroe.transform.position) < minDist)
            {
                Ray        ray     = new Ray(gun.transform.position, heroe.transform.position - gun.transform.position);
                RaycastHit rayInfo = new RaycastHit();
                if (Physics.Raycast(ray, out rayInfo, minDist))
                {
                    if (rayInfo.transform.gameObject == heroe)
                    {
                        navMeshA.SetDestination(heroe.transform.position);
                    }
                }
            }
        };
        pursuit.OnExit += x =>
        {
            alarmTrigger = false;
        };

        /*pursuit.GetTransition(TankieInputs.IDLE).OnTransition += x =>
         * {
         *  PlaySound("EndPursuit");
         * };*/

        #endregion

        #region ShootSettings

        shoot.OnFixedUpdate += () =>
        {
            gun.transform.forward     = Vector3.Lerp(gun.transform.forward, heroe.transform.position - gun.transform.position, Time.deltaTime / timeToAim);
            arm.transform.forward     = Vector3.Lerp(arm.transform.forward, heroe.transform.position - arm.transform.position, Time.deltaTime / timeToAim);
            arm.transform.eulerAngles = new Vector3(0, arm.transform.eulerAngles.y, 0);
            Shoot();

            if (navMeshA.velocity.x <= 0 && navMeshA.velocity.z == 0)
            {
                SendInputToFSM(TankieInputs.IDLE);
            }

            if (Vector3.Distance(transform.position, heroe.transform.position) > minDist)
            {
                SendInputToFSM(TankieInputs.IDLE);
            }

            if (health <= 0)
            {
                SendInputToFSM(TankieInputs.DISABLE);
            }

            if (Vector3.Distance(transform.position, heroe.transform.position) < minDist)
            {
                SendInputToFSM(TankieInputs.PURSUIT);
            }
        };
        #endregion

        #region DisableSettings
        disable.OnFixedUpdate += () =>
        {
            isDisabled = true;


            //si no se destruye,falta hacer que si isDisable es true haga algo o que directamente haga algo.

            //si se destruye.

            /*if (transform.parent != null)
             *  transform.parent.GetComponent<Room>().enemies.Remove(this.gameObject);
             *
             * GameObject explo = Instantiate(prefabExplosion);
             * explo.transform.position = transform.position;
             * Destroy(this.gameObject);*/
        };
        #endregion

        _myFsm = new EventFSM <TankieInputs>(idle);
    }
Esempio n. 9
0
    //////////////////////////////////////////////////////
    // STATE MACHINE
    //////////////////////////////////////////////////////

    void StateMachine()
    {
        #region STATE MACHINE CONFIGS

        var idle    = new State <PlayerInputs>(CommonState.IDLE);
        var moving  = new State <PlayerInputs>(CommonState.MOVING);
        var jumping = new State <PlayerInputs>(CommonState.JUMPING);
        var crouch  = new State <PlayerInputs>(CommonState.CROUCH);
        var die     = new State <PlayerInputs>(CommonState.DIE);

        StateConfigurer.Create(idle)
        .SetTransition(PlayerInputs.MOVE, moving)
        .SetTransition(PlayerInputs.JUMP, jumping)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.CROUCH, crouch)
        .Done();

        StateConfigurer.Create(moving)
        .SetTransition(PlayerInputs.IDLE, idle)
        .SetTransition(PlayerInputs.JUMP, jumping)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.CROUCH, crouch)
        .Done();

        StateConfigurer.Create(jumping)
        .SetTransition(PlayerInputs.IDLE, idle)
        .SetTransition(PlayerInputs.MOVE, moving)
        .SetTransition(PlayerInputs.DIE, die)
        .SetTransition(PlayerInputs.CROUCH, crouch)
        .Done();

        StateConfigurer.Create(die).Done();

        StateConfigurer.Create(crouch)
        .SetTransition(PlayerInputs.IDLE, idle)
        .SetTransition(PlayerInputs.DIE, die)
        .Done();

        #endregion
        #region STATE MACHINE DO SOMETING

        // <IDLE>
        idle.OnEnter += x =>
        {
            Deb_Est = "IDLE";
        };
        idle.OnUpdate += () =>
        {
            if (Input.GetAxis(PHYSICAL_INPUT.HORIZONTAL) != 0 || Input.GetAxis(PHYSICAL_INPUT.VERTICAL) != 0)
            {
                SendInputToFSM(PlayerInputs.MOVE);
            }
            else if (Input.GetButtonDown(PHYSICAL_INPUT.JUMP))
            {
                SendInputToFSM(PlayerInputs.JUMP);
            }
            else if (Input.GetButtonDown(PHYSICAL_INPUT.CROUCH))
            {
                SendInputToFSM(PlayerInputs.CROUCH);
            }
        };
        idle.OnFixedUpdate += () =>
        {
            StateStill();
        };
        // </IDLE>

        // <MOVING>
        moving.OnEnter += x =>
        {
            Deb_Est = "MOVING";
        };
        moving.OnUpdate += () =>
        {
            if (Input.GetAxis(PHYSICAL_INPUT.HORIZONTAL) == 0 && Input.GetAxis(PHYSICAL_INPUT.VERTICAL) == 0)
            {
                SendInputToFSM(PlayerInputs.IDLE);
            }
            else if (Input.GetKeyDown(KeyCode.Space))
            {
                SendInputToFSM(PlayerInputs.JUMP);
            }
            else if (Input.GetButtonDown(PHYSICAL_INPUT.CROUCH))
            {
                SendInputToFSM(PlayerInputs.CROUCH);
            }
        };
        moving.OnFixedUpdate += () =>
        {
            if (!check.IsGrounded)
            {
                moveFall.y -= gravity;
            }
            else
            {
                moveFall = Vector3.zero;
            }
            movevertical   = transform.forward * Input.GetAxis("Vertical") * speed;
            movehorizontal = transform.right * Input.GetAxis("Horizontal") * speed;
        };
        moving.GetTransition(PlayerInputs.JUMP).OnTransition += x =>
        {
            Deb_Trans = "salto en largo";
        };
        moving.GetTransition(PlayerInputs.CROUCH).OnTransition += x =>
        {
            Deb_Trans = "Rodar";
        };
        // </MOVING>

        // <JUMP>
        jumping.OnEnter += x =>
        {
            GoToJump = true;
            Deb_Est  = "JUMP";
        };
        jumping.OnFixedUpdate += () =>
        {
            if (GoToJump && check.IsGrounded)// aca salto
            {
                moveFall.y = jumpForce;
                GoToJump   = false;
            }
            else if (!GoToJump && check.IsGrounded)//aca no salto mas y toque el piso
            {
                moveFall.y = 0;
                if (Input.GetAxis(PHYSICAL_INPUT.HORIZONTAL) == 0 && Input.GetAxis(PHYSICAL_INPUT.VERTICAL) == 0)
                {
                    SendInputToFSM(PlayerInputs.IDLE);
                }
                else if (Input.GetAxis(PHYSICAL_INPUT.HORIZONTAL) != 0 || Input.GetAxis(PHYSICAL_INPUT.VERTICAL) != 0)
                {
                    SendInputToFSM(PlayerInputs.MOVE);
                }
            }
            else// la caida
            {
                moveFall.y -= gravity;

                if (Input.GetButtonDown(PHYSICAL_INPUT.CROUCH))
                {
                    SendInputToFSM(PlayerInputs.CROUCH);
                }
            }
        };
        jumping.GetTransition(PlayerInputs.CROUCH).OnTransition += x =>
        {
            isJumpFall = true;
            Deb_Trans  = "Caida Potente";
        };
        // </JUMP>

        // <CROUCH>
        crouch.OnEnter += x =>
        {
            Deb_Est = "CROUCH";
            if (x != PlayerInputs.JUMP)
            {
                movehorizontal = Vector3.zero;
                movevertical   = Vector3.zero;
            }

            transform.localScale = new Vector3(1, 0.5f, 1);
        };
        crouch.OnUpdate += () =>
        {
            if (Input.GetButtonUp(PHYSICAL_INPUT.CROUCH))
            {
                SendInputToFSM(PlayerInputs.IDLE);
                transform.localScale = new Vector3(1, 1, 1);
            }
        };
        crouch.OnFixedUpdate += () =>
        {
            if (check.IsGrounded && isJumpFall)
            {
                particles_JumpFall.Play();
                isJumpFall = false;

                qActions.Button_JumpKill();
                qActions.EjectWeakAndScared();
            }

            if (!check.IsGrounded)
            {
                moveFall.y -= gravity * 3;
            }
            else
            {
                movevertical   = transform.forward * Input.GetAxis(PHYSICAL_INPUT.VERTICAL) * (speed / 2);
                movehorizontal = transform.right * Input.GetAxis(PHYSICAL_INPUT.HORIZONTAL) * (speed / 2);
            }
        };
        // </CROUCH>

        #endregion
        _myFsm = new EventFSM <PlayerInputs>(idle);
    }
Esempio n. 10
0
    void Awake()
    {
        _ent = GetComponent <Entity>();

        var any          = new State <IAAction>("any");
        var idle         = new State <IAAction>("idle");
        var planStep     = new State <IAAction>("planStep");
        var failStep     = new State <IAAction>("failStep");
        var pickup       = new State <IAAction>("pickup");
        var create       = new State <IAAction>("create");
        var upgrade      = new State <IAAction>("upgrade");
        var attack       = new State <IAAction>("attack");
        var super_attack = new State <IAAction>("super_attack");
        var success      = new State <IAAction>("success");


        failStep.OnEnter += a => { _ent.Stop(); Debug.Log("Plan failed"); };

        pickup.OnEnter += a => {
            Debug.Log("pickup.OnEnter");
            _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformPickUp;
            Debug.Log("pickup.OnEnter finish");
        };
        pickup.OnExit += a =>
        {
            Debug.Log("pickup.OnExit");
            action_started  = false;
            _ent.OnHitItem -= PerformPickUp;
            Debug.Log("pickup.OnExit finish");
        };


        create.OnEnter += a => {
            print("entro en el create.onEnter");
            _ent.GoTo(_target.transform.position);
            _ent.OnHitItem += PerformCreate;
            print("salgo en el create.onEnter");
        };
        create.OnExit += a => {
            action_started  = false;
            _ent.OnHitItem -= PerformCreate;
        };


        attack.OnEnter += a => {
            _ent.GoTo(_target.transform.position);
            _ent.OnHitItem += PerformAttack;
        };
        attack.OnExit += a =>
        {
            action_started  = false;
            _ent.OnHitItem -= PerformAttack;
        };

        super_attack.OnEnter += a => { _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformSuperAttack; };
        super_attack.OnExit  += a =>
        {
            action_started  = false;
            _ent.OnHitItem -= PerformSuperAttack;
        };



        upgrade.OnEnter += a => { _ent.GoTo(_target.transform.position); _ent.OnHitItem += PerformUpgrade; };
        upgrade.OnExit  += a => {
            action_started  = false;
            _ent.OnHitItem -= PerformUpgrade;
        };

        planStep.OnEnter += a => {
            if (shouldRePlan)
            {
                var planner = this.GetComponent <Planner>();
                _plan        = planner.RecalculatePlan(GetCurState());
                shouldRePlan = false;
            }
            Debug.Log("Plan next step");
            var step = _plan.FirstOrDefault();
            if (step != null)
            {
                Debug.Log("Next step:" + step.Item1 + "," + step.Item2);

                _plan = _plan.Skip(1);
                var oldTarget = _target;
                _target = step.Item2;
                if (!_fsm.Feed(step.Item1))
                {
                    _target = oldTarget;                                //Revert if feed failed.
                }
            }
            else
            {
                Debug.Log("TipitoAction.Success");
                _fsm.Feed(IAAction.Success);
            }
        };


        success.OnEnter  += a => { Debug.Log("Success"); };
        success.OnUpdate += () => { _ent.Jump(); };

        StateConfigurer.Create(any)
        .SetTransition(IAAction.NextStep, planStep)
        .SetTransition(IAAction.FailedStep, idle)
        .Done();

        StateConfigurer.Create(planStep)
        .SetTransition(IAAction.PickUp, pickup)
        .SetTransition(IAAction.Create, create)
        .SetTransition(IAAction.Attack, attack)
        .SetTransition(IAAction.SuperAttack, super_attack)
        .SetTransition(IAAction.Upgrade, upgrade)
        .SetTransition(IAAction.Success, success)
        .Done();
        print("new fsm!");
        _fsm = new EventFSM <IAAction>(idle, any);
    }
Esempio n. 11
0
    // Start is called before the first frame update
    void Start()
    {
        _as  = GetComponent <AudioSource>();
        _nav = new Navigation(transform, 1.5f, 0.5f, nodeLayerMask);

        var sitting       = new State <OwnerAction>("Sitting");
        var walk          = new State <OwnerAction>("Walk");
        var drunkWalk     = new State <OwnerAction>("DrunkWalk");
        var idle          = new State <OwnerAction>("Idle");
        var throwingDarts = new State <OwnerAction>("ThrowingDarts");

        walk.OnEnter += a =>
        {
            animator.Play("Walking");
            StartNavigation();
        };

        walk.OnUpdate += () =>
        {
            if (isDrunk)
            {
                _fsm.Feed(OwnerAction.DrunkWalk);
            }
            Navigate();
        };


        drunkWalk.OnEnter += a =>
        {
            animator.Play("DrunkWalking");
            StartNavigation();
        };

        drunkWalk.OnUpdate += () => Navigate();

        throwingDarts.OnEnter += a =>
        {
            animator.Play("Throw");
        };

        idle.OnEnter += a =>
        {
            animator.Play("Idle");
            OnOwnerFinishedPlaying();
        };

        StateConfigurer.Create(sitting)
        .SetTransition(OwnerAction.Walk, walk)
        .Done();

        StateConfigurer.Create(walk)
        .SetTransition(OwnerAction.DrunkWalk, drunkWalk)
        .SetTransition(OwnerAction.ThrowDarts, throwingDarts)
        .Done();

        StateConfigurer.Create(drunkWalk)
        .SetTransition(OwnerAction.ThrowDarts, throwingDarts)
        .Done();

        StateConfigurer.Create(throwingDarts)
        .SetTransition(OwnerAction.Idle, idle)
        .Done();

        StateConfigurer.Create(idle)
        .SetTransition(OwnerAction.ThrowDarts, throwingDarts)
        .Done();

        _fsm = new EventFSM <OwnerAction>(sitting);
    }
Esempio n. 12
0
    void Awake()
    {
        var idle    = new State <CamaraTankInputs>("Idle");
        var move    = new State <CamaraTankInputs>("Move");
        var pursuit = new State <CamaraTankInputs>("Pursuit");
        var alarm   = new State <CamaraTankInputs>("Alarm");

        #region StateConfiguration
        StateConfigurer.Create(idle)
        .SetTransition(CamaraTankInputs.MOVE, move)
        .SetTransition(CamaraTankInputs.PURSUIT, pursuit)
        .SetTransition(CamaraTankInputs.ALARM, alarm)
        .Done();

        StateConfigurer.Create(move)
        .SetTransition(CamaraTankInputs.IDLE, idle)
        .SetTransition(CamaraTankInputs.PURSUIT, pursuit)
        .SetTransition(CamaraTankInputs.ALARM, alarm)
        .Done();

        StateConfigurer.Create(pursuit)
        .SetTransition(CamaraTankInputs.IDLE, idle)
        .SetTransition(CamaraTankInputs.MOVE, move)
        .SetTransition(CamaraTankInputs.ALARM, alarm)
        .Done();

        StateConfigurer.Create(alarm)
        .SetTransition(CamaraTankInputs.IDLE, idle)
        .SetTransition(CamaraTankInputs.MOVE, move)
        .SetTransition(CamaraTankInputs.PURSUIT, pursuit)
        .Done();

        #endregion
        move.OnUpdate += () =>
        {
            if (Vector3.Distance(transform.position, _heroe.transform.position) <= minDist)
            {
                if (Vector3.Angle(transform.forward, _heroe.transform.position - transform.position) <= visionAngle)
                {
                    Ray        r       = new Ray(transform.position, _heroe.transform.position - transform.position);
                    RaycastHit rayInfo = new RaycastHit();
                    if (Physics.Raycast(r, out rayInfo))
                    {
                        if (rayInfo.collider.tag == "Heroe")
                        {
                            SendInputToFSM(CamaraTankInputs.ALARM);
                        }
                    }
                }
            }
        };
        move.OnFixedUpdate += () =>
        {
            if (wayIndex >= waySystem.wayPoints.Length || wayIndex < 0)
            {
                wayDirection *= -1;
                wayIndex     += wayDirection;
            }

            if (moving)
            {
                Vector3 dist = waySystem.wayPoints[wayIndex].transform.position - transform.position;

                //Mientras esté lejos del waypoint, me voy acercando
                if (dist.magnitude > speed * Time.deltaTime)
                {
                    transform.forward   = Vector3.Lerp(transform.forward, dist.normalized, 0.25f);
                    transform.position += transform.forward * speed * Time.deltaTime;
                }
                //Si me acerco, me fijo en el waypoint y apunto al siguiente
                else
                {
                    transform.position = new Vector3(waySystem.wayPoints[wayIndex].transform.position.x, transform.position.y,
                                                     waySystem.wayPoints[wayIndex].transform.position.z);
                    wayIndex += wayDirection;
                }
            }
        };

        alarm.OnFixedUpdate += () =>
        {
            if (Vector3.Distance(transform.position, _heroe.transform.position) <= minDist)
            {
                if (Vector3.Angle(transform.forward, _heroe.transform.position - transform.position) <= visionAngle)
                {
                    Ray        r       = new Ray(transform.position, _heroe.transform.position - transform.position);
                    RaycastHit rayInfo = new RaycastHit();
                    if (Physics.Raycast(r, out rayInfo))
                    {
                        if (rayInfo.collider.tag == "Heroe")
                        {
                            cam.transform.up          = Vector3.Lerp(cam.transform.up, cam.transform.position - _heroe.transform.position, Time.deltaTime / 2);
                            arm.transform.forward     = Vector3.Lerp(arm.transform.forward, _heroe.transform.position - arm.transform.position, Time.deltaTime / 2);
                            arm.transform.eulerAngles = new Vector3(0, arm.transform.eulerAngles.y, 0);
                            if (!GetComponent <AudioSource>().isPlaying)
                            {
                                GetComponent <AudioSource>().Play();
                            }
                            moving = false;
                            _heroe.GetComponent <Heroe>().isLocked = true;
                            AchievementData.spotted = true;
                            //_lost = true;
                            StartCoroutine(Lost());
                        }
                    }
                }
            }
        };

        _myFsm = new EventFSM <CamaraTankInputs>(move);
    }