Esempio n. 1
0
        //checks if object is dead
        public void checkHealth(GameObject obj)
        {
            //check if health is finished
            if (healthLeft <= 0)
            {
                this.setMarkedForDestruction(true);
                healthLeft = 0;

                //get points for destroying an enemy/player object
                if (obj is BombObject)
                {
                    BombObject bombObj = (BombObject)obj;
                    if (bombObj.source is Player)
                    {
                        if (bombObj.source.Equals(this))
                        {
                            GameMap.tempscores.Add(this.playerID + " committed suicide.");
                        }
                        else
                        {
                            ((Player)(bombObj).source).score += 15;
                            GameMap.tempscores.Add(((Player)bombObj.source).playerID + ": +15");//(killed an enemy with a bomb!)
                            GameMap.tempscores.Add(this.playerID + " was blown up.");
                        }
                    }
                }
                else if (obj is BullitObject)
                {
                    if (((BullitObject)obj).getSource() is Player)
                    {
                        Player temp = (Player)((BullitObject)obj).getSource();
                        temp.score += 10;
                        GameMap.tempscores.Add(temp.playerID + ": +10");//(killed an enemy with bulltis!)
                        GameMap.tempscores.Add(this.playerID + " was shot.");
                    }
                }
            }
        }
Esempio n. 2
0
        //remove objects within blast areas
        public void bombExplosion(BombObject bombObj)
        {
            int xcoord = (int)((bombObj.getPosition().X+bombObj.getTexture().Width/2) / gridDimensions.X-1);
            int ycoord = (int)(((bombObj.getPosition().Y+bombObj.getTexture().Height/2) / gridDimensions.Y));

            //mapPathData[ycoord, xcoord] = -1;

            //loop through blast area
            for (int x = xcoord - 1; x < xcoord + 2; x++)
            {
                for (int y = ycoord - 1; y < ycoord + 2; y++)
                {
                    //loop through all the objects
                    for (int t = 0; t < MainGameClass.objects.Count; t++)
                    {
                        GameObject temp = MainGameClass.objects[t];
                        int temp_x;
                        int temp_y;

                        //checks if the object being examined is the player object due to the use of sprite sheets
                        if (!(temp is Player) && !(temp is EnemyObject))
                        {
                            temp_x = (int)((temp.getPosition().X + temp.getTexture().Width / 2) / gridDimensions.X);
                            temp_y = (int)((temp.getPosition().Y + temp.getTexture().Height / 2) / gridDimensions.Y);
                        }
                        else
                        {
                            temp_x = (int)(((temp.getPosition().X + (temp.getTexture().Width/temp.numberOfColumns) / 2) / gridDimensions.X));
                            temp_y = (int)((temp.getPosition().Y + (temp.getTexture().Height/temp.numberOfRows) / 2) / gridDimensions.Y);
                        }

                        //if within blast area
                        if (temp_x == x && temp_y == y)
                        {
                            if (!temp.Equals(bombObj) && temp is BombObject)
                            {
                                temp.setMarkedForDestruction(true);
                            }
                            else if (!temp.Equals(bombObj) && temp is EnemyObject)
                            {
                                ((EnemyObject)temp).healthLeft -= 40;
                                ((EnemyObject)temp).checkHealth(bombObj);
                            }
                            else if (!temp.Equals(bombObj) && temp is Player)
                            {
                                ((Player)temp).healthLeft -= 40;
                                ((Player)temp).checkHealth(bombObj);
                            }
                            else if (!temp.Equals(bombObj) && (temp is StationaryObject))
                            {
                                //checks if the block can be destroyed
                                if (((StationaryObject)temp).getState() == StationaryObject.PILLER.CAN_DESTROY)
                                {
                                    destructables.Remove((StationaryObject)temp);
                                    numberOfPointBlocks--;
                                    MainGameClass.objects.Remove(temp);
                                    temp.setMarkedForDestruction(true);

                                    //add points to player
                                    if (bombObj.source is Player)
                                    {
                                        ((Player)bombObj.source).score+=5;
                                        tempscores.Add(((Player)bombObj.source).playerID+ ": +5");
                                    }
                                }
                            }
                            else if(!temp.Equals(bombObj))
                            {
                                MainGameClass.objects.Remove(temp);
                                temp.setMarkedForDestruction(true);

                                //add points to source
                                if (bombObj.source is Player)
                                {
                                    ((Player)bombObj.source).score+=5;
                                    tempscores.Add(((Player)bombObj.source).playerID + ": +5");
                                }
                            }
                        }
                    }//end loop through objects
                }//end loop through rowa
            } //end loop through columns
            bombObj.animationCanStart = true;   //indicates to bomb object that animation can start
        }
