Inheritance: IEnemyState
Exemplo n.º 1
0
    void MakeFSM()
    {
        PatrolState patrol = new PatrolState();
        ChaseState  chase  = new ChaseState();
        AttackState attack = new AttackState();
        DeadState   dead   = new DeadState();
        IdleState   idle   = new IdleState();
        MoveState   move   = new MoveState();

        patrol.AddTransition(Transition.SawEnemy, chase);
        patrol.AddTransition(Transition.NoHP, dead);

        chase.AddTransition(Transition.LostEnemy, patrol);
        chase.AddTransition(Transition.ReachEnemy, attack);
        chase.AddTransition(Transition.NoHP, dead);

        attack.AddTransition(Transition.LostEnemy, patrol);
        attack.AddTransition(Transition.SawEnemy, chase);
        attack.AddTransition(Transition.NoHP, dead);

        AddState(StateID.Patrol, patrol);
        AddState(StateID.Chase, chase);
        AddState(StateID.Attack, attack);
        AddState(StateID.Dead, dead);



        m_stateMachine.Start(patrol);
    }
Exemplo n.º 2
0
    private void ConstructFSM()
    {
        FSMState patrol = new PatrolState(this);

        patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        patrol.AddTransition(Transition.ReachedPlayer, FSMStateID.Attacking);
        AddFSMState(patrol);

        FSMState chase = new ChaseState();

        chase.AddTransition(Transition.LostPlayer, FSMStateID.Searching);
        chase.AddTransition(Transition.ReachedPlayer, FSMStateID.Attacking);
        AddFSMState(chase);

        FSMState search = new SearchState(this);

        search.AddTransition(Transition.GiveUpSearching, FSMStateID.Patrolling);
        search.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        AddFSMState(search);

        FSMState attack = new AttackState(this);

        attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        attack.AddTransition(Transition.LostPlayer, FSMStateID.Searching);
        AddFSMState(attack);
    }
Exemplo n.º 3
0
    protected override void BuildFSM()
    {
        //Other States Here

        PatrolState patrol = new PatrolState(this);

        patrol.AddTransitionState(FSMStateID.Investigate, FSMTransitions.HeardPlayer);
        patrol.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer);
        AddFSMState(patrol);

        InvestigateState investigate = new InvestigateState(this);

        investigate.AddTransitionState(FSMStateID.Patrol, FSMTransitions.FoundNothing);
        investigate.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer);
        AddFSMState(investigate);

        ShootState shoot = new ShootState();

        shoot.AddTransitionState(FSMStateID.Investigate, FSMTransitions.PlayerOutOfRange);
        AddFSMState(shoot);

        DeadState dead = new DeadState();

        AddFSMState(dead);
    }
Exemplo n.º 4
0
    private void MakeFSM()
    {
        PatrolState patrol = new PatrolState(player, transform, path);

        patrol.AddTransition(Transition.GuardPlayer, StateID.GuardID);

        GuardState guard = new GuardState(player, transform);

        guard.AddTransition(Transition.AutoPatrol, StateID.PatrolID);
        guard.AddTransition(Transition.ChasingPlayer, StateID.ChasingID);

        ChaseState chase = new ChaseState(player, transform);

        chase.AddTransition(Transition.GuardPlayer, StateID.GuardID);
        chase.AddTransition(Transition.AttackPlayer, StateID.AttackID);

        AttackState attack = new AttackState(player, transform);

        attack.AddTransition(Transition.GuardPlayer, StateID.GuardID);
        attack.AddTransition(Transition.ChasingPlayer, StateID.ChasingID);

        fsm = new FSMSystem();

        fsm.AddState(patrol);
        fsm.AddState(guard);
        fsm.AddState(chase);
        fsm.AddState(attack);
    }
 private void Awake()
 {
     chaseState  = new ChaseState(this);
     alertState  = new AlertState(this);
     patrolState = new PatrolState(this);
     wanderState = new WanderState(this);
 }
Exemplo n.º 6
0
 public void StartChase()
 {
     agentAI.speed     = chaseSpeed;
     agentAI.isStopped = false;
     currentState      = PatrolState.Chasing;
     chaseTarget       = detectionScript.entryFocus;
 }
Exemplo n.º 7
0
 private void Awake()//Before Start, initializes states and gets component reference for NavMeshAgent attached to enemy
 {
     chaseState   = new ChaseState(this);
     alertState   = new AlertState(this);
     patrolState  = new PatrolState(this);
     navMeshAgent = GetComponent <UnityEngine.AI.NavMeshAgent>();
 }
