Example #1
0
        public bool AddItem(Item item, Point location, IHistoryTransaction trans,
                            CollisionAction collisionAction = CollisionAction.ThrowException)
        {
            if (!item.IsRoot)
            {
                throw new InvalidOperationException("Only root items may be added to the field.");
            }
            Size size = item.GetSize();
            var  l    = mapManager.CurrentLayer;

            var itemRect = new Rectangle(location, size);

            if (IsOccupied(itemRect))
            {
                switch (collisionAction)
                {
                case CollisionAction.ThrowException:
                    throw new InvalidOperationException("The specified location is already occupied.");

                case CollisionAction.Abort:
                    return(false);

                case CollisionAction.Overwrite:
                    DeleteRect(itemRect, trans);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(collisionAction), collisionAction, null);
                }
            }

            if (!IsInWorldBounds(itemRect))
            {
                switch (collisionAction)
                {
                case CollisionAction.ThrowException:
                    throw new InvalidOperationException("The specified location is outside of the world.");

                case CollisionAction.Abort:
                    return(false);

                case CollisionAction.Overwrite:
                    return(false);

                default:
                    throw new ArgumentOutOfRangeException(nameof(collisionAction), collisionAction, null);
                }
            }

            trans?.AddStep(new AddItemStep(item, location, this));
            Item tile = l.GetTile(location);

            l.SetExtensionTiles(item, location.X, location.Y);
            tile.CopyFrom(item);
            return(true);
        }
Example #2
0
        public bool DeleteTile(Point tilePt, IHistoryTransaction?trans, bool resolveExtensions = false)
        {
            if (!IsInWorldBounds(tilePt))
            {
                return(false);
            }

            Item tile = mapManager.CurrentLayer.GetTile(tilePt);

            if (tile.IsNone)
            {
                return(false);
            }

            if (!tile.IsRoot)
            {
                if (resolveExtensions)
                {
                    if (tile.IsExtension)
                    {
                        tilePt.Offset(-tile.ExtensionX, -tile.ExtensionY);
                        tile = mapManager.CurrentLayer.GetTile(tilePt);
                    }
                    else
                    {
                        throw new InvalidOperationException("Specified location is not a root or extension tile.");
                    }
                }
                else
                {
                    throw new InvalidOperationException("Specified location is not a root tile.");
                }
            }

            Debug.Assert(tile.IsRoot);

            trans?.AddStep(new DeleteItemStep(tile, tilePt, this));
            mapManager.CurrentLayer.DeleteExtensionTiles(tile, tilePt);
            tile.Delete();
            return(true);
        }