Esempio n. 3
0
        //handles Xbox user input TODO
        public void handleXboxControls()
        {
            GamePadState pad1State = GamePad.GetState(PlayerIndex.One);
            GamePadState pad2State = GamePad.GetState(PlayerIndex.Two);

            // Allows the game to exit using XBox
            if (pad1State.Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (stateOfGame == GAMESTATE.StartMenu && startMenuDelay <=0)
            {
                if (pad1State.ThumbSticks.Left.Y <= -0.5)//move down menu
                {
                    if (currentMenuOption < 4)
                        currentMenuOption++;
                }
                if (pad1State.ThumbSticks.Left.Y >= 0.5)//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (pad1State.Buttons.A == ButtonState.Pressed && oldPad1State.Buttons.A == ButtonState.Released)//select a menu option
                {
                    if (currentMenuOption == 1)//start new single player game
                    {
                        startNewSinglePlayerGame();
                        playermode = PLAYERMODE.SinglePlayer;
                        stateOfGame = GAMESTATE.Playing;
                    }
                    if (currentMenuOption == 2)//start new multiplayer game
                    {
                        startNewMultiPlayerGame();
                        playermode = PLAYERMODE.TwoPlayer;
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 3)//llok at xbox controls
                    {
                        stateOfGame = GAMESTATE.Controls;
                        previousState = GAMESTATE.StartMenu;
                    }
                    else if (currentMenuOption == 4)//exit game
                        this.Exit();

                    bombPressDelay1 = 50;
                    bombPressDelay2 = 50;
                    startMenuDelay = 10;
                }
                oldPad1State = pad1State;
            }
            else if (stateOfGame == GAMESTATE.Controls && startMenuDelay <= 0)
            {
                if (pad1State.Buttons.A == ButtonState.Pressed && oldPad1State.Buttons.A == ButtonState.Released)//return to previous menu
                {
                    stateOfGame = previousState;
                    currentMenuOption = 1;
                }
                oldPad1State = pad1State;
                startMenuDelay = 10;
            }
            else if (stateOfGame == GAMESTATE.Playing)
            {
                //handle pause condition
                if (pad1State.Buttons.Start == ButtonState.Pressed && oldPad1State.Buttons.Start == ButtonState.Released)
                {
                    if (stateOfGame == GAMESTATE.Paused)
                    {
                        stateOfGame = GAMESTATE.Playing;
                        pausedTime = System.Environment.TickCount;
                        gamePaused = true;
                    }
                    else if (stateOfGame == GAMESTATE.Playing)
                        stateOfGame = GAMESTATE.Paused;
                    currentMenuOption = 1;
                    bombPressDelay1 = 50;
                    bombPressDelay2 = 50;
                }
                oldPad1State = pad1State;

                int direction1 = 0;
                int direction2 = 0;

                //allow input only if player1 is alive
                if (player1Alive)
                {
                    //for player1
                    if (pad1State.ThumbSticks.Left.Y >= 0.5)//move up
                    {
                        canMove_player1 = true;
                        if (pad1State.ThumbSticks.Left.X <= -0.5)
                            direction1 = -45;
                        else if (pad1State.ThumbSticks.Left.X >= 0.5)
                            direction1 = 45;
                        else
                            direction1 = 0;
                    }
                    else if (pad1State.ThumbSticks.Left.Y <= -0.5)//move down
                    {
                        canMove_player1 = true;
                        if (pad1State.ThumbSticks.Left.X <= -0.5)
                            direction1 = 225;
                        else if (pad1State.ThumbSticks.Left.X >= 0.5)
                            direction1 = 135;
                        else
                            direction1 = 180;
                    }
                    else if (pad1State.ThumbSticks.Left.X <= -0.5)//move left
                    {
                        canMove_player1 = true;
                        direction1 = 270;
                    }
                    else if (pad1State.ThumbSticks.Left.X >= 0.5)//move right
                    {
                        canMove_player1 = true;
                        direction1 = 90;
                    }
                    else
                        canMove_player1 = false;

                    //lay bombs for player1
                    if (pad1State.Buttons.A == ButtonState.Pressed && bombPressDelay1 <= 0)
                    {
                        if (bombCount1 < 2)
                        {
                            BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)player1Obj.getPosition().X - 16, (int)player1Obj.getPosition().Y + 4, 0.5f);
                            temp.animationCanStart = false;
                            temp.source = player1Obj;
                            objects.Add(temp);
                            bombsLayed1.Add(temp);
                            bombCount1++;
                        }
                        bombPressDelay1 = 50;
                    }
                    oldPad1State = pad1State;

                    //player1 fires bullet
                    if (player1Obj.cooldownTimer <= 0)
                    {
                        if (pad1State.Triggers.Right >= 0.6)
                        {
                            fireBullet(player1Obj, 90 + player1Obj.getDirection());
                        }
                    }
                    player1Obj.cooldownTimer--;

                    //sprint
                    if (pad1State.Buttons.B == ButtonState.Pressed)
                        player1Obj.setSprinting(true);
                    else
                        player1Obj.setSprinting(false);

                    //update player1 object's position
                    if (canMove_player1)
                    {
                        //stop player from fallingoff edge of window
                        Vector2 tempPos = new Vector2();
                        tempPos.X = MathHelper.Clamp(player1Obj.getPosition().X, 0, screenWidth - player1Obj.getTexture().Width / player1Obj.numberOfColumns);
                        tempPos.Y = MathHelper.Clamp(player1Obj.getPosition().Y, 0, screenHeight - (player1Obj.getTexture().Height / player1Obj.numberOfRows) - 58);
                        player1Obj.setPosition(tempPos);
                        player1Obj.moveInDirection(direction1);
                    }
                }//player1 is alive

                //allow input only if player2 is alive
                if (player2Alive)
                {
                    //for player2
                    if (pad2State.ThumbSticks.Left.Y >= 0.5)//move up
                    {
                        canMove_player2 = true;
                        if (pad2State.ThumbSticks.Left.X <= -0.5)
                            direction2 = -45;
                        else if (pad2State.ThumbSticks.Left.X >= 0.5)
                            direction2 = 45;
                        else
                            direction2 = 0;
                    }
                    else if (pad2State.ThumbSticks.Left.Y <= -0.5)//move down
                    {
                        canMove_player2 = true;
                        if (pad2State.ThumbSticks.Left.X <= -0.5)
                            direction2 = 225;
                        else if (pad2State.ThumbSticks.Left.X >= 0.5)
                            direction2 = 135;
                        else
                            direction2 = 180;
                    }
                    else if (pad2State.ThumbSticks.Left.X <= -0.5)//move left
                    {
                        canMove_player2 = true;
                        direction2 = 270;
                    }
                    else if (pad2State.ThumbSticks.Left.X >= 0.5)//move right
                    {
                        canMove_player2 = true;
                        direction2 = 90;
                    }
                    else
                        canMove_player2 = false;

                    //lay bombs for player2
                    if (pad2State.Buttons.A == ButtonState.Pressed && bombPressDelay2 <= 0)
                    {
                        if (bombCount2 < 2)
                        {
                            BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)player2Obj.getPosition().X - 16, (int)player2Obj.getPosition().Y + 4, 0.5f);
                            temp.animationCanStart = false;
                            temp.source = player2Obj;
                            objects.Add(temp);
                            bombsLayed2.Add(temp);
                            bombCount2++;
                        }
                        bombPressDelay2 = 100;
                    }
                    oldPad2State = pad2State;

                    //player2 fires bullet
                    if (player2Obj.cooldownTimer <= 0)
                    {
                        if (pad2State.Triggers.Right > 0.5)
                        {
                            fireBullet(player2Obj, 90 + player2Obj.getDirection());
                        }
                    }
                    player2Obj.cooldownTimer--;

                    //sprint
                    if (pad2State.Buttons.B == ButtonState.Pressed)
                        player2Obj.setSprinting(true);
                    else
                        player2Obj.setSprinting(false);

                    //update player2 object's position
                    if (canMove_player2)
                    {
                        //stop player from fallingoff edge of window
                        Vector2 tempPos = new Vector2();
                        tempPos.X = MathHelper.Clamp(player2Obj.getPosition().X, 0, screenWidth - player2Obj.getTexture().Width / player2Obj.numberOfColumns);
                        tempPos.Y = MathHelper.Clamp(player2Obj.getPosition().Y, 0, screenHeight - (player2Obj.getTexture().Height / player2Obj.numberOfRows) - 58);
                        player2Obj.setPosition(tempPos);
                        player2Obj.moveInDirection(direction2);
                    }
                }  //player2 is alive
            }
            else if (stateOfGame == GAMESTATE.Paused && startMenuDelay <= 0)
            {
                if (pad1State.ThumbSticks.Left.Y <= -0.5)//move down menu
                {
                    if (currentMenuOption < 5)
                        currentMenuOption++;
                }
                if (pad1State.ThumbSticks.Left.Y >= 0.5)//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (pad1State.Buttons.A == ButtonState.Pressed && oldPad1State.Buttons.A == ButtonState.Released)//select a menu option
                {
                    if (currentMenuOption == 1)//resume current game
                        stateOfGame = GAMESTATE.Playing;
                    else if (currentMenuOption == 2)
                    {
                        if (playermode == PLAYERMODE.TwoPlayer)//restart current mode of playermode
                            startNewMultiPlayerGame();
                        else
                            startNewSinglePlayerGame();
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 3)//look at xbox controls
                    {
                        stateOfGame = GAMESTATE.Controls;
                        previousState = GAMESTATE.Paused;
                    }
                    else if (currentMenuOption == 4)//exit to main menu
                    {
                        stateOfGame = GAMESTATE.StartMenu;
                        currentMenuOption = 1;
                    }
                    else if (currentMenuOption == 5)//exit game
                        this.Exit();

                    bombPressDelay1 = 50;
                    bombPressDelay2 = 50;
                    startMenuDelay = 10;
                }
                oldPad1State = pad1State;
            }
            else if (startMenuDelay <= 0)//display winning/loosing screen
            {
                if (pad1State.ThumbSticks.Left.Y <= -0.5)//move down menu
                {
                    if (currentMenuOption < 4)
                        currentMenuOption++;
                }
                if (pad1State.ThumbSticks.Left.Y >= 0.5)//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (pad1State.Buttons.A == ButtonState.Pressed && oldPad1State.Buttons.A == ButtonState.Released)//select a menu option
                {
                    if (currentMenuOption == 1)
                    {
                        if (playermode == PLAYERMODE.TwoPlayer)//restart current mode of playermode
                            startNewMultiPlayerGame();
                        else
                            startNewSinglePlayerGame();
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 2)//look at xbox controls
                    {
                        stateOfGame = GAMESTATE.Controls;
                        previousState = GAMESTATE.GameOver;
                    }
                    else if (currentMenuOption == 3)//exit to main menu
                    {
                        stateOfGame = GAMESTATE.StartMenu;
                        currentMenuOption = 1;
                    }
                    else if (currentMenuOption == 4)//exit game
                        this.Exit();

                    bombPressDelay1 = 50;
                    bombPressDelay2 = 50;
                    startMenuDelay = 10;
                }
                oldPad1State = pad1State;
            }
        }
