Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (39 sloc) 1.39 KB
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;
}
}
}