public GameCell(GameObject gameObject) { this.gameObject = gameObject; }
public CanMoveResult TryMoveObjectHere(GameObject obj, int startRow, int startColumn) { CanMoveResult result = CanMoveObjectHere(obj, startRow, startColumn); if (result != CanMoveResult.Success) return result; RemoveObject(obj); FillObjectCells(obj, startRow, startColumn); objectDict[obj] = new GameCellPos(startRow, startColumn); return CanMoveResult.Success; }
void FillObjectCells(GameObject obj, int startRow, int startColumn) { for (int i = 0; i < obj.Height; i++) { for (int j = 0; j < obj.Width; j++) { if (!obj.Bitmap[i, j]) continue; cells[i + startRow][j + startColumn] = new GameCell(obj); } } }
public bool TryGetObjectPosition(GameObject obj, out GameCellPos position) { return objectDict.TryGetValue(obj, out position); }
public CanMoveResult TryMoveObject(GameObject obj, int deltaRow, int deltaColumn) { GameCellPos start; if (!TryGetObjectPosition(obj, out start)) return CanMoveResult.ObjectNotFound; return TryMoveObjectHere(obj, start.Row + deltaRow, start.Column + deltaColumn); }
public CanMoveResult CanMoveObjectHere(GameObject obj, int startRow, int startColumn) { for (int i = 0; i < obj.Height; i++) { for (int j = 0; j < obj.Width; j++) { if (!obj.Bitmap[i, j]) continue; int iTable = i + startRow; int jTable = j + startColumn; if (iTable < 0 || iTable >= Height || jTable < 0 || jTable >= Width) return CanMoveResult.TableBoundsCollision; } } for (int i = 0; i < obj.Height; i++) { for (int j = 0; j < obj.Width; j++) { if (!obj.Bitmap[i, j]) continue; int iTable = i + startRow; int jTable = j + startColumn; if (cells[iTable][jTable].GameObject != null && cells[iTable][jTable].GameObject != obj) return CanMoveResult.ObjectCollision; } } return CanMoveResult.Success; }
public bool RemoveObject(GameObject obj) { GameCellPos oldStart; if (!objectDict.TryGetValue(obj, out oldStart)) return false; ClearObjectCells(obj, oldStart.Row, oldStart.Column); objectDict.Remove(obj); return true; }
public void AddObject(GameObject obj, int startRow, int startColumn) { if (TryMoveObjectHere(obj, startRow, startColumn) != CanMoveResult.Success) throw new InvalidOperationException(string.Format("Can't place object here {0}:{1}.", startRow, startColumn)); }
public bool RemoveObjectFromColorCache(GameObject obj) { bool colorResult = objectColorDict.Remove(obj); Image image; if (objectImageDict.TryGetValue(obj, out image)) { image.Dispose(); return true; } return colorResult; }
public DrawCellInfo(GameCellPos cellPos, GameCellPos objectPos, GameObject obj) : this(cellPos) { this.objectPos = objectPos; this.obj = obj; }
public Image GetObjectImage(GameObject obj, Color objectColor) { if (obj == null || grayCellImageData == null) return null; Image objectImage; if (!objectImageDict.TryGetValue(obj, out objectImage)) { objectImage = CommonHelper.GetColorImage(grayCellImageData, objectColor); objectImageDict.Add(obj, objectImage); } return objectImage; }
public Color GetObjectColor(GameObject obj) { if (obj == null) { return Color.MidnightBlue; } Color color; if (!objectColorDict.TryGetValue(obj, out color)) { color = Color.FromArgb(100 + randomize.Next(155), 100 + randomize.Next(155), 100 + randomize.Next(155)); objectColorDict.Add(obj, color); } return color; }