Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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;
}
}
}