Exemplo n.º 8
0
    // Update is called once per frame
    void Update()
    {
        timeLeft -= Time.deltaTime;

        if (timeLeft < 0)
        {
            switch (currentState)
            {
            case PatrolState.STOP:
                currentState  = PatrolState.MOVING;
                rb2d.velocity = new Vector2(4 * dir, 0);
                dir          *= -1;

                timeLeft = 1;
                break;

            case PatrolState.MOVING:
                currentState  = PatrolState.STOP;
                rb2d.velocity = new Vector2(0, 0);

                timeLeft = 1.5f;
                break;
            }
        }
    }
Exemplo n.º 9
0
 void Awake()
 {
     startPosition   = transform.position;
     fighter         = GetComponent <Fighter>();
     moveController  = GetComponent <Mover>();
     patrolBehaviour = GetComponent <PatrolState>();
 }
Exemplo n.º 10
0
    protected override void BuildFSM()
    {
        PatrolState patrol = new PatrolState(PatrolPoints, navAgent, agroDistance);

        patrol.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer);
        patrol.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        ShootState shoot = new ShootState(chargeDistance, spawnerScript);

        shoot.AddTransitionState(FSMStateID.Patrol, FSMTransitions.PlayerOutOfRange);
        shoot.AddTransitionState(FSMStateID.Chase, FSMTransitions.PlayerTooClose);
        shoot.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        ChaseState chase = new ChaseState(navAgent, chargeDistance);

        chase.AddTransitionState(FSMStateID.Shoot, FSMTransitions.PlayerOutOfRange);
        chase.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        DeadState dead = new DeadState();

        AddFSMState(patrol);
        AddFSMState(shoot);
        AddFSMState(chase);
        AddFSMState(dead);
    }
Exemplo n.º 11
0
    private void ReturnToPatrol()  //returns to closest patrol point
    {
        if (currentState == PatrolState.Pausing)
        {
            currentState      = PatrolState.Patrolling;
            agentAI.isStopped = false;
            List <float> distances         = new List <float>();
            int          closestPointIndex = 0;
            foreach (var point in patrolPoints)
            {
                //puts distances to patrol points in a list
                float distance = Vector2.Distance(transform.position, point.position);
                distances.Add(distance);
            }

            for (int i = 0; i < distances.Count; i++)
            {
                //gets index of the closest patrol point
                if (distances[i] < distances[closestPointIndex])
                {
                    closestPointIndex = i;
                }
            }

            agentAI.destination = patrolPoints[closestPointIndex].position;
            currentPatrolPoint  = closestPointIndex;
        }
    }
Exemplo n.º 12
0
    protected override void BuildFSM()
    {
        PatrolState patrol = new PatrolState(PatrolPoints, navAgent, agroDistance);

        patrol.AddTransitionState(FSMStateID.Shoot, FSMTransitions.SawPlayer);
        patrol.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        ShootState shoot = new ShootState(retreatDistance, spawnerScript);

        shoot.AddTransitionState(FSMStateID.Patrol, FSMTransitions.PlayerOutOfRange);
        shoot.AddTransitionState(FSMStateID.Retreat, FSMTransitions.PlayerTooClose);
        shoot.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        RetreatState retreat = new RetreatState(navAgent, retreatDistance);

        retreat.AddTransitionState(FSMStateID.Shoot, FSMTransitions.CloserDistanceReached);
        retreat.AddTransitionState(FSMStateID.Dead, FSMTransitions.OutOfHealth);

        DeadState dead = new DeadState();

        AddFSMState(patrol);
        AddFSMState(shoot);
        AddFSMState(retreat);
        AddFSMState(dead);
    }
Exemplo n.º 13
0
    private void ConstructFSM()             //初始化拥有哪些状态,并为拥有的状态设置转化和状态ID
    {
        PatrolState tPatrolState = new PatrolState(wayPoints);

        tPatrolState.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        tPatrolState.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        ChaseState tChaseState = new ChaseState(wayPoints);

        tChaseState.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
        tChaseState.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        tChaseState.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AttackState tAttackState = new AttackState(wayPoints);

        tAttackState.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        tAttackState.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        tAttackState.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        DeadState tDeadState = new DeadState();

        tDeadState.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AddFSMState(tPatrolState);
        AddFSMState(tPatrolState);
        AddFSMState(tPatrolState);
        AddFSMState(tPatrolState);


        Events.AddEvent(EventSign.GAME_START, DestroySelf, this);
        Events.Send(EventSign.GAME_START, new EventArg("123"));
    }
