/// <summary> /// Event that handles when a MovableObject moves across the Board. /// </summary> /// <param name="movingObject">Object moving across the Board.</param> /// <param name="moveArgs">Event arguments</param> public void HandleMovingObjectOnBoard(MovableObject movingObject, EventArgs moveArgs) { lock (this.lockObject) { BoardObject objectAtCell = this.boardObjectMap[movingObject.CurrentCellCol, movingObject.CurrentCellRow]; if (objectAtCell != null) { objectAtCell.InteractWithMovingObject(movingObject); } } }
/// <summary> /// Update an object on the Board, either moving it from by cellCol/cellRow to the location specified by boardObject, /// or simply updating the properties of the object in place. /// If no object is present at cellCol,cellRow, this function throws. /// </summary> /// <param name="cellCol">Cell column where the object is currently placed</param> /// <param name="cellRow">Cell row where the object is currently placed</param> /// <param name="boardObject">Object to update (update the col/row fields of this param to move it on the board)</param> public void UpdateObjectOnBoard(int cellCol, int cellRow, BoardObject boardObject) { if (!IsCellOccupied(cellCol, cellRow)) { throw new ArgumentException("Cannot update cell [{0},{1}] because nothing is there yet!"); } // If the "update" is really a "move", then the board removes then re-adds it // If it's just changing non-positional properties in the same cell, we just overwrite it // Either way, we present it to the world as an update, not a remove+add or just add if (boardObject.CellCol != cellCol || boardObject.CellRow != cellRow) { this.RemoveObjectFromBoard(cellCol, cellRow); } this.AddObjectToBoard(boardObject, false); // TODO: arrows shouldn't throw...need to be careful with what else use update for! }
/// <summary> /// Add an object to the Board. /// </summary> /// <param name="boardObject">Object to place on the Board</param> /// <param name="throwIfOccupied">True to throw if an object is already at that cell; false to ignore</param> public void AddObjectToBoard(BoardObject boardObject, bool throwIfOccupied = true) { if (IsCellOccupied(boardObject.CellCol, boardObject.CellRow)) { if (throwIfOccupied) { throw new ArgumentException(String.Format("Item is already present on the board at [{0},{1}].", boardObject.CellCol, boardObject.CellRow)); } else { return; } } lock (this.lockObject) { this.boardObjectMap[boardObject.CellCol, boardObject.CellRow] = boardObject; } }