Example #1
0
        //Creates a key object
        protected AttackKey CreateKey(Keys keyType)
        {
            AttackKey keyToReturn = null;

            //Creates the appropriate type of key based on the integer supplied
            if (keyType != Keys.None)
            {
                switch (keyType)
                {
                case Keys.Up:
                    attackSounds[0].Play();
                    keyToReturn = new AttackKey(
                        UpArrow,
                        890,
                        375,
                        keyType,
                        keySpeed);
                    break;

                case Keys.Down:
                    attackSounds[1].Play();
                    keyToReturn = new AttackKey(
                        DownArrow,
                        890,
                        375,
                        keyType,
                        keySpeed);
                    break;

                case Keys.Left:
                    attackSounds[2].Play();
                    keyToReturn = new AttackKey(
                        LeftArrow,
                        890,
                        375,
                        keyType,
                        keySpeed);
                    break;

                case Keys.Right:
                    attackSounds[3].Play();
                    keyToReturn = new AttackKey(
                        RightArrow,
                        890,
                        375,
                        keyType,
                        keySpeed);
                    break;

                default:
                    break;
                }

                /*keyToReturn = new AttackKey(
                 *  keyTexture,
                 *  new Rectangle(1200, 400, keyTexture.Width, keyTexture.Height ),
                 *  keyType,
                 *  keySpeed);*/
            }

            return(keyToReturn);
        }
