Esempio n. 1
0
        /// <summary>
        /// find the closest cell from the specified destination
        /// </summary>
        /// <param name="point">Destination</param>
        /// <returns>Closest cell</returns>
        private CellComponent findClosestCell(PointF point)
        {
            double        distanceSquared = Math.Pow(map.GetWidth(), 2.0) + Math.Pow(map.GetHeight(), 2.0);
            CellComponent cell            = null;

            for (int i = Math.Max((int)building.PointLocation.X - 1, 0); i <= building.PointLocation.X + building.Width + 1; i++)
            {
                for (int j = Math.Max((int)building.PointLocation.Y - 1, 0); j <= building.PointLocation.Y + building.Height + 1; j++)
                {
                    // Ignore cases in the middle of the proposed building site.
                    if (!(i >= (int)building.PointLocation.X && i < (int)building.PointLocation.X + building.Width && j >= (int)building.PointLocation.Y && j < (int)building.PointLocation.Y + building.Height))
                    {
                        double calculatedDistanceSquared = findDistanceSquared(point.X, point.Y, i, j);
                        if (calculatedDistanceSquared <= distanceSquared)
                        {
                            if (map.GetCellAt(i, j) != null)
                            {
                                cell            = map.GetCellAt(i, j);
                                distanceSquared = calculatedDistanceSquared;
                            }
                        }
                    }
                }
            }

            return(cell);
        }
        private void stopListeningToCells(PointF oldPoint)
        {
            // Get the Correct Bounds.
            int startX = getStartX();
            int endX   = getEndX();
            int startY = getStartX();
            int endY   = getEndY();

            // Get the Map object for this game.
            ModelComponent gameWorldComponent = location;

            while (!(gameWorldComponent is Gameworld))
            {
                gameWorldComponent = gameWorldComponent.Parent;
            }
            Map map = ((Gameworld)(gameWorldComponent)).GetMap();

            for (int i = startX; i < endX; i++)
            {
                for (int j = startY; j < endY; j++)
                {
                    CellComponent cell = map.GetCellAt(i, j);
                    cell.UnitAddedEvent -= new EntityInCellChangedHandler(handleUnitAddedToCell);
                }
            }
        }
        private void listenToCellsWithinVisibilityRange()
        {
            // Get the Correct Bounds.
            int startX = getStartX();
            int endX   = getEndX();
            int startY = getStartY();
            int endY   = getEndY();

            // Get the Map object.
            ModelComponent gameWorldComponent = location;

            while (!(gameWorldComponent is Gameworld))
            {
                gameWorldComponent = gameWorldComponent.Parent;
            }
            Map map = ((Gameworld)(gameWorldComponent)).GetMap();

            for (int i = startX; i <= endX; i++)
            {
                for (int j = startY; j <= endY; j++)
                {
                    CellComponent cell = map.GetCellAt(i, j);
                    cell.UnitAddedEvent += new EntityInCellChangedHandler(handleUnitAddedToCell);
                }
            }
        }
        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);
        }
        public TileUI(MapEditorController controller, CellComponent observable)
        {
            InitializeComponent();
            this.controller = controller;
            this.cell = observable;

            // Initialize Image
            if (observable != null)
            {
                // Register for TileChange event.
                observable.TileChangedEvent += this.ChangeTile;
                observable.UnitAddedEvent += this.UnitAddedToCell;
                observable.UnitRemovedEvent += this.UnitRemovedFromCell;

                TileFactory tf = TileFactory.Instance;
                this.Image = tf.getBitmapImproved(observable.GetTile());

                foreach (ModelComponent m in observable.EntitiesContainedWithin)
                {
                    if (m is UnitComponent)
                    {
                        UnitUI unitUI = new UnitUI(controller, m as UnitComponent);
                        Controls.Add(unitUI);
                        unitUI.MouseClick += TileUI_MouseDown;
                    }
                }
            }

            AllowDrop = true;
        }
 public void Visit(CellComponent cell)
 {
     if (CellVisitor != null)
     {
         cell.Accept(CellVisitor);
     }
 }
        /*
         * public functions
         */
        /// <summary>
        /// The core function; calls all valid auxiliary functions and returns the path (advanced features can be toggled)
        /// Includes a boolean toggle for use of advanced pathfinding functions
        /// </summary>
        /// <param name="map">The Map</param>
        /// <param name="start">The starting Cell</param>
        /// <param name="end">The ending Cell</param>
        /// <param name="advanced"> A boolean toggle for advanced functions</param>
        /// <returns>The path as a list of waypoints</returns>
        public static List<CellComponent> between(Map map, CellComponent start, CellComponent end, bool advanced)
        {
            // begin timing the operation
            DateTime startTime = DateTime.Now;

            // convert the given Cell-based data to Node-based data
            NodeMap nodeMap = new NodeMap(map);
            Node nodeStart = nodeMap.getNode(start.X, start.Y);
            Node nodeEnd = nodeMap.getNode(end.X, end.Y);

            // perform advanced pre-calculation tasks
            if (advanced)
            {
                // if the end Node is invalid, replace it with the nearest valid Node
                if (!nodeEnd.isValid)
                {
                    nodeEnd = Advanced.nearestValidEnd(nodeMap, nodeStart, nodeEnd);
                }
            }

            // find the path
            List<Node> nodePath = Basic.findPath(nodeMap, nodeStart, nodeEnd);

            // convert the path from List<Node> format back to List<Cell> format
            List<CellComponent> path = new List<CellComponent>(nodePath.Count);
            for (int i = 0; i < nodePath.Count; i++)
                path.Add(map.GetCellAt(nodePath[i].X, nodePath[i].Y));

            // grab and print path data
            float dist = (float)(nodePath[nodePath.Count - 1].Gscore);
            span = DateTime.Now - startTime;
            //printPath(path, dist);

            return path;
        }