Exemplo n.º 14
0
    void Awake()
    {
        AIBehaviors ai    = GetComponent <AIBehaviors>();
        PatrolState state = ai.GetState <PatrolState>();

        state.SetPatrolPoints(patrolPoints);
    }
    public override void OnFinishPath()
    {
        StateMachine stateMachine = this.GetComponent <StateMachine> ();
        PatrolState  patrolState  = this.GetComponent <PatrolState> ();

        stateMachine.ChangeState(patrolState);
    }
Exemplo n.º 16
0
    void PatrolStateCheck()
    {
        switch (patrolState)
        {
        case PatrolState.PatrolRight:
            if (!stateMachine.WallRight() && stateMachine.EdgeRight())
            {
                horizontalDirection = 1;
            }
            else
            {
                patrolState = PatrolState.PatrolLeft;
            }
            break;

        case PatrolState.PatrolLeft:
            if (!stateMachine.WallLeft() && stateMachine.EdgeLeft())
            {
                horizontalDirection = -1;
            }
            else
            {
                patrolState = PatrolState.PatrolRight;
            }
            break;
        }
    }
Exemplo n.º 17
0
        protected AI(string name, Texture tex, int width, int height, int tilesPerRow, int[] keyFrames, float frameLength, bool show, bool stop) : base(name)
        {
            Renderer = new AnimationRenderer(tex, width, height, tilesPerRow, keyFrames, frameLength, show, stop);
            Renderer.RenderOffset = (int)RenderLayer.AI;
            AddComponent(Renderer);
            this.Layer  = (uint)CollisionLayer.Enemy;
            patrolState = new PatrolState(this);
            chaseState  = new ChaseState(this);

            patrolState.Next = chaseState;
            chaseState.Next  = patrolState;

            BoxCollider2D collider = new BoxCollider2D(new Vector2(1, 1));

            collider.CollisionEnter += OnCollisionEnter;
            AddComponent(collider);

            Rigidbody2D rigidBody = new Rigidbody2D();

            rigidBody.IsGravityAffected = false;
            AddComponent(rigidBody);

            AddComponent(new BoxCollider2DRenderer(new Vector4(1f, 0f, 0f, 0f)));
            patrolState.OnStateEnter();

            AddComponent(new FSMUpdater(patrolState));
        }
    private void ConstructFSM()
    {
        //Get the list of points
        //pointList = GameObject.FindGameObjectsWithTag("PatrolPoint");

        //Transform[] waypoints = new Transform[pointList.Length];
        //int i = 0;
        //foreach(GameObject obj in pointList)
        //{
        //    waypoints[i] = obj.transform;
        //    i++;
        //}

        int pointlength = Random.Range(3, 5);

        Debug.Log("pointlength = " + pointlength);
        Transform[] waypoints       = new Transform[pointlength];
        float       maxdistance     = 50;
        float       mindistance     = -30;
        GameObject  partpointparent = new GameObject();

        partpointparent.transform.position = role.transform.position;
        partpointparent.name = role.name + "[partolpoints]";

        for (int i = 0; i < pointlength; i++)
        {
            Vector3    pos = new Vector3(role.transform.position.x + CreatPatrolRange(maxdistance, mindistance), 0, role.transform.position.z + CreatPatrolRange(maxdistance, mindistance));
            GameObject go  = new GameObject();
            go.transform.position = pos;
            go.transform.parent   = partpointparent.transform;;
            waypoints[i]          = go.transform;
        }
        Debug.Log("waypoints length -> " + waypoints.Length);

        PatrolState patrol = new PatrolState(waypoints);

        patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        ChaseState chase = new ChaseState(waypoints);

        chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
        chase.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AttackState attack = new AttackState(waypoints);

        attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        attack.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        DeadState dead = new DeadState();

        dead.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);
    }
Exemplo n.º 19
0
 private void Awake()
 {
     searchState  = new SearchState(this);
     chaseState   = new ChaseState(this);
     patrolState  = new PatrolState(this);
     navMeshAgent = GetComponent <NavMeshAgent>();
 }
