Beispiel #1
0
        void LoadMap(Button mapButton)
        {
            //currentMap = byte.Parse(mapName.Substring(mapName.Length -1, 1)); //-------------------------------------------------risky.

            if (File.Exists(CurrentGame.MapDir + mapButton.Text + ".txt"))
            {
                //try
                //{
                string[] read;
                HexMap   loadedMap = new HexMap(ParentGame, new char[1, 1], null, null, CurrentGame.players);
                char[,] layout = new char[11, 21];
                List <Point> spawnPoints = new List <Point>();
                List <Point> goalPoints  = new List <Point>();
                List <Wave>  waves       = new List <Wave>();
                byte         initLife;
                int          initEnergy;
                int[]        initGenePoints;
                byte[]       availableTowers = new byte[6];
                List <Tower> initTowers      = new List <Tower>();

                using (StreamReader reader = new StreamReader(CurrentGame.MapDir + mapButton.Text + ".txt"))
                {
                    //for (int i = 0; i < 12; i++)
                    //  Debug.WriteLine(reader.ReadLine());

                    for (int row = 0; row < 11; row++)
                    {
                        for (int col = 0; col < 21; col++)
                        {
                            int ascii = reader.Peek();
                            if (ascii != 32 && ascii != 13 && ascii != 39 && ascii != 46 && ascii != 48)
                            {
                                reader.Peek();
                            }
                            while (reader.Peek() < 32)
                            {
                                reader.Read();                            //run through whitespace characters, except space (32), which is last of the whitespace in the ascii table
                            }
                            layout[row, col] = (char)reader.Read();
                            if ((int)layout[row, col] >= 49 && (int)layout[row, col] <= 57)
                            {
                                spawnPoints.Add(new Point(col, row));
                            }
                            else if ((int)layout[row, col] >= 97 && (int)layout[row, col] <= 122)
                            {
                                goalPoints.Add(new Point(col, row));
                            }
                            else
                            {
                                for (int i = 0; i < HexMap.ExampleTowers.Length; i++)
                                {
                                    if (ascii == (int)HexMap.ExampleTowers[i].Symbol)
                                    {
                                        Tower tempTower = Tower.Clone(HexMap.ExampleTowers[i]);      //-------------------------------------ADD MAPCOORD---------------------------------- tavallaan tehty mut tower-olemassaoloa pitäs hienontaa
                                        tempTower.ParentMap = CurrentGame.currentMap;                //---------------------------------------------------------------------- mukaanlukien nää ihme initit (tää koska MapCoordToScrLoc ei static!)
                                        tempTower.MapCoord  = new Point(col, row);
                                        //tempTower.buildTimer = 0;
                                        //tempTower.buildFinishedCounter = 0;
                                        initTowers.Add(tempTower);
                                    }
                                }
                            }
                        }
                    }
                    reader.ReadLine();
                    reader.ReadLine();

                    read = reader.ReadLine().Split(':', ' ');
                    for (int i = 0; i < availableTowers.Length; i++)
                    {
                        byte.TryParse(read[i + 3], out availableTowers[i]);
                    }

                    initLife       = byte.Parse(reader.ReadLine().Split(':')[1]);
                    initEnergy     = int.Parse(reader.ReadLine().Split(':')[1]);
                    read           = reader.ReadLine().Split(':', ',');
                    initGenePoints = new int[] { int.Parse(read[1]), int.Parse(read[2]), int.Parse(read[3]) };

                    loadedMap = new HexMap(ParentGame, layout, spawnPoints.ToArray(), goalPoints.ToArray(), CurrentGame.players);
                    CurrentGame.currentMap    = loadedMap;
                    CurrentGame.HUD.ParentMap = loadedMap;
                    for (int i = 0; i < initTowers.Count; i++)
                    {
                        initTowers[i].ParentMap = loadedMap;
                        CurrentGame.players[0].Towers.Add(Tower.Clone(initTowers[i]));
                        CurrentGame.players[0].Towers[i].MapCoord = initTowers[i].MapCoord;
                    }
                    loadedMap.InitTowers = initTowers;

                    while (!reader.ReadLine().Contains("Creamt"))
                    {
                        ;
                    }

                    for (int w = 0; reader.Peek() == 87; w++)     //wave lines begin with a W (87)
                    {
                        waves.Add(new Wave(loadedMap));
                        waves[w].TempGroups = new List <SpawnGroup>();

                        reader.ReadLine();
                        while (reader.Peek() == 9)     //group lines begin with a tab (9)
                        {
                            read = reader.ReadLine().Split(new char[] { '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                            waves[w].TempGroups.Add(new SpawnGroup(int.Parse(read[0]),
                                                                   new Creature(read[1],
                                                                                read[2],
                                                                                loadedMap,
                                                                                read[3],
                                                                                int.Parse(read[4]) - 1,
                                                                                (int)(char.Parse(read[5])) - 97, //Goalpoint
                                                                                int.Parse(read[6]),
                                                                                                                 //(Element)Enum.Parse(typeof(Element), read[7], true),
                                                                                float.Parse(read[7], System.Globalization.NumberFormatInfo.InvariantInfo),
                                                                                new GeneSpecs(),                 //TÄHÄ VÄLIIN ELEMS
                                                                                byte.Parse(read[8]),
                                                                                int.Parse(read[9]),
                                                                                1f),                                 // SCALE HARDCODED-----------------------------------------------------------------
                                                                   int.Parse(read[13]),
                                                                   int.Parse(read[14])));
                        }
                        waves[w].Groups = waves[w].TempGroups.ToArray();
                        waves[w].Initialize();
                    }
                }
                #region OLD LOAD

                /*using (StreamReader reader = new StreamReader(filePath))
                 * {
                 *
                 * //--------Map size------------------------------------------
                 * for (int i = 0; i < 8; i++)
                 *  reader.ReadLine();
                 * columns = reader.ReadLine().Length;
                 * while (reader.ReadLine() != "")
                 *  rows++;
                 * layout = new char[rows + 1, columns]; // Y,X -----------!!!!!
                 *
                 * reader.DiscardBufferedData();
                 * reader.BaseStream.Position = 0;
                 * //--------Map layout----------------------------------------
                 * for (int i = 0; i < 8; i++)
                 *  reader.ReadLine();
                 * for (int row = 0; row <= layout.GetUpperBound(0); row++)
                 * { for (int col = 0; col <= layout.GetUpperBound(1); col++)
                 *  {
                 *      while (reader.Peek() < 32) reader.Read(); //run through whitespace characters, except space (32), which is the last of the whitespace in the ascii table
                 *      int ascii = reader.Peek();
                 *      switch (ascii)
                 *      {
                 *          case 32: layout[row, col] = 0; break; // 32 = space -> empty (3)
                 *          case 39: layout[row, col] = 3; break; // 39 = ' -> path (1)
                 *          case 46: layout[row, col] = 3; break; // 46 = . -> path (1) (odd columns)
                 *          case 48: layout[row, col] = 1; break; // 48 = 0 -> wall (0)
                 *          case 49: layout[row, col] = 6; break; // 49 = 1 -> tower type 1 (6)
                 *      }
                 *      reader.Read();
                 *  }
                 * }
                 *
                 *
                 * //--------Initial resources--------------------------------
                 * while (!reader.ReadLine().Contains("Life/Energy/Genes")) ;
                 * initLife = byte.Parse(reader.ReadLine());
                 * initEnergy = int.Parse(reader.ReadLine());
                 * initUpgPoints = int.Parse(reader.ReadLine());
                 *
                 * //--------Spawnpoints--------------------------------------
                 * while (!reader.ReadLine().Contains("SpawnPoints")) ;
                 * read = reader.ReadLine().Split('\t');
                 * while (read.Length > 1)
                 * {
                 *  spawnPoints.Add(new Point(int.Parse(read[0]), int.Parse(read[1])));
                 *  read = reader.ReadLine().Split('\t');
                 * }
                 *
                 * //--------Goalpoints and openingtimes---------------------
                 * while (!reader.ReadLine().Contains("GoalPoints")) ;
                 * read = reader.ReadLine().Split('\t');
                 * while (read.Length > 1)
                 * {
                 *  goalPoints.Add(new Point(int.Parse(read[0]), int.Parse(read[1])));
                 *  goalPointOpeningTimes.Add(int.Parse(read[2]));
                 *  read = reader.ReadLine().Split('\t');
                 * }
                 *
                 * loadedMap = new HexMap(ParentGame, layout, spawnPoints.ToArray(), goalPoints.ToArray(), goalPointOpeningTimes.ToArray(), ParentGame.players);
                 *
                 * while (!reader.ReadLine().Contains("Wave 1")) ;
                 *
                 * bool newWaveLine;
                 * bool newSpawnGroupLine;
                 * do
                 * {
                 *  read = reader.ReadLine().Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                 *  //string[] av = read[5].Split(',');
                 *  spawnGroups.Add(new SpawnGroup(int.Parse(read[0]),   //Number of creatures in group
                 *                                 new Creature(read[1],     //Type
                 *                                    read[2],               //Name
                 *                                    loadedMap,             //Map
                 *                                    ParentGame.Content.Load<Texture2D>("Creatures\\" + read[3]), //Texture
                 *                                    int.Parse(read[4]),    //SpawnPoint
                 *                                    Array.ConvertAll(read[5].Split(','), int.Parse), //GoalPoints----------COOL SH*T
                 *                                    int.Parse(read[6]),    //InitHp
                 *                                    float.Parse(read[7]),  //RRes
                 *                                    float.Parse(read[8]),  //GRes
                 *                                    float.Parse(read[9]),  //BRes
                 *                                    float.Parse(read[10], NumberFormatInfo.InvariantInfo), //DefSpd
                 *                                    float.Parse(read[11], NumberFormatInfo.InvariantInfo), //CellDmg
                 *                                    byte.Parse(read[12]),  //LifeDmg
                 *                                    int.Parse(read[13]),   //GeneBounty
                 *                                    1f, // SCALE HARDCODED-----------------------------------------------------------------
                 *                                    read[3]),              //TextureName -------------------------------------------not cool
                 *                                 int.Parse(read[14]), //Spawn rate
                 *                                 int.Parse(read[15])));//Wave duration
                 *  //Check next line
                 *  newWaveLine = reader.Peek() == 87; //87 = W (as in Wave)
                 *  newSpawnGroupLine = reader.Peek() == 9; //9 = tab (spawnGroup lines begin with a tab)
                 *  if (newWaveLine)
                 *  {
                 *      waves.Add(new Wave(loadedMap, spawnGroups.ToArray()));
                 *      spawnGroups.Clear();
                 *      reader.ReadLine();
                 *  }
                 *  else if (newSpawnGroupLine)
                 *  {
                 *      continue;
                 *  }
                 *  else waves.Add(new Wave(loadedMap, spawnGroups.ToArray()));
                 * } while (newWaveLine || newSpawnGroupLine);
                 *
                 * loadedMap.Waves = waves.ToArray();*/
                #endregion

                loadedMap.PlayerInitLife       = initLife;
                loadedMap.PlayerInitEnergy     = initEnergy;
                loadedMap.PlayerInitGenePoints = initGenePoints;
                loadedMap.Waves = waves.ToArray();
                loadedMap.Pathfinder.InitializeTiles();                 //-----------------------hmmmmmmmmm
                loadedMap.SpawnPoints     = spawnPoints.ToArray();
                loadedMap.GoalPoints      = goalPoints.ToArray();
                loadedMap.AvailableTowers = availableTowers;
                loadedMap.ResetMap();
                CurrentGame.gameState = GameState.InGame;
                //}
                //catch (Exception)
                //{
                //    mapButton.Text += " (bad file!)";
                //    CurrentGame.gameState = GameState.MainMenu;
                //}
            }
            else
            {
                Debug.WriteLine("\"" + mapButton.Text + "\" doesn't exist");
            }
        }
