Skip to content

Commit

Permalink
Merge pull request #7 from anno-mods/devel/add-islands
Browse files Browse the repository at this point in the history
add/remove islands
  • Loading branch information
jakobharder authored Jul 10, 2022
2 parents 1029517 + 7ac33ae commit 27d5ec8
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 162 deletions.
3 changes: 0 additions & 3 deletions AnnoMapEditor/AnnoMapEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@
</ItemGroup>

<ItemGroup>
<Compile Update="UI\Controls\SessionProperties.xaml.cs">
<SubType>Code</SubType>
</Compile>
<Compile Update="UserSettings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
Expand Down
35 changes: 29 additions & 6 deletions AnnoMapEditor/MapTemplates/Island.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public class Island : ObservableBase
public string? MapPath { get; set; }
public int MapSizeInTiles { get; private set; }
public string? AssumedMapPath { get; private set; }
public bool IsPool => string.IsNullOrEmpty(MapPath);
public bool IsPool => ElementType != 2 && string.IsNullOrEmpty(MapPath);
public string? Label { get; set; }

public bool IsNew => template is null;

private static readonly Random rnd = new((int)DateTime.Now.Ticks);

private Serializing.A7tinfo.TemplateElement template;
private Serializing.A7tinfo.TemplateElement? template;

private Region Region { get; }

Expand All @@ -47,7 +49,6 @@ public class Island : ObservableBase
private Island(Region region)
{
Region = region;
template = new Serializing.A7tinfo.TemplateElement();
}

public async Task<Island> CloneAsync()
Expand Down Expand Up @@ -87,11 +88,33 @@ public static async Task<Island> FromSerialized(Serializing.A7tinfo.TemplateElem
return island;
}

public Serializing.A7tinfo.TemplateElement ToTemplate()
public static Island Create(IslandSize size, IslandType type, Vector2 position)
{
var island = new Island(Region.Moderate)
{
ElementType = 1,
Position = position,
Size = size,
Type = type
};
return island;
}

public void CreateTemplate()
{
template = new Serializing.A7tinfo.TemplateElement
{
Element = new Serializing.A7tinfo.Element()
};
IslandChanged?.Invoke();
}

