-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
CellLayer.cs
173 lines (146 loc) · 4.24 KB
/
CellLayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace OpenRA
{
// Represents a layer of "something" that covers the map
public class CellLayer<T> : IEnumerable<T>
{
public readonly Size Size;
readonly Rectangle bounds;
public readonly TileShape Shape;
public event Action<CPos> CellEntryChanged = null;
readonly T[] entries;
public CellLayer(Map map)
: this(map.TileShape, new Size(map.MapSize.X, map.MapSize.Y)) { }
public CellLayer(TileShape shape, Size size)
{
Size = size;
bounds = new Rectangle(0, 0, Size.Width, Size.Height);
Shape = shape;
entries = new T[size.Width * size.Height];
}
public void CopyValuesFrom(CellLayer<T> anotherLayer)
{
if (Size != anotherLayer.Size || Shape != anotherLayer.Shape)
throw new ArgumentException(
"layers must have a matching size and shape.", "anotherLayer");
if (CellEntryChanged != null)
throw new InvalidOperationException(
"Cannot copy values when there are listeners attached to the CellEntryChanged event.");
Array.Copy(anotherLayer.entries, entries, entries.Length);
}
public static CellLayer<T> CreateInstance(Func<MPos, T> initialCellValueFactory, Size size, TileShape tileShape)
{
var cellLayer = new CellLayer<T>(tileShape, size);
for (var v = 0; v < size.Height; v++)
{
for (var u = 0; u < size.Width; u++)
{
var mpos = new MPos(u, v);
cellLayer[mpos] = initialCellValueFactory(mpos);
}
}
return cellLayer;
}
// Resolve an array index from cell coordinates
int Index(CPos cell)
{
return Index(cell.ToMPos(Shape));
}
// Resolve an array index from map coordinates
int Index(MPos uv)
{
return uv.V * Size.Width + uv.U;
}
/// <summary>Gets or sets the <see cref="OpenRA.CellLayer"/> using cell coordinates</summary>
public T this[CPos cell]
{
get
{
return entries[Index(cell)];
}
set
{
entries[Index(cell)] = value;
if (CellEntryChanged != null)
CellEntryChanged(cell);
}
}
/// <summary>Gets or sets the layer contents using raw map coordinates (not CPos!)</summary>
public T this[MPos uv]
{
get
{
return entries[Index(uv)];
}
set
{
entries[Index(uv)] = value;
if (CellEntryChanged != null)
CellEntryChanged(uv.ToCPos(Shape));
}
}
/// <summary>Clears the layer contents with a known value</summary>
public void Clear(T clearValue)
{
for (var i = 0; i < entries.Length; i++)
entries[i] = clearValue;
}
public IEnumerator<T> GetEnumerator()
{
return ((IEnumerable<T>)entries).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool Contains(CPos cell)
{
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the Diamond coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (Shape == TileShape.Diamond && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(Shape));
}
public bool Contains(MPos uv)
{
return bounds.Contains(uv.U, uv.V);
}
public CPos Clamp(CPos uv)
{
return Clamp(uv.ToMPos(Shape)).ToCPos(Shape);
}
public MPos Clamp(MPos uv)
{
return uv.Clamp(new Rectangle(0, 0, Size.Width - 1, Size.Height - 1));
}
}
// Helper functions
public static class CellLayer
{
/// <summary>Create a new layer by resizing another layer. New cells are filled with defaultValue.</summary>
public static CellLayer<T> Resize<T>(CellLayer<T> layer, Size newSize, T defaultValue)
{
var result = new CellLayer<T>(layer.Shape, newSize);
var width = Math.Min(layer.Size.Width, newSize.Width);
var height = Math.Min(layer.Size.Height, newSize.Height);
result.Clear(defaultValue);
for (var j = 0; j < height; j++)
for (var i = 0; i < width; i++)
result[new MPos(i, j)] = layer[new MPos(i, j)];
return result;
}
}
}