Exemple #1
0
        /// <summary>
        /// Draws the entire map, using the chunk handler to get the cell values
        /// </summary>
        /// <param name="chunkHandler"></param>
        public void DrawMap(ChunkHandler chunkHandler, int offsetX, int offsetY)
        {
            //
            // Only draw if the graphics are available
            //
            if (_drawingGraphics == null)
            {
                return;
            }

            int   cell;
            Brush toUse = _activeCellBrush;

            _drawingGraphics.Clear(_backgroundColor);

            //
            // Iterate through all of the cells onscreen
            //
            for (int y = 0; y < _screenHeight; y++)
            {
                for (int x = 0; x < _screenWidth; x++)
                {
                    //
                    // Apply the pan factor, and get the cell value
                    //
                    cell = chunkHandler.GetCellAt(x - GetNearestCell(offsetX), y - GetNearestCell(offsetY));

                    //
                    // Set the color based on the cell value
                    //
                    if (cell == 1)
                    {
                        toUse = _activeCellBrush;
                    }
                    else if (cell > 0)
                    {
                        toUse = _secondaryCellBrush;
                    }

                    //
                    // If the cell is active, fill it in
                    // No matter what, give it an outline
                    //
                    if (cell > 0)
                    {
                        _drawingGraphics.FillRectangle(toUse, x * _cellWidth, y * _cellWidth, _cellWidth, _cellWidth);
                    }
                    if (_cellWidth > 4)
                    {
                        _drawingGraphics.DrawRectangle(_outlinePen, x * _cellWidth, y * _cellWidth, _cellWidth, _cellWidth);
                    }
                }
            }

            //
            // Apply everything we have been drawing to the buffer onto the drawing area
            //
            _buffer.Render(_renderTo);
        }
        /// <summary>
        /// Returns the value of a specific cell in the chunk, using the ChunkHandler to get it if it is in a neighboring chunk
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="chunkHandler"></param>
        /// <returns></returns>
        public int GetChunkCell(int x, int y, ChunkHandler chunkHandler)
        {
            //
            // Declare variables
            //
            int  chunkDeltaX = 0; // Describes if the cell is in a neighboring chunk
            int  chunkDeltaY = 0;
            int  cellX       = x;
            int  cellY       = y;
            bool makeNeighboringChunk;

            //
            // We allow x and y values that go out of the bounds of the chunk,
            // and use this area to check the neighboring chunk if that is the case.
            //
            if (x < 0)
            {
                chunkDeltaX = -1;
                cellX       = _width - 1;
            }
            else if (x >= _width)
            {
                chunkDeltaX = 1;
                cellX       = 0;
            }

            if (y < 0)
            {
                chunkDeltaY = -1;
                cellY       = _height - 1;
            }
            else if (y >= _height)
            {
                chunkDeltaY = 1;
                cellY       = 0;
            }

            //
            // Do not allow hibernating chunks to make more chunks,
            // this would lead to infinite chunks being made.
            //
            makeNeighboringChunk = (_activityLevel == ActivityState.ACTIVE);

            //
            // This if statement only fires if we are checking the neighboring chunk
            //
            if (chunkDeltaX != 0 || chunkDeltaY != 0)
            {
                return(chunkHandler.GetChunkCellAt(_xIndex + chunkDeltaX, _yIndex + chunkDeltaY, cellX, cellY, makeNeighboringChunk));
            }
            //
            // Elsewise, we just return the cell's value
            //
            else
            {
                return(_board[GetUnrolledIndex(x, y)]);
            }
        }
        /// <summary>
        /// Queues up all of the changes for the chunk, according to the
        /// rules of Brian's Brain
        /// </summary>
        /// <param name="chunkHandler"></param>
        public void QueueChangesForBriansBrain(ChunkHandler chunkHandler)
        {
            //
            // For Brian's Brain, we consider
            // 0 as dead
            // 1 as alive
            // 2 as "dying"
            //
            int neighbors;
            int cellValue;

            //
            // Dump the queued up changes
            //
            _cellChanges.Clear();

            //
            // Iterate through the board
            //
            for (int y = 0; y < _height; y++)
            {
                for (int x = 0; x < _width; x++)
                {
                    //
                    // Get the neighbor count, and cell value for each cell
                    //
                    neighbors = GetNeighborCount(x, y, chunkHandler);
                    cellValue = GetChunkCell(x, y, chunkHandler);

                    if (cellValue == 0)
                    {
                        if (neighbors == 2)
                        {
                            _cellChanges.Add(GetUnrolledIndex(x, y), 1);
                        }
                    }
                    else if (cellValue == 1)
                    {
                        _cellChanges.Add(GetUnrolledIndex(x, y), 2);
                    }
                    else
                    {
                        _cellChanges.Add(GetUnrolledIndex(x, y), 0);
                    }
                }
            }
        }
        /// <summary>
        /// Queues up all of the changes for the chunk, according to the rules of Conway's Game of Life
        /// </summary>
        /// <param name="chunkHandler"></param>
        public void QueueChangesForConways(ChunkHandler chunkHandler)
        {
            int neighbors;

            //
            // Dump the queued up changes
            //
            _cellChanges.Clear();

            //
            // Iterate through the board
            //
            for (int y = 0; y < _height; y++)
            {
                for (int x = 0; x < _width; x++)
                {
                    //
                    // Get the neighbor count for each cell
                    //
                    neighbors = GetNeighborCount(x, y, chunkHandler);

                    //
                    // If the cell is alive, only toggle the value if
                    // there are too few or too many neighbors
                    //
                    if (GetChunkCell(x, y, chunkHandler) == 1)
                    {
                        if (neighbors < 2 || neighbors > 3)
                        {
                            _cellChanges.Add(GetUnrolledIndex(x, y), 0);
                        }
                    }
                    //
                    // Elsewise, only toggle the value if there are exactly three neighbors
                    //
                    else
                    {
                        if (neighbors == 3)
                        {
                            _cellChanges.Add(GetUnrolledIndex(x, y), 1);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets the number of neighbor cells active around a particular cell
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="chunkHandler"></param>
        /// <returns></returns>
        private int GetNeighborCount(int x, int y, ChunkHandler chunkHandler)
        {
            int neighbors = 0;

            //
            // Iterate through all of the neighbors
            //
            for (int deltaY = -1; deltaY <= 1; deltaY++)
            {
                for (int deltaX = -1; deltaX <= 1; deltaX++)
                {
                    //
                    // First, check that it is only a neighbor, not the cell itself.
                    // Second, check if it's value is one, if so, increment the neighbor variable.
                    //
                    if ((deltaX != 0 || deltaY != 0) && GetChunkCell(x + deltaX, y + deltaY, chunkHandler) == 1)
                    {
                        neighbors++;
                    }
                }
            }

            return(neighbors);
        }