Start adding level links functionality, refactor, other bits.

This commit is contained in:
Aaron Yarborough 2020-09-09 19:21:47 +02:00
parent 42bc9ed385
commit e0a0222150
9 changed files with 189 additions and 89 deletions

View file

@ -2,28 +2,32 @@
{
public class Gmap
{
public string Name { get; set; }
public string Name { get; }
public int Width { get; set; }
public int Width { get; }
public int Height { get; set; }
public bool NoAutomapping { get; set; }
public bool NoAutomapping { get; }
public bool LoadFullMap { get; set; }
public bool LoadFullMap { get; }
public bool AddLevelLinks { get; }
public Gmap(
string name,
int width,
int height,
bool noAutomapping = false,
bool loadFullMap = false)
bool loadFullMap = false,
bool addLevelLinks = false)
{
Name = name;
Width = width;
Height = height;
NoAutomapping = noAutomapping;
LoadFullMap = loadFullMap;
AddLevelLinks = addLevelLinks;
}
}
}

View file

@ -7,6 +7,7 @@
private int _height;
private bool _noAutomapping;
private bool _loadFullMap;
private bool _addLevelLinks;
public GmapBuilder SetName(string name)
{
@ -34,12 +35,24 @@
return this;
}
public GmapBuilder AddLevelLinks(bool value)
{
_addLevelLinks = value;
return this;
}
/// <summary>
/// Builds the gmap
/// </summary>
public Gmap Build()
{
return new Gmap(_name, _width, _height, _noAutomapping, _loadFullMap);
return new Gmap(
_name,
_width,
_height,
_noAutomapping,
_loadFullMap,
_addLevelLinks);
}
}
}

View file

@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace GraalGmapGenerator
{
public class GmapContent
{
public string Content { get; }
public IEnumerable<Level> Levels { get; }
public GmapContent(string content, IEnumerable<Level> levels)
{
Content = content;
Levels = levels;
}
}
}

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraalGmapGenerator.Enums;
@ -18,9 +17,10 @@ namespace GraalGmapGenerator
/// Returns the gmap file contents
/// </summary>
/// <returns></returns>
public string Generate(Gmap gmap)
public GmapContent Generate(Gmap gmap)
{
var stringBuilder = new StringBuilder();
(string Content, IEnumerable<Level> Levels) data = GenerateLevelNamesList(gmap);
stringBuilder.AppendLine("GRMAP001");
stringBuilder.AppendLine($"WIDTH {gmap.Width}");
@ -33,16 +33,34 @@ namespace GraalGmapGenerator
stringBuilder.AppendLine("LOADFULLMAP");
stringBuilder.AppendLine("LEVELNAMES");
stringBuilder.AppendLine(data.Content);
stringBuilder.Append("LEVELNAMESEND");
List<string> levelNames = GetLevelNames(gmap).ToList();
for (var i = 0; i < levelNames.Count; i++)
string content = stringBuilder.ToString();
return new GmapContent(content, data.Levels);
}
private (string content, IEnumerable<Level> levelNames) GenerateLevelNamesList(Gmap gmap)
{
// TODO: Split this method up - it's doing too much. But I want to be able to generate
// the level names and also create a new instance of Level for each level within a single
// loop, rather than having a loop for each.
var stringBuilder = new StringBuilder();
var levels = new List<Level>();
int gmapArea = gmap.Width * gmap.Height;
for (int i = 0; i < gmapArea; i++)
{
// 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 levelName = GetLevelName(i, gmap.Name, _levelType);
stringBuilder.Append($"\"{levelName}\"");
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
if (i % gmap.Width < (gmap.Width - 1))
@ -51,37 +69,7 @@ namespace GraalGmapGenerator
}
}
stringBuilder.AppendLine();
stringBuilder.Append("LEVELNAMESEND");
return stringBuilder.ToString();
}
public IEnumerable<string> GetLevelNames(Gmap gmap)
{
for (int i = 0; i < (gmap.Width * gmap.Height); i++)
{
yield return GetLevelName(i, gmap.Name, _levelType);
}
}
private string GetLevelName(int index, string name, LevelType levelType)
{
string extension;
switch (levelType)
{
default:
case LevelType.Nw:
extension = ".nw";
break;
case LevelType.Graal:
extension = ".graal";
break;
}
return $"{name}_{index}{extension}";
return (stringBuilder.ToString(), levels);
}
}
}

