예제 #1
0
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                {
                    content = new ContentManager(ScreenManager.Game.Services, "Content");
                }

                gameFont = content.Load <SpriteFont>("gamefont");

                //INITIALISING CAMERA
                _camera = new GameObject.Camera2D(ScreenManager.GraphicsDevice.Viewport)
                {
                    Limits = new Rectangle(0, 0, 16388, 2860), Zoom = 1.0f
                };


                //INITIALISING PLAYERS
                playerOne                 = new GameObject.Player(PlayerIndex.One, "lucas", 9, 6);
                playerOne.Position        = new Vector2(500, 1890);
                playerOne.Scale           = 3.0f;
                playerOne.FramesPerSecond = 8;

                playerTwo                 = new GameObject.Player(PlayerIndex.Two, "lucas", 9, 6);
                playerTwo.Position        = new Vector2(550, 1890);
                playerTwo.Scale           = 3.0f;
                playerTwo.FramesPerSecond = 8;
                playerTwo.Colour          = Color.Violet;

                playerThree                 = new GameObject.Player(PlayerIndex.Three, "lucas", 9, 6);
                playerThree.Position        = new Vector2(600, 1890);
                playerThree.Scale           = 3.0f;
                playerThree.FramesPerSecond = 8;
                playerThree.Colour          = Color.Green;

                playerFour                 = new GameObject.Player(PlayerIndex.Four, "lucas", 9, 6);
                playerFour.Position        = new Vector2(650, 1890);
                playerFour.Scale           = 3.0f;
                playerFour.FramesPerSecond = 8;
                playerFour.Colour          = Color.Blue;

                playerList = new List <GameObject.Player>();
                playerList.Add(playerOne);
                //playerList.Add(playerTwo);
                //playerList.Add(playerThree);
                //playerList.Add(playerFour);


                //INITIALISING LAYERS
                _layerDictionary = new Dictionary <string, int>();

                _layerDictionary.Add("back", 0);
                _layerDictionary.Add("cloud1", 1);
                _layerDictionary.Add("cloud2", 2);
                _layerDictionary.Add("cloud3", 3);
                _layerDictionary.Add("horizon", 4);
                _layerDictionary.Add("distant", 5);
                _layerDictionary.Add("far", 6);
                _layerDictionary.Add("rear", 7);
                _layerDictionary.Add("mid", 8);
                _layerDictionary.Add("c", 9);
                _layerDictionary.Add("interactive", 10);
                _layerDictionary.Add("player", 11);
                _layerDictionary.Add("front", 12);
                _layerDictionary.Add("near", 13);
                _layerDictionary.Add("fore", 14);

                // Create 15 layers with parallax ranging from 0% to 100% (only horizontal)
                _layers = new List <GameObject.Layer>
                {
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.1f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.2f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.3f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.6f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.7f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(0.8f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.0f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.1f, 1.0f)
                    },
                    new GameObject.Layer(_camera)
                    {
                        Parallax = new Vector2(1.2f, 1.0f)
                    },
                };


                //LevelType level = content.Load<LevelType>("Levels/tutorial2");
                LevelType level = new LevelType();

                importLayers(level);

                //ADD THE PLAYERS TO THE LAYER LIST
                foreach (GameObject.Player player in playerList)
                {
                    _layers[_layerDictionary["player"]].Sprites.Add(player);
                }

                //load content for every sprite in the layers
                foreach (GameObject.Layer layer in _layers)
                {
                    for (int i = 0; i < layer.Sprites.Count; i++)
                    {
                        layer.Sprites[i].LoadContent(content, layer.Sprites[i].AssetName);
                    }
                }

                //START PLAYING THE LEVEL MUSIC
                AudioManager.PlayBgmMusic("Tutorial");

                //MOVE THE CAMERA TO LOOK AT PLAYER 1
                _camera.LookAt(playerList[0].Position, BaseScreenSize);

                // once the load has finished, we use ResetElapsedTime to tell the game's
                // timing mechanism that we have just finished a very long frame, and that
                // it should not try to catch up.
                ScreenManager.Game.ResetElapsedTime();
            }
        }
