Exemplo n.º 1
0
    void Update()
    {
        if (!isDead)
        {
            if (target == null)
            {
                target = GameObject.FindGameObjectWithTag("Player").transform;
                EnemyPatrolState newState = new EnemyPatrolState();
                stateMachine.ChangeState(newState);
            }
            stateMachine.Update();
        }
        else
        {
            // Fade.
            if (rend.material.color.a > 0.0f)
            {
                Color newColor = rend.material.color;
                newColor.a         -= Time.deltaTime * 5.0f;
                rend.material.color = newColor;
            }
            if (!deadParticles.IsAlive())
            {
                Destroy(this.gameObject);
            }
        }

        // Gravity is always present.
        controller.Move(new Vector3(0.0f, -9.8f * Time.deltaTime, 0.0f));
    }
Exemplo n.º 2
0
 void Awake()
 {
     enemyStateMachine = GetComponent <EnemyStateMachine> ();
     navMeshController = GetComponent <NavMeshController> ();
     visionController  = GetComponent <VisionController> ();
     enemyPatrolState  = GetComponent <EnemyPatrolState> ();
 }
Exemplo n.º 3
0
    void Start()
    {
        controller = GetComponent <CharacterController>();

        stateMachine = new FiniteStateMachine <SmartEnemy>();
        stateMachine.Init(this);

        EnemyPatrolState newState = new EnemyPatrolState();

        stateMachine.ChangeState(newState);
    }
Exemplo n.º 4
0
    public override void MakeFSM()
    {
        EnemyPatrolState patrol = new EnemyPatrolState();

        patrol.AddTransition(EntityTransition.TargetSpotted, EntityStateID.Chase);

        EnemyChaseState chase = new EnemyChaseState();

        chase.AddTransition(EntityTransition.LostTarget, EntityStateID.Patrol);

        fsm = new EntityFSMSystem();
        fsm.AddState(patrol);
        fsm.AddState(chase);
    }
Exemplo n.º 5
0
    private void Start()
    {
        EnemyFollowState followState = new EnemyFollowState();

        followState.onFixedUpdateMethod += onUpdateFollowState;
        EnemyPatrolState patrolState = new EnemyPatrolState();

        patrolState.onFixedUpdateMethod += onUpdatePatrolState;

        fsmSystem = new FsmSystem();
        fsmSystem.AddState(followState);
        fsmSystem.AddState(patrolState);
        fsmSystem.ChangeState(StateType.EnemyPatrol);

        enemy  = this.gameObject.GetComponent <EnemyCharacter>();
        player = GameObject.FindGameObjectWithTag("Player");
    }
Exemplo n.º 6
0
    public override void Execute()
    {
        if (Time.time > searchTime)
        {
            EnemyPatrolState newState = new EnemyPatrolState();
            entity.stateMachine.ChangeState(newState);
        }

        if (!positionReached)
        {
            // Move to the last position where the target was seen...
            entity.Move(lastTargetPosition - entity.transform.position, entity.chaseSpeed);
            if (Vector3.Distance(lastTargetPosition, entity.transform.position) < 1.0f)
            {
                positionReached   = true;
                directionToRotate = lastTargetDirection;
                timeToRotate      = Time.time + 1.0f;
            }
        }
        else
        {
            // ... and look around from that position.
            if (Time.time > timeToRotate)
            {
                directionToRotate = Quaternion.Euler(0.0f, (Random.value > 0.5f) ? 90.0f : -90.0f, 0.0f) * entity.transform.forward;
                timeToRotate      = Time.time + 2.0f;
            }

            entity.Move(directionToRotate, entity.chaseSpeed * 0.35f);
        }

        if (Time.time > timeToCheck)
        {
            if (entity.LookForTarget() == true)
            {
                EnemyChaseState newState = new EnemyChaseState();
                entity.stateMachine.ChangeState(newState);
            }
            else
            {
                timeToCheck = Time.time + 0.4f; // Check with a moderated frecuency.
            }
        }
    }
Exemplo n.º 7
0
    private void Update()
    {
        if (!isDead)
        {
            if (target == null)
            {
                target = GameObject.FindGameObjectWithTag("Player").transform;
                EnemyPatrolState newState = new EnemyPatrolState();
                stateMachine.ChangeState(newState);
            }

            stateMachine.Update();

            // If the target has been detected, start attacking.
            if (stateMachine.currentState is EnemyChaseState)
            {
                if (Time.time > nextAttack)
                {
                    Attack();
                    nextAttack = Time.time + attackRate;
                }
            }
        }
        else
        {
            if (mesh.material.color.a > 0.0f)
            {
                Color newColor = mesh.material.color;
                newColor.a         -= Time.deltaTime * 5.0f;
                mesh.material.color = newColor;
            }
            if (!deadParticles.IsAlive())
            {
                Destroy(this.gameObject);
            }
        }

        // Gravity is allways present.
        controller.Move(new Vector3(0.0f, -9.8f * Time.deltaTime, 0.0f));
    }
Exemplo n.º 8
0
 void Awake()
 {
     patrolState = GetComponent <EnemyPatrolState> ();
     actionState = GetComponent <EnemyActionState> ();
 }
Exemplo n.º 9
0
        void EnemyPatrolAI()
        {
            switch (_currentPatrolState)
            {
            case EnemyPatrolState.MOVING_LEFT:
                if (!reachedLeftPoint)
                {
                    transform.position = Vector3.MoveTowards(transform.position, new Vector3(enemyMovePoints[0].position.x, transform.position.y, transform.position.z), enemyWalkSpeed * Time.deltaTime);
                    if (!isMoving)
                    {
                        transform.localScale = new Vector3(-1, 1, 1);

                        isMoving = true;
                    }
                }
                else if (reachedLeftPoint)
                {
                    _currentPatrolState = EnemyPatrolState.IDLE;
                    isMoving            = false;
                }
                break;

            case EnemyPatrolState.MOVING_RIGHT:
                if (!reachedRightPoint)
                {
                    transform.position = Vector3.MoveTowards(transform.position, new Vector3(enemyMovePoints[1].position.x, transform.position.y, transform.position.z), enemyWalkSpeed * Time.deltaTime);

                    if (!isMoving)
                    {
                        transform.localScale = new Vector3(1, 1, 1);

                        isMoving = true;
                    }
                }
                else if (reachedRightPoint)
                {
                    _currentPatrolState = EnemyPatrolState.IDLE;
                    isMoving            = false;
                }
                break;

            case EnemyPatrolState.IDLE:
                if (!isIdling && !isMoving)
                {
                    taskIndexPicked = false;

                    if (!taskIndexPicked)
                    {
                        nextTaskIndex   = Random.Range(0, 10);
                        taskIndexPicked = true;
                    }

                    //Move Left
                    if (nextTaskIndex <= 4)
                    {
                        if (!reachedLeftPoint)
                        {
                            _currentPatrolState = EnemyPatrolState.MOVING_LEFT;
                            taskIndexPicked     = false;
                        }
                    }    //Move Right
                    else if (nextTaskIndex >= 5 && nextTaskIndex <= 8)
                    {
                        if (!reachedRightPoint)
                        {
                            _currentPatrolState = EnemyPatrolState.MOVING_RIGHT;
                            taskIndexPicked     = false;
                        }
                    }    //Idle
                    else if (nextTaskIndex >= 9)
                    {
                        _currentPatrolState = EnemyPatrolState.IDLE;
                        isIdling            = true;
                        StartCoroutine(enemyWait());
                        return;
                    }
                }
                break;
            }
        }