protected override void OnRender(DrawingContext drawingContext)
        {
            bool needUpdatedCells = false;
            if (board == null)
            {
                needUpdatedCells = false;
            } 
            else
            {
                needUpdatedCells = board.cells.Count != cells.Count;
                // do we need more conditions?
                needUpdatedCells = true;
            }
            int location;

            // TODO has the size of render window been changed?
            // if so need to update all cells sizes.

            if (needUpdatedCells)
            {
                grid.Children.Clear();
                cellsX = (byte)board.width;
                cellsY = (byte)board.height;

                if (board.cells.Count != cells.Count)
                {
                    grid.RowDefinitions.Clear();
                    grid.ColumnDefinitions.Clear();

                    for (uint i = 0; i < cellsY; i++)
                    {
                        var rowDefinition = new RowDefinition();
                        rowDefinition.Height = GridLength.Auto;
                        grid.RowDefinitions.Add(rowDefinition);
                    }

                    for (uint i = 0; i < cellsX; i++)
                    {
                        var colDefinition = new ColumnDefinition();
                        colDefinition.Width = GridLength.Auto;
                        grid.ColumnDefinitions.Add(colDefinition);
                    }

                    if (board.cells.Count > cells.Count)
                    {
                        ushort toadd = (ushort)(board.cells.Count - cells.Count);
                        for (uint i = 0; i < toadd; i++)
                        {
                            CellCtrl cell = new CellCtrl();
                            cell.mode = mode;
                            cells.Add(cell);
                        }
                    }
                    else
                    {
                        ushort toremove = (ushort)(cells.Count - board.cells.Count);
                        cells.RemoveRange(cells.Count - toremove, toremove);
                    }
                }

                ushort cur = 0;

                for (byte curX = 0; curX < cellsX; curX++)
                {
                    for (byte curY = 0; curY < cellsY; curY++)
                    {
                        // set grid items size
                        grid.Children.Add(cells[cur]);
                        Grid.SetColumn(cells[cur], curY);
                        Grid.SetRow(cells[cur], curX);

                        cur++;
                    }
                }

                needUpdatedCells = false;
            }

            if (board != null)
            {

                for (location = 0; location < cells.Count; location++)
                {
                    cells[location].data = board.cells[location];
                    cells[location].withEntity = EntityType.None;
                }

                // TODO note this is not a true wall determination for left and up.
                // Maybe this should be fixed?
                for (byte cur = 0; cur < cellsX * cellsY; cur += cellsX)
                {
                    cells[cur].hasLeftWall = true;
                }
                for (byte cur = 0; cur < cellsY; cur++)
                {
                    cells[cur].hasUpWall = true;
                }

                location = (int)((entities[EntityType.Theseus].Y * cellsX) + entities[EntityType.Theseus].X);
                cells[location].withEntity = EntityType.Theseus;

                location = (int)((entities[EntityType.Minotaur].Y * cellsX) + entities[EntityType.Minotaur].X);
                cells[location].withEntity = EntityType.Minotaur;
            }

            if (board == null)
            {
                grid.Children.Clear();
                cells.Clear();
            }

            base.OnRender(drawingContext);
        }
Exemplo n.º 2
0
        protected override void OnRender(DrawingContext drawingContext)
        {
            bool needUpdatedCells = false;

            if (board == null)
            {
                needUpdatedCells = false;
            }
            else
            {
                needUpdatedCells = board.cells.Count != cells.Count;
                // do we need more conditions?
                needUpdatedCells = true;
            }
            int location;

            // TODO has the size of render window been changed?
            // if so need to update all cells sizes.

            if (needUpdatedCells)
            {
                grid.Children.Clear();
                cellsX = (byte)board.width;
                cellsY = (byte)board.height;

                if (board.cells.Count != cells.Count)
                {
                    grid.RowDefinitions.Clear();
                    grid.ColumnDefinitions.Clear();

                    for (uint i = 0; i < cellsY; i++)
                    {
                        var rowDefinition = new RowDefinition();
                        rowDefinition.Height = GridLength.Auto;
                        grid.RowDefinitions.Add(rowDefinition);
                    }

                    for (uint i = 0; i < cellsX; i++)
                    {
                        var colDefinition = new ColumnDefinition();
                        colDefinition.Width = GridLength.Auto;
                        grid.ColumnDefinitions.Add(colDefinition);
                    }

                    if (board.cells.Count > cells.Count)
                    {
                        ushort toadd = (ushort)(board.cells.Count - cells.Count);
                        for (uint i = 0; i < toadd; i++)
                        {
                            CellCtrl cell = new CellCtrl();
                            cell.mode = mode;
                            cells.Add(cell);
                        }
                    }
                    else
                    {
                        ushort toremove = (ushort)(cells.Count - board.cells.Count);
                        cells.RemoveRange(cells.Count - toremove, toremove);
                    }
                }

                ushort cur = 0;

                for (byte curX = 0; curX < cellsX; curX++)
                {
                    for (byte curY = 0; curY < cellsY; curY++)
                    {
                        // set grid items size
                        grid.Children.Add(cells[cur]);
                        Grid.SetColumn(cells[cur], curY);
                        Grid.SetRow(cells[cur], curX);

                        cur++;
                    }
                }

                needUpdatedCells = false;
            }

            if (board != null)
            {
                for (location = 0; location < cells.Count; location++)
                {
                    cells[location].data       = board.cells[location];
                    cells[location].withEntity = EntityType.None;
                }

                // TODO note this is not a true wall determination for left and up.
                // Maybe this should be fixed?
                for (byte cur = 0; cur < cellsX * cellsY; cur += cellsX)
                {
                    cells[cur].hasLeftWall = true;
                }
                for (byte cur = 0; cur < cellsY; cur++)
                {
                    cells[cur].hasUpWall = true;
                }

                location = (int)((entities[EntityType.Theseus].Y * cellsX) + entities[EntityType.Theseus].X);
                cells[location].withEntity = EntityType.Theseus;

                location = (int)((entities[EntityType.Minotaur].Y * cellsX) + entities[EntityType.Minotaur].X);
                cells[location].withEntity = EntityType.Minotaur;
            }

            if (board == null)
            {
                grid.Children.Clear();
                cells.Clear();
            }

            base.OnRender(drawingContext);
        }