Ejemplo n.º 1
0
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            FileStream stream;
            XmlSerializer serializer;

            if (reloadLevel)
            {
                currentLevel = LoadLevel(gameState.Number);
                currentDrawStyle = currentLevel.DrawStyle;
                colorPalette = currentLevel.ColorPalette;
                whiteTexture = SquareTexture(Color.White);
                colorTextures = LoadColorTextures(colorPalette);
                goalPosition = currentLevel.Goal;
                data = currentLevel.Data;
                reloadLevel = false;
                return;
            }

            int levelIndex = 1;

            /*
            stream = File.Open("level" + levelIndex.ToString() + ".xml", FileMode.Open, FileAccess.Read);
            serializer = new XmlSerializer(typeof(Level));
            currentLevel = (Level)serializer.Deserialize(stream);
            stream.Close();

            for (int i = 0; i < currentLevel.Data.Length; i++)
            {
                for (int j = 0; j < currentLevel.Data[i].Length; j++)
                {
                    if(currentLevel.Data[i][j].ColorIndex == 2)
                    {
                        currentLevel.Data[i][j].IsPersistent = true;
                    }
                }
            }

            stream = File.Open("level1.xml", FileMode.Create);
            serializer = new XmlSerializer(typeof(Level));
            serializer.Serialize(stream, currentLevel);
            stream.Close();
            return;*/

            if (File.Exists("gamestate.xml"))
            {
                stream = File.Open("gamestate.xml", FileMode.Open, FileAccess.Read);
                serializer = new XmlSerializer(typeof(GameState));
                gameState = (GameState)serializer.Deserialize(stream);
                stream.Close();

                levelIndex = gameState == null ? 1 : gameState.Number;
            }

            stream = File.Open("level" + levelIndex.ToString() + ".xml", FileMode.Open, FileAccess.Read);
            serializer = new XmlSerializer(typeof(Level));
            currentLevel = (Level)serializer.Deserialize(stream);
            stream.Close();

            levelLastModified = File.GetLastWriteTime("level" + levelIndex.ToString() + ".xml");

            if (gameState != null)
            {
                data = gameState.Data;
                backgroundColorIndex = gameState.BackgroundColorIndex;
                playerColorIndex = gameState.PlayerColorIndex;
                playerPosition = gameState.PlayerPosition;
            }
            else
            {
                data = currentLevel.Data;
                backgroundColorIndex = currentLevel.BackgroundColorIndex;
                playerColorIndex = currentLevel.PlayerColorIndex;
                playerPosition = currentLevel.PlayerStart;

                gameState = new GameState()
                {
                    Number = levelIndex,
                    Data = data,
                    BackgroundColorIndex = backgroundColorIndex,
                    PlayerColorIndex = playerColorIndex,
                    PlayerPosition = playerPosition
                };
            }

            colorPalette = currentLevel.ColorPalette;
            goalPosition = currentLevel.Goal;
            currentDrawStyle = currentLevel.DrawStyle;

            whiteTexture = SquareTexture(Color.White);

            colorTextures = LoadColorTextures(colorPalette);
        }
