コード例 #1
0
    /// <summary>
    /// 状态机初始化
    /// </summary>
    private void ConstructFSM()
    {
        //针对每一个行为进行初始化, 针对于每个行为进行状态到行为转变的添加
        //巡逻
        Patrol patrol = new Patrol(this);

        patrol.AddTransition(Transition.SawPlayer, FSMActionID.Chasing);
        patrol.AddTransition(Transition.NoHealth, FSMActionID.Dead);
        //追逐
        Chase chase = new Chase(this);

        chase.AddTransition(Transition.ReachPalyer, FSMActionID.Attacking);
        chase.AddTransition(Transition.LostPlayer, FSMActionID.Patroling);
        chase.AddTransition(Transition.NoHealth, FSMActionID.Dead);
        //攻击
        Attack attack = new Attack(this);

        attack.AddTransition(Transition.SawPlayer, FSMActionID.Chasing);
        attack.AddTransition(Transition.LostPlayer, FSMActionID.Patroling);
        attack.AddTransition(Transition.NoHealth, FSMActionID.Dead);
        //死亡
        Dead dead = new Dead(this);

        dead.AddTransition(Transition.NoHealth, FSMActionID.Dead);
        //把状态列表进行初始化
        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);
    }
コード例 #2
0
            // returns initial state
            private State InitStates()
            {
                aiTools = GetComponent <AiTools>();
                aiTools.Init();
                SearchEnemy      searchEnemyState      = new SearchEnemy(aiTools);
                SearchHealthPack searchHealthPackState = new SearchHealthPack(aiTools);
                Attack           attackState           = new Attack(aiTools);
                Defence          defenceState          = new Defence(aiTools);

                searchEnemyState.AddTransition(
                    new Transition(
                        () => aiTools.agentState.isEnemyVisible,
                        attackState
                        )
                    );

                searchEnemyState.AddTransition(
                    new Transition(
                        () => (aiTools.world.healthPacksAvailable > 0) && (aiTools.agentState.health <= GameDefines.LOW_HP),
                        searchHealthPackState
                        )
                    );

                attackState.AddTransition(
                    new Transition(
                        () => (aiTools.agentState.health <= GameDefines.MEDIUM_HP),
                        defenceState
                        )
                    );

                attackState.AddTransition(
                    new Transition(
                        () => (aiTools.agentState.health > GameDefines.MEDIUM_HP) && !aiTools.agentState.isEnemyVisible,
                        searchEnemyState
                        )
                    );

                defenceState.AddTransition(
                    new Transition(
                        () => (aiTools.world.healthPacksAvailable > 0) && ((aiTools.agentState.health <= GameDefines.LOW_HP) || !aiTools.agentState.isEnemyVisible),
                        searchHealthPackState
                        )
                    );

                defenceState.AddTransition(
                    new Transition(
                        () => (!aiTools.agentState.isEnemyVisible && ((aiTools.world.healthPacksAvailable == 0) || aiTools.agentState.health > GameDefines.LOW_HP)),
                        searchEnemyState
                        )
                    );

                searchHealthPackState.AddTransition(
                    new Transition(
                        () => (aiTools.agentState.health > GameDefines.LOW_HP) || (aiTools.world.healthPacksAvailable == 0),
                        searchEnemyState
                        )
                    );

                return(searchEnemyState);
            }