public Serializing.A7tinfo.TemplateElement? ToTemplate()
{
if (template.Element is null)
if (template?.Element is null)
return template;

template.ElementType = ElementType;
template.Element.Position = new int[] { Position.X, Position.Y };
template.Element.Size = Size.ElementValue;
if (IsPool)
Expand Down Expand Up @@ -138,7 +161,7 @@ public async Task InitAsync(Region region)
{
if (ElementType == 2)
{
MapSizeInTiles = 1;
MapSizeInTiles = Vector2.Tile.X;
return;
}

Expand Down
97 changes: 76 additions & 21 deletions AnnoMapEditor/MapTemplates/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnnoMapEditor.MapTemplates
{
public record Vector2
{
private int Normalize(int x) => (x + 4) / 8 * 8;
public static readonly Vector2 Zero = new(0, 0);
public static readonly Vector2 Tile = new(8, 8);

public int X
{
Expand All @@ -24,6 +26,8 @@ public int Y
}
private int _y = 0;

public double Length => Math.Sqrt(X * X + Y * Y);

public Vector2(int x, int y)
{
X = x;
Expand Down Expand Up @@ -51,14 +55,61 @@ public Vector2(int[]? numbers)
Y = numbers[1];
}
}

public Vector2(Point point)
{
X = (int)point.X;
Y = (int)point.Y;
}

public static Vector2 operator + (Vector2 a, Vector2 b)
{
return new Vector2(a.X + b.X, a.Y + b.Y);
}

public static Vector2 operator -(Vector2 a, Vector2 b)
{
return new Vector2(a.X - b.X, a.Y - b.Y);
}

public static Vector2 operator -(Vector2 a, int b)
{
return new Vector2(a.X - b, a.Y - b);
}

public Vector2 Clamp(Rect2 area)
{
return new Vector2(Math.Clamp(X, area.X, area.Max.X), Math.Clamp(Y, area.Y, area.Max.Y));
}

public Vector2 Clamp(Vector2 min, Vector2 max)
{
return new Vector2(Math.Clamp(X, min.X, max.X), Math.Clamp(Y, min.Y, max.Y));
}

public Vector2 FlipY(int sessionSize)
{
return new Vector2(X, sessionSize - Y - 8);
}

public bool Within(Rect2 area)
{
return X >= area.X && Y >= area.Y && X < area.X + area.Width && Y < area.Y + area.Height;
}

private static int Normalize(int x) => (x + 4) / 8 * 8;
}

public struct Rect2
{
public int X;
public int Y;
public int Width;
public int Height;
public Vector2 Position;
public Vector2 Size;
public Vector2 Max => Position + Size - Vector2.Tile;

public int X => Position.X;
public int Y => Position.Y;
public int Width => Size.X;
public int Height => Size.Y;

public Rect2(string? area)
{
Expand All @@ -67,35 +118,39 @@ public Rect2(string? area)
string[] parts = area.Split(' ');
if (parts.Length == 4)
{
X = int.Parse(parts[0]);
Y = int.Parse(parts[1]);
Width = int.Parse(parts[2]) - X;
Height = int.Parse(parts[3]) - Y;
Position = new Vector2(int.Parse(parts[0]), int.Parse(parts[1]));
Size = new Vector2(int.Parse(parts[2]), int.Parse(parts[3])) - Position;
return;
}
}

X = 0;
Y = 0;
Width = 0;
Height = 0;
Position = Vector2.Zero;
Size = Vector2.Zero;
}

public Rect2(int[]? numbers)
{
if (numbers?.Length == 4)
{
X = numbers[0];
Y = numbers[1];
Width = numbers[2] - X;
Height = numbers[3] - Y;
Position = new Vector2(numbers);
Size = new Vector2(numbers[2], numbers[3]) - Position;
return;
}

X = 0;
Y = 0;
Width = 0;
Height = 0;
Position = Vector2.Zero;
Size = Vector2.Zero;
}

public Rect2(Vector2 max)
{
Position = Vector2.Zero;
Size = max - Position;
}

public Rect2(Vector2 min, Vector2 max)
{
Position = min;
Size = max - Position;
}
}
}
27 changes: 23 additions & 4 deletions AnnoMapEditor/MapTemplates/Session.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -10,19 +11,23 @@ namespace AnnoMapEditor.MapTemplates
{
public class Session
{
public List<Island> Islands { get; private set; }
public IReadOnlyList<Island> Islands => _islands;
private List<Island> _islands;

public Vector2 Size { get; private set; }
public Rect2 PlayableArea { get; private set; }
public Region Region { get; set; }

private Serializing.A7tinfo.MapTemplateDocument template;

public event NotifyCollectionChangedEventHandler? IslandCollectionChanged;

public string MapSizeText => $"Size: {Size.X}, Playable: {PlayableArea.Width}";

public Session()
{
Size = new Vector2(0, 0);
Islands = new List<Island>();
_islands = new List<Island>();
template = new Serializing.A7tinfo.MapTemplateDocument();
}

Expand Down Expand Up @@ -77,7 +82,7 @@ where element?.Element is not null
Session session = new()
{
Region = region,
Islands = new List<Island>(await Task.WhenAll(islands)),
_islands = new List<Island>(await Task.WhenAll(islands)),
Size = new Vector2(document.MapTemplate?.Size),
PlayableArea = new Rect2(document.MapTemplate?.PlayableArea),
template = document
Expand Down Expand Up @@ -112,7 +117,7 @@ public async Task UpdateAsync()

return new Serializing.A7tinfo.MapTemplateDocumentExport()
{
MapTemplate = new Serializing.A7tinfo.MapTemplateExport(template.MapTemplate, Islands.Select(x => x.ToTemplate()))
MapTemplate = new Serializing.A7tinfo.MapTemplateExport(template.MapTemplate, Islands.Select(x => x.ToTemplate()).Where(x => x is not null)!)
};
}

Expand Down Expand Up @@ -140,5 +145,19 @@ public async Task SaveAsync(string filePath)
file.SetLength(0); // clear
await Serializer.WriteAsync(export, file);
}

public void AddIsland(Island island)
{
island.CreateTemplate();
_islands.Add(island);
Task.Run(async () => await island.InitAsync(Region));
IslandCollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

public void RemoveIsland(Island island)
{
_islands.Remove(island);
IslandCollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}
23 changes: 10 additions & 13 deletions AnnoMapEditor/Mods/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public static bool CanSave(Session? session)
return true;
}

public async Task<bool> Save(string modsFolderPath, string modName, string? modID)
public async Task<bool> Save(string modPath, string modName, string? modID)
{
string fullModName = "[Map] " + modName;
string modPath = Path.Combine(modsFolderPath, fullModName);

try
{
Expand Down Expand Up @@ -198,26 +197,24 @@ private class MapTemplateInfo
{
[2160] = new[] { "moderate_atoll_ll_01", "moderate_atoll_lm_01", "moderate_atoll_ls_01",
"moderate_islandarc_ll_01", "moderate_islandarc_lm_01", "moderate_islandarc_ls_01" },

[1650] = new[] { "moderate_archipel_ll_01", "moderate_archipel_lm_01", "moderate_archipel_ls_01",
"moderate_corners_ll_02", "moderate_corners_lm_01", "moderate_corners_ls_01",
[1656] = new[] { "moderate_archipel_ll_01", "moderate_archipel_lm_01", "moderate_archipel_ls_01",
"moderate_corners_lm_01", "moderate_corners_ll_02", "moderate_corners_ls_01",
"moderate_snowflake_ll_02", "moderate_snowflake_lm_01", "moderate_snowflake_ls_01" },
[1648] = new[] { "moderate_atoll_ml_01", "moderate_atoll_mm_01", "moderate_atoll_ms_01",
"moderate_islandarc_ml_01", "moderate_islandarc_mm_01", "moderate_islandarc_ms_01" },
[1436] = new[] { "moderate_corners_ml_01" },
[1440] = new[] { "moderate_corners_ml_01" },
[1392] = new[] { "moderate_archipel_ml_01" },
[1366] = new[] { "moderate_snowflake_mm_01" },
[1358] = new[] { "moderate_corners_sl_01" },
[1356] = new[] { "moderate_snowflake_ml_01" },
[1348] = new[] { "moderate_snowflake_sm_01" },
[1338] = new[] { "moderate_corners_sm_01" },
[1368] = new[] { "moderate_snowflake_mm_01" },
[1360] = new[] { "moderate_corners_sl_01", "moderate_snowflake_ml_01" },
[1352] = new[] { "moderate_snowflake_sm_01" },
[1344] = new[] { "moderate_corners_sm_01" },
[1336] = new[] { "moderate_atoll_sl_01", "moderate_atoll_sm_01", "moderate_atoll_ss_01",
"moderate_corners_mm_01", "moderate_corners_ms_01",
"moderate_islandarc_sl_01", "moderate_islandarc_sm_01", "moderate_islandarc_ss_01",
"moderate_snowflake_ms_01" },
[1327] = new[] { "moderate_snowflake_sl_01" },
[1328] = new[] { "moderate_snowflake_sl_01" },
[1312] = new[] { "moderate_archipel_mm_01", "moderate_archipel_ms_01" },
[1220] = new[] { "moderate_archipel_sm_01" },
[1224] = new[] { "moderate_archipel_sm_01" },
[1208] = new[] { "moderate_corners_ss_01", "moderate_snowflake_ss_01" },
[1200] = new[] { "moderate_archipel_sl_01", "moderate_archipel_ss_01" },
};
Expand Down
1 change: 1 addition & 0 deletions AnnoMapEditor/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private set
if (_dataArchive is System.IDisposable disposable)
disposable.Dispose();
SetProperty(ref _dataArchive, value);
OnPropertyChanged(nameof(DataPath));
}
}
private IDataArchive _dataArchive = DataArchives.DataArchive.Default;
Expand Down
Loading

0 comments on commit 27d5ec8

Please sign in to comment.