Esempio n. 4
0
        //handles keyboard user input
        public void handleKeyboardControls()
        {
            KeyboardState keyState = Keyboard.GetState();

            //exit game using keyboard
            if (keyState.IsKeyDown(Keys.Escape))
                this.Exit();

            if (stateOfGame == GAMESTATE.StartMenu && startMenuDelay <= 0)
            {
                if (keyState.IsKeyDown(Keys.Down))//move down menu
                {
                    if (currentMenuOption < 4)
                        currentMenuOption++;
                }
                if (keyState.IsKeyDown(Keys.Up))//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (keyState.IsKeyDown(Keys.Enter))//select a menu option
                {
                    if (currentMenuOption == 1)//start new single player game
                    {
                        startNewSinglePlayerGame();
                        playermode = PLAYERMODE.SinglePlayer;
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 2)//start new multiplayer game
                    {
                        startNewMultiPlayerGame();
                        playermode = PLAYERMODE.TwoPlayer;
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 3)//look at xbox controls
                    {
                        previousState = GAMESTATE.StartMenu;
                        stateOfGame = GAMESTATE.Controls;
                    }
                    else if (currentMenuOption == 4)//exit game
                        this.Exit();

                    startMenuDelay = 10;
                }
            }
            else if (stateOfGame == GAMESTATE.Controls && startMenuDelay <= 0)
            {
                if (keyState.IsKeyDown(Keys.Enter))//return to previous menu
                {
                    stateOfGame = previousState;
                    currentMenuOption = 1;
                }
                startMenuDelay = 10;
            }
            else if (stateOfGame == GAMESTATE.Playing)
            {
                handlePauseControl();
                int direction1 = 0;
                int direction2 = 0;

                //allow input only if player1 is alive
                if (player1Alive)
                {
                    //for player1
                    if (keyState.IsKeyDown(Keys.W))//move up
                    {
                        canMove_player1 = true;
                        if (keyState.IsKeyDown(Keys.A) && !(keyState.IsKeyDown(Keys.D)))
                            direction1 = -45;
                        else if (keyState.IsKeyDown(Keys.D) && !(keyState.IsKeyDown(Keys.A)))
                            direction1 = 45;
                        else
                            direction1 = 0;
                    }
                    else if (keyState.IsKeyDown(Keys.S))//move down
                    {
                        canMove_player1 = true;
                        if (keyState.IsKeyDown(Keys.A) && !(keyState.IsKeyDown(Keys.D)))
                            direction1 = 225;
                        else if (keyState.IsKeyDown(Keys.D) && !(keyState.IsKeyDown(Keys.A)))
                            direction1 = 135;
                        else
                            direction1 = 180;
                    }
                    else if (keyState.IsKeyDown(Keys.A) && !(keyState.IsKeyDown(Keys.D)))//move left
                    {
                        canMove_player1 = true;
                        direction1 = 270;
                    }
                    else if (keyState.IsKeyDown(Keys.D) && !((keyState.IsKeyDown(Keys.A))))//move right
                    {
                        canMove_player1 = true;
                        direction1 = 90;
                    }
                    else
                        canMove_player1 = false;

                    //lay bombs for player1
                    if (keyState.IsKeyDown(Keys.Space) && (bombPressDelay1 <= 0))
                    {
                        if (bombCount1 < 2)
                        {
                            BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)player1Obj.getPosition().X - 16, (int)player1Obj.getPosition().Y + 4, 0.5f);
                            temp.animationCanStart = false;
                            temp.source = player1Obj;
                            objects.Add(temp);
                            bombsLayed1.Add(temp);
                            bombCount1++;
                        }
                        bombPressDelay1 = 50;
                    }

                    //player1 fires bullet
                    if (player1Obj.cooldownTimer <= 0)
                    {
                        if (keyState.IsKeyDown(Keys.B))
                        {
                            fireBullet(player1Obj, 90 + player1Obj.getDirection());
                        }
                    }
                    player1Obj.cooldownTimer--;

                    //sprint
                    if (keyState.IsKeyDown(Keys.LeftShift))
                        player1Obj.setSprinting(true);
                    else
                        player1Obj.setSprinting(false);

                    //update player1 object's position
                    if (canMove_player1)
                    {
                        //stop player from fallingoff edge of window
                        Vector2 tempPos = new Vector2();
                        tempPos.X = MathHelper.Clamp(player1Obj.getPosition().X, 0, screenWidth - player1Obj.getTexture().Width / player1Obj.numberOfColumns);
                        tempPos.Y = MathHelper.Clamp(player1Obj.getPosition().Y, 0, screenHeight - (player1Obj.getTexture().Height / player1Obj.numberOfRows) - 58);
                        player1Obj.setPosition(tempPos);
                        player1Obj.moveInDirection(direction1);
                    }
                }//player1 is alive

                //allow input only if player2 is alive
                if (player2Alive)
                {
                    //for player2
                    if (keyState.IsKeyDown(Keys.Up))//move up
                    {
                        canMove_player2 = true;
                        if (keyState.IsKeyDown(Keys.Left) && !(keyState.IsKeyDown(Keys.Right)))
                            direction2 = -45;
                        else if (keyState.IsKeyDown(Keys.Right) && !(keyState.IsKeyDown(Keys.Left)))
                            direction2 = 45;
                        else
                            direction2 = 0;
                    }
                    else if (keyState.IsKeyDown(Keys.Down))//move down
                    {
                        canMove_player2 = true;
                        if (keyState.IsKeyDown(Keys.Left) && !(keyState.IsKeyDown(Keys.Right)))
                            direction2 = 225;
                        else if (keyState.IsKeyDown(Keys.Right) && !(keyState.IsKeyDown(Keys.Left)))
                            direction2 = 135;
                        else
                            direction2 = 180;
                    }
                    else if (keyState.IsKeyDown(Keys.Left) && !(keyState.IsKeyDown(Keys.Right)))//move left
                    {
                        canMove_player2 = true;
                        direction2 = 270;
                    }
                    else if (keyState.IsKeyDown(Keys.Right) && !((keyState.IsKeyDown(Keys.Left))))//move right
                    {
                        canMove_player2 = true;
                        direction2 = 90;
                    }
                    else
                        canMove_player2 = false;

                    //lay bombs for player2
                    if (keyState.IsKeyDown(Keys.RightAlt) && (bombPressDelay2 <= 0))
                    {
                        if (bombCount2 < 2)
                        {
                            BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)player2Obj.getPosition().X - 16, (int)player2Obj.getPosition().Y + 4, 0.5f);
                            temp.animationCanStart = false;
                            temp.source = player2Obj;
                            bombsLayed2.Add(temp);
                            bombCount2++;
                            objects.Add(temp);
                        }
                        bombPressDelay2 = 50;
                    }

                    //player2 fires bullet
                    if (player2Obj.cooldownTimer <= 0)
                    {
                        if (keyState.IsKeyDown(Keys.RightControl))
                        {
                            fireBullet(player2Obj, 90 + player2Obj.getDirection());
                        }
                    }
                    player2Obj.cooldownTimer--;

                    //sprint
                    if (keyState.IsKeyDown(Keys.RightShift))
                        player2Obj.setSprinting(true);
                    else
                        player2Obj.setSprinting(false);

                    //update player2 object's position
                    if (canMove_player2)
                    {
                        //stop player from fallingoff edge of window
                        Vector2 tempPos = new Vector2();
                        tempPos.X = MathHelper.Clamp(player2Obj.getPosition().X, 0, screenWidth - player2Obj.getTexture().Width / player2Obj.numberOfColumns);
                        tempPos.Y = MathHelper.Clamp(player2Obj.getPosition().Y, 0, screenHeight - (player2Obj.getTexture().Height / player2Obj.numberOfRows) - 58);
                        player2Obj.setPosition(tempPos);
                        player2Obj.moveInDirection(direction2);
                    }
                }  //player2 is alive
            }
            else if (stateOfGame == GAMESTATE.Paused && startMenuDelay <= 0)
            {
                if (keyState.IsKeyDown(Keys.Down))//move down menu
                {
                    if (currentMenuOption < 5)
                        currentMenuOption++;
                }
                if (keyState.IsKeyDown(Keys.Up))//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (keyState.IsKeyDown(Keys.Enter))//select a menu option
                {
                    if (currentMenuOption == 1) // resume game
                        stateOfGame = GAMESTATE.Playing;
                    else if (currentMenuOption == 2)
                    {
                        if (playermode == PLAYERMODE.TwoPlayer)//restart current mode of game
                            startNewMultiPlayerGame();
                        else
                            startNewSinglePlayerGame();
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 3)//look at controls
                    {
                        stateOfGame = GAMESTATE.Controls;
                        previousState = GAMESTATE.Paused;
                    }
                    else if (currentMenuOption == 4)//exit to main menu
                    {
                        stateOfGame = GAMESTATE.StartMenu;
                        currentMenuOption = 1;
                    }
                    else if (currentMenuOption == 5)//exit game
                        this.Exit();

                    startMenuDelay = 10;
                }

            }
            else if (startMenuDelay <= 0)//display winning/loosing screen
            {
                if (keyState.IsKeyDown(Keys.Down))//move down menu
                {
                    if (currentMenuOption < 4)
                        currentMenuOption++;
                }
                if (keyState.IsKeyDown(Keys.Up))//move up menu
                {
                    if (currentMenuOption > 1)
                        currentMenuOption--;
                }
                startMenuDelay = 7;
                if (keyState.IsKeyDown(Keys.Enter))//select a menu option
                {
                    if (currentMenuOption == 1)
                    {
                        if (playermode == PLAYERMODE.TwoPlayer)//restart current mode of game
                            startNewMultiPlayerGame();
                        else
                            startNewSinglePlayerGame();
                        stateOfGame = GAMESTATE.Playing;
                    }
                    else if (currentMenuOption == 2)//look at controls
                    {
                        stateOfGame = GAMESTATE.Controls;
                        previousState = GAMESTATE.GameOver;
                    }
                    else if (currentMenuOption == 3)//return to main menu
                    {
                        stateOfGame = GAMESTATE.StartMenu;
                        currentMenuOption = 1;
                    }
                    else if (currentMenuOption == 4)//exit game
                        this.Exit();

                    startMenuDelay = 10;
                }
            }
        }
