コード例 #1
0
        /// <summary>
        /// Initializes a player of the Terran race.
        /// </summary>
        /// <param name="player">The player to be initialized.</param>
        private void TerranInitializer(Player player)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }

            /// Add a Terran Command Center to the position of the start location.
            Scenario scenario = player.StartLocation.Scenario;
            //Starport commandCenter = new Starport();
            CommandCenter commandCenter = new CommandCenter();

            scenario.AddElementToScenario(commandCenter);
            player.AddBuilding(commandCenter);
            commandCenter.AttachToMap(scenario.Map.GetQuadTile(player.QuadraticStartPosition.Location));

            /// TEST: Add a Terran Comsat Station
            //ComsatStation comsatStation = new ComsatStation();
            //scenario.AddElementToScenario(comsatStation);
            //player.AddAddon(comsatStation);
            //comsatStation.AttachToMap(scenario.Map.GetQuadTile(player.QuadraticStartPosition.Location + new RCIntVector(4, 1)));
            /// TEST END

            /// Find place for the given number of SCVs using an EntityNeighbourhoodIterator.
            EntityNeighbourhoodIterator cellIterator   = new EntityNeighbourhoodIterator(commandCenter);
            IEnumerator <ICell>         cellEnumerator = cellIterator.GetEnumerator();

            for (int scvCount = 0; scvCount < NUM_OF_SCVS; scvCount++)
            {
                /// Create the next SCV
                //Wraith scv = new Wraith();
                SCV scv = new SCV();
                //Unit scv = scvCount % 2 == 0 ? (Unit)new SCV() : (Unit)new Marine();
                scenario.AddElementToScenario(scv);
                player.AddUnit(scv);

                /// Search a place for the new SCV on the map.
                bool scvPlacedSuccessfully = false;
                while (cellEnumerator.MoveNext())
                {
                    if (scv.AttachToMap(cellEnumerator.Current.MapCoords))
                    {
                        scvPlacedSuccessfully = true;
                        break;
                    }
                }

                /// Remove the SCV and stop initializing if there is no more place on the map.
                if (!scvPlacedSuccessfully)
                {
                    player.RemoveUnit(scv);
                    scenario.RemoveElementFromScenario(scv);
                    scv.Dispose();
                    break;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Creates the given type of unit in the given factory building.
        /// </summary>
        /// <typeparam name="T">The type of the unit to create.</typeparam>
        /// <param name="factoryBuilding">The factory building for which the unit is created.</param>
        /// <returns>True if the unit has been created successfully; otherwise false.</returns>
        private bool CreateUnit <T>(Building factoryBuilding) where T : Unit, new()
        {
            if (factoryBuilding == null)
            {
                throw new ArgumentNullException("factoryBuilding");
            }
            if (factoryBuilding.Scenario == null)
            {
                throw new ArgumentException("The factory building is not added to a scenario!");
            }
            if (factoryBuilding.MapObject == null)
            {
                throw new ArgumentException("The factory building is detached from the map!", "factoryBuilding");
            }

            EntityNeighbourhoodIterator cellIterator   = new EntityNeighbourhoodIterator(factoryBuilding);
            IEnumerator <ICell>         cellEnumerator = cellIterator.GetEnumerator();

            /// Create the unit.
            T unit = new T();

            factoryBuilding.Scenario.AddElementToScenario(unit);
            if (factoryBuilding.Owner != null)
            {
                factoryBuilding.Owner.AddUnit(unit);
            }

            /// Search a place for the new unit on the map.
            bool unitPlacedSuccessfully = false;

            while (cellEnumerator.MoveNext())
            {
                if (unit.AttachToMap(cellEnumerator.Current.MapCoords))
                {
                    unitPlacedSuccessfully = true;
                    break;
                }
            }

            /// Remove the unit if there is no more place on the map.
            if (!unitPlacedSuccessfully)
            {
                if (factoryBuilding.Owner != null)
                {
                    factoryBuilding.Owner.RemoveUnit(unit);
                }
                factoryBuilding.Scenario.RemoveElementFromScenario(unit);
                unit.Dispose();
            }

            return(unitPlacedSuccessfully);
        }