Esempio n. 1
0
        //Vector2 debugHitLocation = new Vector2();

        public override void TickAI(float delta)
        {
            Player player = Player;

            if (player == null)
            {
                return;
            }

            Movable locomotion = Locomotion;
            bool    move       = false;

            switch (state)
            {
            case ChaseAIState.Idle:
            {
                //Wait a while until deciding what to do next.
                TickDownTime(delta);

                if (timeAccumulator <= 0)
                {
                    if (locomotion.CanSeeTarget(player, Controller.searchRadius))
                    {
                        target          = player;
                        state           = ChaseAIState.Chasing;
                        timeAccumulator = Controller.chaseTime;
                    }
                    else
                    {
                        //Random chance of either continue idling or start wandering.
                        if (GenRandom.Bool())
                        {
                            //Wander.
                            state = ChaseAIState.Wandering;

                            //Pick a random spot to wander to.
                            int     randomDirection       = GenRandom.Integer(GenDirection.EightWay.Length);
                            Vector2 randomDirectionVector = GenDirection.EightWay[randomDirection];
                            Vector2 randomLocationVector  = GenMath.OffsetInDirectionVector(locomotion.GlobalPosition, randomDirectionVector, Controller.wanderDistance);
                            //Vector2 randomLocationVector = locomotion.Position + (randomDirectionVector.Normalized() * Controller.wanderDistance);

                            bool    rayHit       = false;
                            Vector2 rayHitVector = GenPhysics.SimpleRayCast(locomotion, locomotion.GlobalPosition, randomLocationVector, out rayHit);
                            //debugHitLocation = rayHitVector;
                            if (!rayHit)
                            {
                                target = randomLocationVector;
                            }
                            else
                            {
                                target = rayHitVector;
                            }
                        }
                        else
                        {
                            timeAccumulator = Controller.idleTime;
                        }
                    }
                }
            }
            break;

            case ChaseAIState.Chasing:
            {
                //Chase!
                ChasePlayer(delta);
                move = true;

                if (locomotion.CanSeeTarget(player, Controller.searchRadius))
                {
                    //Reset chase time
                    timeAccumulator = Controller.chaseTime;
                }
                else
                {
                    //Go back to idling after time passed.
                    TickDownTime(delta);

                    if (timeAccumulator <= 0)
                    {
                        SwitchToIdleState();
                    }
                }
            }
            break;

            case ChaseAIState.Wandering:
            {
                if (locomotion.hitObstacle)
                {
                    SwitchToIdleState();
                }
                else
                {
                    //Stop wandering if nearby the wandering location.
                    if (locomotion.GlobalPosition.DistanceTo(target.Location) <= 16f)
                    {
                        SwitchToIdleState();
                    }
                    else
                    {
                        MoveToward(target.Location, Controller.movementSpeed * 0.5f);
                        move = true;
                    }
                }
            }
            break;
            }

            if (!move)
            {
                StopLocomotion();
            }
        }