Generate and write level links to level
This commit is contained in:
parent
58c9355896
commit
1fb4d13e64
8 changed files with 203 additions and 17 deletions
|
@ -1,16 +1,17 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using GraalGmapGenerator.Enums;
|
using GraalGmapGenerator.Options;
|
||||||
|
|
||||||
namespace GraalGmapGenerator
|
namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
public class GmapContentGenerator
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -53,13 +54,13 @@ namespace GraalGmapGenerator
|
||||||
|
|
||||||
for (int i = 0; i < gmapArea; i++)
|
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
|
// Start a new line once the current line has hit the width of the gmap
|
||||||
if (i > 0 && i % gmap.Width == 0)
|
if (i > 0 && i % gmap.Width == 0)
|
||||||
stringBuilder.AppendLine();
|
stringBuilder.AppendLine();
|
||||||
|
|
||||||
var level = new Level(gmap, i, _levelType);
|
|
||||||
levels.Add(level);
|
|
||||||
|
|
||||||
stringBuilder.Append($"\"{level.FileName}\"");
|
stringBuilder.Append($"\"{level.FileName}\"");
|
||||||
|
|
||||||
// Only append a comma if its NOT the end of the row
|
// Only append a comma if its NOT the end of the row
|
||||||
|
|
|
@ -5,21 +5,36 @@ namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
public static class GmapWriter
|
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))
|
if (!Directory.Exists(destinationPath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(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);
|
GmapContent gmapContent = gmapContentGen.Generate(gmap);
|
||||||
|
|
||||||
|
var levelContentGen = new LevelContentGenerator(new Options.LevelContentGenerationOptions
|
||||||
|
{
|
||||||
|
AddLevelLinks = gmap.AddLevelLinks,
|
||||||
|
TemplateFilePath = DefaultTemplateFilePath
|
||||||
|
});
|
||||||
|
|
||||||
foreach (Level level in gmapContent.Levels)
|
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);
|
File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent.Content);
|
||||||
|
|
|
@ -5,18 +5,21 @@ namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
public class Level
|
public class Level
|
||||||
{
|
{
|
||||||
|
public const int Width = 64;
|
||||||
|
public const int Height = 64;
|
||||||
|
|
||||||
public string FileName { get; }
|
public string FileName { get; }
|
||||||
public int Index { get; }
|
public int Index { get; }
|
||||||
public LevelType LevelType { get; }
|
public LevelType LevelType { get; }
|
||||||
|
|
||||||
public Level(Gmap gmap, int index, LevelType levelType)
|
public Level(Gmap gmap, int index, LevelType levelType)
|
||||||
{
|
{
|
||||||
FileName = $"{gmap.Name}_{index}{GetFileExtensionForLevelType(levelType)}";
|
FileName = Level.GetFileName(gmap.Name, index, levelType);
|
||||||
Index = index;
|
Index = index;
|
||||||
LevelType = levelType;
|
LevelType = levelType;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetFileExtensionForLevelType(LevelType levelType)
|
private static string GetFileExtensionForLevelType(LevelType levelType)
|
||||||
{
|
{
|
||||||
switch (levelType)
|
switch (levelType)
|
||||||
{
|
{
|
||||||
|
@ -30,5 +33,10 @@ namespace GraalGmapGenerator
|
||||||
return ".graal";
|
return ".graal";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetFileName(string gmapName, int index, LevelType levelType)
|
||||||
|
{
|
||||||
|
return $"{gmapName}_{index}{GetFileExtensionForLevelType(levelType)}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
95
GraalGmapGenerator/LevelContentGenerator.cs
Normal file
95
GraalGmapGenerator/LevelContentGenerator.cs
Normal file
|
@ -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<LevelLink> links = GetLevelLinks(gmap, level);
|
||||||
|
foreach(LevelLink link in links)
|
||||||
|
{
|
||||||
|
stringBuilder.AppendLine(link.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<LevelLink> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
29
GraalGmapGenerator/LevelLink.cs
Normal file
29
GraalGmapGenerator/LevelLink.cs
Normal file
|
@ -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}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
using GraalGmapGenerator.Enums;
|
||||||
|
|
||||||
|
namespace GraalGmapGenerator.Options
|
||||||
|
{
|
||||||
|
public class GmapContentGenerationOptions
|
||||||
|
{
|
||||||
|
public LevelType LevelType { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace GraalGmapGenerator.Options
|
||||||
|
{
|
||||||
|
public class LevelContentGenerationOptions
|
||||||
|
{
|
||||||
|
public string TemplateFilePath { get; set; }
|
||||||
|
public bool AddLevelLinks { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,8 @@ namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
|
private const string ValidationMessageYesNo = "Please provide a valid \"y\" or \"n\" value!";
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
|
@ -71,7 +73,7 @@ namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
if (!GmapPropertyValidators.IsValidYesNoInput(input))
|
if (!GmapPropertyValidators.IsValidYesNoInput(input))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Please provide a valid \"y\" or \"n\" value!");
|
Console.WriteLine(ValidationMessageYesNo);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +92,7 @@ namespace GraalGmapGenerator
|
||||||
{
|
{
|
||||||
if (!GmapPropertyValidators.IsValidYesNoInput(input))
|
if (!GmapPropertyValidators.IsValidYesNoInput(input))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Please provide a valid \"y\" or \"n\" value!");
|
Console.WriteLine(ValidationMessageYesNo);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,6 +102,25 @@ namespace GraalGmapGenerator
|
||||||
|
|
||||||
mapBuilder.NoAutomapping(Helpers.YesNoToBool(noAutoMappingStr));
|
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("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}/)");
|
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();
|
var gmap = mapBuilder.Build();
|
||||||
|
|
||||||
Console.WriteLine("Saving gmap...");
|
Console.WriteLine("Saving gmap...");
|
||||||
GmapWriter.SaveGmap(saveDirectory, gmap);
|
GmapWriter.Write(saveDirectory, gmap);
|
||||||
|
|
||||||
Console.WriteLine("Done!");
|
Console.WriteLine("Done!");
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
|
Loading…
Add table
Reference in a new issue