Exemplo n.º 20
0
    //Construct the Finite State Machine for the AI Car behavior
    private void ConstructFSM()
    {
        //Get the list of points
        pointList = GameObject.FindGameObjectsWithTag("WandarPoints");

        Transform[] waypoints = new Transform[pointList.Length];
        int i = 0;
        foreach (GameObject obj in pointList)
        {
            waypoints[i] = obj.transform;
            i++;
        }

        PatrolState patrol = new PatrolState(waypoints);
        patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        ChaseState chase = new ChaseState(waypoints);
        chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
        chase.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AttackState attack = new AttackState(waypoints);
        attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        attack.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        DeadState dead = new DeadState();
        dead.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);
    }
Exemplo n.º 21
0
    private void ConstructFSM()
    {
        pointList = GameObject.FindGameObjectsWithTag("DestinationPoint");

        Transform[] wayPoints = new Transform[pointList.Length];

        int i = 0;

        foreach (GameObject obj in pointList)
        {
            wayPoints[i] = obj.transform;
            i++;
        }

        PatrolState patrol = new PatrolState(wayPoints);

        patrol.AddTransition(Transition.FoundPlayer, FSMStateID.Chasing);

        ChaseState chase = new ChaseState(wayPoints);

        chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        chase.AddTransition(Transition.ReachedPlayer, FSMStateID.Attacking);


        AddFSMState(patrol);
        AddFSMState(chase);
    }
Exemplo n.º 22
0
    // The NPC has two states: FollowPath and ChasePlayer
    // If it's on the first state and SawPlayer transition is fired, it changes to ChasePlayer
    // If it's on ChasePlayerState and LostPlayer transition is fired, it returns to FollowPath
    private void MakeFSM()
    {
        FollowPathState follow = new FollowPathState(/*path*/);

        follow.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);

        ChasePlayerState chase = new ChasePlayerState();

        chase.AddTransition(Transition.LostPlayer, StateID.FollowingPath);

        PatrolState patrolState = new PatrolState(PatrolWayPoints);

        patrolState.AddTransition(Transition.Alert, StateID.AlertNpc);

        AlertState alertState = new AlertState();

        alertState.AddTransition(Transition.Attack, StateID.AttackingPlayer);
        alertState.AddTransition(Transition.Patrol, StateID.Patroling);

        AttackState attackState = new AttackState();

        attackState.AddTransition(Transition.Alert, StateID.AlertNpc);


        fsm = new FSMSystem();
        fsm.AddState(patrolState);
        fsm.AddState(alertState);
        fsm.AddState(attackState);
        //fsm.AddState(chase);
        //fsm.AddState(follow);

        Debug.Log("First State: " + fsm.CurrentState.ToString());
    }
Exemplo n.º 23
0
    void Awake () {
        patrolState = new PatrolState(this);
        alertState = new AlertState(this);
        chaseState = new ChaseState(this);

        navMeshAgent = GetComponent<NavMeshAgent>();
    }
Exemplo n.º 24
0
    private void OnEnable()
    {
        //find player if player == null
        if (_player == null)
        {
            _player = FindObjectOfType <Player>();
        }
        _source = GetComponent <AudioSource>();

        if (_base == null)
        {
            _base = GameObject.FindGameObjectWithTag("Base");
        }

        _agent       = GetComponent <NavMeshAgent>();
        _agent.speed = _movementSpeed;

        //depends on _isWaveEnemy
        if (_isWaveEnemy == false)
        {
            SetState(PatrolState.GetInstance());
        }
        else
        {
            SetState(ChargeBaseState.GetInstance());
        }
    }
        public override void Execute(EnemyController enemyController)
        {
            PatrolController patrolController = (PatrolController)enemyController;

            if (!_currentState.checkValid(patrolController) || _isFinished)
            {
                // randomly change current state
                int randomStateCase;
                do
                {
                    randomStateCase = UnityEngine.Random.Range(0, 3);
                } while (randomStateCase == _currentStateCase);

                _currentStateCase = randomStateCase;
                switch (_currentStateCase)
                {
                case 0:
                    _currentState = new Idle();
                    break;

                case 1:
                    _currentState = new WalkingLeft();
                    break;

                case 2:
                    _currentState = new WalkingRight();
                    break;
                }

                patrolController.StartCoroutine(executeCoroutine(patrolController.behaveInterval()));
            }

            _currentState.Execute(patrolController);
        }
 public void SetToStuntState()
 {
     mTintColor    = kStuntTint;
     mStateTimer   = kStunCycle;
     mCurrentState = PatrolState.StuntState;
     AudioSupport.PlayACue("Stun");
 }