Esempio n. 8
0
 public void Visit(CellComponent cell)
 {
     output.WriteStartElement("Cell");
     output.WriteAttributeString("X", cell.X.ToString());
     output.WriteAttributeString("Y", cell.Y.ToString());
     VisitChildren(cell);
     output.WriteEndElement();
 }
 public void UnregisterFromEvents()
 {
     cell.TileChangedEvent -= this.ChangeTile;
     cell.UnitAddedEvent -= this.UnitAddedToCell;
     cell.UnitRemovedEvent -= this.UnitRemovedFromCell;
     AllowDrop = false;
     cell = null;
     // Keep the image from disposing.
     this.Image = null;
 }
Esempio n. 10
0
 /// <summary>
 /// Remove cell from the map
 /// </summary>
 /// <param name="child"></param>
 public override void RemoveChild(ModelComponent child)
 {
     if (GetChildren().Contains(child))
     {
         // This ensures that the child is a cell, and that it is actually contained in the map.
         CellComponent cell = (CellComponent)child;
         cells[cell.X, cell.Y] = null;
         base.RemoveChild(child);
     }
 }
 private void initializeToGrass(int width, int height)
 {
     for (int i = 0; i < width; i++)
     {
         for (int j = 0; j < height; j++)
         {
             cells[i, j] = new CellComponent();
             cells[i, j].AddChild(new Grass());
             cells[i, j].SetContainer(this);
         }
     }
 }
        /// <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();
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Add cell to the map (aka tile)
 /// </summary>
 /// <param name="child"></param>
 public override void AddChild(ModelComponent child)
 {
     // Ensure that only cells are children to the map
     if (child is CellComponent)
     {
         CellComponent cell = (CellComponent)child;
         // Ensure that the cell is inbounds
         if (cell.X >= 0 && cell.X < width && cell.Y >= 0 && cell.Y < height)
         {
             // Remove cell currently located at that position
             if (cells[cell.X, cell.Y] != null)
             {
                 RemoveChild(cells[cell.X, cell.Y]);
             }
             cells[cell.X, cell.Y] = cell;
             base.AddChild(cell);
         }
     }
 }
