Example #1
0
        public override void Update(Chunk chunk)
        {
            float dt = Game1.DeltaT;

            Sprite.Update(dt);
            ViewCone.UpdatePosition(Position);
            ViewCone.Update(chunk);
        }
Example #2
0
        public override void Update(Chunk chunk)
        {
            if (FlyingSound == null)
            {
                FlyingSound = Game.SoundEngine.Play("Enemy_DroneFly", Position, 1, true);
            }

            if (PositionKnown > 0)
            {
                --PositionKnown;
            }

            float dt = Game1.DeltaT;

            Velocity = new Vector2();

            switch (State)
            {
            case AIState.Patrolling:
                if (MoveTo(WanderLocation, PatrolSpeed))
                {
                    Wait();
                }
                break;

            case AIState.Searching:
                if (MoveTo(WanderLocation, SearchSpeed))
                {
                    Velocity = new Vector2(0);
                    bool foundSearch = false;
                    for (int i = 0; i < WanderSearchAttempts; ++i)
                    {
                        if (FindWander(TargetLocation, PatrolRange, chunk))
                        {
                            foundSearch = true;
                            break;
                        }
                    }
                    if (!foundSearch)
                    {
                        if (Target(TargetLocation, chunk, AIState.Targeting))
                        {
                            FinishPath = () => { };
                        }
                    }
                }
                break;

            case AIState.CursorySearching:
                if (MoveTo(WanderLocation, PatrolSpeed))
                {
                    Velocity = new Vector2(0);
                    bool foundSearch = false;
                    for (int i = 0; i < WanderSearchAttempts; ++i)
                    {
                        if (FindWander(TargetLocation, PatrolRange, chunk))
                        {
                            foundSearch = true;
                            break;
                        }
                    }
                    if (!foundSearch)
                    {
                        if (Target(TargetLocation, chunk, AIState.Investigating))
                        {
                            FinishPath = () => { };
                        }
                    }
                }

                StateTimer -= dt;
                if (StateTimer <= 0)
                {
                    Return(chunk);
                }
                break;

            case AIState.Waiting:
                StateTimer += dt;
                if (StateTimer <= 0.05F * WaitTime)
                {
                }
                if (StateTimer <= 0.25F * WaitTime)
                {
                    Direction += dt * WaitAngularVelocity;
                }
                else if (StateTimer <= 0.3F * WaitTime)
                {
                }
                else if (StateTimer <= 0.7F * WaitTime)
                {
                    Direction -= dt * WaitAngularVelocity;
                }
                else if (StateTimer <= 0.75F * WaitTime)
                {
                }
                else if (StateTimer <= 0.95F * WaitTime)
                {
                    Direction += dt * WaitAngularVelocity;
                }
                else if (StateTimer <= WaitTime)
                {
                }
                else
                {
                    bool found = false;
                    for (int i = 0; i < WanderSearchAttempts; ++i)
                    {
                        if (FindWander(Spawn, PatrolRange, chunk))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        Return(chunk);
                    }
                    else
                    {
                        State = AIState.Patrolling;
                    }
                }
                break;

            case AIState.Targeting:
                Vector2 nextPos = NextNode < Path.Count - 1 ? Path[NextNode] : TargetLocation;
                if (MoveTo(nextPos, TargetSpeed))
                {
                    Velocity = new Vector2();
                    ++NextNode;

                    nextPos = NextNode < Path.Count - 1 ? Path[NextNode] : TargetLocation;

                    if (NextNode >= Path.Count)
                    {
                        Search(nextPos, chunk, float.PositiveInfinity);
                    }
                    else
                    {
                        MoveTo(nextPos, TargetSpeed);
                    }
                }
                break;

            case AIState.Investigating:
                if (MoveTo(Path[NextNode], SearchSpeed))
                {
                    Velocity = new Vector2();
                    ++NextNode;
                    if (NextNode >= Path.Count)
                    {
                        Search(Path[NextNode - 1], chunk, SearchTime, true);
                    }
                    else
                    {
                        MoveTo(Path[NextNode], SearchSpeed);
                    }
                }
                break;

            case AIState.Returning:
                if (MoveTo(Path[NextNode], PatrolSpeed))
                {
                    ++NextNode;
                    if (NextNode >= Path.Count)
                    {
                        Wait();
                    }
                }
                break;
            }

            if (Pathfinding != null)
            {
                if (Pathfinding.IsCompleted)
                {
                    var newPath = Pathfinding.Result;

                    Pathfinding.Dispose();
                    Pathfinding = null;

                    if (newPath.Count > 1)
                    {
                        Path = newPath;

                        TargetLocation = Path.Last();

                        NextNode = 1;
                    }

                    State = NextState;

                    FinishPath.Invoke();
                }
            }

            { // Kill if touched.
                if (!chunk.Level.Player.IsHiding &&
                    chunk.Level.Player.DeathTimer <= 0 &&
                    GetBoundingBox().Intersects(chunk.Level.Player.GetBoundingBox()))
                {
                    chunk.Level.Player.Kill();
                }
            }

            if (State == AIState.Targeting || State == AIState.Searching)
            {
                Sprite.Play("chase");
            }
            else
            {
                Sprite.Play("idle");
            }

            Position += dt * Velocity;
            ViewCone.UpdatePosition(Position);
            ViewCone.Middle = Direction;
            ViewCone.Update(chunk);

            AlertSignal.Update(dt);
            Sprite.Update(dt);

            FlyingSound.Position = Position;

            base.Update(chunk);
        }