예제 #1
0
        /// <summary>
        /// Constructor for pulling enemies from level map coordinator
        /// </summary>
        /// <param name="type"> Type of Enemy [Stationary = type from Egg / Alive = Following with a*] </param>
        /// <param name="visionStandard"> determines how far away the enemy can "see"  </param>
        public Enemy(EnemyType type, Rectangle hitbox, Texture2D defaultSprite, int visionStandard)
        {
            hitpoints          = 3;
            verticalVelocity   = 0;
            horizontalVelocity = 0;

            this.hitbox = hitbox;


            Type = type;

            this.defaultSprite  = defaultSprite;
            this.visionStandard = visionStandard;
            UpdateEnemyVision();

            // Starts the enemy as inactive and not being drawn.
            isActive         = false;
            isDrawn          = false;
            isFacingRight    = false;
            bottomIntersects = false;
            topIntersects    = false;
            canJump          = true;
            goingDown        = false;

            // Enemy is facing left
            isFacingRight = false;
            enemyState    = EnemyState.IdleLeft;
            walkingState  = EnemyWalkingState.Waiting;
        }
예제 #2
0
        /// <summary>
        /// Updates the Enemy Appropriately
        /// </summary>
        /// <param name="pM"></param>
        public void UpdateEnemy(Player p1, Player p2, Vector2 target)
        {
            this.target          = target;
            previousPosition     = position;
            previousHitbox       = hitbox;
            previousEnemyState   = enemyState;
            previousWalkingState = walkingState;

            position = new Vector2(X, Y);


            if (previousPosition.Y < position.Y) //player is going downwards relative to last frame
            {
                goingDown = true;
            }
            else //the player is standing normally or going upwards
            {
                goingDown = false;
            }

            // Sets Collision Boxes

            #region COLLISIONBOXES

            if (horizontalVelocity > 0)
            {
                //X is right of player, Y is the same as player, width depends on horizontalVelocity, height is same as player
                sideChecker = new Rectangle(X + hitbox.Width, Y + 10, Math.Abs(horizontalVelocity), hitbox.Height - 20);
            }
            //Facing left
            else if (horizontalVelocity < 0)
            {
                //X is same as player (which is left edge), Y is the same as player
                //width depends on horizontalVelocity, height is same as player
                sideChecker = new Rectangle(X - Math.Abs(horizontalVelocity), Y + 10, Math.Abs(horizontalVelocity), hitbox.Height - 20);
            }
            else
            {
                if (isFacingRight)
                {
                    sideChecker = new Rectangle(X + hitbox.Width, Y + 10, Math.Abs(horizontalVelocity), hitbox.Height - 20);
                }
                else
                {
                    sideChecker = new Rectangle(X - Math.Abs(horizontalVelocity), Y + 10, Math.Abs(horizontalVelocity), hitbox.Height - 20);
                }
            }

            //height is player height with vertical velocity added on (subtracting makes the height go "up" aka toward the ceiling)
            if (verticalVelocity <= 0)
            {
                topChecker = new Rectangle(X + 10, Y - Math.Abs(verticalVelocity), hitbox.Width - 20, Math.Abs(verticalVelocity));
            }

            else
            {
                topChecker = new Rectangle(X + 10, Y, hitbox.Width - 20, 0);
            }

            bottomChecker = new Rectangle(X + 10, Y + hitbox.Height, hitbox.Width - 20, Math.Abs(verticalVelocity));

            // Double check Figure out this condition
            if (verticalVelocity == 0 && goingDown == true)
            {
                bottomChecker = new Rectangle(X + 10, Y + hitbox.Height, hitbox.Width - 20, 1);
            }

            #endregion COLLISIONBOXES

            if (bottomIntersects)
            {
                canJump = true;
            }

            // Decides how the enemy will move

            enemyState = DecideState(target);

            #region FSM

            switch (enemyState)
            {
            case EnemyState.IdleLeft:
                isFacingRight = false;
                FollowMovementLogic();

                break;

            case EnemyState.IdleRight:
                isFacingRight = true;
                FollowMovementLogic();

                break;

            case EnemyState.WalkLeft:
                isFacingRight = false;
                FollowMovementLogic();

                break;

            case EnemyState.WalkRight:
                isFacingRight = true;
                FollowMovementLogic();

                break;

            case EnemyState.JumpLeft:
                isFacingRight = false;
                FollowMovementLogic();

                break;

            case EnemyState.JumpRight:
                isFacingRight = true;
                FollowMovementLogic();

                break;

            case EnemyState.Fall:
                isFacingRight = false;
                FollowMovementLogic();

                break;
            }

            #endregion FSM
        }