Exemplo n.º 27
0
    void InitializeExtensibleStateMachine()
    {
        this.gameObject.AddComponent(typeof(ExtensibleStateMachine));
        stateMachineEx = this.gameObject.GetComponent <ExtensibleStateMachine>();

        var idle   = new IdleState(this);
        var patrol = new PatrolState(this);
        var allert = new AllertState(this);
        var search = new SearchState(this);


        AtAny(idle, Disable());
        At(idle, patrol, Enable());

        At(patrol, allert, TargetVisible());

        At(allert, search, NotTargetVisible());

        At(search, allert, TargetVisible());
        At(search, patrol, NotTargetVisible());

        stateMachineEx.SetState(idle);

        void At(IState _to, IState _from, Func <bool> _condition) => stateMachineEx.AddTransition(_to, _from, _condition);
        void AtAny(IState _state, Func <bool> _conditionstate) => stateMachineEx.AddAnyTransition(_state, _conditionstate);

        Func <bool> TargetVisible() => () => (GetTarget() != null);
        Func <bool> NotTargetVisible() => () => isEscaped;

        Func <bool> Enable() => () => OpenEye;
        Func <bool> Disable() => () => !OpenEye;
    }
        /// <summary>
        /// Randomly generate a next target position for patrolling
        /// </summary>
        private void RandomNextTarget()
        {
            mStateTimer   = kStateTimer;
            mCurrentState = PatrolState.PatrolState;
            // Generate a random begin state
            double initState = Game1.sRan.NextDouble();

            if (initState < 0.25)
            {
                mTargetPosition = RandomBottomRightPosition();
            }
            else if (initState < 0.5)
            {
                mTargetPosition = RandomTopRightPosition();
            }
            else if (initState < 0.75)
            {
                mTargetPosition = RandomTopLeftPosition();
            }
            else
            {
                mTargetPosition = RandomBottomLeftPosition();
            }

            ComputeNewSpeedAndResetTimer();
        }
Exemplo n.º 29
0
    //建造状态机
    private void MakeFSM()
    {
        InspectionState inspection = new InspectionState();

        inspection.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);
        inspection.AddTransition(Transition.LostPlayer, StateID.FollowingPath);

        PatrolState follow = new PatrolState(transform.position, scope);

        follow.AddTransition(Transition.SawPlayer, StateID.ChasingPlayer);
        follow.AddTransition(Transition.ArrivePath, StateID.Inspection);

        ChasePlayerState chase = new ChasePlayerState();

        chase.AddTransition(Transition.LostPlayer, StateID.FollowingPath);
        chase.AddTransition(Transition.NearPlayer, StateID.AttackPlayer);

        AttackPlayerState attack = new AttackPlayerState();

        attack.AddTransition(Transition.LostPlayer, StateID.FollowingPath);

        fsm = new FsmSystem();
        fsm.AddState(inspection);//添加状态到状态机,第一个添加的状态将作为初始状态
        fsm.AddState(follow);
        fsm.AddState(chase);
        fsm.AddState(attack);
    }
Exemplo n.º 30
0
    private void ConstructFSM()
    {
        //Get the list of points
        pointList = GameObject.FindGameObjectsWithTag("WanderPoint");

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

        foreach (GameObject obj in pointList)
        {
            waypoints[i] = obj.transform;
            i++;
        }

        //Add Transition and State Pairs
        PatrolState patrol = new PatrolState(waypoints, this);

        //If the tank sees the player while patrolling, move to chasing state
        patrol.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        //If the tank loses health while patrolling, move to dead state
        patrol.AddTransition(Transition.NoHealth, FSMStateID.Dead);
        patrol.AddTransition(Transition.HalfHealth, FSMStateID.Rage);

        ChaseState chase = new ChaseState(waypoints, this);

        //If the tank loses the player while chasing, move to patrol state
        chase.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        //If the tank reaches the player while attacking, move to attack state
        chase.AddTransition(Transition.ReachPlayer, FSMStateID.Attacking);
        //If the tank loses health while patrolling, move to dead state
        chase.AddTransition(Transition.NoHealth, FSMStateID.Dead);
        chase.AddTransition(Transition.HalfHealth, FSMStateID.Rage);

        AttackState attack = new AttackState(waypoints, this);

        //If the tank loses the player while attacking, move to patrol state
        attack.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);
        //If the player is within sight while attacking, move to chase state
        attack.AddTransition(Transition.SawPlayer, FSMStateID.Chasing);
        //If the tank loses health while attacking, move to dead state
        attack.AddTransition(Transition.NoHealth, FSMStateID.Dead);
        attack.AddTransition(Transition.HalfHealth, FSMStateID.Rage);

        DeadState dead = new DeadState(this);

        //When there is no health, go to dead state
        dead.AddTransition(Transition.NoHealth, FSMStateID.Dead);

        RageState rage = new RageState(this);

        rage.AddTransition(Transition.NoHealth, FSMStateID.Dead);
        rage.AddTransition(Transition.LostPlayer, FSMStateID.Patrolling);

        //Add states to the state list
        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);
        AddFSMState(rage);
    }
