Пример #1
0
        private void LoadTiles(string fileName)
        {
            string[] loadPathDirs      = fileName.Split('\\');
            string   relativeNotesPath = "notes_" + loadPathDirs.Last();

            string notesLoadPath = "";

            foreach (string dir in loadPathDirs)
            {
                if (dir != loadPathDirs.Last())
                {
                    notesLoadPath += dir + '\\';
                }
            }

            notesLoadPath += relativeNotesPath;

            using (StreamReader sr = new StreamReader(fileName)) {
                // Exception: Get first two lines differently
                // Line 1
                string   line        = sr.ReadLine();
                string[] line1Values = line.Split(' ');
                levelWidthInTiles  = Convert.ToInt32(line1Values[0]);
                levelHeightInTiles = Convert.ToInt32(line1Values[1]);

                textboxLevelWidth.Text  = levelWidthInTiles.ToString();
                textboxLevelHeight.Text = levelHeightInTiles.ToString();
                textboxNumLayers.Text   = (line1Values.Length - 2).ToString();

                // Line 2
                line = sr.ReadLine();
                Console.WriteLine(line);

                setImageSource(line);
                createTables();

                int count = 0;

                while ((line = sr.ReadLine()) != null)
                {
                    string[] splitLine = line.Split(' ');

                    if (splitLine[0].StartsWith("-"))
                    {
                        int skipValue = int.Parse(splitLine[0].Split('-').Last());
                        count += skipValue;
                        continue;
                    }

                    List <Button> buttonsAtLocation = new List <Button>(numLayers);
                    for (int i = 0; i < numLayers; i++)
                    {
                        buttonsAtLocation.Add(gameboardButtons[i][count]);
                    }

                    List <Tile> tilesAtLocation = new List <Tile>(numLayers);
                    for (int i = 0; i < numLayers; i++)
                    {
                        tilesAtLocation.Add(null);
                    }

                    int       currentLayer        = 0;
                    const int tileCoordFirstIndex = 10;
                    int       tileCoordLastIndex  = tileCoordFirstIndex - 1 + numLayers * 2;
                    for (int i = tileCoordFirstIndex; i <= tileCoordLastIndex; i++)
                    {
                        if (i >= splitLine.Length)
                        {
                            break;
                        }

                        if (splitLine[i] == "-1")
                        {
                            tileCoordLastIndex--;
                            currentLayer++;
                            continue;
                        }

                        if (i + 1 >= splitLine.Length)
                        {
                            Console.WriteLine("Looking past splitLine's length for the y-coord of a tile. This shouldn't happen.");
                            break;
                        }

                        tilesAtLocation[currentLayer] = srcTiles.SingleOrDefault(t => t.x * tileWidth == int.Parse(splitLine[i]) && t.y * tileHeight == int.Parse(splitLine[i + 1]));
                        currentLayer++;
                        i++;
                    }

                    UnitEnum unit = UnitEnum.None;
                    ItemEnum item = ItemEnum.None;

                    unit = (UnitEnum)Enum.Parse(typeof(UnitEnum), splitLine[8]);
                    item = (ItemEnum)Enum.Parse(typeof(ItemEnum), splitLine[9]);

                    // If has unit or item, indicate this via string in the button
                    // For now, let's assume that the best button to indicate this in is in the foremost layer
                    if (unit != UnitEnum.None)
                    {
                        foreach (Button button in buttonsAtLocation)
                        {
                            button.Content = unit.ToString();
                        }

                        int numNullsAtLocation    = numLayers - tilesAtLocation.Count(tile => tile != null);
                        int numNotNullsAtLocation = numLayers - numNullsAtLocation;

                        // If moving platform, set up that object as well
                        if (unit == UnitEnum.MovingPlatformRed || unit == UnitEnum.MovingPlatformPink)
                        {
                            int            platformDestFirstIndex = tileCoordFirstIndex + numNotNullsAtLocation * 2 + numNullsAtLocation;
                            MovingPlatform platform = GetOrCreatePlatform(count);

                            // Read in destinations from the last expected property to the end of the line
                            for (int i = platformDestFirstIndex; i < splitLine.Length; i += 2)
                            {
                                platform.tileDests.Add(new Tuple <int, int>(int.Parse(splitLine[i]), int.Parse(splitLine[i + 1])));
                            }

                            for (int i = 0; i < platform.tileDests.Count; i++)
                            {
                                Button destButton = (Button)FindName("levelTile" + GetButtonIndexFromCoordinates(platform.tileDests[i]));
                                destButton.Content = "D" + (i + 1);
                            }

                            platforms.Add(platform);
                        }
                    }
                    if (item != ItemEnum.None)
                    {
                        foreach (Button button in buttonsAtLocation)
                        {
                            button.Content = item.ToString();
                        }
                    }

                    // Set location properties
                    LocationData location = locations[count];
                    if (location != null)
                    {
                        location.unit = unit;
                        location.item = item;
                    }

                    // Set button images for each tile at location
                    for (int i = 0; i < tilesAtLocation.Count; i++)
                    {
                        if (tilesAtLocation[i] != null)
                        {
                            buttonsAtLocation[i].Background = new ImageBrush(tilesAtLocation[i].image.Source);
                        }
                    }

                    int?lastNonNullIndex = null;
                    for (int i = 0; i < tilesAtLocation.Count; i++)
                    {
                        if (tilesAtLocation[i] == null)
                        {
                            continue;
                        }

                        placedTiles[i][count] = tilesAtLocation[i];
                        lastNonNullIndex      = i;
                    }

                    // Set foremost-layer tile's properties
                    if (lastNonNullIndex.HasValue)
                    {
                        Tile lastTile = placedTiles[lastNonNullIndex.Value][count];
                        lastTile.leftHeight      = int.Parse(splitLine[0]);
                        lastTile.rightHeight     = int.Parse(splitLine[1]);
                        lastTile.topCollision    = splitLine[2].ToString() == "1" ? true : false;
                        lastTile.rightCollision  = splitLine[3].ToString() == "1" ? true : false;
                        lastTile.bottomCollision = splitLine[4].ToString() == "1" ? true : false;
                        lastTile.leftCollision   = splitLine[5].ToString() == "1" ? true : false;
                        lastTile.isEdge          = splitLine[6].ToString() == "1" ? true : false;

                        // Mutex properties will default to false, so only need to think about setting them to true
                        string mutexProperty = splitLine[7].ToString();
                        if (mutexProperty == "1")
                        {
                            lastTile.isPole = true;
                        }
                        else if (mutexProperty == "2")
                        {
                            lastTile.isPoleEdge = true;
                        }
                        else if (mutexProperty == "3")
                        {
                            lastTile.isDeadly = true;
                        }
                    }

                    count++;
                }
            }

            Console.WriteLine("File loaded.");
            isLevelLoaded = true;
        }