コード例 #3
0
    // Use this for initialization
    void Start()
    {
        spriteRenderer = gameObject.GetComponentInChildren <SpriteRenderer> ();
        shadow         = gameObject.GetComponentInChildren <MeshRenderer>();
        gc             = GameController.Instance;
        rb             = GetComponent <Rigidbody> ();
        maxHealth      = health;
        Reset();
        Hide();

        //STATE MACHINE INITIALISATION
        fsm        = new FSM();
        idle       = new Idle();
        move       = new Move();
        attack     = new Attack();
        moveAttack = new MoveAttack();

        //idle:			moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0
        //move:			moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0
        //attack:		moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0
        //moveattack:	moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0

        idle.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0));
        idle.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));
        idle.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        move.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0));
        move.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));
        move.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        //attack not moving
        attack.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        attack.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        attack.AddTransition(new Transition(moveAttack, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude != 0));

        //attack while moving
        moveAttack.AddTransition(new Transition(move, () => moveVec.sqrMagnitude != 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        moveAttack.AddTransition(new Transition(idle, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude == 0 && !CurrentWeaponScript.shootDelaying));
        moveAttack.AddTransition(new Transition(attack, () => moveVec.sqrMagnitude == 0 && shootVec.sqrMagnitude != 0));


        fsm.AddState(idle);
        fsm.AddState(move);
        fsm.AddState(attack);

        fsm.Init(idle, Directions.Unspecified, Directions.Unspecified);
    }
コード例 #4
0
    /// <summary>
    /// 初始化
    /// </summary>
    void Init()
    {
        fsm       = new FSMSystem();
        agent     = GetComponent <NavMeshAgent>();
        anim      = GetComponent <Animator>();
        player    = GameObject.FindWithTag("Player").transform;
        wayPoints = GameObject.FindGameObjectsWithTag("PatrolPoint");

        patrol = new Patrol(fsm, transform, player, agent, anim, wayPoints);
        chase  = new Chase(fsm, transform, player, agent, anim);
        attack = new Attack(fsm, transform, player, agent, anim);
        dead   = new Dead(fsm, transform, player, agent, anim);
        hit    = new Hit(fsm, transform, player, agent, anim);

        #region 改变状态
        //看家玩家之后改变自己的状态为追击  看见玩家 -> 追击玩家
        patrol.AddTransition(Transition.SawPlayer, FSMState.Chase);
        //巡逻过程中,可能会被玩家攻击
        patrol.AddTransition(Transition.GetHit, FSMState.Hit);
        //巡逻中玩家进入它的范围 -> 开始攻击
        patrol.AddTransition(Transition.ReachPlayer, FSMState.Attack);

        //追击的过程中,玩家移速过快,距离范围过大  丢失玩家 -> 开始巡逻
        chase.AddTransition(Transition.LostPlayer, FSMState.Patrol);
        //追击过程中,追到了玩家 -> 开始攻击
        chase.AddTransition(Transition.ReachPlayer, FSMState.Attack);
        //收到攻击
        chase.AddTransition(Transition.GetHit, FSMState.Hit);

        //攻击过程中 玩家移动,只要还在距离范围内,就会触发追击,直至玩家超出指定距离
        attack.AddTransition(Transition.SawPlayer, FSMState.Chase);
        //攻击过程中,玩家突然死亡,或者距离超出  就去巡逻
        attack.AddTransition(Transition.LostPlayer, FSMState.Patrol);
        //收到攻击
        attack.AddTransition(Transition.GetHit, FSMState.Hit);

        //被击打的时候,也会存在丢失玩家的情况
        hit.AddTransition(Transition.LostPlayer, FSMState.Patrol);
        #endregion

        fsm.AddState(patrol);
        fsm.AddState(chase);
        fsm.AddState(attack);
        fsm.AddState(dead);
        fsm.AddState(hit);
    }
コード例 #5
0
    /// <summary>
    /// 状态机初始化
    /// </summary>
    private void ConstructFSM()
    {
        //进行路点初始化
        pointList = GameObject.FindGameObjectsWithTag("PatrolPoint");

        Transform[] wayPoints = new Transform[pointList.Length];
        int         i         = 0;

        foreach (GameObject obj in pointList)
        {
            wayPoints[i] = obj.transform;
            i++;
        }
        //针对每一个行为进行初始化, 针对于每个行为进行状态到行为转变的添加
        //巡逻
        Patrol patrol = new Patrol(wayPoints, this);

        patrol.AddTransition((int)Transition.SawPlayer, (int)FSMActionID.Chasing);
        patrol.AddTransition((int)Transition.NoHealth, (int)FSMActionID.Dead);
        //追逐
        Chase chase = new Chase(wayPoints, this);

        chase.AddTransition((int)Transition.ReachPalyer, (int)FSMActionID.Attacking);
        chase.AddTransition((int)Transition.LostPlayer, (int)FSMActionID.Patroling);
        chase.AddTransition((int)Transition.NoHealth, (int)FSMActionID.Dead);
        //攻击
        Attack attack = new Attack(wayPoints, this);

        attack.AddTransition((int)Transition.SawPlayer, (int)FSMActionID.Chasing);
        attack.AddTransition((int)Transition.LostPlayer, (int)FSMActionID.Patroling);
        attack.AddTransition((int)Transition.NoHealth, (int)FSMActionID.Dead);
        //死亡
        Dead dead = new Dead();

        dead.AddTransition((int)Transition.NoHealth, (int)FSMActionID.Dead);

        //把状态列表进行初始化
        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);

        StartFSM(patrol);
    }
