public void Visit(PlayerComponent player)
 {
     if (PlayerVisitor != null)
     {
         player.Accept(PlayerVisitor);
     }
 }
        private bool unitIsAnEnemy(UnitComponent unit)
        {
            // Find the PlayerComponent of unit.
            ModelComponent temp = unit.Parent;

            while (!(temp is PlayerComponent || temp == null))
            {
                temp = temp.Parent;
            }
            if (temp == null)
            {
                return(false);
            }
            PlayerComponent unitOwner = (PlayerComponent)temp;

            // Find the PlayerComponent of this UnitComponent;
            temp = this.Parent;
            while (!(temp is PlayerComponent || temp == null))
            {
                temp = temp.Parent;
            }
            if (temp == null)
            {
                return(false);
            }
            PlayerComponent myOwner = (PlayerComponent)temp;

            return(myOwner.EnemyList.Contains(unitOwner));
        }
        public LoseWhenAllPlayersUnitsAreDead(PlayerComponent player, ScenarioComponent scenario)
        {
            condition = new ConditionAllPlayerUnitsDead(this,player);
            this.scenario = scenario;

            action = new PlayerLoseAction(this, scenario);
        }
        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);
        }
Esempio n. 5
0
 public void Visit(PlayerComponent player)
 {
     output.WriteStartElement("Player");
     output.WriteAttributeString("Name", player.Name);
     output.WriteAttributeString("Race", player.Race);
     output.WriteAttributeString("Gold", player.Gold.ToString());
     output.WriteAttributeString("Metal", player.Metal.ToString());
     output.WriteAttributeString("Wood", player.Wood.ToString());
     VisitChildren(player);
     output.WriteEndElement();
 }
        public AlterPlayerCommand(PlayerComponent player, PlayerList list)
        {
            data = player;
            playerList = list;

            name = player.GetName();
            race = player.GetRace();
            gold = player.GetGold();
            wood = player.GetWood();
            metal = player.GetMetal();
            added = false;
        }
        public PlayerDataGridAdapter(PlayerComponent player, PlayerList list)
        {
            data = player;
            playerList = list;

            name = player.Name;
            race = player.Race;
            gold = player.Gold;
            wood = player.Wood;
            metal = player.Metal;
            added = false;
        }
Esempio n. 8
0
        /// <summary>
        /// Retrieve a player by its name
        /// </summary>
        /// <param name="p">Player's name</param>
        /// <returns>Requested PlayerComponent</returns>
        public PlayerComponent GetPlayerByName(string p)
        {
            PlayerComponent player = null;

            foreach (PlayerComponent pc in GetChildren())
            {
                if (pc.Name.Equals(p))
                {
                    player = pc;
                    break;
                }
            }
            return(player);
        }
Esempio n. 9
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);
        }
 public AddBuildingCommand(Building building, PlayerComponent player, CellComponent cell)
 {
     this.building = building;
     this.player = player;
     this.cell = cell;
 }
 public AddUnitCommand(UnitComponent unit, PlayerComponent player, CellComponent cell)
 {
     this.unit = unit;
     this.player = player;
     this.cell = cell;
 }
 public void Visit(PlayerComponent player)
 {
     output.WriteStartElement("Player");
     output.WriteAttributeString("Name", player.Name);
     output.WriteAttributeString("Race", player.Race);
     output.WriteAttributeString("Gold", player.Gold.ToString());
     output.WriteAttributeString("Metal", player.Metal.ToString());
     output.WriteAttributeString("Wood", player.Wood.ToString());
     VisitChildren(player);
     output.WriteEndElement();
 }
Esempio n. 13
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 virtual void Visit(PlayerComponent player)
 {
     Visit((ModelComponent)player);
 }
        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 ConditionAllPlayerUnitsDead(Trigger decorated, PlayerComponent player)
     : base(decorated)
 {
     this.player = player;
     this.unitList = player.GetUnitList();
 }
 public virtual void Visit(PlayerComponent player)
 {
     Visit((ModelComponent)player);
 }
 private PlayerComponent buildValidPlayer()
 {
     PlayerComponent player = new PlayerComponent();
     player.Name = "Player 2";
     player.Race = "Zombie";
     player.Gold = 100;
     player.Metal = 100;
     player.Wood = 100;
     return player;
 }
 public void Visit(PlayerComponent player)
 {
     if (PlayerVisitor != null)
         player.Accept(PlayerVisitor);
 }