View file

@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.IO;
using GraalGmapGenerator.Enums;
@ -16,15 +15,14 @@ namespace GraalGmapGenerator
}
var gmapContentGen = new GmapContentGenerator(LevelType.Nw);
GmapContent gmapContent = gmapContentGen.Generate(gmap);
IEnumerable<string> levelNames = gmapContentGen.GetLevelNames(gmap);
foreach (string levelName in levelNames)
foreach (Level level in gmapContent.Levels)
{
File.Copy(TemplateFile, $"{destinationPath}/{levelName}");
File.Copy(TemplateFile, $"{destinationPath}/{level.FileName}");
}
string gmapContent = gmapContentGen.Generate(gmap);
File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent);
File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent.Content);
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using GraalGmapGenerator.Enums;
namespace GraalGmapGenerator
{
public class Level
{
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)}";
Index = index;
LevelType = levelType;
}
private string GetFileExtensionForLevelType(LevelType levelType)
{
switch (levelType)
{
default:
throw new NotImplementedException($"{levelType} has not been implemented.");
case LevelType.Nw:
return ".nw";
case LevelType.Graal:
return ".graal";
}
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.IO;
namespace GraalGmapGenerator
{
@ -118,6 +119,7 @@ namespace GraalGmapGenerator
if (saveDirectory == "")
{
saveDirectory = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "gmaps", gmapName);
}
Console.WriteLine("Generating gmap...");

View file

@ -0,0 +1,58 @@
using GraalGmapGenerator;
namespace GraalGmapGeneratorTests.Fake
{
internal static class GmapFake
{
private const string DefaultName = "Test gmap";
private const int DefaultWidth = 8;
private const int DefaultHeight = 10;
private const bool DefaultNoAutomapping = false;
private const bool DefaultLoadFullMap = false;
private const bool DefaultAddLevelLinks = false;
internal static Gmap Get()
{
return new Gmap(
DefaultName,
DefaultWidth,
DefaultHeight,
noAutomapping: DefaultNoAutomapping,
loadFullMap: DefaultLoadFullMap,
addLevelLinks: DefaultAddLevelLinks);
}
internal static Gmap GetWithAutomappingTrue()
{
return new Gmap(
DefaultName,
DefaultWidth,
DefaultHeight,
noAutomapping: true,
loadFullMap: DefaultLoadFullMap,
addLevelLinks: DefaultAddLevelLinks);
}
internal static Gmap GetWithLoadFullMapTrue()
{
return new Gmap(
DefaultName,
DefaultWidth,
DefaultHeight,
noAutomapping: DefaultNoAutomapping,
loadFullMap: true,
addLevelLinks: DefaultAddLevelLinks);
}
internal static Gmap GetWithAddLevelLinksTrue()
{
return new Gmap(
DefaultName,
DefaultWidth,
DefaultHeight,
noAutomapping: DefaultNoAutomapping,
loadFullMap: DefaultLoadFullMap,
addLevelLinks: true);
}
}
}

View file

@ -1,5 +1,6 @@
using GraalGmapGenerator;
using GraalGmapGenerator.Enums;
using GraalGmapGeneratorTests.Fake;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
@ -12,29 +13,24 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_SavesCorrectDimensions()
{
var expectedWidth = 5;
var expectedHeight = 6;
var gmap = GetTestGmap();
gmap.Width = expectedWidth;
gmap.Height = expectedHeight;
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = result.Split("\n\r".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
string result = generator.Generate(gmap).Content;
string[] lines = result.Split("\n\r".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual($"WIDTH {expectedWidth}", lines[1]);
Assert.AreEqual($"HEIGHT {expectedHeight}", lines[2]);
Assert.AreEqual($"WIDTH {gmap.Width}", lines[1]);
Assert.AreEqual($"HEIGHT {gmap.Height}", lines[2]);
}
[Test]
public void Generate_SavesHeader()
{
var gmap = GetTestGmap();
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = SplitContentByLines(result);
string result = generator.Generate(gmap).Content;
List<string> lines = SplitContentByLines(result);
Assert.AreEqual("GRMAP001", lines[0]);
}
@ -42,12 +38,11 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_SaveNoAutomappingLine_WhenNoAutomappingIsTrue()
{
var gmap = GetTestGmap();
gmap.NoAutomapping = true;
Gmap gmap = GmapFake.GetWithAutomappingTrue();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = SplitContentByLines(result);
string result = generator.Generate(gmap).Content;
List<string> lines = SplitContentByLines(result);
Assert.IsTrue(lines.Contains("NOAUTOMAPPING"));
}
@ -55,12 +50,11 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_DoesntSaveNoAutomappingLine_WhenNoAutomappingIsFalse()
{
var gmap = GetTestGmap();
gmap.NoAutomapping = false;
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = SplitContentByLines(result);
string result = generator.Generate(gmap).Content;
List<string> lines = SplitContentByLines(result);
Assert.IsFalse(lines.Contains("NOAUTOMAPPING"));
}
@ -68,12 +62,11 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_SaveLoadFullMapLine_WhenLoadFullMapIsTrue()
{
var gmap = GetTestGmap();
gmap.LoadFullMap = true;
Gmap gmap = GmapFake.GetWithLoadFullMapTrue();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = SplitContentByLines(result);
string result = generator.Generate(gmap).Content;
List<string> lines = SplitContentByLines(result);
Assert.IsTrue(lines.Contains("LOADFULLMAP"));
}
@ -81,12 +74,11 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_DoesntSaveLoadFullMapLine_WhenLoadFullMapIsFalse()
{
var gmap = GetTestGmap();
gmap.LoadFullMap = false;
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
var lines = SplitContentByLines(result);
string result = generator.Generate(gmap).Content;
List<string> lines = SplitContentByLines(result);
Assert.IsFalse(lines.Contains("LOADFULLMAP"));
}
@ -96,13 +88,13 @@ namespace GraalGmapGeneratorTests
[TestCase(LevelType.Graal, ".graal")]
public void Generate_SavesValidLevels_ForLevelType(LevelType levelType, string expectedFileExtension)
{
var gmap = GetTestGmap();
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(levelType);
var content = generator.Generate(gmap);
string content = generator.Generate(gmap).Content;
var levelNames = GetLevelNamesFromContent(content);
var isAllCorrectFileExtension = levelNames.All(levelName => levelName.EndsWith(expectedFileExtension));
IEnumerable<string> levelNames = GetLevelNamesFromContent(content);
bool isAllCorrectFileExtension = levelNames.All(levelName => levelName.EndsWith(expectedFileExtension));
Assert.IsTrue(isAllCorrectFileExtension);
}
@ -110,9 +102,9 @@ namespace GraalGmapGeneratorTests
[Test]
public void Generate_DoesSaveLevelNamesTags()
{
var gmap = GetTestGmap();
Gmap gmap = GmapFake.Get();
var generator = new GmapContentGenerator(LevelType.Graal);
var result = generator.Generate(gmap);
string result = generator.Generate(gmap).Content;
Assert.IsTrue(result.Contains("LEVELNAMES", System.StringComparison.Ordinal));
Assert.IsTrue(result.Contains("LEVELNAMESEND", System.StringComparison.Ordinal));
@ -124,11 +116,6 @@ namespace GraalGmapGeneratorTests
return levelNamePattern.Matches(content).Select(x => x.Groups[1].Value);
}
private Gmap GetTestGmap()
{
return new Gmap("My test gmap", 10, 11, true, true);
}
private List<string> SplitContentByLines(string content)
{
return content.Split("\n\r".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries).ToList();