Exemplo n.º 31
0
    virtual protected void InitState()
    {
        State idleState = new IdleState();

        idleState.Init(this);
        _stateDictionary.Add(eState.IDLE, idleState);

        State moveState = new MoveState();

        moveState.Init(this);
        _stateDictionary.Add(eState.MOVE, moveState);

        State attackState = new AttackState();

        attackState.Init(this);
        _stateDictionary.Add(eState.ATTACK, attackState);

        State chaseState = new ChaseState();

        chaseState.Init(this);
        _stateDictionary.Add(eState.CHASE, chaseState);

        State patrolState = new PatrolState();

        patrolState.Init(this);
        _stateDictionary.Add(eState.PATROL, patrolState);
    }
Exemplo n.º 32
0
    protected void ConstructFSM()
    {
        // ________________
        // PatrolState
        PatrolState patrolState = new PatrolState(new EnemyStateData(GetComponent <EnemyBase>(), this), Points);

        patrolState.AddTransition(Transition.FoundTarget, FSMStateID.TrackTarget);
        AddFSMState(patrolState);

        // ________________
        // TrackPlayerState
        TrackPlayerState rushState = new TrackPlayerState(new EnemyStateData(GetComponent <EnemyBase>(), this));

        rushState.AddTransition(Transition.LostTarget, FSMStateID.Patrol);
        rushState.AddTransition(Transition.FoundTarget, FSMStateID.ShootPlayer);
        rushState.AddTransition(Transition.ApproachedPlayer, FSMStateID.Retreating);
        AddFSMState(rushState);


        // ________________
        // ShootPlayerState
        ShootPlayerState shootPlayerState = new ShootPlayerState(new EnemyStateData(GetComponent <EnemyBase>(), this));

        shootPlayerState.AddTransition(Transition.LostTarget, FSMStateID.Patrol);
        shootPlayerState.AddTransition(Transition.ApproachedPlayer, FSMStateID.Retreating);
        AddFSMState(shootPlayerState);


        // ________________
        // ShootPlayerState
        RetreatingState retreatingState = new RetreatingState(new EnemyStateData(GetComponent <EnemyBase>(), this));

        retreatingState.AddTransition(Transition.LostTarget, FSMStateID.Patrol);
        AddFSMState(retreatingState);
    }
Exemplo n.º 33
0
	// Update is called once per frame
	void Update () {
        timeLeft -= Time.deltaTime;

        if(timeLeft < 0)
        {
            switch (currentState)
            {
                case PatrolState.STOP:
                    currentState = PatrolState.MOVING;
                    rb2d.velocity = new Vector2(4 * dir, 0);
                    dir *= -1;

                    timeLeft = 1;
                    break;

                case PatrolState.MOVING:
                    currentState = PatrolState.STOP;
                    rb2d.velocity = new Vector2(0, 0);

                    timeLeft = 1.5f;
                    break;
            }
        }

	}
 private void Awake()
 {
     chaseState = new ChaseState(this);
     alertState = new AlertState(this);
     patrolState = new PatrolState(this);
     aim = GetComponent<Aim>();
     autoFire = GetComponent<AutoFire>();
 }
    private void Awake()
    {
        chaseTarget = new ChaseState(this);
        alertState = new AlertState(this);
        patrolState = new PatrolState(this);

        navMeshAgent = GetComponent<NavMeshAgent>();
    }
Exemplo n.º 36
0
	void Awake() {
		attackState = new AttackState(this);
		chaseState = new ChaseState(this);
		idleState = new IdleState(this);
		patrolState = new PatrolState(this);
		playerBuddy = GameObject.FindGameObjectWithTag("Player").transform;
		buddyScript = playerBuddy.GetComponent<Movement>();
	}
