diff --git a/GraalGmapGenerator/GmapContentGenerator.cs b/GraalGmapGenerator/GmapContentGenerator.cs index 9bb9be4..3cb178e 100644 --- a/GraalGmapGenerator/GmapContentGenerator.cs +++ b/GraalGmapGenerator/GmapContentGenerator.cs @@ -1,16 +1,17 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Text; -using GraalGmapGenerator.Enums; +using GraalGmapGenerator.Options; namespace GraalGmapGenerator { public class GmapContentGenerator { - readonly LevelType _levelType; + readonly GmapContentGenerationOptions _options; - public GmapContentGenerator(LevelType levelType) + public GmapContentGenerator(GmapContentGenerationOptions options) { - _levelType = levelType; + _options = options ?? throw new ArgumentNullException(nameof(options)); } /// @@ -53,13 +54,13 @@ namespace GraalGmapGenerator for (int i = 0; i < gmapArea; i++) { + var level = new Level(gmap, i, _options.LevelType); + levels.Add(level); + // Start a new line once the current line has hit the width of the gmap if (i > 0 && i % gmap.Width == 0) stringBuilder.AppendLine(); - var level = new Level(gmap, i, _levelType); - levels.Add(level); - stringBuilder.Append($"\"{level.FileName}\""); // Only append a comma if its NOT the end of the row diff --git a/GraalGmapGenerator/GmapWriter.cs b/GraalGmapGenerator/GmapWriter.cs index 6f851bc..349a20c 100644 --- a/GraalGmapGenerator/GmapWriter.cs +++ b/GraalGmapGenerator/GmapWriter.cs @@ -5,21 +5,36 @@ namespace GraalGmapGenerator { public static class GmapWriter { - private const string TemplateFile = "template.nw"; + private const string DefaultTemplateFilePath = "template.nw"; - public static void SaveGmap(string destinationPath, Gmap gmap) + public static void Write(string destinationPath, Gmap gmap) { if (!Directory.Exists(destinationPath)) { Directory.CreateDirectory(destinationPath); } - var gmapContentGen = new GmapContentGenerator(LevelType.Nw); + var gmapContentGen = new GmapContentGenerator(new Options.GmapContentGenerationOptions + { + LevelType = LevelType.Nw + }); + GmapContent gmapContent = gmapContentGen.Generate(gmap); + var levelContentGen = new LevelContentGenerator(new Options.LevelContentGenerationOptions + { + AddLevelLinks = gmap.AddLevelLinks, + TemplateFilePath = DefaultTemplateFilePath + }); + foreach (Level level in gmapContent.Levels) { - File.Copy(TemplateFile, $"{destinationPath}/{level.FileName}"); + File.Copy(DefaultTemplateFilePath, $"{destinationPath}/{level.FileName}"); + + string filePath = $"{destinationPath}/{level.FileName}"; + string fileContent = levelContentGen.GenerateLevelContent(gmap, level); + + File.WriteAllText(filePath, fileContent); } File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent.Content); diff --git a/GraalGmapGenerator/Level.cs b/GraalGmapGenerator/Level.cs index 4154cf7..27974ee 100644 --- a/GraalGmapGenerator/Level.cs +++ b/GraalGmapGenerator/Level.cs @@ -5,18 +5,21 @@ namespace GraalGmapGenerator { public class Level { + public const int Width = 64; + public const int Height = 64; + public string FileName { get; } public int Index { get; } public LevelType LevelType { get; } public Level(Gmap gmap, int index, LevelType levelType) { - FileName = $"{gmap.Name}_{index}{GetFileExtensionForLevelType(levelType)}"; + FileName = Level.GetFileName(gmap.Name, index, levelType); Index = index; LevelType = levelType; } - private string GetFileExtensionForLevelType(LevelType levelType) + private static string GetFileExtensionForLevelType(LevelType levelType) { switch (levelType) { @@ -30,5 +33,10 @@ namespace GraalGmapGenerator return ".graal"; } } + + public static string GetFileName(string gmapName, int index, LevelType levelType) + { + return $"{gmapName}_{index}{GetFileExtensionForLevelType(levelType)}"; + } } } \ No newline at end of file diff --git a/GraalGmapGenerator/LevelContentGenerator.cs b/GraalGmapGenerator/LevelContentGenerator.cs new file mode 100644 index 0000000..a4f0cbc --- /dev/null +++ b/GraalGmapGenerator/LevelContentGenerator.cs @@ -0,0 +1,95 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using GraalGmapGenerator.Options; + +namespace GraalGmapGenerator +{ + public class LevelContentGenerator + { + private string _templateContent; + private LevelContentGenerationOptions _options; + + public LevelContentGenerator(LevelContentGenerationOptions options) + { + _options = options; + _templateContent = GetTemplateFileContent(options.TemplateFilePath); + } + + public string GenerateLevelContent(Gmap gmap, Level level) + { + var stringBuilder = new StringBuilder(); + + stringBuilder.AppendLine(_templateContent); + + if (_options.AddLevelLinks) + { + IEnumerable links = GetLevelLinks(gmap, level); + foreach(LevelLink link in links) + { + stringBuilder.AppendLine(link.ToString()); + } + } + + return stringBuilder.ToString(); + } + + private IEnumerable GetLevelLinks(Gmap gmap, Level level) + { + // Level is not on the first row + if (level.Index > gmap.Width) + { + int linkLevelIndex = level.Index - gmap.Width; + string levelFileName = Level.GetFileName(gmap.Name, linkLevelIndex, level.LevelType); + + yield return new LevelLink( + levelFileName, 0, 0, Level.Width, 1, "playerx", "61" + ); + } + + // If level is not the left-most on its row + if (level.Index % gmap.Width > 1) + { + int linkLevelIndex = level.Index - 1; + string levelFileName = Level.GetFileName(gmap.Name, linkLevelIndex, level.LevelType); + + yield return new LevelLink( + levelFileName, 0, 0, 1, Level.Height, "63", "playery" + ); + } + + // If level is not on the bottom row + if (level.Index < (gmap.Width * gmap.Height) - gmap.Width - 1) + { + int linkLevelIndex = level.Index + gmap.Width; + string levelFileName = Level.GetFileName(gmap.Name, linkLevelIndex, level.LevelType); + + yield return new LevelLink( + levelFileName, 0, Level.Height - 1, Level.Width, 1, "playerx", "3" + ); + } + + // If level is not the right-most on its row + if (level.Index % gmap.Width > 0) + { + int linkLevelIndex = level.Index - 1; + string levelFileName = Level.GetFileName(gmap.Name, linkLevelIndex, level.LevelType); + + yield return new LevelLink( + levelFileName, 0, 0, 1, Level.Height, "63", "playery" + ); + } + } + + private string GetTemplateFileContent(string templateFilePath) + { + if (!File.Exists(templateFilePath)) + { + throw new FileNotFoundException($"Template file not found", templateFilePath); + } + + return File.ReadAllText(templateFilePath); + } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/LevelLink.cs b/GraalGmapGenerator/LevelLink.cs new file mode 100644 index 0000000..47af138 --- /dev/null +++ b/GraalGmapGenerator/LevelLink.cs @@ -0,0 +1,29 @@ +namespace GraalGmapGenerator +{ + public class LevelLink + { + public string LevelFileName { get; } + public int X { get; } + public int Y { get; } + public int Width { get; } + public int Height { get; } + public string DestinationX { get; } + public string DestinationY { get; } + + public LevelLink(string levelFileName, int x, int y, int width, int height, string destinationX, string destinationY) + { + LevelFileName = levelFileName; + X = x; + Y = y; + Width = width; + Height = height; + DestinationX = destinationX; + DestinationY = destinationY; + } + + public override string ToString() + { + return $"LINK {LevelFileName} {X} {Y} {Width} {Height} {DestinationX} {DestinationY}"; + } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/Options/GmapContentGenerationOptions.cs b/GraalGmapGenerator/Options/GmapContentGenerationOptions.cs new file mode 100644 index 0000000..906dc5e --- /dev/null +++ b/GraalGmapGenerator/Options/GmapContentGenerationOptions.cs @@ -0,0 +1,9 @@ +using GraalGmapGenerator.Enums; + +namespace GraalGmapGenerator.Options +{ + public class GmapContentGenerationOptions + { + public LevelType LevelType { get; set; } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/Options/LevelContentGenerationOptions.cs b/GraalGmapGenerator/Options/LevelContentGenerationOptions.cs new file mode 100644 index 0000000..781b20a --- /dev/null +++ b/GraalGmapGenerator/Options/LevelContentGenerationOptions.cs @@ -0,0 +1,8 @@ +namespace GraalGmapGenerator.Options +{ + public class LevelContentGenerationOptions + { + public string TemplateFilePath { get; set; } + public bool AddLevelLinks { get; set; } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/Program.cs b/GraalGmapGenerator/Program.cs index 3e1ffc7..7285d51 100644 --- a/GraalGmapGenerator/Program.cs +++ b/GraalGmapGenerator/Program.cs @@ -5,6 +5,8 @@ namespace GraalGmapGenerator { class Program { + private const string ValidationMessageYesNo = "Please provide a valid \"y\" or \"n\" value!"; + static void Main(string[] args) { Console.WriteLine( @@ -71,7 +73,7 @@ namespace GraalGmapGenerator { if (!GmapPropertyValidators.IsValidYesNoInput(input)) { - Console.WriteLine("Please provide a valid \"y\" or \"n\" value!"); + Console.WriteLine(ValidationMessageYesNo); return false; } @@ -90,7 +92,7 @@ namespace GraalGmapGenerator { if (!GmapPropertyValidators.IsValidYesNoInput(input)) { - Console.WriteLine("Please provide a valid \"y\" or \"n\" value!"); + Console.WriteLine(ValidationMessageYesNo); return false; } @@ -100,6 +102,25 @@ namespace GraalGmapGenerator mapBuilder.NoAutomapping(Helpers.YesNoToBool(noAutoMappingStr)); + Console.WriteLine("Add level links? (y/n)..."); + Console.WriteLine("INFO: If \"y\" is selected, level links will be automatically added between GMAP levels."); + + string addLevelLinksStr = GetInput( + () => Console.ReadLine(), + validator: (input) => + { + if (!GmapPropertyValidators.IsValidYesNoInput(input)) + { + Console.WriteLine(ValidationMessageYesNo); + return false; + } + + return true; + } + ); + + mapBuilder.AddLevelLinks(Helpers.YesNoToBool(addLevelLinksStr)); + Console.WriteLine("Save directory..."); Console.WriteLine($"INFO: If you do not wish to provide a save directory, you can leave this setting blank (hit ENTER) and the GMAP will be created under \"gmaps/\" in the application directory ({AppDomain.CurrentDomain.BaseDirectory}/gmaps/{gmapName}/)"); @@ -126,7 +147,7 @@ namespace GraalGmapGenerator var gmap = mapBuilder.Build(); Console.WriteLine("Saving gmap..."); - GmapWriter.SaveGmap(saveDirectory, gmap); + GmapWriter.Write(saveDirectory, gmap); Console.WriteLine("Done!"); Console.ReadLine();