Esempio n. 14
0
        /// <summary>
        /// This function will perform a building cycle if the number of ticks since the last cycle is equal to TICKS_PER_CYCLE.
        /// </summary>
        /// <returns>true if the building is complete and the action is finished, false otherwise.</returns>
        public override bool Work()
        {
            if (!building.Completed)
            {
                if (curTicks % TICKS_PER_CYCLE == 0)
                {
                    // Check if unit is adjacent to building.
                    if (isUnitNextToBuilding())
                    {
                        UnitComponent unit = (UnitComponent)Parent.Parent;
                        unit.State = UnitComponent.UnitState.BUILDING;

                        UnitComponent worker = (UnitComponent)Parent.Parent;
                        // Add the building to the model if we have not done so yet.
                        if (building.Parent == null)
                        {
                            // TODO: Ensure that the spaces are cleared.  Perhaps wait/give up, as with move?
                            PlayerComponent player = Parent.Parent.Parent.Parent as PlayerComponent;

                            if (!map.addBuildingToMap(building))    // add building to the map
                            {
                                return(false);
                            }

                            player.addBuilding(building);       // add building to player's building list
                        }

                        updateBuildingProgress(building, worker);
                    }
                    else
                    {
                        // Move towards the building. Insert a move action into the Unit's action queue.
                        CellComponent targetCell = findClosestCell(((UnitComponent)Parent.Parent).PointLocation);
                        MoveAction    moveAction = new MoveAction(targetCell.X, targetCell.Y, map, ((UnitComponent)Parent.Parent));
                        Parent.AddChildAt(moveAction, 0);
                    }
                }
            }
            curTicks++;
            return(building.Completed);
        }
 private void initializeToGrass(int width, int height)
 {
     for (int i = 0; i < width; i++)
     {
         for (int j = 0; j < height; j++)
         {
             cells[i, j] = new CellComponent();
             cells[i, j].AddChild(new Grass());
             cells[i, j].SetContainer(this);
         }
     }
 }
        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);
        }
 public void Visit(CellComponent cell)
 {
     output.WriteStartElement("Cell");
     output.WriteAttributeString("X", cell.X.ToString());
     output.WriteAttributeString("Y", cell.Y.ToString());
     VisitChildren(cell);
     output.WriteEndElement();
 }
 public AddUnitCommand(UnitComponent unit, PlayerComponent player, CellComponent cell)
 {
     this.unit = unit;
     this.player = player;
     this.cell = cell;
 }
 public virtual void Visit(CellComponent cell)
 {
     Visit((ModelComponent)cell);
 }
        internal void OnClickMapCell(CellComponent cellComponent, float xPercent, float yPercent)
        {
            if (model.GetSelectionState().SelectionType == typeof(ZRTSModel.Tile))
            {
                TileFactory tf = TileFactory.Instance;
                ZRTSModel.Tile tile = tf.GetImprovedTile(model.GetSelectionState().SelectedTileType);
                ChangeCellTileCommand command = new ChangeCellTileCommand(cellComponent, tile);

                if (command.CanBeDone())
                {
                    model.GetCommandStack().ExecuteCommand(command);
                }
            }
            else if (model.GetSelectionState().SelectionType == typeof(UnitComponent))
            {
                UnitFactory uf = UnitFactory.Instance;
                UnitComponent unit = uf.Create(model.GetSelectionState().SelectedUnitType);
                //unit.PointLocation = new PointF((float)cellComponent.X + xPercent, (float)cellComponent.Y + yPercent);
                PlayerComponent player = model.GetScenario().GetGameWorld().GetPlayerList().GetPlayerByName(model.GetSelectionState().SelectedPlayer);
                AddUnitCommand command = new AddUnitCommand(unit, player, cellComponent);

                if (command.CanBeDone())
                {
                    model.GetCommandStack().ExecuteCommand(command);
                }
            }
            else if (model.GetSelectionState().SelectionType == typeof(Building))
            {
                BuildingFactory bf = BuildingFactory.Instance;
                Building building = bf.Build(model.GetSelectionState().SelectedBuildingType, true);
                //building.PointLocation = new PointF((float)cellComponent.X + xPercent, (float)cellComponent.Y + yPercent);
                PlayerComponent player = model.GetScenario().GetGameWorld().GetPlayerList().GetPlayerByName(model.GetSelectionState().SelectedPlayer);
                AddBuildingCommand command = new AddBuildingCommand(building, player, cellComponent);

                if (command.CanBeDone())
                {
                    model.GetCommandStack().ExecuteCommand(command);
                }
            }
        }
        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);
        }
 public virtual void Visit(CellComponent cell)
 {
     Visit((ModelComponent)cell);
 }
 /// <summary>
 /// the basic function; requires only the map and the start and end Cells (advanced features are turned on)
 /// </summary>
 /// <param name="map">The Map</param>
 /// <param name="start">The starting Cell</param>
 /// <param name="end">The ending Cell</param>
 /// <returns>The path as a list of waypoints</returns>
 public static List<CellComponent> between(Map map, CellComponent start, CellComponent end)
 {
     return between(map, start, end, true);
 }
        internal void OnDragMapCell(CellComponent cell)
        {
            if (model.GetSelectionState().SelectionType == typeof(ZRTSModel.Tile))
            {
                TileFactory tf = TileFactory.Instance;
                ZRTSModel.Tile tile = tf.GetImprovedTile(model.GetSelectionState().SelectedTileType);
                ChangeCellTileCommand command = new ChangeCellTileCommand(cell, tile);

                if (command.CanBeDone())
                {
                    model.GetCommandStack().ExecuteCommand(command);
                }
            }
        }
        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 AddBuildingCommand(Building building, PlayerComponent player, CellComponent cell)
 {
     this.building = building;
     this.player = player;
     this.cell = cell;
 }
 public void Visit(CellComponent cell)
 {
     if (CellVisitor != null)
         cell.Accept(CellVisitor);
 }