Esempio n. 5
0
        protected override void Update(GameTime gameTime)
        {
            //update delay variables for better user interaction
            if (pauseDelay > 0)
                pauseDelay--;
            if (bombPressDelay1 > 0)
                bombPressDelay1--;
            if (bombPressDelay2 > 0)
                bombPressDelay2--;
            if (startMenuDelay > 0)
                startMenuDelay--;

            //determine method of input
            if(GamePad.GetState(PlayerIndex.One).IsConnected)
                handleXboxControls();
            else
                handleKeyboardControls();

            if (stateOfGame == GAMESTATE.StartMenu)//navigate start menu
            {
                //do nothing
            }
            else if (stateOfGame == GAMESTATE.Playing)//play game
            {
                //game over because player(s) have all died
                if (!player2Alive && !player1Alive)
                {
                    //add bonus to enemy score for killing you
                    highestEnemyScore += 50;

                    stateOfGame = GAMESTATE.GameOver;
                }

                //keep record of player scores
                if (player1Alive)
                    player1Score = player1Obj.score;
                if (player2Alive)
                    player2Score = player2Obj.score;

                //keep record of heighest enemy score
                for (int i = 0; i < enemyObjects.Count; i++)
                    if (enemyObjects[i].score > highestEnemyScore)
                        highestEnemyScore = enemyObjects[i].score;

                //check if game had previously been paused
                if (gamePaused)
                {
                    elapsedPauseTime = System.Environment.TickCount - pausedTime;
                    gamePaused = false;
                    startTime = startTime + elapsedPauseTime;
                }
                currentTime = System.Environment.TickCount - (startTime);
                if ((int)(currentTime / 1000) > totalTime)
                    stateOfGame = GAMESTATE.GameOver;//win conditions

                //checks if you've lasted for long enough
                currentTime = System.Environment.TickCount - (startTime);
                if ((int)(currentTime / 1000) > totalTime)
                    stateOfGame = GAMESTATE.GameOver;//win conditions

                //update enemy objects
                if (enemyObjects != null)
                {
                    for (int i = 0; i < enemyObjects.Count; i++)
                    {
                        enemyObjects[i].bombLayingDelay++;
                        enemyObjects[i].firingDelay++;
                    }
                }

                //update map objects
                map.spawnNewDestructables();

                //spawn new enemies if needed
                if (playermode == PLAYERMODE.SinglePlayer)
                    spawnNewEnemies(4);
                else
                    spawnNewEnemies(3);

                //collision detection
                for (int i = 0; i < objects.Count; i++)
                {
                    for (int j = i + 1; j < objects.Count; j++)
                    {
                        GameObject object1 = objects.ElementAt(i);
                        GameObject object2 = objects.ElementAt(j);

                        if (!object1.isMarkedForDestruction() && !object2.isMarkedForDestruction())
                        {
                            if (collisionDetector.isCollision(object1, object2))
                            {
                                if ((object1 is Player) && (object2 is Player))
                                {
                                    //do nothing
                                }
                                else if (((object1 is BombObject) && (object2 is Player)))
                                {
                                    //do nothing
                                }
                                else if (((object2 is BombObject) && (object1 is Player)))
                                {
                                    //do nothing
                                }
                                else if ((object1 is BullitObject) && (object2 is StationaryObject))
                                {
                                    ((BullitObject)object1).setMarkedForDestruction(true);
                                }
                                else if ((object2 is BullitObject) && (object1 is StationaryObject))
                                {
                                    ((BullitObject)object2).setMarkedForDestruction(true);
                                }
                                else if ((object1 is BullitObject) && (object2 is EnemyObject))
                                {
                                    if (!object2.Equals(((BullitObject)object1).getSource()))
                                    {
                                        ((EnemyObject)object2).healthLeft -= 5;
                                        ((EnemyObject)object2).checkHealth(object1);
                                        ((BullitObject)object1).setMarkedForDestruction(true);
                                    }
                                }
                                else if ((object2 is BullitObject) && (object1 is EnemyObject))
                                {
                                    if (!object1.Equals(((BullitObject)object2).getSource()))
                                    {
                                        ((EnemyObject)object1).healthLeft -= 5;
                                        ((EnemyObject)object1).checkHealth(object2);
                                        ((BullitObject)object2).setMarkedForDestruction(true);
                                    }
                                }
                                else if ((object1 is BullitObject) && (object2 is Player))
                                {
                                    if (! object2.Equals(((BullitObject)object1).getSource()))
                                    {
                                        ((Player)object2).healthLeft -= 5;
                                        ((Player)object2).checkHealth(object1);
                                        ((BullitObject)object1).setMarkedForDestruction(true);
                                    }
                                }
                                else if ((object2 is BullitObject) && (object1 is Player))
                                {
                                    if (! object1.Equals(((BullitObject)object2).getSource()))
                                    {
                                        ((Player)object1).healthLeft -= 5;
                                        ((Player)object1).checkHealth(object2);
                                        ((BullitObject)object2).setMarkedForDestruction(true);
                                    }
                                }
                                else if (object1 is EnemyObject && (object2 is StationaryObject))
                                {
                                    //enemy collides with something and needs to change direction
                                    EnemyObject temp_enemy = ((EnemyObject)object1);
                                    int rand = randomNumber.Next(1, 3);
                                    if (rand == 1)
                                        temp_enemy.setDirection(temp_enemy.getDirection() + 90);
                                    else if (rand == 2)
                                        temp_enemy.setDirection(temp_enemy.getDirection() - 90);
                                    temp_enemy.samePathCounter = 0;
                                    temp_enemy.revertPosition();

                                    //lay bomb
                                    if (temp_enemy.bombCount < 1 && temp_enemy.bombLayingDelay > 150)
                                    {
                                        BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1,
                                            (int)temp_enemy.getPosition().X - 16, (int)temp_enemy.getPosition().Y + 4, 0.5f);
                                        temp.animationCanStart = false;
                                        temp.source = temp_enemy;
                                        objects.Add(temp);
                                        temp.source = temp_enemy;
                                        temp_enemy.bombsLayed.Add((BombObject)temp);
                                        temp_enemy.bombCount++;
                                        temp_enemy.bombLayingDelay = 0;
                                    }
                                }
                                else if (object2 is EnemyObject && (object1 is StationaryObject))
                                {
                                    //enemy collides with something and needs to change direction
                                    EnemyObject temp_enemy = ((EnemyObject)object2);
                                    int rand = randomNumber.Next(1,3);
                                    if(rand == 1)
                                        temp_enemy.setDirection(temp_enemy.getDirection() + 90);
                                    else if (rand == 2)
                                        temp_enemy.setDirection(temp_enemy.getDirection() - 90);

                                    temp_enemy.samePathCounter = 0;
                                    temp_enemy.revertPosition();

                                    //lay bomb
                                    if (temp_enemy.bombCount < 1 && temp_enemy.bombLayingDelay > 150)
                                    {
                                        BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1,
                                            (int)temp_enemy.getPosition().X - 16, (int)temp_enemy.getPosition().Y + 4, 0.5f);
                                        temp.animationCanStart = false;
                                        temp.source = temp_enemy;
                                        objects.Add(temp);
                                        temp.source = temp_enemy;
                                        temp_enemy.bombsLayed.Add((BombObject)temp);
                                        temp_enemy.bombCount++;
                                        temp_enemy.bombLayingDelay = 0;
                                    }
                                }
                                else if ((object2 is Player) && !(object1 is Player))
                                    ((Player)object2).revertPosition();
                                else if ((object1 is Player) && !(object2 is Player))
                                    ((Player)object1).revertPosition();

                            }//if there is a cillision
                        }//if neither object is marked for destruction
                    }//end j-loop
                }//end i-loop

                //call update method on all objects
                for (int i = 0; i < objects.Count; i++)
                {
                    if (objects[i] is BombObject)
                        ((BombObject)objects[i]).update();
                    else if (objects[i] is BullitObject)
                        ((BullitObject)objects[i]).update();
                    else if (objects[i] is EnemyObject)
                    {
                        //the enemy  AI begins here
                        //move enemy object
                        EnemyObject temp_enemy = (EnemyObject)objects[i];
                        Vector2 tempPos = new Vector2();

                        //stop object from fallingoff edge of window
                        tempPos.X = MathHelper.Clamp(temp_enemy.getPosition().X, 0,
                            screenWidth - temp_enemy.getTexture().Width / temp_enemy.numberOfColumns);
                        tempPos.Y = MathHelper.Clamp(temp_enemy.getPosition().Y, 0,
                            screenHeight - (temp_enemy.getTexture().Height / temp_enemy.numberOfRows) - 58);

                        //randomly moves enemy object around map, and allows for random responses to environment
                        //if objects collides with edge of grid or hasn't started moving yet
                        if (tempPos.X != temp_enemy.getPosition().X || tempPos.Y != temp_enemy.getPosition().Y)
                        {
                            int rand = randomNumber.Next(1, 3);

                            if (rand == 1)
                                enemy_direction = (int)(temp_enemy.getDirection() + 90);
                            else
                                enemy_direction = (int)(temp_enemy.getDirection() - 90);

                            temp_enemy.samePathCounter = 0;
                        }
                        else if (!temp_enemy.startedMoving)//start moving object
                        {
                            int rand = randomNumber.Next(1, 3);

                            if(rand == 1)
                                enemy_direction = 0;
                            else
                                enemy_direction = 180;
                            temp_enemy.startedMoving = true;
                        }
                        else
                        {
                            int rand = randomNumber.Next(1, 3);

                            //check to see if enemy has been moving in the same direction for too long or not
                            if (temp_enemy.samePathCounter < 200)
                            {
                                enemy_direction = (int)(temp_enemy.getDirection());
                                temp_enemy.samePathCounter++;
                            }
                            else
                            {
                                if (rand == 1)
                                    enemy_direction = (int)(temp_enemy.getDirection() + 90);
                                else
                                    enemy_direction = (int)(temp_enemy.getDirection() - 90);
                                temp_enemy.samePathCounter = 0;
                            }
                        }

                        temp_enemy.setPosition(tempPos);
                        temp_enemy.moveInDirection(enemy_direction);
                        if (player1Alive && temp_enemy.getDistanceTo(player1Obj) < 100)
                        {
                            //close enough to shoot
                            if (temp_enemy.firingDelay > 30)
                            {
                                fireBullet(temp_enemy, temp_enemy.getDirectionTo(player1Obj.getPosition()));
                                temp_enemy.firingDelay = 0;
                            }

                            //close enough to player object to lay bombs
                            if (temp_enemy.bombCount < 2 && temp_enemy.bombLayingDelay > 150)
                            {
                                BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)temp_enemy.getPosition().X - 16,
                                    (int)temp_enemy.getPosition().Y + 4, 0.5f);
                                temp.animationCanStart = false;
                                temp.source = temp_enemy;
                                objects.Add(temp);
                                temp.source = temp_enemy;
                                temp_enemy.bombsLayed.Add((BombObject)temp);
                                temp_enemy.bombCount++;
                                temp_enemy.bombLayingDelay = 0;
                            }
                        }
                        if (player2Alive && temp_enemy.getDistanceTo(player2Obj) < 100)
                        {
                            //close enough to shoot
                            if (temp_enemy.firingDelay > 30)
                            {
                                fireBullet(temp_enemy, temp_enemy.getDirectionTo(player2Obj.getPosition()));
                                temp_enemy.firingDelay = 0;
                            }

                            //close enough to player object to lay bombs
                            if (temp_enemy.bombCount < 2 && temp_enemy.bombLayingDelay > 150)
                            {
                                BombObject temp = new BombObject(bombTexture, explosionTexture, 1, 1, (int)temp_enemy.getPosition().X - 16,
                                    (int)temp_enemy.getPosition().Y + 4, 0.5f);
                                temp.animationCanStart = false;
                                temp.source = temp_enemy;
                                objects.Add(temp);
                                temp.source = temp_enemy;
                                temp_enemy.bombsLayed.Add((BombObject)temp);
                                temp_enemy.bombCount++;
                                temp_enemy.bombLayingDelay = 0;
                            }
                        }

                        //update enemy sprite
                        ((EnemyObject)objects[i]).update();
                    }
                }
                if (canMove_player1 && player1Alive)
                    player1Obj.update();
                else
                {
                    //set LOOSE conditions TODO
                    if (!player1Alive && playermode == PLAYERMODE.SinglePlayer)
                        stateOfGame = GAMESTATE.GameOver;
                }
                if (canMove_player2 && player2Alive)
                    player2Obj.update();

                //delete objects that have been destroyed
                for (int i = 0; i < objects.Count; i++)
                    if (objects[i].isMarkedForDestruction())
                    {
                        GameObject temp = objects[i];
                        if (temp is BombObject)
                        {
                            if (((BombObject)temp).source is EnemyObject)
                            {
                                BombObject currentBomb = (BombObject)temp;
                                if (((EnemyObject)currentBomb.source).bombsLayed.Contains((BombObject)temp))
                                {
                                    EnemyObject enemy = (EnemyObject)(((BombObject)temp).source);
                                    //bomb explodes!
                                    map.bombExplosion((BombObject)temp);
                                    enemy.bombsLayed.Remove((BombObject)temp);
                                    enemy.bombCount--;
                                }
                            }
                            else if ((((BombObject)temp).source).Equals(player1Obj) && bombsLayed1.Contains(((BombObject)temp)))
                            {
                                //bomb explodes!
                                map.bombExplosion((BombObject)temp);
                                bombsLayed1.Remove((BombObject)temp);
                                bombCount1--;
                            }
                            else if ((((BombObject)temp).source).Equals(player2Obj) && bombsLayed2.Contains(((BombObject)temp)))
                            {
                                //bomb explodes!
                                map.bombExplosion((BombObject)temp);
                                bombsLayed2.Remove((BombObject)temp);
                                bombCount2--;
                            }
                        }
                        else if (temp.Equals(player1Obj))
                        {
                            objects.Remove(temp);
                            player1Alive = false;
                            player1Obj = null;
                        }
                        else if (temp.Equals(player2Obj))
                        {
                            objects.Remove(temp);
                            player2Alive = false;
                            player2Obj = null;
                        }
                        else if (temp is EnemyObject)
                        {
                            enemyObjects.Remove((EnemyObject)temp);
                            objects.Remove(temp);
                            temp = null;
                        }
                        else if (!(temp is BombObject))
                            objects.Remove(temp);
                    }
            }
            else if (stateOfGame == GAMESTATE.Paused)//navigate pause menu
            {
                //do nothing
            }
            base.Update(gameTime);
        }
