/// <summary>
        /// Called when the scenario has changed.  Unregisters the map view with all pieces of model it was associated with, and registers with
        /// the new scenario.
        /// </summary>
        /// <param name="scenario"></param>
        public void SetScenario(ScenarioComponent scenario)
        {
            context = scenario;

            // Empty the view.
            this.mapPanel.Hide();
            foreach (Control c in mapPanel.Controls)
            {
                c.AllowDrop = false;
                if (c is TileUI)
                    ((TileUI)c).UnregisterFromEvents();
            }
            mapPanel.Controls.Clear();
            realizedComponents.Clear();
            this.mapPanel.Show();

            if (scenario != null)
            {
                ZRTSModel.Map map = scenario.GetGameWorld().GetMap();

                this.mapPanel.Size = new System.Drawing.Size(map.GetWidth() * PIXELS_PER_COORDINATE, map.GetHeight() * PIXELS_PER_COORDINATE);

                // Location of the map panel
                int xLoc, yLoc;

                if (this.mapPanel.Size.Width > this.Size.Width)
                {
                    xLoc = 0;
                }
                else
                {
                    // Place the panel in the center.
                    xLoc = (this.Size.Width - this.mapPanel.Size.Width) / 2;
                }

                if (this.mapPanel.Size.Height > this.Size.Height)
                {
                    yLoc = 0;
                }
                else
                {
                    // Place the panel in the center.
                    yLoc = (this.Size.Height - this.mapPanel.Size.Height) / 2;
                }

                this.mapPanel.Location = new System.Drawing.Point(xLoc, yLoc);
                this.RealizeView();

                if (scenario.GetGameWorld().GetPlayerList() != null)
                {
                    scenario.GetGameWorld().GetPlayerList().PlayerAddedEvent += this.PlayerAdded;
                    scenario.GetGameWorld().GetPlayerList().PlayerRemovedEvent += this.PlayerRemoved;
                    foreach (PlayerComponent p in scenario.GetGameWorld().GetPlayerList().GetChildren())
                    {
                        p.BuildingList.BuildingAddedEventHandlers += this.BuildingAdded;
                        p.BuildingList.BuildingRemovedEventHandlers += this.BuildingRemoved;
                    }
                }
            }
        }
        /// <summary>
        /// Creates a CreateNewScenario dialog and uses it to determine the name and size of the new scenario.  Then, generates a 
        /// new scenario of the appropriate size.
        /// </summary>
        public void createNewScenario()
        {
            if (model.GetScenario() != null)
            {
                model.GetScenario().RemoveChild(model.GetScenario().GetGameWorld());
                // TODO: Ask if the user wants to discard the current scenario or save it.
            }
            CreateNewScenarioDialog dialog = new CreateNewScenarioDialog();
            dialog.ShowDialog();

            if (dialog.ExitWithCreate)
            {
                // Create a scenario with a map of the appropriate size
                ScenarioComponent scenario = new ScenarioComponent(dialog.ScenarioWidth, dialog.ScenarioHeight);

                // Add grass cells at each cell.
                ZRTSModel.Map map = scenario.GetGameWorld().GetMap();
                for (int i = 0; i < map.GetWidth(); i++)
                {
                    for (int j = 0; j < map.GetHeight(); j++)
                    {
                        CellComponent cell = new CellComponent();
                        cell.AddChild(new Grass());
                        cell.X = i;
                        cell.Y = j;
                        map.AddChild(cell);
                    }
                }

                // TODO: Update SaveInfo model to change filename and UpToDate flag.

                // Automatically discards old scenario, by overloaded AddChild function.
                model.AddChild(scenario);

                // Empty the command queue
                model.GetCommandStack().EmptyStacks();

                // We may have just destroyed a large scenario, so collect that garbage.
                // Commented out - only used for testing purposes.  The C# garbage collector takes a LONG time to be activated if this call is not made,
                // but if the call is made, it disrupts UI.
                // GC.Collect();
            }
        }
        private void setupModel()
        {
            model = new GameModel();
            ScenarioComponent scenario = new ScenarioComponent(20, 20); // 20 x 20 Gameworld.

            // Add grass cells at each cell.
            ZRTSModel.Map map = scenario.GetGameWorld().GetMap();
            for (int i = 0; i < map.GetWidth(); i++)
            {
                for (int j = 0; j < map.GetHeight(); j++)
                {
                    CellComponent cell = new CellComponent();
                    cell.AddChild(new Sand());
                    cell.X = i;
                    cell.Y = j;
                    map.AddChild(cell);
                }
            }
            model.AddChild(scenario);

            //Create two players and set them to be enemies.
            model.GetScenario().GetGameWorld().GetPlayerList().AddChild(new PlayerComponent());
            model.GetScenario().GetGameWorld().GetPlayerList().AddChild(new PlayerComponent());
            PlayerComponent player1 = (PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[0];
            PlayerComponent player2 = (PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[1];
            player1.EnemyList.Add(player2);
            player2.EnemyList.Add(player1);
        }
        public void SetScenario(ScenarioComponent scenario)
        {
            if (gameworld != null)
            {
                gameworld.UnregisterObserver(this);
            }
            context = scenario;

            if (scenario != null)
            {
                gameworld = scenario.GetGameWorld();
                if (gameworld != null)
                {
                    gameworld.RegisterObserver(this);
                }
            }
        }
        private void setUpModel()
        {
            model = new GameModel();
            ScenarioComponent scenario = new ScenarioComponent(20, 20); // 20 x 20 Gameworld.
            Building obstruction = new Building();

            // Add grass cells at each cell.
            ZRTSModel.Map map = scenario.GetGameWorld().GetMap();
            for (int i = 0; i < map.GetWidth(); i++)
            {
                for (int j = 0; j < map.GetHeight(); j++)
                {
                    CellComponent cell = new CellComponent();
                    cell.AddChild(new Sand());
                    cell.X = i;
                    cell.Y = j;

                    if (i >= 2 && i <= 10 && j >= 2 && j <= 10)
                        cell.AddEntity(obstruction);

                    if (i >= 15 && i <= 18 && j >= 15 && j <= 18)
                        cell.AddEntity(obstruction);
                    if (i == 16 && j == 16)
                        cell.RemoveEntity(obstruction);

                    map.AddChild(cell);

                }
            }
            model.AddChild(scenario);
        }