private static ChickenUnitLogicExecutor CreateLogicExecutor( GameEngine engine, ChickenTeamSettings teamSettings, GameTeam team) { #region Argument Check if (engine == null) { throw new ArgumentNullException("engine"); } if (teamSettings == null) { throw new ArgumentNullException("teamSettings"); } #endregion var logic = (ChickenUnitLogic)Activator.CreateInstance(teamSettings.Type).EnsureNotNull(); var result = new ChickenUnitLogicExecutor( engine, teamSettings.UnitCount, team, engine._makeMoveEvent, logic); return(result); }
/// <summary> /// Initializes a new instance of the <see cref="GameEngine"/> class. /// </summary> public GameEngine(GameEngineSettings settings) { #region Argument Check if (settings == null) { throw new ArgumentNullException("settings"); } #endregion if (SettingsCache.Instance.UsePerformanceCounters) { PerformanceCounterHelper.Initialize(); } // Pre-initialized fields and properties _paintCallback = settings.PaintCallback; _positionCallback = settings.PositionCallback.EnsureNotNull(); this.Data = new GameEngineData(settings.NominalSize); _moveCount = new ThreadSafeValue <long>(); _winningTeam = new ThreadSafeValue <GameTeam?>(); _lastGamePresentation = new ThreadSafeValue <GamePresentation>(); // Post-initialized fields and properties _lightTeamLogicExecutor = CreateLogicExecutor(this, settings.LightTeamSettings, GameTeam.Light); _darkTeamLogicExecutor = CreateLogicExecutor(this, settings.DarkTeamSettings, GameTeam.Dark); _logicExecutors = new[] { _lightTeamLogicExecutor, _darkTeamLogicExecutor }.ToArray().AsReadOnly(); _allChickens = _logicExecutors.SelectMany(item => item.Units).ToArray().AsReadOnly(); _allChickens.DoForEach((item, index) => item.UniqueId = new GameObjectId(index + 1)); _aliveChickensDirect = new List <ChickenUnit>(); this.AliveChickens = _aliveChickensDirect.AsReadOnly(); _shotUnitsDirect = new List <ShotUnit>(_allChickens.Count); this.ShotUnits = _shotUnitsDirect.AsReadOnly(); _moveInfos = new Dictionary <ChickenUnit, MoveInfo>(_allChickens.Count); _moveInfoStates = new Dictionary <ChickenUnit, MoveInfoStates>(_allChickens.Count); _previousMoves = new Dictionary <ChickenUnit, MoveInfo>(_allChickens.Count); _newShotUnits = new List <ShotUnit>(_allChickens.Count); #region Argument Check var maxChickenCount = this.Data.NominalSize.Width * this.Data.NominalSize.Height / 2; if (_allChickens.Count > maxChickenCount) { throw new ArgumentException( string.Format( "Too many chickens ({0}) for the board of nominal size {1}x{2}. Maximum is {3}.", _allChickens.Count, this.Data.NominalSize.Width, this.Data.NominalSize.Height, maxChickenCount), "settings"); } #endregion ResetInternal(); }