コード例 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("状态模式:");

            Context context = new Context(new StateA());

            context.Request();
            context.Request();
            context.Request();
            context.Request();

            #region 角色系统
            //Role role = new Role(new Primary(), 0.0f);
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = true;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            //role.IsVictory = false;
            //role.Play();
            #endregion

            #region  输门
            //TransmissionGate transGate = new TransmissionGate(new Closed());
            //transGate.Complete();
            //transGate.Click();
            //transGate.TimeOut();
            //transGate.Click();
            //transGate.Complete();
            //transGate.TimeOut();
            //transGate.Click();
            //transGate.Complete();
            //transGate.TimeOut();
            //transGate.Click();
            //transGate.Complete();
            //transGate.Click();
            //transGate.Click();
            //transGate.Complete();
            #endregion

            Idle   idle   = new Idle();
            Walk   walk   = new Walk();
            Jump   jump   = new Jump();
            Attack attack = new Attack();

            idle.AddTransition(new JyTransition(idle, walk, 'w'));
            idle.AddTransition(new JyTransition(idle, jump, 'j'));
            idle.AddTransition(new JyTransition(idle, attack, 'k'));

            walk.AddTransition(new JyTransition(walk, idle, 's'));
            walk.AddTransition(new JyTransition(walk, jump, 'j'));
            walk.AddTransition(new JyTransition(walk, attack, 'k'));

            jump.AddTransition(new JyTransition(jump, idle, 's'));
            jump.AddTransition(new JyTransition(jump, walk, 'w'));
            jump.AddTransition(new JyTransition(jump, attack, 'k'));

            attack.AddTransition(new JyTransition(attack, idle, 's'));
            attack.AddTransition(new JyTransition(attack, walk, 'w'));
            attack.AddTransition(new JyTransition(attack, jump, 'j'));

            HeroStateMachine heroStateMachine = new HeroStateMachine(idle);
            heroStateMachine.AddState(walk);
            heroStateMachine.AddState(jump);
            heroStateMachine.AddState(attack);

            ConsoleKeyInfo ConsoleKeyInfo = new ConsoleKeyInfo();
            while (ConsoleKeyInfo.Key != ConsoleKey.Escape)
            {
                ConsoleKeyInfo = Console.ReadKey();
                Console.WriteLine(ConsoleKeyInfo.Key.ToString());
                heroStateMachine.Word = ConsoleKeyInfo.KeyChar;
                heroStateMachine.OnStay();
            }
        }
コード例 #7
0
ファイル: Enemy.cs プロジェクト: kreeds/TestProjectDemo
    protected virtual void InitializeStateMachine()
    {
        Idle idleState = new Idle(this);
        idleState.AddTransition(Transition.E_FAILGESTURE, StateID.E_ATTACK);
        idleState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);
        idleState.AddTransition(Transition.E_NOHP, StateID.E_DEATH);

        Attack atkState = new Attack(this);
        atkState.AddTransition(Transition.E_FINISHATTACK, StateID.E_IDLE);
        atkState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);

        Damaged dmgState = new Damaged(this);
        dmgState.AddTransition(Transition.E_NOHP, StateID.E_DEATH);
        dmgState.AddTransition(Transition.E_FINISHATTACK, StateID.E_IDLE);
        dmgState.AddTransition(Transition.E_LOSTHP, StateID.E_DAMAGED);

        Death deathState = new Death(this);
        enemyState = new FiniteStateMachine();
        enemyState.AddState(idleState);

        enemyState.AddState(atkState);
        enemyState.AddState(dmgState);
        enemyState.AddState(deathState);
    }