Permalink
Cannot retrieve contributors at this time
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?
strategic-game/Server/Grid.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (39 sloc)
1.39 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |