-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.cs
46 lines (39 loc) · 1.39 KB
/
Grid.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
using StrategicGame.Common;
using System.Collections.Generic;
namespace StrategicGame.Server {
class Grid {
Dictionary<Unit, Int2> _positionByUnit = new Dictionary<Unit, Int2>();
Dictionary<Int2, Unit> _unitByPosition = new Dictionary<Int2, Unit>();
public Grid(int width, int height) {
Width = width;
Height = height;
}
public int Width { get; }
public int Height { get; }
public int UnitCount { get { return _unitByPosition.Count; } }
public Unit this[int x, int y] {
get { return this[new Int2(x, y)]; }
}
public Unit this[Int2 position] {
get {
Unit unit = null;
_unitByPosition.TryGetValue(position, out unit);
return unit;
}
}
public Int2 this[Unit unit] {
get { return _positionByUnit[unit]; }
set {
Int2 previousPosition;
if (_positionByUnit.TryGetValue(unit, out previousPosition))
_unitByPosition.Remove(previousPosition);
_positionByUnit[unit] = value;
_unitByPosition[value] = unit;
}
}
public bool Contains(Int2 position) {
return position.X >= 0 && position.X < Width
&& position.Y >= 0 && position.Y < Height;
}
}
}