Esempio n. 28
0
        public ScenarioComponent GenerateScenarioFromXML()
        {
            ScenarioComponent scenario = null;

            if (reader.Read())
            {
                // Go to the Scenario (skip the XML line)
                reader.Read();
                scenario = new ScenarioComponent();
                ModelComponent currentComponent = scenario;
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        switch (reader.Name)
                        {
                        case "Gameworld":
                            Gameworld gameworld = new Gameworld();
                            currentComponent.AddChild(gameworld);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = gameworld;
                            }
                            break;

                        case "Map":
                            int width  = Int32.Parse(reader.GetAttribute("Width"));
                            int height = Int32.Parse(reader.GetAttribute("Height"));
                            Map map    = new Map(width, height);
                            currentComponent.AddChild(map);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = map;
                            }
                            break;

                        case "Cell":
                            int           x    = Int32.Parse(reader.GetAttribute("X"));
                            int           y    = Int32.Parse(reader.GetAttribute("Y"));
                            CellComponent cell = new CellComponent();
                            cell.X = x;
                            cell.Y = y;
                            currentComponent.AddChild(cell);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = cell;
                            }
                            break;

                        case "PlayerList":
                            PlayerList playerList = new PlayerList();
                            currentComponent.AddChild(playerList);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = playerList;
                            }
                            break;

                        case "Player":
                            PlayerComponent player = new PlayerComponent();
                            player.Name  = reader.GetAttribute("Name");
                            player.Race  = reader.GetAttribute("Race");
                            player.Gold  = Int32.Parse(reader.GetAttribute("Gold"));
                            player.Metal = Int32.Parse(reader.GetAttribute("Metal"));
                            player.Wood  = Int32.Parse(reader.GetAttribute("Wood"));
                            currentComponent.AddChild(player);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = player;
                            }
                            break;

                        case "BuildingList":
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = ((PlayerComponent)currentComponent).BuildingList;
                            }
                            break;

                        case "UnitList":
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = ((PlayerComponent)currentComponent).GetUnitList();
                            }
                            break;

                        case "Sand":
                            Sand sand = new Sand();
                            currentComponent.AddChild(sand);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = sand;
                            }
                            break;

                        case "Mountain":
                            Mountain mountain = new Mountain();
                            currentComponent.AddChild(mountain);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = mountain;
                            }
                            break;

                        case "Grass":
                            Grass grass = new Grass();
                            currentComponent.AddChild(grass);
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = grass;
                            }
                            break;

                        case "Unit":
                            UnitComponent unit = new UnitComponent();
                            currentComponent.AddChild(unit);
                            float unitX = float.Parse(reader.GetAttribute("X"));
                            float unitY = float.Parse(reader.GetAttribute("Y"));
                            unit.PointLocation = new PointF(unitX, unitY);
                            unit.Type          = reader.GetAttribute("Type");
                            unit.MaxHealth     = short.Parse(reader.GetAttribute("MaxHealth"));
                            unit.CurrentHealth = short.Parse(reader.GetAttribute("CurrentHealth"));
                            unit.CanHarvest    = bool.Parse(reader.GetAttribute("CanHarvest"));
                            unit.CanAttack     = bool.Parse(reader.GetAttribute("CanAttack"));
                            unit.Attack        = short.Parse(reader.GetAttribute("Attack"));
                            unit.AttackRange   = float.Parse(reader.GetAttribute("AttackRange"));
                            unit.AttackTicks   = byte.Parse(reader.GetAttribute("AttackTicks"));
                            unit.CanBuild      = bool.Parse(reader.GetAttribute("CanBuild"));
                            unit.BuildSpeed    = byte.Parse(reader.GetAttribute("BuildSpeed"));
                            unit.Speed         = float.Parse(reader.GetAttribute("Speed"));

                            /*
                             * Type="zombie" CanAttack="True"
                             * Attack="20" AttackRange="4" AttackTicks="10"
                             * BuildSpeed="30" CanBuild="True" CanHarvest="False"
                             * CurrentHealth="100" MaxHealth="100" X="12" Y="13"
                             * Speed="0.1"
                             */
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = unit;
                            }
                            break;

                        case "Building":
                            Building building = new Building();
                            currentComponent.AddChild(building);
                            building.Width         = Int32.Parse(reader.GetAttribute("Width"));
                            building.Height        = Int32.Parse(reader.GetAttribute("Height"));
                            building.PointLocation = new PointF(float.Parse(reader.GetAttribute("X")), float.Parse(reader.GetAttribute("Y")));
                            building.Type          = reader.GetAttribute("Type");
                            building.CanProduce    = bool.Parse(reader.GetAttribute("CanProduce"));
                            if (!reader.IsEmptyElement)
                            {
                                currentComponent = building;
                            }
                            break;

                        default:
                            break;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (currentComponent != null)
                        {
                            currentComponent = currentComponent.Parent;
                        }
                        break;
                    }
                }
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("===================");
                // Read this element's properties and display them on console
                Console.WriteLine("Name:" + reader.Name);
                Console.WriteLine("Base URI:" + reader.BaseURI);
                Console.WriteLine("Local Name:" + reader.LocalName);
                Console.WriteLine("Attribute Count:" + reader.AttributeCount.ToString());
                Console.WriteLine("Depth:" + reader.Depth.ToString());
                Console.WriteLine("Node Type:" + reader.NodeType.ToString());
                Console.WriteLine("Attribute Count:" + reader.Value.ToString());
            }
            return(scenario);
        }
 public ChangeCellTileCommand(CellComponent cell, ZRTSModel.Tile tile)
 {
     targetCell = cell;
     targetTile = tile;
 }
        public ScenarioComponent GenerateScenarioFromXML()
        {
            ScenarioComponent scenario = null;
            if (reader.Read())
            {
                // Go to the Scenario (skip the XML line)
                reader.Read();
                scenario = new ScenarioComponent();
                ModelComponent currentComponent = scenario;
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.Name)
                            {
                                case "Gameworld":
                                    Gameworld gameworld = new Gameworld();
                                    currentComponent.AddChild(gameworld);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = gameworld;
                                    break;
                                case "Map":
                                    int width = Int32.Parse(reader.GetAttribute("Width"));
                                    int height = Int32.Parse(reader.GetAttribute("Height"));
                                    Map map = new Map(width, height);
                                    currentComponent.AddChild(map);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = map;
                                    break;
                                case "Cell":
                                    int x = Int32.Parse(reader.GetAttribute("X"));
                                    int y = Int32.Parse(reader.GetAttribute("Y"));
                                    CellComponent cell = new CellComponent();
                                    cell.X = x;
                                    cell.Y = y;
                                    currentComponent.AddChild(cell);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = cell;
                                    break;
                                case "PlayerList":
                                    PlayerList playerList = new PlayerList();
                                    currentComponent.AddChild(playerList);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = playerList;
                                    break;
                                case "Player":
                                    PlayerComponent player = new PlayerComponent();
                                    player.Name = reader.GetAttribute("Name");
                                    player.Race = reader.GetAttribute("Race");
                                    player.Gold = Int32.Parse(reader.GetAttribute("Gold"));
                                    player.Metal = Int32.Parse(reader.GetAttribute("Metal"));
                                    player.Wood = Int32.Parse(reader.GetAttribute("Wood"));
                                    currentComponent.AddChild(player);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = player;
                                    break;
                                case "BuildingList":
                                    if (!reader.IsEmptyElement)
                                        currentComponent = ((PlayerComponent)currentComponent).BuildingList;
                                    break;
                                case "UnitList":
                                    if (!reader.IsEmptyElement)
                                        currentComponent = ((PlayerComponent)currentComponent).GetUnitList();
                                    break;
                                case "Sand":
                                    Sand sand = new Sand();
                                    currentComponent.AddChild(sand);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = sand;
                                    break;
                                case "Mountain":
                                    Mountain mountain = new Mountain();
                                    currentComponent.AddChild(mountain);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = mountain;
                                    break;
                                case "Grass":
                                    Grass grass = new Grass();
                                    currentComponent.AddChild(grass);
                                    if (!reader.IsEmptyElement)
                                        currentComponent = grass;
                                    break;
                                case "Unit":
                                    UnitComponent unit = new UnitComponent();
                                    currentComponent.AddChild(unit);
                                    float unitX = float.Parse(reader.GetAttribute("X"));
                                    float unitY = float.Parse(reader.GetAttribute("Y"));
                                    unit.PointLocation = new PointF(unitX, unitY);
                                    unit.Type = reader.GetAttribute("Type");
                                    unit.MaxHealth = short.Parse(reader.GetAttribute("MaxHealth"));
                                    unit.CurrentHealth = short.Parse(reader.GetAttribute("CurrentHealth"));
                                    unit.CanHarvest = bool.Parse(reader.GetAttribute("CanHarvest"));
                                    unit.CanAttack = bool.Parse(reader.GetAttribute("CanAttack"));
                                    unit.Attack = short.Parse(reader.GetAttribute("Attack"));
                                    unit.AttackRange = float.Parse(reader.GetAttribute("AttackRange"));
                                    unit.AttackTicks = byte.Parse(reader.GetAttribute("AttackTicks"));
                                    unit.CanBuild = bool.Parse(reader.GetAttribute("CanBuild"));
                                    unit.BuildSpeed = byte.Parse(reader.GetAttribute("BuildSpeed"));
                                    unit.Speed = float.Parse(reader.GetAttribute("Speed"));
                                    /*
                                     * Type="zombie" CanAttack="True"
                                     * Attack="20" AttackRange="4" AttackTicks="10"
                                     * BuildSpeed="30" CanBuild="True" CanHarvest="False"
                                     * CurrentHealth="100" MaxHealth="100" X="12" Y="13"
                                     * Speed="0.1"
                                     */
                                    if (!reader.IsEmptyElement)
                                        currentComponent = unit;
                                    break;
                                case "Building":
                                    Building building = new Building();
                                    currentComponent.AddChild(building);
                                    building.Width = Int32.Parse(reader.GetAttribute("Width"));
                                    building.Height = Int32.Parse(reader.GetAttribute("Height"));
                                    building.PointLocation = new PointF(float.Parse(reader.GetAttribute("X")), float.Parse(reader.GetAttribute("Y")));
                                    building.Type = reader.GetAttribute("Type");
                                    building.CanProduce = bool.Parse(reader.GetAttribute("CanProduce"));
                                    if (!reader.IsEmptyElement)
                                        currentComponent = building;
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case XmlNodeType.EndElement:
                            if (currentComponent != null)
                                currentComponent = currentComponent.Parent;
                            break;
                    }

                }
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("===================");
                // Read this element's properties and display them on console
                Console.WriteLine("Name:" + reader.Name);
                Console.WriteLine("Base URI:" + reader.BaseURI);
                Console.WriteLine("Local Name:" + reader.LocalName);
                Console.WriteLine("Attribute Count:" + reader.AttributeCount.ToString());
                Console.WriteLine("Depth:" + reader.Depth.ToString());
                Console.WriteLine("Node Type:" + reader.NodeType.ToString());
                Console.WriteLine("Attribute Count:" + reader.Value.ToString());
            }
            return scenario;
        }