Exemplo n.º 37
0
        private void ChaseMovement(PatrolState patrolState)
        {
            switch (patrolState)
            {
                case PatrolState.idle:
                    this.patrolState = PatrolState.right;
                    break;
                case PatrolState.left:
                    if (this.location.X < 10 && this.location.Y <= game.currentLevel.levelHeight)
                    {
                        spriteEffect = SpriteEffects.FlipHorizontally;
                        this.patrolState = PatrolState.right;
                        return;
                    }
                    else
                    {
                        this.location.X -= movementSpeed;
                    }

                    if (target.location.X + target.textureWidth >= this.location.X - 10 &&
                        this.location.Y == 300)
                    {
                        jumping = true;
                    }

                    PatrolJump();
                    break;
                case PatrolState.right:

                    if (this.location.X + texture.Width > game.currentLevel.levelWidth && this.location.Y >= 300)
                    {
                        spriteEffect = SpriteEffects.None;
                        this.patrolState = PatrolState.left;
                        return;
                    }
                    else
                    {
                        this.location.X += movementSpeed;
                    }

                    if (this.location.X + this.texture.Width <= target.location.X - 10 &&
                        this.location.Y == 300)
                    {
                        jumping = true;
                    }

                    PatrolJump();
                    break;
                default: break;

            }
        }
Exemplo n.º 38
0
 // Use this for initialization
 void Start()
 {
     rb2d = GetComponent<Rigidbody2D>();
     currentState = PatrolState.STOP;
     dir = 1;
 }
Exemplo n.º 39
0
    /// <summary>
    /// Awake this instance.
    /// </summary>
    private void Awake()
    {
        GameObject pathFinding = GameObject.Find("Pathfinding");
        enemy = GetComponent<HorrorAI>();
        grid = pathFinding.GetComponent<Grid>();
        enemySight = GetComponent<EnemySight> ();

        chaseState = new ChaseState(this, enemy);
        alertState = new AlertState(this, enemy);
        patrolState = new PatrolState(this, enemy, grid);

        pathfindingStrategy = "A*";

        processedActions = new Queue<TreeAction>();
    }
Exemplo n.º 40
0
    private void ConstructFSM()
    {
        //Get the list of points
        //   pointList = GameObject.FindGameObjectsWithTag("WandarPoint");
        var pointList = gameObject.transform.Cast<Transform>().Where(c => c.gameObject.tag == "WandarPoint").Select(c => c.gameObject).ToArray();

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

        foreach (GameObject obj in pointList)
        {
            obj.GetComponent<Transform>().position = transform.position + new Vector3(Random.onUnitSphere.x * 5,
                                                                 Random.onUnitSphere.y * 5, Random.onUnitSphere.z * 5);
            waypoints[i] = obj.transform;
            //	waypoints[i].position = new Vector3(Random.insideUnitSphere.x * 5,
            //	                                    Random.insideUnitSphere.z * 5, Random.insideUnitSphere.z * 5);

            i++;
        }

        PatrolState patrol = new PatrolState(waypoints);
        patrol.AddTransition(FSMTransition.PlayerSeen, StateID.Chasing);
        patrol.AddTransition(FSMTransition.NoHealth, StateID.Dead);
        patrol.SetStateDistances(chaseStartDist, attackStartDist);
        patrol.SetSpeed(speed / 2, rotationSpeed / 2);
        patrol.SetBounds(patrolBounds.x, patrolBounds.y, patrolBounds.z);

        ChaseState chase = new ChaseState(waypoints);
        chase.AddTransition(FSMTransition.PlayerLost, StateID.Patrolling);
        chase.AddTransition(FSMTransition.PlayerReached, StateID.Attacking);
        chase.AddTransition(FSMTransition.NoHealth, StateID.Dead);
        chase.SetStateDistances(chaseStartDist, attackStartDist);
        chase.SetSpeed(speed, rotationSpeed);

        AttackState attack = new AttackState(waypoints);
        attack.AddTransition(FSMTransition.PlayerLost, StateID.Patrolling);
        attack.AddTransition(FSMTransition.PlayerSeen, StateID.Chasing);
        attack.AddTransition(FSMTransition.NoHealth, StateID.Dead);
        attack.SetStateDistances(chaseStartDist, attackStartDist);
        attack.SetSpeed(speed, rotationSpeed);
        attack.kamikaze = this.kamikaze;

        DeadState dead = new DeadState();
        dead.AddTransition(FSMTransition.NoHealth, StateID.Dead);

        AddFSMState(patrol);
        AddFSMState(chase);
        AddFSMState(attack);
        AddFSMState(dead);

        //Start patrolling at given points
        patrol.FindNextPoint();
    }