Ejemplo n.º 2
0
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            //Reload level when it is modified
            if (File.GetLastWriteTime("level" + gameState.Number.ToString() + ".xml") > levelLastModified)
            {
                levelLastModified = File.GetLastWriteTime("level" + gameState.Number.ToString() + ".xml");
                reloadLevel = true;
                LoadContent();
            }

            currKeyboard = Keyboard.GetState();

            //Gravity
            if ((int)((playerPosition.Y + BLOCK_SIZE.Y) / BLOCK_SIZE.Y) < data[0].Length)
            {
                if (data[(int)(playerPosition.X / BLOCK_SIZE.X)][(int)((playerPosition.Y + BLOCK_SIZE.Y) / BLOCK_SIZE.Y)].ColorIndex == backgroundColorIndex)
                {
                    playerPosition.Y += GRAVITY.Y;
                }
            }

            //Player movement
            if (prevKeyboard.IsKeyDown(Keys.Down))
            {
                if ((int)((playerPosition.Y + BLOCK_SIZE.Y) / BLOCK_SIZE.Y) < data[0].Length)
                {
                    if (data[(int)(playerPosition.X / BLOCK_SIZE.X)][(int)((playerPosition.Y + BLOCK_SIZE.Y) / BLOCK_SIZE.Y)].ColorIndex == backgroundColorIndex)
                    {
                        //Move if player isn't colliding with a block below
                        playerPosition.Y += BLOCK_SIZE.Y;
                    }
                    else
                    {
                        //Acquire color of block that the player collides with
                        playerColorIndex = data[(int)(playerPosition.X / BLOCK_SIZE.X)][(int)((playerPosition.Y + BLOCK_SIZE.Y) / BLOCK_SIZE.Y)].ColorIndex;
                    }
                }
            }
            else if (prevKeyboard.IsKeyDown(Keys.Up))
            {
                if ((int)((playerPosition.Y - BLOCK_SIZE.Y) / BLOCK_SIZE.Y) >= 0)
                {
                    if (data[(int)(playerPosition.X / BLOCK_SIZE.X)][(int)((playerPosition.Y - BLOCK_SIZE.Y) / BLOCK_SIZE.Y)].ColorIndex == backgroundColorIndex)
                    {
                        //Move if player isn't colliding with a block above
                        playerPosition.Y -= BLOCK_SIZE.Y;
                    }
                    else
                    {
                        //Acquire color of block that the player collides with
                        playerColorIndex = data[(int)(playerPosition.X / BLOCK_SIZE.X)][(int)((playerPosition.Y - BLOCK_SIZE.Y) / BLOCK_SIZE.Y)].ColorIndex;
                    }
                }
            }

            if (prevKeyboard.IsKeyDown(Keys.Right))
            {
                if ((int)((playerPosition.X + BLOCK_SIZE.X) / BLOCK_SIZE.X) < data.Length)
                {
                    if (data[(int)((playerPosition.X + BLOCK_SIZE.X) / BLOCK_SIZE.X)][(int)(playerPosition.Y / BLOCK_SIZE.Y)].ColorIndex == backgroundColorIndex)
                    {
                        //Move if player isn't colliding with a block to right
                        playerPosition.X += BLOCK_SIZE.X;
                    }
                    else
                    {
                        //Acquire color of block that the player collides with
                        playerColorIndex = data[(int)((playerPosition.X + BLOCK_SIZE.X) / BLOCK_SIZE.X)][(int)(playerPosition.Y / BLOCK_SIZE.Y)].ColorIndex;
                    }
                }
            }
            else if (prevKeyboard.IsKeyDown(Keys.Left))
            {
                if ((int)((playerPosition.X - BLOCK_SIZE.X) / BLOCK_SIZE.X) >= 0)
                {
                    if (data[(int)((playerPosition.X - BLOCK_SIZE.X) / BLOCK_SIZE.X)][(int)(playerPosition.Y / BLOCK_SIZE.Y)].ColorIndex == backgroundColorIndex)
                    {
                        //Move if player isn't colliding with a block to left
                        playerPosition.X -= BLOCK_SIZE.X;
                    }
                    else
                    {
                        //Acquire color of block that the player collides with
                        playerColorIndex = data[(int)((playerPosition.X - BLOCK_SIZE.X) / BLOCK_SIZE.X)][(int)(playerPosition.Y / BLOCK_SIZE.Y)].ColorIndex;
                    }
                }
            }

            if (currKeyboard.IsKeyUp(Keys.Space) && prevKeyboard.IsKeyDown(Keys.Space))
            {
                FillAround((int)(playerPosition.X / BLOCK_SIZE.X), (int)(playerPosition.Y / BLOCK_SIZE.Y));

                int tempColor = playerColorIndex;

                playerColorIndex = backgroundColorIndex;
                backgroundColorIndex = tempColor;
            }

            //Destroy gamestate
            if (currKeyboard.IsKeyUp(Keys.Escape) && prevKeyboard.IsKeyDown(Keys.Escape))
            {
                File.Delete("gamestate.xml");
                gameState = null;
                LoadContent();
            }

            prevKeyboard = currKeyboard;
        }