Esempio n. 6
0
        //remove objects within blast areas
        public void bombExplosion(BombObject bombObj)
        {
            int xcoord = (int)((bombObj.getPosition().X + bombObj.getTexture().Width / 2) / gridDimensions.X - 1);
            int ycoord = (int)(((bombObj.getPosition().Y + bombObj.getTexture().Height / 2) / gridDimensions.Y));

            //mapPathData[ycoord, xcoord] = -1;

            //loop through blast area
            for (int x = xcoord - 1; x < xcoord + 2; x++)
            {
                for (int y = ycoord - 1; y < ycoord + 2; y++)
                {
                    //loop through all the objects
                    for (int t = 0; t < MainGameClass.objects.Count; t++)
                    {
                        GameObject temp = MainGameClass.objects[t];
                        int        temp_x;
                        int        temp_y;

                        //checks if the object being examined is the player object due to the use of sprite sheets
                        if (!(temp is Player) && !(temp is EnemyObject))
                        {
                            temp_x = (int)((temp.getPosition().X + temp.getTexture().Width / 2) / gridDimensions.X);
                            temp_y = (int)((temp.getPosition().Y + temp.getTexture().Height / 2) / gridDimensions.Y);
                        }
                        else
                        {
                            temp_x = (int)(((temp.getPosition().X + (temp.getTexture().Width / temp.numberOfColumns) / 2) / gridDimensions.X));
                            temp_y = (int)((temp.getPosition().Y + (temp.getTexture().Height / temp.numberOfRows) / 2) / gridDimensions.Y);
                        }

                        //if within blast area
                        if (temp_x == x && temp_y == y)
                        {
                            if (!temp.Equals(bombObj) && temp is BombObject)
                            {
                                temp.setMarkedForDestruction(true);
                            }
                            else if (!temp.Equals(bombObj) && temp is EnemyObject)
                            {
                                ((EnemyObject)temp).healthLeft -= 40;
                                ((EnemyObject)temp).checkHealth(bombObj);
                            }
                            else if (!temp.Equals(bombObj) && temp is Player)
                            {
                                ((Player)temp).healthLeft -= 40;
                                ((Player)temp).checkHealth(bombObj);
                            }
                            else if (!temp.Equals(bombObj) && (temp is StationaryObject))
                            {
                                //checks if the block can be destroyed
                                if (((StationaryObject)temp).getState() == StationaryObject.PILLER.CAN_DESTROY)
                                {
                                    destructables.Remove((StationaryObject)temp);
                                    numberOfPointBlocks--;
                                    MainGameClass.objects.Remove(temp);
                                    temp.setMarkedForDestruction(true);

                                    //add points to player
                                    if (bombObj.source is Player)
                                    {
                                        ((Player)bombObj.source).score += 5;
                                        tempscores.Add(((Player)bombObj.source).playerID + ": +5");
                                    }
                                }
                            }
                            else if (!temp.Equals(bombObj))
                            {
                                MainGameClass.objects.Remove(temp);
                                temp.setMarkedForDestruction(true);

                                //add points to source
                                if (bombObj.source is Player)
                                {
                                    ((Player)bombObj.source).score += 5;
                                    tempscores.Add(((Player)bombObj.source).playerID + ": +5");
                                }
                            }
                        }
                    } //end loop through objects
                }     //end loop through rowa
            }         //end loop through columns
            bombObj.animationCanStart = true;   //indicates to bomb object that animation can start
        }