Beispiel #1
0
        public override void Update(GameTime gameTime)
        {
            Position.X += Velocity.X;
            Position.Y += Velocity.Y;
            Hitbox.X    = (int)Position.X + HitboxDisplacement;
            Hitbox.Y    = (int)Position.Y;
            if (focusedPlayer == null)
            {
                FacingLeft = true;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X < Hitbox.Center.X)
            {
                FacingLeft = true;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X > Hitbox.Center.X)
            {
                FacingLeft = false;
            }

            if (FacingLeft)
            {
                AttackHitbox.X = (int)(Hitbox.X - AttackRange.X);
            }
            else
            {
                // 72 is the width that the image actually takes up in the frame
                AttackHitbox.X = (int)Position.X + HitboxDisplacement + 72;
            }
            AttackHitbox.Y = Hitbox.Y;
            CurrentAnimation.Update(gameTime);
            if (EnemyState != EnemyStates.Damaged)
            {
                ProcessDamage();
            }
            switch (EnemyState)
            {
            case EnemyStates.Idle:
                foreach (Player player in PlayerManager.Players)
                {
                    if (Math.Abs(player.Character.Hitbox.Center.X - Hitbox.Center.X) <= 200 &&
                        Math.Abs(player.Character.Hitbox.Center.Y - player.Character.Hitbox.Center.Y) <= 200)
                    {
                        focusedPlayer = player;
                        //animation will be changed
                        //animatedSprite.SelectAnimation(4, 1);
                        EnemyState = EnemyStates.SeePlayer;
                    }
                }
                break;

            case EnemyStates.SeePlayer:
                //input delay from SeePlayer state to Chase state
                StateTimer += 1;
                if (StateTimer >= 120)
                {
                    //animatedSprite.SelectAnimation(3, 1);
                    EnemyState = EnemyStates.Chase;
                }
                break;

            case EnemyStates.Chase:
                float distance = float.MaxValue;
                foreach (Player player in PlayerManager.Players)
                {
                    float playerDistance = Vector2.Distance(player.Character.Position, Position);
                    if (distance > playerDistance)
                    {
                        distance      = playerDistance;
                        focusedPlayer = player;
                    }
                }

                if (AttackHitbox.Intersects(focusedPlayer.Character.Hitbox))
                {
                    Velocity.X = 0;
                    Velocity.Y = 0;
                    //animatedSprite.SelectAnimation(1, 2);
                    EnemyState = EnemyStates.Attack;
                    break;
                }

                if (FacingLeft && AttackHitbox.Left >= focusedPlayer.Character.Hitbox.Right)
                {
                    Velocity.X = -2;
                }
                else if (!FacingLeft && AttackHitbox.Right <= focusedPlayer.Character.Hitbox.Left)
                {
                    Velocity.X = 2;
                }

                if (AttackHitbox.Top >= focusedPlayer.Character.Hitbox.Bottom)
                {
                    Velocity.Y = -2;
                    if (Position.Y == focusedPlayer.Character.Position.Y)
                    {
                        Velocity.Y = 0;
                    }
                }
                else if (AttackHitbox.Bottom <= focusedPlayer.Character.Hitbox.Top)
                {
                    Velocity.Y = 2;
                    if (Position.Y == focusedPlayer.Character.Position.Y)
                    {
                        Velocity.Y = 0;
                    }
                }
                break;

            case EnemyStates.Attack:
                if (!AttackHitbox.Intersects(focusedPlayer.Character.Hitbox))
                {
                    //animatedSprite.SelectAnimation(3, 1);
                    EnemyState = EnemyStates.Chase;
                }
                else
                {
                    // attack here
                }

                break;

            /*case EnemyStates.Swat:
             *  if ( )
             *  {
             *      ball.SourcePosition = Position;
             *          ball.DestinationPosition = PlayerManager.GetPlayer(Player.PlayerIndex).Target.Position;
             *          ball.Velocity.X = (PlayerManager.GetPlayer(Player.PlayerIndex.get()).Target.Position.X - Position.X) / 60;
             *          ball.Velocity.Y = -(PlayerManager.GetPlayer(Player.PlayerIndex).Target.Position.Y - .5F * 60 * 60 / 2 - Position.Y) / 60;
             *  }
             *
             *  break;*/

            case EnemyStates.Retreat:
                /* if( Health <  30)
                 * {
                 *   velocity of enemy is opposite direction of focusedPlayer
                 * }*/
                break;

            case EnemyStates.Damaged:
                StateTimer += (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                if (FacingLeft)
                {
                    Position.X += 5;
                }
                else
                {
                    Position.X -= 5;
                }
                if (StateTimer > 20)
                {
                    StateTimer = 0;
                    //animatedSprite.SelectAnimation(3, 1);
                    EnemyState = EnemyStates.Chase;
                }
                break;

            case EnemyStates.Dead:
                //input animation
                //animatedSprite.SelectAnimation(x, x);
                MarkForDelete = true;
                break;

                /* case CharacterStates.DefaultState:
                 *  ProcessMovement(Speed);
                 *   if (InputManager.GetCharacterActionState(PlayerIndex, CharacterActions.Pass) == ActionStates.Pressed
                 *       && PlayerManager.Players.Count > 1 && HasBall)
                 *   {
                 *       CharacterState = CharacterStates.PassState;
                 *   }
                 *   break;
                 */
            }
        }
Beispiel #2
0
        public override void Update(GameTime gameTime)
        {
            CurrentAnimation.Update(gameTime);
            if (!(EnemyState == EnemyStates.Damaged || EnemyState == EnemyStates.Dead))
            {
                ProcessDamage();
                Position.X += Velocity.X;
                Position.Y += Velocity.Y;
            }
            foreach (Player player in PlayerManager.Players)
            {
                if (player.Character.PickAggroBox.Intersects(Hitbox) && player.Character.Defending)
                {
                    focusedPlayer = player;
                }
            }
            if (focusedPlayer == null)
            {
                FacingLeft = false;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X < Hitbox.Center.X)
            {
                FacingLeft = false;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X > Hitbox.Center.X)
            {
                FacingLeft = true;
            }

            if (!FacingLeft)
            {
                AttackHitbox.X = (int)(Hitbox.X - AttackRange.X);
            }
            else
            {
                AttackHitbox.X = (int)Position.X + HitboxXDisplacement + Hitbox.Width;
            }
            AttackHitbox.Y = (int)Hitbox.Y;
            Hitbox.X       = (int)Position.X + HitboxXDisplacement;
            Hitbox.Y       = (int)Position.Y + HitboxYDisplacement - (int)PositionZ;
            switch (EnemyState)
            {
            case EnemyStates.Idle:
                foreach (Player player in PlayerManager.Players)
                {
                    if (Vector2.Distance(new Vector2(player.Character.Hitbox.Center.X, player.Character.Hitbox.Center.Y), new Vector2(Hitbox.Center.X, Hitbox.Center.Y)) <= 300)
                    {
                        focusedPlayer = player;
                        SelectAnimation(RunningAnimation);
                        EnemyState = EnemyStates.Chase;
                    }
                }
                break;

            case EnemyStates.Chase:
                float distance = float.MaxValue;
                foreach (Player player in PlayerManager.Players)
                {
                    if (player.Character.PickAggroBox.Intersects(Hitbox) && player.Character.Defending)
                    {
                        focusedPlayer = player;
                        break;
                    }

                    float playerDistance = Vector2.Distance(player.Character.Position, Position);
                    if (distance > playerDistance)
                    {
                        distance      = playerDistance;
                        focusedPlayer = player;
                    }
                }

                if (AttackHitbox.Intersects(focusedPlayer.Character.Hitbox) && focusedPlayer.Character.Hitbox.Bottom > Hitbox.Bottom - 50 &&
                    focusedPlayer.Character.Hitbox.Bottom < Hitbox.Bottom + 50)
                {
                    Velocity.X = 0;
                    Velocity.Y = 0;
                    SelectAnimation(StandingAnimation);
                    EnemyState = EnemyStates.Attack;
                    break;
                }

                if ((FacingLeft && AttackHitbox.Left >= focusedPlayer.Character.Hitbox.Right) ||
                    (!FacingLeft && AttackHitbox.Left >= focusedPlayer.Character.Hitbox.Right))
                {
                    Velocity.X = -2;
                }
                else if ((!FacingLeft && AttackHitbox.Right <= focusedPlayer.Character.Hitbox.Left) ||
                         (FacingLeft && AttackHitbox.Right <= focusedPlayer.Character.Hitbox.Left))
                {
                    Velocity.X = 2;
                }

                if (Hitbox.Bottom >= focusedPlayer.Character.Hitbox.Bottom + 50)
                {
                    Velocity.Y = -2;
                }
                else if (Hitbox.Bottom <= focusedPlayer.Character.Hitbox.Bottom - 50)
                {
                    Velocity.Y = 2;
                }
                break;

            case EnemyStates.Attack:
                if (!AttackHitbox.Intersects(focusedPlayer.Character.Hitbox))
                {
                    SelectAnimation(RunningAnimation);
                    EnemyState = EnemyStates.Chase;
                }
                else
                {
                    AttackWaitTime -= (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                    if (AttackWaitTime <= 0)
                    {
                        Random rnd = new Random();
                        AttackWaitTime = rnd.Next(30, 90);
                        SelectAnimation(AttackingAnimation);
                        EnemyState = EnemyStates.Attacking;
                    }
                }
                break;

            case EnemyStates.Attacking:
                StateTimer += (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                if (StateTimer == 20)
                {
                    AttackVisible = true;
                }
                else
                {
                    AttackVisible = false;
                }
                if (StateTimer > AttackingAnimation.NumFrames * AttackingAnimation.Frequency)
                {
                    StateTimer = 0;
                    SelectAnimation(StandingAnimation);
                    EnemyState = EnemyStates.Attack;
                }
                break;

            case EnemyStates.SpecialAttack:

                break;

            case EnemyStates.Retreat:
                /* if( Health <  30)
                 * {
                 *   velocity of enemy is opposite direction of focusedPlayer
                 * }*/
                break;

            case EnemyStates.Damaged:
                StateTimer += (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                if (StateTimer > DamagedAnimation.NumFrames * DamagedAnimation.Frequency)
                {
                    StateTimer = 0;
                    SelectAnimation(StandingAnimation);
                    EnemyState = EnemyStates.Chase;
                }
                break;

            case EnemyStates.Dead:
                StateTimer += 1;
                if (StateTimer > DeathAnimation.NumFrames * DeathAnimation.Frequency - 1)
                {
                    StateTimer    = 0;
                    MarkForDelete = true;
                }
                break;
            }
        }
Beispiel #3
0
        public override void Update(GameTime gameTime)
        {
            CurrentAnimation.Update(gameTime);
            if (!(EnemyState == EnemyStates.Damaged || EnemyState == EnemyStates.Dead))
            {
                ProcessDamage();
            }
            foreach (Player player in PlayerManager.Players)
            {
                if (player.Character.PickAggroBox.Intersects(Hitbox) && player.Character.Defending)
                {
                    focusedPlayer = player;
                }
            }
            if (focusedPlayer == null)
            {
                FacingLeft = true;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X < Hitbox.Center.X)
            {
                FacingLeft = true;
            }
            else if (focusedPlayer.Character.Hitbox.Center.X > Hitbox.Center.X)
            {
                FacingLeft = false;
            }

            Hitbox.X = (int)Position.X + HitboxXDisplacement;
            Hitbox.Y = (int)Position.Y + HitboxYDisplacement - (int)PositionZ;
            if (FacingLeft)
            {
                AttackHitbox.X = (int)(Hitbox.X - AttackRange.X);
            }
            else
            {
                AttackHitbox.X = (int)Position.X + HitboxXDisplacement + Hitbox.Width;
            }
            AttackHitbox.Y = (int)Position.Y + 35 * (int)scale;
            switch (EnemyState)
            {
            case EnemyStates.Chase:
                if (Velocity.X != 0 || Velocity.Y != 0)
                {
                    inGoodPositionTimer = 0;
                    SelectAnimation(RunningAnimation);
                }
                else if (inGoodPositionTimer <= 3)
                {
                    inGoodPositionTimer++;
                }
                else
                {
                    SelectAnimation(StandingAnimation);
                }
                if (waitTime > 0)
                {
                    waitTime--;
                    return;
                }
                if (Position.X + Velocity.X >= 0 && Position.X + Hitbox.Width + Velocity.X <= 1280)
                {
                    Position.X += Velocity.X;
                }
                if (Position.Y + Hitbox.Height + Velocity.Y >= 720 / 2 && Position.Y + Hitbox.Height + Velocity.Y <= 720)
                {
                    Position.Y += Velocity.Y;
                }
                float distance = float.MaxValue;
                foreach (Player player in PlayerManager.Players)
                {
                    if (player.Character.PickAggroBox.Intersects(Hitbox) && player.Character.Defending)
                    {
                        focusedPlayer = player;
                        break;
                    }

                    float playerDistance = Vector2.Distance(player.Character.Position, Position);
                    if (distance > playerDistance)
                    {
                        distance      = playerDistance;
                        focusedPlayer = player;
                    }
                }

                if (StateTimer > 0)
                {
                    StateTimer -= 1;
                    if (StateTimer <= 0)
                    {
                        waitFlag = true;
                    }
                    MoveTowardsPlayer(Speed);
                }
                else if (attackFlag)
                {
                    if (AttackHitbox.Intersects(focusedPlayer.Character.Hitbox) && focusedPlayer.Character.Hitbox.Bottom >= Hitbox.Bottom - 50 &&
                        focusedPlayer.Character.Hitbox.Bottom < Hitbox.Bottom + 50)
                    {
                        Velocity.X = 0;
                        Velocity.Y = 0;
                        attackFlag = false;
                        EnemyState = EnemyStates.Attack;
                        break;
                    }
                    MoveTowardsPlayer(Speed);
                }
                else if (waitFlag)
                {
                    waitTime   = GetRandomInt(80, 160);
                    Velocity.X = 0;
                    Velocity.Y = 0;
                    waitFlag   = false;
                    return;
                }
                else if (Vector2.Distance(Position, focusedPlayer.Character.Position) <= surroundDistance)
                {
                    if (GetRandomInt(0, 120) == 1)
                    {
                        attackFlag = true;
                    }
                    else
                    {
                        bool    enemyTooClose      = false;
                        Vector2 distanceFromPlayer = new Vector2(Position.X - focusedPlayer.Character.Position.X, Position.Y - focusedPlayer.Character.Position.Y);
                        float   xSpeedFactor       = (surroundDistance - distanceFromPlayer.X) / (surroundDistance * 2);
                        float   ySpeedFactor       = (surroundDistance - distanceFromPlayer.Y) / (surroundDistance * 2);
                        foreach (Enemy enemy in PlayerManager.EnemyManager.Enemies)
                        {
                            if (!enemy.Equals(this) && Vector2.Distance(enemy.Position, Position) <= 200)
                            {
                                enemyTooClose = true;
                                if ((distanceFromPlayer.X > 0 && distanceFromPlayer.Y > 0) ||
                                    (distanceFromPlayer.X < 0 && distanceFromPlayer.Y < 0))
                                {
                                    Velocity.Y = Math.Sign(Position.Y - enemy.Position.Y) * Speed * ySpeedFactor;
                                    Velocity.X = Math.Sign(Velocity.Y) * Speed * xSpeedFactor;
                                }
                                else if ((distanceFromPlayer.X > 0 && distanceFromPlayer.Y < 0) ||
                                         (distanceFromPlayer.X < 0 && distanceFromPlayer.Y > 0))
                                {
                                    Velocity.Y = Math.Sign(Position.Y - enemy.Position.Y) * Speed * ySpeedFactor;
                                    Velocity.X = -Math.Sign(Velocity.Y) * Speed * xSpeedFactor;
                                }
                                else if (distanceFromPlayer.X == 0)
                                {
                                    if (GetRandomInt(0, 1) == 1)
                                    {
                                        Velocity.X = -Speed * xSpeedFactor;
                                    }
                                    else
                                    {
                                        Velocity.X = Speed * xSpeedFactor;
                                    }
                                }
                                else if (distanceFromPlayer.Y == 0)
                                {
                                    if (GetRandomInt(0, 1) == 1)
                                    {
                                        Velocity.Y = -Speed * ySpeedFactor;
                                    }
                                    else
                                    {
                                        Velocity.Y = Speed * ySpeedFactor;
                                    }
                                }
                                break;
                            }
                        }
                        if (!enemyTooClose)
                        {
                            if (Vector2.Distance(Position, focusedPlayer.Character.Position) <= surroundDistance / 2)
                            {
                                Velocity.X = Math.Sign(distanceFromPlayer.X) * Speed * xSpeedFactor;
                                Velocity.Y = Math.Sign(distanceFromPlayer.Y) * Speed * ySpeedFactor;
                            }
                            else
                            {
                                inGoodPositionTimer++;
                                Velocity.X = 0;
                                Velocity.Y = 0;
                            }
                        }
                    }
                }
                else if (AttackHitbox.Intersects(focusedPlayer.Character.Hitbox) && focusedPlayer.Character.Hitbox.Bottom >= Hitbox.Bottom - 50 &&
                         focusedPlayer.Character.Hitbox.Bottom < Hitbox.Bottom + 50)
                {
                    Velocity.X = 0;
                    Velocity.Y = 0;
                    attackFlag = false;
                    EnemyState = EnemyStates.Attack;
                    break;
                }
                else
                {
                    StateTimer = GetRandomInt(45, 90);
                }
                break;

            case EnemyStates.Attack:
                if (!AttackHitbox.Intersects(focusedPlayer.Character.Hitbox))
                {
                    EnemyState = EnemyStates.Chase;
                }
                else
                {
                    AttackWaitTime -= (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                    if (AttackWaitTime <= 0)
                    {
                        AttackWaitTime = PlayerManager.EnemyManager.RandomGen.Next(30, 90);
                        SelectAnimation(AttackingAnimation);
                        EnemyState = EnemyStates.Attacking;
                    }
                }
                break;

            case EnemyStates.Attacking:
                StateTimer += (int)Math.Ceiling(gameTime.ElapsedGameTime.TotalSeconds * 60F);
                if (StateTimer == (AttackingAnimation.NumFrames - 1) * AttackingAnimation.Frequency + 2)
                {
                    AttackVisible = true;
                }
                else
                {
                    AttackVisible = false;
                }
                if (StateTimer > AttackingAnimation.NumFrames * AttackingAnimation.Frequency)
                {
                    StateTimer = 0;
                    if (GetRandomInt(0, 60) == 1)
                    {
                        SelectAnimation(StandingAnimation);
                        EnemyState = EnemyStates.Attack;
                    }
                    {
                        EnemyState = EnemyStates.Chase;
                    }
                }
                break;

            case EnemyStates.SpecialAttack:

                break;

            case EnemyStates.Retreat:
                /* if( Health <  30)
                 * {
                 *   velocity of enemy is opposite direction of focusedPlayer
                 * }*/
                break;

            case EnemyStates.Damaged:
                StateTimer += 1;
                if (StateTimer > DamagedAnimation.NumFrames * DamagedAnimation.Frequency)
                {
                    StateTimer = 0;
                    EnemyState = EnemyStates.Chase;
                }
                break;

            case EnemyStates.Dead:
                StateTimer += 1;
                if (StateTimer > DeathAnimation.NumFrames * DeathAnimation.Frequency - 1)
                {
                    StateTimer    = 0;
                    MarkForDelete = true;
                }
                break;
            }
        }