Example #2
0
        //Methods
        public void Update(GameTime gameTime)
        {
            //Keybaord state
            pbState = kbState;
            kbState = Keyboard.GetState();

            //Blend timer for player flashing red when damaged
            if (blendTimer > 0)
            {
                blendTimer -= 1;
            }
            if (blendTimer == 0 && player.BlendColor == Color.Red)
            {
                player.BlendColor = Color.White;
            }

            //Update logic based on current game state
            switch (battleState)
            {
            case BattleState.Idle:
                //Waits for the player to pick fight or runaway
                //Player picks fight, change state
                timerOriginal++;
                if (timerOriginal >= 10)
                {
                    //Update the arrow position to decide which choice the user in highlighting
                    if (Helpers.SingleKeyPress(Keys.Up, pbState, kbState))
                    {
                        //If no choice has been selected yet or the top choice is selcted
                        if (arrowPosition == 1)
                        {
                            arrowPosition = 2;
                        }
                        //Otherwise just move the position up 1
                        else
                        {
                            arrowPosition--;
                        }
                    }
                    else if (Helpers.SingleKeyPress(Keys.Down, pbState, kbState))
                    {
                        //If the bottom choice is selcted reset the position to the top choice.
                        if (arrowPosition == 2)
                        {
                            arrowPosition = 1;
                        }
                        //Otherwise just move the position down 1
                        else
                        {
                            arrowPosition++;
                        }
                    }

                    if (kbState.IsKeyDown(Keys.Enter) && pbState.IsKeyUp(Keys.Enter) && arrowPosition == 1)
                    {
                        battleState   = BattleState.Fight;
                        timerOriginal = 0;
                        failedRun     = false;
                    }
                    else if (kbState.IsKeyDown(Keys.Enter) && arrowPosition == 2 && !IsBoss)
                    {
                        //Check to see if the player successfully ran away
                        bool success = enemy.RunAway(chance, rng);
                        if (success && player.Stamina > 20)
                        {
                            battleState   = BattleState.RanAway;
                            success       = true;
                            timerOriginal = timer;
                        }
                        else
                        {
                            if (player.Stamina > 20)
                            {
                                player.Stamina -= 10;
                            }
                            else
                            {
                                runText = "Not enough stamina to run away!";
                            }

                            failedRun = true;
                        }

                        timerOriginal = 0;
                    }
                }

                break;

            case BattleState.Fight:

                //
                //Processes attacks and damage
                //Timer keeps track of elapsed battle time. Every attack tick, the next attack in the enemy's pattern will be
                //created. The attack pattern will loop by default.
                //


                //Checks the key at the front of the list. Is it intersecting the hitbox?

                if (listKeys.Count != 0 && listKeys[0] != null)
                {
                    AttackKey firstAttack = listKeys[0];

                    if (firstAttack.Position.Intersects(hitbox))
                    {
                        //If it is intersecting the hitbox, we check if the player has pressed the appropriate key
                        if (Helpers.SingleKeyPress(firstAttack.KeyType, pbState, kbState))
                        {
                            //Get the Pressed Keys and save as an array of keys
                            Keys[] keys = kbState.GetPressedKeys();

                            //Find which key should be pressed

                            //If the player has pressed the correct key, then enemy stamina is lowered otherwise player stamina is lowered.
                            if (listKeys[0].KeyType == keys[0] && keys.Length == 1)
                            {
                                if (perfectHitbox.Contains(firstAttack.Position))
                                {
                                    if (IsBoss)
                                    {
                                        boss.Stamina -= 20;
                                        hitCounter   += 2;
                                        if (hitCounter % 5 == 0)
                                        {
                                            attackSounds[5].Play();
                                            bossBar -= 20;
                                        }
                                    }
                                    else
                                    {
                                        attackSounds[5].Play();
                                        enemy.Stamina -= 20;
                                    }

                                    perfectHit = true;

                                    timerOriginal = timer;
                                }
                                else
                                {
                                    if (IsBoss)
                                    {
                                        attackSounds[5].Play();
                                        boss.Stamina -= 10;
                                        hitCounter++;
                                        if (hitCounter % 5 == 0)
                                        {
                                            bossBar -= 20;
                                        }
                                    }
                                    else
                                    {
                                        attackSounds[5].Play();
                                        enemy.Stamina -= 10;
                                    }
                                }
                            }
                            else
                            {
                                player.Stamina   -= 10;
                                player.BlendColor = Color.Red;
                                blendTimer        = 15;
                            }

                            //Either way remove the key
                            listKeys.RemoveAt(0);
                        }
                    }
                    else if (kbState.GetPressedKeys().Length > 0 && pbState.GetPressedKeys().Length == 0)
                    {
                        player.Stamina   -= 5;
                        player.BlendColor = Color.Red;
                        blendTimer        = 15;
                    }
                }

                //
                //Generate new attacks
                //

                timer += 1;

                if (timer % attackTick == 0)
                {
                    //Add a newly-created key to the list

                    if (queueAttacks.Peek() != Keys.None)
                    {
                        listKeys.Add(CreateKey(queueAttacks.Peek()));
                    }

                    //Add first entry to the end of the queue, remove it from the start.
                    //This behaviour is essentially looping an attack pattern.
                    queueAttacks.Enqueue(queueAttacks.Dequeue());
                }

                //
                //Update the keys
                //

                //Remove a key if it is passed the hitbox- and damages the player!
                if (listKeys.Count > 0 && (listKeys[0].Position.X + listKeys[0].Position.X < hitbox.X))
                {
                    listKeys.RemoveAt(0);
                    player.Stamina -= 10;
                    //Play damage effect
                    attackSounds[4].Play();
                    player.BlendColor = Color.Red;
                    blendTimer        = 15;
                }

                //Calls update on each key.
                foreach (AttackKey key in listKeys)
                {
                    if (key != null)
                    {
                        key.Update(gameTime);
                    }
                }


                //Then checks Win/Loss conditions
                if (player.Stamina <= 0)
                {
                    battleState = BattleState.Death;
                }
                else if (IsBoss)
                {
                    if (boss.Stamina <= 0)
                    {
                        battleState   = BattleState.Victory;
                        timerOriginal = timer;
                        VictoryScreen = true;
                    }
                    //If the player is fighting Salsa and Salsa is losing, the fight becomes harder!
                    else if (boss.Stamina <= 100 && keySpeed == 5)
                    {
                        //TODO: Maybe add a sound effect to indicate salsa got stronger?
                        keySpeed      = 7;
                        boss.Stamina += 200;
                    }
                }
                else
                {
                    if (enemy.Stamina <= 0)
                    {
                        battleState   = BattleState.Victory;
                        timerOriginal = timer;
                        VictoryScreen = true;
                    }
                }
                break;

            case BattleState.Death:
                //What happens when the player stamina reaches 0
                if (!GameOver)
                {
                    GameOver = true;
                }
                player.BlendColor = Color.White;
                break;

            case BattleState.Victory:
                //What happens when the enemy stamina reaches  0
                //TODO: Add a battle transition?
                //int temp = timer - timerOriginal;
                timer++;
                if (!Victory && (timer - timerOriginal) >= 200)
                {
                    Victory       = true;
                    VictoryScreen = false;
                    player.X      = player.PrevPos.X;
                    player.Y      = player.PrevPos.Y;
                }
                player.BlendColor = Color.White;
                break;

            case BattleState.RanAway:
                //If the player chooses to run away
                timer++;

                if (!RanAway && (timer - timerOriginal) >= 200)
                {
                    RanAway  = true;
                    player.X = player.PrevPos.X;
                    player.Y = player.PrevPos.Y;
                    enemy.X  = enemy.PrevPos.X;
                    enemy.Y  = enemy.PrevPos.Y;
                    //enemy.Active = false;
                    enemy.Transparent = true;
                    enemy.Timer       = 0;
                    enemy.InBattle    = false;
                    enemy.Top         = true;
                    enemy.Right       = true;
                    enemy.Once        = true;
                }
                player.BlendColor = Color.White;
                break;
            }
        }