Beispiel #2
0
        protected override void Update(GameTime gameTime)
        {
            if (IsActive)
            {
                mouse    = Mouse.GetState();
                keyboard = Keyboard.GetState();

                switch (gameState)
                {
                case GameState.MainMenu:
                    if (!IsMouseVisible)
                    {
                        IsMouseVisible = true;                      //------------------------yöllinen örvellystarve
                    }
                    mainMenu.Update(mouse, keyboard);
                    break;

                case GameState.MapTest:
                case GameState.InGame:
                    if (keyboard.IsKeyDown(Keys.Escape) && !prevKeyboard.IsKeyDown(Keys.Escape))
                    {
                        gameState = GameState.Paused;
                    }
                    if (keyboard.IsKeyDown(Keys.S))
                    {
                        if (TargetElapsedTime.Ticks != 40000)
                        {
                            TargetElapsedTime = new TimeSpan(40000);
                        }
                        if (gameTimer % 2 == 0)
                        {
                            SuppressDraw();
                        }
                    }
                    else if (keyboard.IsKeyDown(Keys.D))
                    {
                        if (TargetElapsedTime.Ticks != 400000)
                        {
                            TargetElapsedTime = new TimeSpan(400000);
                        }
                    }
                    else
                    {
                        if (TargetElapsedTime.Ticks != LoopTimeTicks)
                        {
                            TargetElapsedTime = new TimeSpan(LoopTimeTicks);
                        }
                    }

                    HUD.Update(mouse, prevMouse, keyboard, prevKeyboard);
                    currentMap.Update();
                    //if (keyboard.IsKeyDown(Keys.Down)) brightnessValue -= 0.01f;
                    //else if (keyboard.IsKeyDown(Keys.Up)) brightnessValue += 0.01f;
                    //brightnessFX.Parameters["value"].SetValue(brightnessValue);
                    break;

                case GameState.Paused:
                    if (keyboard.IsKeyDown(Keys.Space))
                    {
                        this.Exit();
                    }
                    else if (keyboard.IsKeyDown(Keys.Escape) && !prevKeyboard.IsKeyDown(Keys.Escape))
                    {
                        gameState = prevState;
                    }
                    else if (keyboard.IsKeyDown(Keys.M) && !prevKeyboard.IsKeyDown(Keys.M))
                    {
                        gameState          = GameState.MainMenu;
                        mainMenu.menuState = MainMenu.MenuState.Main;
                    }
                    HUD.Update(mouse, prevMouse, keyboard, prevKeyboard);
                    break;

                case GameState.GameOver:
                    IsMouseVisible = true;
                    if (keyboard.IsKeyDown(Keys.Escape) || (mouse.LeftButton == ButtonState.Released && prevMouse.LeftButton == ButtonState.Pressed))
                    {
                        if (prevState == GameState.InGame)
                        {
                            gameState = GameState.MainMenu;
                        }
                        else if (prevState == GameState.MapTest)
                        {
                            currentMap.ResetMap();
                            HUD.MapEditorTopButtons[1].Pos = HUD.mapEditWTEditPos;
                            gameState = GameState.MapEditor;
                        }
                    }
                    break;

                case GameState.MapEditor:
                    if (keyboard.IsKeyDown(Keys.M) && !prevKeyboard.IsKeyDown(Keys.M) && keyboard.IsKeyDown(Keys.Escape))
                    {
                        gameState          = GameState.MainMenu;
                        mainMenu.menuState = MainMenu.MenuState.Main;
                    }
                    HUD.Update(mouse, prevMouse, keyboard, prevKeyboard);
                    //currentMap.Update();
                    break;
                }
                prevKeyboard = keyboard;
                prevMouse    = mouse;
                audioEngine.Update();
                base.Update(gameTime);
                GameTime = gameTime;
                gameTimer++;
                if (gameState == GameState.InGame || gameState == GameState.MapTest)
                {
                    prevState = gameState;
                }
            }
        }