コード例 #1
0
ファイル: Game1.cs プロジェクト: marceldev89/Maziacs
        private void UpdateLoading(GameTime gameTime)
        {
            if (loadSaveGame == true)
            {
                GameData data = LoadGameData();

                if (data.GameState == GameState.Playing || data.GameState == GameState.Fighting || data.GameState == GameState.Paused ||
                    data.PreviousGameState == GameState.Playing || data.PreviousGameState == GameState.Fighting || data.PreviousGameState == GameState.Paused)
                {
                    pastSteps = data.PastSteps;
                    showPath = data.ShowPath;
                    count = data.Count;
                    fightCounter = data.FightCounter;
                    faith = data.Faith;
                    minimumMoves = data.MinimumMoves;

                    maze = new Maze();
                    maze.Initialize(mazeWidth, mazeHeight, difficulty);

                    items = new Dictionary<Vector2, Item>();

                    MazeData mazeData = new MazeData().Load();

                    int x = 0;
                    int y = 0;

                    for (int i = 0; i < mazeData.CellState.Count; i++)
                    {
                        maze.Cells[i] = new Cell();
                        maze.Cells[i].X = x;
                        maze.Cells[i].Y = y;
                        maze.Cells[i].State = mazeData.CellState[i];

                        if (maze.Cells[i].State == Maze.State.Start)
                        {
                            maze.StartCell = maze.Cells[i];
                        }

                        if (maze.Cells[i].State == Maze.State.Treasure)
                        {
                            maze.GoalCell = maze.Cells[i];
                        }

                        if (x == mazeWidth - 1)
                        {
                            x = 0;
                            y++;
                        }
                        else
                        {
                            x++;
                        }
                    }

                    player = new Player();
                    player.Initialize(playerAnimation, new Vector2(data.PlayerX, data.PlayerY));
                    player.Steps = data.Steps;
                    player.Score = data.Score;
                    player.HasSword = data.HasSword;
                    player.HasTreasure = data.HasTreasure;
                    player.Energy = data.Energy;

                    playerAnimation.newFrameRow = 0;

                    playerAnimation.Active = data.PlayerAnimationActive;
                    fightAnimation.Active = data.FightAnimationActive;
                    winAnimation.Active = data.WinAnimationActive;
                    loseAnimation.Active = data.LoseAnimationActive;

                    playerAnimation.Position = player.Position;
                    fightAnimation.Position = player.Position;
                    winAnimation.Position = player.Position;
                    loseAnimation.Position = player.Position;

                    player.Move(Vector2.Zero, Player.State.Idle);

                    camera = new Camera2D();
                    camera.Position = new Vector2(data.PlayerX + 24, data.PlayerY + 48);

                    if (data.PlayerX < 240)
                    {
                        camera._pos.X = 240 + 24;
                    }
                    else if (data.PlayerX > (mazeWidth * 48) - 288)
                    {
                        camera._pos.X = ((mazeWidth * 48) - 288) + 24;
                    }

                    if (data.PlayerY < 240)
                    {
                        camera._pos.Y = 240 + 48;
                    }
                    else if (data.PlayerY > (mazeHeight * 48) - 288)
                    {
                        camera._pos.Y = ((mazeHeight * 48) - 288) + 48;
                    }

                    int width, height;

                    x = (int)camera.Position.X - (GraphicsDevice.Viewport.Width / 2);
                    y = (int)camera.Position.Y - (GraphicsDevice.Viewport.Height / 2) + 112;
                    width = GraphicsDevice.Viewport.Width + 48;
                    height = GraphicsDevice.Viewport.Height - 272;

                    mazeRectangle = new Rectangle(x, y, width, height);
                    mazeTexture = new RenderTarget2D(GraphicsDevice, mazeRectangle.Width, mazeRectangle.Height);

                    enemies = new Dictionary<Vector2, Enemy>();

                    for (int i = 0; i < data.EnemyX.Count; i++)
                    {
                        Vector2 position = new Vector2(data.EnemyX[i], data.EnemyY[i]);
                        Enemy enemy = new Enemy();

                        Animation enemyAnimation = new Animation();
                        enemyAnimation.Initialize(enemyTexture, Vector2.Zero, 48, 48, 2, 500, Color.White, 1f, true);
                        enemy.Initialize(enemyAnimation, position);
                        enemies.Add(position, enemy);
                    }

                    items = new Dictionary<Vector2, Item>();

                    for (int i = 0; i < data.ItemX.Count; i++)
                    {
                        Item item = new Item();
                        Vector2 position = new Vector2(data.ItemX[i], data.ItemY[i]);

                        switch (data.ItemType[i])
                        {
                            case Maze.State.Prisoner:
                                prisonerAnimation = new Animation();
                                prisonerAnimation.Initialize(prisonerTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                                item.Initialize(prisonerAnimation, position, Maze.State.Prisoner);
                                items.Add(position, item);
                                break;
                            case Maze.State.Sword:
                                swordAnimation = new Animation();
                                swordAnimation.Initialize(swordTexture, Vector2.Zero, 48, 48, 1, 400, Color.White, 1f, true);
                                item.Initialize(swordAnimation, position, Maze.State.Sword);
                                items.Add(position, item);
                                break;
                            case Maze.State.Food:
                                foodAnimation = new Animation();
                                foodAnimation.Initialize(foodTexture, Vector2.Zero, 48, 48, 1, 400, Color.White, 1f, true);
                                item.Initialize(foodAnimation, position, Maze.State.Food);
                                items.Add(position, item);
                                break;
                            case Maze.State.Treasure:
                                treasureAnimation = new Animation();
                                treasureAnimation.Initialize(treasureTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                                item.Initialize(treasureAnimation, position, Maze.State.Treasure);
                                items.Add(position, item);
                                break;
                            case Maze.State.Start:
                                startAnimation = new Animation();
                                startAnimation.Initialize(startTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                                item.Initialize(startAnimation, position, Maze.State.Start);
                                items.Add(position, item);
                                break;
                            default:
                                break;
                        }
                    }

                    for (int i = 0; i < maze.Cells.Length; i++)
                    {
                        if (maze.Cells[i].State == Maze.State.Prisoner ||
                            maze.Cells[i].State == Maze.State.Food ||
                            maze.Cells[i].State == Maze.State.Start ||
                            maze.Cells[i].State == Maze.State.Sword ||
                            maze.Cells[i].State == Maze.State.Treasure
                           )
                        {
                            if (items.ContainsKey(new Vector2(maze.Cells[i].X * 48, maze.Cells[i].Y * 48)) == false)
                            {
                                maze.Cells[i].State = Maze.State.Wall;
                            }
                        }
                    }

                    if (showPath == true)
                    {
                        if (player.HasTreasure == true)
                        {
                            maze.BuildDistanceTable(maze.StartCell);
                            maze.FindPath(maze.StartCell, maze.Cells[(int)(player.Position.X / 48) + (int)(player.Position.Y / 48) * mazeWidth]);
                        }
                        else
                        {
                            maze.BuildDistanceTable(maze.GoalCell);
                            maze.FindPath(maze.GoalCell, maze.Cells[(int)(player.Position.X / 48) + (int)(player.Position.Y / 48) * mazeWidth]);
                        }
                    }

                    mazeNeedsRedraw = true;
                    RedrawMaze();

                    loadSaveGame = false;

                    if (data.GameState == GameState.Paused)
                    {
                        if (saveGameAvailable == true)
                        {
                            gameState = data.PreviousGameState;
                        }
                        else
                        {
                            previousGameState = data.PreviousGameState;
                            gameState = data.GameState;
                        }
                    }
                    else if (data.GameState == GameState.Playing)
                    {
                        if (saveGameAvailable == true)
                        {
                            gameState = data.GameState;
                        }
                        else
                        {
                            previousGameState = GameState.Playing;
                            gameState = GameState.Paused;
                        }
                    }
                    else if (data.GameState == GameState.Fighting)
                    {
                        if (saveGameAvailable == true)
                        {
                            gameState = data.GameState;
                        }
                        else
                        {
                            previousGameState = GameState.Fighting;
                            gameState = GameState.Paused;
                        }
                    }
                    else
                    {
                        if (saveGameAvailable == true)
                        {
                            gameState = data.PreviousGameState;
                        }
                        else
                        {
                            //gameState = data.GameState;

                            //if (gameState == GameState.Instructions)
                            //{
                            //    instructionsPage = data.InstructionsPage;
                            //}
                            gameState = GameState.Menu;
                        }
                    }

                    if (saveGameAvailable == true)
                    {
                        saveGameAvailable = false;
                    }
                }
            }
            else
            {
                maze = new Maze();
                player = new Player();
                camera = new Camera2D();

                pastSteps = 0;

                showPath = false;

                count = 0;
                fightCounter = 0;
                energyCounter = 0;

                faith = 0;

                mazeNeedsRedraw = true;

                playerAnimation.Active = true;
                fightAnimation.Active = false;
                winAnimation.Active = false;
                loseAnimation.Active = false;

                items = new Dictionary<Vector2, Item>();

                maze.Initialize(mazeWidth, mazeHeight, difficulty);
                maze.Generate();

                MazeData mazeData = new MazeData();
                mazeData.CellState = new List<Maze.State>();

                foreach (Cell c in maze.Cells)
                {
                    mazeData.CellState.Add(c.State);
                }

                mazeData.Save(mazeData);

                minimumMoves = (maze.StartCell.Distance * 2) - 4;

                int[] dx = { 0, 0, -1, 1 };
                int[] dy = { -1, 1, 0, 0 };

                for (int i = 0; i < 4; i++)
                {
                    if (maze.Cells[(maze.StartCell.X + dx[i]) + (maze.StartCell.Y + dy[i]) * mazeWidth].State == Maze.State.Path)
                    {
                        playerPosition = new Vector2((maze.StartCell.X + dx[i]) * 48, (maze.StartCell.Y + dy[i]) * 48);
                    }
                }

                player.Initialize(playerAnimation, playerPosition);
                playerAnimation.newFrameRow = 0;

                camera.Position = new Vector2(playerPosition.X + 24, playerPosition.Y + 48);

                if (playerPosition.X < 240)
                {
                    camera._pos.X = 240 + 24;
                }
                else if (playerPosition.X > (mazeWidth * 48) - 288)
                {
                    camera._pos.X = ((mazeWidth * 48) - 288) + 24;
                }

                if (playerPosition.Y < 240)
                {
                    camera._pos.Y = 240 + 48;
                }
                else if (playerPosition.Y > (mazeHeight * 48) - 288)
                {
                    camera._pos.Y = ((mazeHeight * 48) - 288) + 48;
                }

                int x, y, width, height;

                x = (int)camera.Position.X - (GraphicsDevice.Viewport.Width / 2);
                y = (int)camera.Position.Y - (GraphicsDevice.Viewport.Height / 2) + 112;
                width = GraphicsDevice.Viewport.Width + 48;
                height = GraphicsDevice.Viewport.Height - 272;

                mazeRectangle = new Rectangle(x, y, width, height);

                mazeTexture = new RenderTarget2D(GraphicsDevice, mazeRectangle.Width, mazeRectangle.Height);

                for (x = 0; x < mazeWidth; x++)
                {
                    for (y = 0; y < mazeHeight; y++)
                    {
                        Cell c = maze.Cells[x + y * mazeWidth];
                        Vector2 position = new Vector2(x * 48, y * 48);
                        Item item = new Item();
                        if (c.State == Maze.State.Prisoner)
                        {
                            prisonerAnimation = new Animation();
                            prisonerAnimation.Initialize(prisonerTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                            item.Initialize(prisonerAnimation, position, Maze.State.Prisoner);
                            items.Add(position, item);
                        }

                        if (c.State == Maze.State.Sword)
                        {
                            swordAnimation = new Animation();
                            swordAnimation.Initialize(swordTexture, Vector2.Zero, 48, 48, 1, 400, Color.White, 1f, true);
                            item.Initialize(swordAnimation, position, Maze.State.Sword);
                            items.Add(position, item);
                        }

                        if (c.State == Maze.State.Food)
                        {
                            foodAnimation = new Animation();
                            foodAnimation.Initialize(foodTexture, Vector2.Zero, 48, 48, 1, 400, Color.White, 1f, true);
                            item.Initialize(foodAnimation, position, Maze.State.Food);
                            items.Add(position, item);
                        }

                        if (c.State == Maze.State.Treasure)
                        {
                            treasureAnimation = new Animation();
                            treasureAnimation.Initialize(treasureTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                            item.Initialize(treasureAnimation, position, Maze.State.Treasure);
                            items.Add(position, item);
                        }

                        if (c.State == Maze.State.Start)
                        {
                            startAnimation = new Animation();
                            startAnimation.Initialize(startTexture, Vector2.Zero, 48, 48, 2, 400, Color.White, 1f, true);
                            item.Initialize(startAnimation, position, Maze.State.Start);
                            items.Add(position, item);
                        }
                    }
                }

                enemies = new Dictionary<Vector2, Enemy>();

                for (int i = 0; i < GameSettings.Maziacs[difficulty]; i++)
                {
                    Animation enemyAnimation = new Animation();
                    enemyAnimation.Initialize(enemyTexture, Vector2.Zero, 48, 48, 2, 500, Color.White, 1f, true);

                    Vector2 enemyPosition = Vector2.Zero;
                    bool found = false;

                    while (found == false)
                    {
                        x = random.Next(0, mazeWidth - 1);
                        y = random.Next(0, mazeHeight - 1);

                        if (x % 2 == 0)
                        {
                            x++;
                        }

                        if (y % 2 == 0)
                        {
                            y++;
                        }

                        if (maze.Cells[x + y * mazeWidth].State == Maze.State.Path && enemies.ContainsKey(new Vector2(x * 48, y * 48)) == false)
                        {
                            if (x < (player.Position.X / 48) - 5 || x > (player.Position.X / 48) + 5)
                            {
                                if (y < (player.Position.Y / 48) - 5 || y > (player.Position.Y / 48) + 5)
                                {
                                    enemyPosition = new Vector2(x * 48, y * 48);
                                    found = true;
                                }
                            }
                        }
                    }

                    Enemy enemy = new Enemy();
                    enemy.Initialize(enemyAnimation, enemyPosition);

                    enemies.Add(enemyPosition, enemy);
                }

                gameState = GameState.Playing;
            }
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: marceldev89/Maziacs
        public MazeData Load()
        {
            #if DEBUG
            Stopwatch sw = new Stopwatch();
            sw.Start();
            #endif

            MazeData data = new MazeData();

            IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication();

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("maze.lvl", FileMode.Open, myStore))
            {
                SharpSerializer serializer = new SharpSerializer(true);

                try
                {
                    data = (MazeData)serializer.Deserialize(stream);
                }
                catch
                {
                    List<string> buttons = new List<string>();
                    buttons.Add("OK");
                    Guide.BeginShowMessageBox("Error", "Your savegame files are corrupt, returning to main menu!", buttons, 0, MessageBoxIcon.Error, null, null);
                }
            }

            #if DEBUG
            sw.Stop();
            Debug.WriteLine("Loading maze took: {0}", sw.Elapsed);
            #endif

            return data;
        }