public void Update(GameTime gameTime, GameState gameState)
        {
            this.gameState = gameState;

            // If the gameState is null, do nothing, this is bad...
            if (gameState == null)
            {
                return;
            }

            // Identify all of my units
            units = gameState.Units.Where(y => y.AgentNbr == AgentNbr).ToList();

            // Identify all of my peons
            peons = units.Where(y => y.UnitType == UnitType.PEON).ToList();

            // Identify all of my soldiers
            soldiers = units.Where(y => y.UnitType == UnitType.SOLDIER).ToList();

            // Identify all of my barracks
            barracks = units.Where(y => y.UnitType == UnitType.BARRACKS).ToList();

            // Identify all of my bases
            bases = units.Where(y => y.UnitType == UnitType.BASE).ToList();

            //// Identify all of my refineries
            refineries = units.Where(y => y.UnitType == UnitType.REFINERY).ToList();

            if (bases.Count > 0)
            {
                mainBase = bases[0];
            }

            if (units.Count == 0)
            {
                isDead = true;
            }

            FindClosestMine();
            EstimateGoldIncome();
        }
        public void Update(GameTime gameTime, GameState gameState)
        {
            this.gameState = gameState;

            // If the gameState is null, do nothing, this is bad...
            if (gameState == null)
                return;

            // Identify all of my units
            units = gameState.Units.Where(y => y.AgentNbr == AgentNbr).ToList();

            // Identify all of my peons
            peons = units.Where(y => y.UnitType == UnitType.PEON).ToList();

            // Identify all of my soldiers
            soldiers = units.Where(y => y.UnitType == UnitType.SOLDIER).ToList();

            // Identify all of my barracks
            barracks = units.Where(y => y.UnitType == UnitType.BARRACKS).ToList();

            // Identify all of my bases
            bases = units.Where(y => y.UnitType == UnitType.BASE).ToList();

            //// Identify all of my refineries
            refineries = units.Where(y => y.UnitType == UnitType.REFINERY).ToList();

            if (bases.Count > 0)
            {
                mainBase = bases[0];
            }

            if (units.Count == 0)
            {
                isDead = true;
            }

            FindClosestMine();
            EstimateGoldIncome();
        }
        public override void Update(GameTime gameTime, GameState gameState)
        {
            this.gameState = gameState;

            // If the gameState is null, do nothing, this is bad...
            if (gameState == null)
            {
                return;
            }

            // Identify all my units
            myUnits = gameState.Units.Where(y => y.AgentNbr == AgentNbr).ToList();

            // Identify all of my peons
            myPeons = myUnits.Where(y => y.UnitType == UnitType.PEON).ToList();

            // Identify all of my soldiers
            mySoldiers = myUnits.Where(y => y.UnitType == UnitType.SOLDIER).ToList();

            // Identify all of my barracks
            myBarracks = myUnits.Where(y => y.UnitType == UnitType.BARRACKS).ToList();

            // Identify all of my bases
            myBases = myUnits.Where(y => y.UnitType == UnitType.BASE).ToList();

            //// Identify all of my refineries
            myRefineries = myUnits.Where(y => y.UnitType == UnitType.REFINERY).ToList();

            if (!ranOnce)
            {
                ranOnce = true;
                RunOnce();
            }



            if (myBases.Count > 0)
            {
                mainBase = myBases[0];
            }



            for (int i = enemies.Count - 1; i >= 0; i--)
            {
                enemies[i].Update(gameTime, gameState);
                if (enemies[i].isDead)
                {
                    enemies.RemoveAt(i);
                    Console.WriteLine("Enemy removed");
                }
            }

            timer += gameTime.ElapsedGameTime.Milliseconds;
            if (timer > timeForUpdates)
            {
                FindClosestMine();

                ProcessPeons();

                ProcessBarracks();

                ProcessBases();

                timer = 0;
            }

            EstimateGoldIncome();
            ProcessSoldiers();


            // Use this debugger to display any messages you want to the
            // primary XNA window, just add a string to this list.  As an
            // alternative, you can of course print anything to the Console
            // using WriteLine() as you usually would, but that can get
            // confusing as you might not know which agent the output
            // belongs to!
            if (Constants.SHOW_MESSAGES)
            {
                lock (debugger.messages)
                {
                    debugger.messages = new List <string>();
                    debugger.messages.Add("Gold       " + Gold);
                    debugger.messages.Add("Peons      " + myPeons.Count);
                    debugger.messages.Add("Soldiers   " + mySoldiers.Count);
                    debugger.messages.Add("Barracks   " + myBarracks.Count);
                    debugger.messages.Add("Bases      " + myBases.Count);
                    debugger.messages.Add("Refineries " + myRefineries.Count);
                    debugger.messages.Add("Income     " + estimatedGoldIncomePerSecond);
                }
            }
        }
        private void ProcessSoldiers()
        {
            // For each soldier, determine what they should attack
            foreach (UnitSprite unit in mySoldiers)
            {
                float      closeDistance = 9999;
                UnitSprite target        = null;

                if (unit.CurrentAction == UnitAction.IDLE)
                {
                    //First check to see if there are any soldiers or peons in our radius
                    //If so, have each soldier attack the nearest enemy that is in our bounds.
                    foreach (Enemy e in enemies)
                    {
                        foreach (UnitSprite soldier in e.soldiers)
                        {
                            if (Vector2.Distance(soldier.Position, averageBuildingLocation) < radiusToCheck)
                            {
                                float distance = Vector2.Distance(unit.Position, soldier.Position);
                                if (distance < closeDistance)
                                {
                                    closeDistance = distance;
                                    target        = soldier;
                                }
                            }
                        }
                        foreach (UnitSprite peon in e.peons)
                        {
                            if (Vector2.Distance(peon.Position, averageBuildingLocation) < radiusToCheck)
                            {
                                float distance = Vector2.Distance(unit.Position, peon.Position) * 2;
                                if (distance < closeDistance)
                                {
                                    closeDistance = distance;
                                    target        = peon;
                                }
                            }
                        }
                    }

                    //if we found someone we should be attacking, attack them.
                    if (target != null)
                    {
                        Attack(unit, target);
                    }

                    //there is only one enemy left, attack them.
                    else if (enemies.Count == 1)
                    {
                        foreach (UnitSprite soldier in enemies[0].soldiers)
                        {
                            float distance = Vector2.Distance(unit.Position, soldier.Position);
                            if (distance < closeDistance)
                            {
                                closeDistance = distance;
                                target        = soldier;
                            }
                        }
                        foreach (UnitSprite peon in enemies[0].peons)
                        {
                            float distance = Vector2.Distance(unit.Position, peon.Position) * 2;
                            if (distance < closeDistance)
                            {
                                closeDistance = distance;
                                target        = peon;
                            }
                        }
                        foreach (UnitSprite barracks in enemies[0].barracks)
                        {
                            float distance = Vector2.Distance(unit.Position, barracks.Position) * 3;
                            if (distance < closeDistance)
                            {
                                closeDistance = distance;
                                target        = barracks;
                            }
                        }
                        foreach (UnitSprite b in enemies[0].bases)
                        {
                            float distance = Vector2.Distance(unit.Position, b.Position) * 4;
                            if (distance < closeDistance)
                            {
                                closeDistance = distance;
                                target        = b;
                            }
                        }
                        foreach (UnitSprite r in enemies[0].refineries)
                        {
                            float distance = Vector2.Distance(unit.Position, r.Position) * 8;
                            if (distance < closeDistance)
                            {
                                closeDistance = distance;
                                target        = r;
                            }
                        }

                        //if there are still targets to attack
                        if (target != null)
                        {
                            unit.AttackUnit = target;
                        }
                    }

                    //nothing is in our range and we are still playing defensive
                    //just make the soldiers move around to intelligent locations
                    else
                    {
                        Move(unit, FindRandomOpenCellToBuildWithinRange(
                                 2, Constants.GRID_HEIGHT / 2 - 4,
                                 2, Constants.GRID_WIDTH / 2 - 6));
                    }
                }
                else if (enemies.Count != 1 && unit.CurrentAction == UnitAction.ATTACK)
                {
                    if (Vector2.Distance(unit.Position, averageBuildingLocation) > radiusToCheck)
                    {
                        Move(unit, FindRandomOpenCellToBuildWithinRange(
                                 2, Constants.GRID_HEIGHT / 2 - 4,
                                 2, Constants.GRID_WIDTH / 2 - 6));
                    }
                }
                else if (unit.CurrentAction == UnitAction.MOVE)
                {
                    foreach (Enemy e in enemies)
                    {
                        foreach (UnitSprite soldier in e.soldiers)
                        {
                            if (Vector2.Distance(soldier.Position, averageBuildingLocation) < radiusToCheck)
                            {
                                float distance = Vector2.Distance(unit.Position, soldier.Position);
                                if (distance < closeDistance)
                                {
                                    closeDistance = distance;
                                    target        = soldier;
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
 private void LoadUnitSprite()
 {
     _unitSprite = gameObject.GetComponentInChildren <UnitSprite>();
 }
        public override void Update(GameTime gameTime, GameState gameState)
        {
            this.gameState = gameState;

            // If the gameState is null, do nothing, this is bad...
            if (gameState == null)
                return;

            // Identify all my units
            myUnits = gameState.Units.Where(y => y.AgentNbr == AgentNbr).ToList();

            // Identify all of my peons
            myPeons = myUnits.Where(y => y.UnitType == UnitType.PEON).ToList();

            // Identify all of my soldiers
            mySoldiers = myUnits.Where(y => y.UnitType == UnitType.SOLDIER).ToList();

            // Identify all of my barracks
            myBarracks = myUnits.Where(y => y.UnitType == UnitType.BARRACKS).ToList();

            // Identify all of my bases
            myBases = myUnits.Where(y => y.UnitType == UnitType.BASE).ToList();

            //// Identify all of my refineries
            myRefineries = myUnits.Where(y => y.UnitType == UnitType.REFINERY).ToList();

            if (!ranOnce)
            {
                ranOnce = true;
                RunOnce();
            }



            if (myBases.Count > 0)
            {
                mainBase = myBases[0];
            }



                for (int i = enemies.Count - 1; i >= 0; i--)
                {
                    enemies[i].Update(gameTime, gameState);
                    if (enemies[i].isDead)
                    {
                        enemies.RemoveAt(i);
                        Console.WriteLine("Enemy removed");
                    }
                }
           
            timer += gameTime.ElapsedGameTime.Milliseconds;
            if (timer > timeForUpdates)
            {
                FindClosestMine();

                ProcessPeons();

                ProcessBarracks();

                ProcessBases();

                timer = 0;
            }

            EstimateGoldIncome();
            ProcessSoldiers();


            // Use this debugger to display any messages you want to the
            // primary XNA window, just add a string to this list.  As an
            // alternative, you can of course print anything to the Console
            // using WriteLine() as you usually would, but that can get
            // confusing as you might not know which agent the output
            // belongs to!
            if (Constants.SHOW_MESSAGES)
            {
                lock(debugger.messages)
                {
                    debugger.messages = new List<string>();
                    debugger.messages.Add("Gold       " + Gold);
                    debugger.messages.Add("Peons      " + myPeons.Count);
                    debugger.messages.Add("Soldiers   " + mySoldiers.Count);
                    debugger.messages.Add("Barracks   " + myBarracks.Count);
                    debugger.messages.Add("Bases      " + myBases.Count);
                    debugger.messages.Add("Refineries " + myRefineries.Count);
                    debugger.messages.Add("Income     " + estimatedGoldIncomePerSecond);
                }
            }

        }
예제 #7
0
 protected override void Awake()
 {
     base.Awake();
     sprite     = transform.Find("Sprite").gameObject;
     unitSprite = sprite.GetComponent <UnitSprite>();
 }
예제 #8
0
 public static void AddUnit(UnitSprite unit)
 {
     unitList.Add(unit);
 }