fCraft.BuildingCommands.DrawOneBlock C# (CSharp) Method

DrawOneBlock() private static method

private static DrawOneBlock ( [ player, [ map, Block drawBlock, Vector3I coord, BlockChangeContext context, int &blocks, int &blocksDenied, UndoState undoState ) : void
player [
map [
drawBlock Block
coord Vector3I
context BlockChangeContext
blocks int
blocksDenied int
undoState UndoState
return void
        private static void DrawOneBlock( [NotNull] Player player, [NotNull] Map map, Block drawBlock, Vector3I coord,
            BlockChangeContext context, ref int blocks, ref int blocksDenied, UndoState undoState)
        {
            if ( player == null )
                throw new ArgumentNullException( "player" );

            if ( !map.InBounds( coord ) )
                return;
            Block block = map.GetBlock( coord );
            if ( block == drawBlock )
                return;

            if ( player.CanPlace( map, coord, drawBlock, context ) != CanPlaceResult.Allowed ) {
                blocksDenied++;
                return;
            }

            map.QueueUpdate( new BlockUpdate( null, coord, drawBlock ) );
            Player.RaisePlayerPlacedBlockEvent( player, map, coord, block, drawBlock, context );

            if ( !undoState.IsTooLargeToUndo ) {
                if ( !undoState.Add( coord, block ) ) {
                    player.MessageNow( "NOTE: This draw command is too massive to undo." );
                    player.LastDrawOp = null;
                }
            }
            blocks++;
        }

Usage Example

Example #1
0
        public void Draw(Bitmap img)
        {
            //guess how big the draw will be
            int white = System.Drawing.Color.White.ToArgb();
            int left, right, top, bottom;
            int count = Crop(img, out left, out right, out top, out bottom);

            //check if player can make the drawing
            if (!player.CanDraw(count))
            {
                player.Message(String.Format("You are only allowed to run commands that affect up to {0} blocks. " +
                                             "This one would affect {1} blocks.",
                                             player.Info.Rank.DrawLimit, count));
                return;
            }

            int dirX = 0, dirY = 0;

            if (direction == Direction.PlusX)
            {
                dirX = 1;
            }
            if (direction == Direction.MinusX)
            {
                dirX = -1;
            }
            if (direction == Direction.PlusZ)
            {
                dirY = 1;
            }
            if (direction == Direction.MinusZ)
            {
                dirY = -1;
            }
            if (dirX == 0 && dirY == 0)
            {
                return;                         //if blockcount = 0, message is shown and returned
            }
            for (int yy = top; yy <= bottom; yy++)
            {
                for (int xx = left; xx <= right; xx++)
                {
                    if (img.GetPixel(xx, yy).ToArgb() == white)
                    {
                        continue;
                    }
                    int dx = xx - left, dy = bottom - yy;

                    Vector3I coords = new Vector3I(origin.X + dirX * dx, origin.Y + dirY * dx, origin.Z + dy);
                    BuildingCommands.DrawOneBlock(
                        player, player.World.Map, blockColor,
                        coords, BlockChangeContext.Drawn,
                        ref blocks, ref blocksDenied, undoState);
                    blockCount++;
                }
            }
        }