/// <summary> /// Gets the item associated with the specified key. /// </summary> /// <param name="key">The key of the value to get.</param> /// <param name="block">When this method returns, contains the item associated with the specified key, if the key is found; otherwise, the default value for the type of the item parameter. This parameter is passed uninitialized.</param> /// <returns><c>true</c> if the <see cref="GridBlockCollection{TGridBlock}"/> contains an element with the specified key; otherwise, false.</returns> public bool TryGetItem(ShortPoint key, out TGridBlock block) { if (Dictionary != null) { return(Dictionary.TryGetValue(key, out block)); } block = this.FirstOrDefault(b => b.Location == key); return(block != null); }
public GridBlock(int blockSize, ShortPoint location, IEqualityComparer <TCell> cellEqualityComparer) { if (cellEqualityComparer == null) { throw new ArgumentNullException(nameof(cellEqualityComparer)); } BlockSize = blockSize; Location = location; cells = new TCell[BlockSize * BlockSize]; this.cellEqualityComparer = cellEqualityComparer; }
public TCell this[int x, int y] { get { CheckLocation(ref x, ref y); TGridBlock block = null; ShortPoint blockLocation = default(ShortPoint); int cellX, cellY; int blockSize = BlockSize; GridBlock.GetBlockCellLocation(ref x, ref y, ref blockSize, out blockLocation, out cellX, out cellY); if (Blocks.TryGetItem(blockLocation, out block)) { return(block.GetCell(cellX, cellY)); } return(default(TCell)); } set { CheckLocation(ref x, ref y); TGridBlock block = null; ShortPoint blockLocation = default(ShortPoint); int tileX, tileY; int blockSize = BlockSize; GridBlock.GetBlockCellLocation(ref x, ref y, ref blockSize, out blockLocation, out tileX, out tileY); if (!Blocks.TryGetItem(blockLocation, out block)) { if (CellEqualityComparer.Equals(value, default(TCell))) { return; } block = CreateBlock(blockLocation); AddBlock(block); } var currentValue = block.GetCell(tileX, tileY); if (block.SetCell(tileX, tileY, value)) { OnCellContentChanged(tileX, tileY, block, currentValue, value); } } }
protected abstract TGridBlock CreateBlock(ShortPoint blockLocation);