예제 #2
0
        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            //Sets previous camera position to the last position it was
            //(only when panning is not initiated)
            _camera.PreviousCameraPosition = _averagePlayerPosition;

            _averagePlayerPosition = Vector2.Zero;
            int NumberOfActivePlayers = 0;

            for (int playerIndex = 0; playerIndex < playerList.Count; playerIndex++)
            {
                // Look up inputs for the active player profile.
                // sets the index by default to player one.
                PlayerIndex pI = PlayerIndex.One;

                switch (playerIndex)
                {
                case 0:
                    pI = PlayerIndex.One;
                    break;

                case 1:
                    pI = PlayerIndex.Two;
                    break;

                case 2:
                    pI = PlayerIndex.Three;
                    break;

                case 3:
                    pI = PlayerIndex.Four;
                    break;
                }

                KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
                GamePadState  gamePadState  = input.CurrentGamePadStates[playerIndex];

                // The game pauses either if the user presses the pause button, or if
                // they unplug the active gamepad. This requires us to keep track of
                // whether a gamepad was ever plugged in, because we don't want to pause
                // on PC if they are playing with a keyboard and have no gamepad at all!
                bool gamePadDisconnected = !gamePadState.IsConnected &&
                                           input.GamePadWasConnected[playerIndex];

                PlayerIndex player;


                //Reset the level
                if (resetAction.Evaluate(input, pI, out player))
                {
                    ResetLevel();
                }


                //Toggle the collision markers on and off
                if (playerIndex == 0)
                {
                    if (toggleCollisionVisibility.Evaluate(input, pI, out player))
                    {
                        foreach (CollisionSurface c in _layers[_layerDictionary["c"]].Sprites)
                        {
                            c.Visible = !c.Visible;
                        }
                    }
                }

                //If the game is paused, add the pause screen
                if (pauseAction.Evaluate(input, pI, out player) || gamePadDisconnected)
                {
                    ScreenManager.AddScreen(new PauseMenuScreen(), pI);
                }
                else
                {
                    if (stunPlayers.Evaluate(input, pI, out player))
                    {
                        playerList[playerIndex].IsStunned = true;
                    }

                    // Otherwise update the player position.
                    playerList[playerIndex].Update(gameTime, input, _layers[_layerDictionary["c"]].Sprites, _layers[_layerDictionary["interactive"]].Sprites);
                }

                //Gets the bounds of the screen to check whether the player is within them
                Rectangle screenBounds = new Rectangle((int)_camera.Position.X, (int)_camera.Position.Y, (int)(BaseScreenSize.X / _camera.Zoom), (int)(BaseScreenSize.Y / _camera.Zoom));

                if (!playerList[playerIndex].BoundingBox().Intersects(screenBounds))
                {
                    //If the player is outside the bounds of the screen and still alive, kill them.
                    if (playerList[playerIndex].IsAlive)
                    {
                        //Initiate the camera pan to the new average position
                        _camera.SlideCamera = true;

                        playerList[playerIndex].IsAlive = false;
                    }
                }

                //If the player is alive, then add their position to the average
                if (playerList[playerIndex].IsAlive)
                {
                    _averagePlayerPosition += playerList[playerIndex].Position;
                    NumberOfActivePlayers++;
                }
            }

            //Find the average screen position of all active players (dead players are inactive).
            _averagePlayerPosition.X = _averagePlayerPosition.X / NumberOfActivePlayers;
            _averagePlayerPosition.Y = _averagePlayerPosition.Y / NumberOfActivePlayers;


            //Move the camera to look at this position
            _camera.LookAt(_averagePlayerPosition, BaseScreenSize);

            if (NumberOfActivePlayers == 0)
            {
                ResetLevel();
            }
        }