private bool addUnit()
        {
            // Get the player who owns this building.
            ModelComponent temp = building.Parent;

            while (!(temp is PlayerComponent))
            {
                temp = temp.Parent;
            }
            PlayerComponent player = (PlayerComponent)temp;

            // Get the Gameworld.
            while (!(temp is ZRTSModel.GameModel.GameModel))
            {
                temp = temp.Parent;
            }
            ZRTSModel.GameModel.GameModel model = (ZRTSModel.GameModel.GameModel)temp;

            // Get the CellComponent to insert into.
            CellComponent insertCell = findEmptyNeighborCell(model);

            if (insertCell == null)
            {
                return(false);                // No empty CellComponent.
            }

            // Add Unit to the Map.
            UnitComponent unit = new UnitComponent(stats);

            unit.PointLocation = new PointF(insertCell.X + 0.5f, insertCell.Y + 0.5f);

            // Add Unit to the Player who owns the building.
            player.GetUnitList().AddChild(unit);
            return(true);
        }
        private CellComponent findEmptyNeighborCell(ZRTSModel.GameModel.GameModel model)
        {
            CellComponent insertCell = null;
            int           width      = model.GetScenario().GetGameWorld().GetMap().GetWidth();
            int           height     = model.GetScenario().GetGameWorld().GetMap().GetWidth();

            foreach (CellComponent cell in building.CellsContainedWithin)
            {
                int x = cell.X;
                int y = cell.Y;

                if (x < width - 1)
                {
                    CellComponent c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x + 1, y);
                    if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                    {
                        insertCell = c;
                        break;
                    }

                    if (y < height - 1)
                    {
                        c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x + 1, y + 1);
                        if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                        {
                            insertCell = c;
                            break;
                        }
                    }

                    if (y > 0)
                    {
                        c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x + 1, y);
                        if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                        {
                            insertCell = c;
                            break;
                        }
                    }
                }

                if (x > 0)
                {
                    CellComponent c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x - 1, y);
                    if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                    {
                        insertCell = c;
                        break;
                    }

                    if (y < height - 1)
                    {
                        c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x - 1, y + 1);
                        if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                        {
                            insertCell = c;
                            break;
                        }
                    }

                    if (y > 0)
                    {
                        c = model.GetScenario().GetGameWorld().GetMap().GetCellAt(x - 1, y);
                        if (c.GetTile().Passable() && c.EntitiesContainedWithin.Count == 0)
                        {
                            insertCell = c;
                            break;
                        }
                    }
                }
            }

            return(insertCell);
        }
        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);
        }
        /// <summary>
        /// Load information from the map file
        /// </summary>
        /// <param name="filename"></param>
        protected void LoadModelFromFile(string filename)
        {
            // Create or load the model.
            model = new GameModel();
            ZRTSCompositeViewUIFactory.Initialize(this);

            FileStream mapFile = File.OpenRead(filename);
            ScenarioXMLReader reader = new ScenarioXMLReader(mapFile);
            ScenarioComponent scenario = reader.GenerateScenarioFromXML();

            model.AddChild(scenario);
            model.PlayerInContext = (PlayerComponent)model.GetScenario().GetGameWorld().GetPlayerList().GetChildren()[0];

            foreach (PlayerComponent p in scenario.GetGameWorld().GetPlayerList().GetChildren())
            {
                foreach (PlayerComponent po in scenario.GetGameWorld().GetPlayerList().GetChildren())
                {
                    if (p != po)
                    {
                        p.EnemyList.Add(po);
                    }
                }
            }

            Console.WriteLine(ZRTSModel.Factories.BuildingFactory.Instance.getBuildingTypes()[0]);

            // Create the controller, Remove the old one if it exists.
            if (this.controller != null)
            {
                Components.Remove(this.controller);
            }

            controller = new ZRTSController(this);
            Components.Add(controller);

            // Set the mouse visible
            this.IsMouseVisible = true;

            PlayerComponent player = model.PlayerInContext;

            foreach(PlayerComponent enemy in player.EnemyList)
            {
                WinWhenAllEnemyUnitsDead win = new WinWhenAllEnemyUnitsDead(enemy, scenario);
                scenario.triggers.Add(win);
            }
            LoseWhenAllPlayersUnitsAreDead lose = new LoseWhenAllPlayersUnitsAreDead(player, scenario);
            scenario.triggers.Add(lose);
        }
        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);
        }