Exemplo n.º 41
0
    //
    // Member Functions
    //
    /* Performs a basic sanity check to ensure we've setup the variables in Unity correctly */
    void Start()
    {
        // Ensure our Waypoint list has at least two entries
        if (!WaypointSanityCheck())
            return;

        // Ensure our GameObject has been assigned and is valid
        if (gameObject.active != true)
        {
            Debug.Log("PatrolPath: Unable to start GO Patrol (Assigned GameObject is invalid, or is not active)");
            Active = false;
            return;
        }

        // The GO passed the sanity check; start patrolling!
        m_startTime = Time.time;
        CurrentState = PatrolState.PATROL_WALKING;

        m_currentWaypoint = 0;
        m_targetWaypoint = 1;

        //Debug.Log("PatrolPath: moving to waypoint: " + (m_targetWaypoint));
    }
Exemplo n.º 42
0
    private void Awake()
    {
        chaseState = new ChaseState(this);
        patrolState = new PatrolState(this);
        guardState = new GuardState(this);
        dazedState = new DazedState(this);
        distractedState = new DistractedState(this);
        searchingState = new SearchingState(this);
        suspiciousState = new SuspiciousState(this);
        koState = new KOState(this);
        walkState = new WalkState(this);
        pointSearchState = new PointSearchState(this);

        navMeshAgent = GetComponent<NavMeshAgent>();
        player = GameObject.FindGameObjectWithTag("Player");
        Path = Pathways[0];
        AIPath CheckpointScript = Path.GetComponent<AIPath>();
        navPoint = CheckpointScript.getPoints()[0];
        currentState = patrolState; //sets the current state
        NoOcclusionLM = 1 << noOcclusionLayer;
        NoOcclusionLM = ~NoOcclusionLM;
    }
Exemplo n.º 43
0
    /* The patrol function that will move the GO towards its target waypoint */
    private void Patrol()
    {
        if (!WaypointSanityCheck())
            return;

        // Calculate the distance between the current and next waypoint
        // If the distance is less than one metre, jump to the next waypoint and carry on!
        if (Vector3.Distance(transform.position, Waypoints[m_targetWaypoint]) < WaypointBias)
        {
            if (m_movingForward)
            {
                m_currentWaypoint++;
                m_targetWaypoint++;
            }
            else
            {
                m_currentWaypoint--;
                m_targetWaypoint--;
            }

            //Debug.Log("Reached destination, moving to waypoint: " + (m_targetWaypoint));

            if (m_movingForward && m_targetWaypoint >= Waypoints.Length)
            {
                m_movingForward = false;
                m_currentWaypoint = Waypoints.Length - 1;
                m_targetWaypoint = m_currentWaypoint - 1;

                //Debug.Log("Reached end, moving back to waypoint: " + (m_targetWaypoint));

                CurrentState = PatrolState.PATROL_ROTATING;
            }

            if (!m_movingForward && m_targetWaypoint < 0)
            {
                m_movingForward = true;
                m_currentWaypoint = 0;
                m_targetWaypoint = 1;

                CurrentState = PatrolState.PATROL_ROTATING;
            }

            m_startTime = Time.time;
        }

        if(CurrentState == PatrolState.PATROL_WALKING)
            MoveTowards(Waypoints[m_currentWaypoint], Waypoints[m_targetWaypoint]);

        if (CurrentState == PatrolState.PATROL_ROTATING)
            ChangeDirection(Waypoints[m_targetWaypoint]);
    }
Exemplo n.º 44
0
        public override void Update(GameTime gameTime)
        {
            if (this.location.X - game.player.location.X < 50 || this.location.X - game.player.location.X > -50)
            {
                if (this.GetRectangle().Intersects(game.player.GetRectangle()))
                {
                    game.player.Hit(this);
                }
            }

            if (isChasing)
            {
                Chase();
            }
            else
            {
                Move(currentState);
            }

            if (!jumping && !falling &&
                this.currentState != MovementState.jumping &&
                this.currentState != MovementState.falling &&
                this.currentState != MovementState.attacking)
            {
                int chance = game.currentLevel.random.Next(100);

                if (chance < 2)
                {
                    if (currentState == MovementState.moveLeft)
                    {
                        patrolState = PatrolState.left;
                    }
                    else if (currentState == MovementState.moveRight)
                    {
                        patrolState = PatrolState.right;
                    }
                    else if (patrolState == PatrolState.left)
                    {
                        currentState = MovementState.moveLeft;
                    }
                    else if (patrolState == PatrolState.right)
                    {
                        currentState = MovementState.moveRight;
                    }
                    isChasing = !isChasing;
                }
            }
        }