Пример #2
0
        private void SaveTiles(List <Tile> finalPlacedTiles)
        {
            string[] savePathDirs      = savePath.Split('\\');
            string   relativeNotesPath = "notes_" + savePathDirs.Last();

            string notesSavePath = "";

            foreach (string dir in savePathDirs)
            {
                if (dir != savePathDirs.Last())
                {
                    notesSavePath += dir + '\\';
                }
            }

            notesSavePath += relativeNotesPath;

            using (StreamWriter sw = new StreamWriter(savePath)) {
                /* File format:
                 * First line is # tiles wide, # tiles tall, # tiles (non-blank) per layer
                 * Second line is src file name
                 * After that, one line per tile
                 * Each line is leftHeight, rightHeight, then 1 or 0 for collision top, right, bottom, left, isEdge.
                 *  Then mutexProperty, unit, item.
                 *  Then for each layer, tile's xSrc and ySrc.
                 *  Then, if platform, for each platform destination, tile's xSrc and ySrc
                 * -x indicates x blank locations follow
                 * -1 inline indicates null tile on that layer
                 */
                string firstLine = levelWidthInTiles + " " + levelHeightInTiles + " ";
                for (int i = 0; i < numLayers; i++)
                {
                    firstLine += placedTiles[i].Count(t => t != null);
                    if (i != numLayers - 1)
                    {
                        firstLine += " ";
                    }
                }

                sw.WriteLine(firstLine);
                sw.WriteLine(loadImageSrcLabel.Content);

                for (int i = 0; i < locations.Count; i++)
                {
                    int nullCount = 0;
                    while (i < locations.Count && placedTiles.All(layer => layer[i] == null))
                    {
                        nullCount++;
                        i++;
                    }

                    if (nullCount != 0)
                    {
                        sw.WriteLine("-" + nullCount);
                        nullCount = 0;

                        if (i >= locations.Count)
                        {
                            break;
                        }
                    }

                    List <Tile> tilesAtLocation = new List <Tile>(numLayers);

                    foreach (var layer in placedTiles)
                    {
                        if (layer[i] != null)
                        {
                            tilesAtLocation.Add(layer[i]);
                        }
                    }

                    int leftHeight = tilesAtLocation.Any(tile => tile.leftHeight != 0)
                        ? tilesAtLocation.First(tile => tile.leftHeight != 0).leftHeight
                        : 0;

                    int rightHeight = tilesAtLocation.Any(tile => tile.rightHeight != 0)
                        ? tilesAtLocation.First(tile => tile.rightHeight != 0).rightHeight
                        : 0;

                    int topCollision    = tilesAtLocation.Any(tile => tile.topCollision) ? 1 : 0;
                    int rightCollision  = tilesAtLocation.Any(tile => tile.rightCollision) ? 1 : 0;
                    int bottomCollision = tilesAtLocation.Any(tile => tile.bottomCollision) ? 1 : 0;
                    int leftCollision   = tilesAtLocation.Any(tile => tile.leftCollision) ? 1 : 0;
                    int isEdge          = tilesAtLocation.Any(tile => tile.isEdge) ? 1 : 0;

                    int mutexProperty = 0;
                    if (tilesAtLocation.Any(tile => tile.isPole))
                    {
                        mutexProperty = 1;
                    }
                    else if (tilesAtLocation.Any(tile => tile.isPoleEdge))
                    {
                        mutexProperty = 2;
                    }
                    else if (tilesAtLocation.Any(tile => tile.isDeadly))
                    {
                        mutexProperty = 3;
                    }

                    int unit = (int)locations[i].unit;
                    int item = (int)locations[i].item;

                    List <string> tileSourceCoordsList = new List <string>();

                    for (int j = 0; j < placedTiles.Count; j++)
                    {
                        if (placedTiles[j][i] == null)
                        {
                            tileSourceCoordsList.Add("-1");
                            continue;
                        }

                        tileSourceCoordsList.Add((placedTiles[j][i].x * tileWidth).ToString());
                        tileSourceCoordsList.Add((placedTiles[j][i].y * tileHeight).ToString());
                    }

                    string tileSourceCoords = string.Join(" ", tileSourceCoordsList);

                    string line =
                        leftHeight + " " +
                        rightHeight + " " +
                        topCollision + " " +
                        rightCollision + " " +
                        bottomCollision + " " +
                        leftCollision + " " +
                        isEdge + " " +
                        mutexProperty + " " +
                        unit + " " +
                        item;

                    if (tileSourceCoordsList.Any())
                    {
                        line += " " + tileSourceCoords;
                    }

                    if (locations[i].unit == UnitEnum.MovingPlatformRed || locations[i].unit == UnitEnum.MovingPlatformPink)
                    {
                        MovingPlatform platform = platforms.Single(p => p.buttonIndex == i);

                        foreach (Tuple <int, int> dest in platform.tileDests)
                        {
                            line += " " + dest.Item1 + " " + dest.Item2;
                        }
                    }

                    sw.WriteLine(line);
                }
            }

            using (StreamWriter sw = new StreamWriter(notesSavePath)) {
                foreach (List <Tile> layer in placedTiles)
                {
                    foreach (Tile tile in layer.Where(tile => tile != null))
                    {
                        sw.WriteLine(tile.notes);
                    }
                }
            }

            Console.WriteLine("File saved.");
        }