/// <summary> /// Initializes a new instance of the <see cref="ChickenPresentation"/> class. /// </summary> internal ChickenPresentation(GamePresentation gamePresentation, ChickenUnit chickenUnit) { #region Argument Check if (gamePresentation == null) { throw new ArgumentNullException("gamePresentation"); } if (chickenUnit == null) { throw new ArgumentNullException("chickenUnit"); } #endregion this.GamePresentation = gamePresentation; this.UniqueId = chickenUnit.UniqueId; this.Team = chickenUnit.Team; this.KillCount = chickenUnit.KillCount; this.InitialPosition = chickenUnit.Position; this.Movement = chickenUnit.Movement; this.InitialBeakAngle = chickenUnit.BeakAngle; this.BeakMovement = chickenUnit.BeakMovement; }
/// <summary> /// Initializes a new instance of the <see cref="ViewInfo"/> class. /// </summary> internal ViewInfo(ChickenUnit unit) { #region Argument Check if (unit == null) { throw new ArgumentNullException("unit"); } #endregion var engine = unit.LogicExecutor.Engine; var unitTeam = unit.Team; if (unit.IsDead) { this.Chickens = EmptyChickens; this.Shots = EmptyShots; } else { var chickens = new List <ChickenViewData>(unit.IsDead ? 0 : engine.AliveChickens.Count); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var aliveChicken in engine.AliveChickens) { if (!aliveChicken.IsDead && aliveChicken != unit && (aliveChicken.Team == unitTeam || unit.CanSee(aliveChicken.Position))) { chickens.Add(new ChickenViewData(aliveChicken)); } } this.Chickens = chickens.AsReadOnly(); var shots = new List <ShotViewData>(unit.IsDead ? 0 : engine.ShotUnits.Count); // ReSharper disable once LoopCanBeConvertedToQuery foreach (var shotUnit in engine.ShotUnits) { if (unit.CanSee(shotUnit.Position)) { shots.Add(new ShotViewData(shotUnit)); } } this.Shots = shots.AsReadOnly(); } }
/// <summary> /// Initializes a new instance of the <see cref="ChickenViewData"/> class. /// </summary> internal ChickenViewData(ChickenUnit unit) { #region Argument Check if (unit == null) { throw new ArgumentNullException("unit"); } #endregion this.UniqueId = unit.UniqueId; this.Position = unit.Position; this.BeakAngle = unit.BeakAngle; this.Team = unit.Team; }
private bool ProcessChickenUnitMoves(IList <ChickenUnit> aliveChickens) { //// TODO: [vmcl] Use bisection to get conflicting units closer to each other //// TODO: [vmcl] Optimize number of collision checks! //// TODO: [vmcl] Divide move: eg. unit couldn't move but could turn beak or vice versa _moveInfoStates.Clear(); for (var unitIndex = 0; unitIndex < aliveChickens.Count; unitIndex++) { if (IsStopping()) { return(false); } var unit = aliveChickens[unitIndex]; _moveInfoStates[unit] = MoveInfoStates.Handled; var moveInfo = _moveInfos.GetValueOrDefault(unit); if (moveInfo == null) { continue; } DebugHelper.WriteLine( "{0} is processing move {{{1}}} of chicken {{{2}}}.", GetType().Name, moveInfo, unit); var movementAndNewPosition = GameHelper.GetMovementAndNewPosition( unit.Position, unit.BeakAngle, moveInfo.MoveDirection, GameConstants.ChickenUnit.DefaultRectilinearSpeed); var beakMovementAndNewAngle = GameHelper.GetBeakMovementAndNewAngle(unit.BeakAngle, moveInfo.BeakTurn); var newPositionElement = new ChickenElement( movementAndNewPosition.Position, beakMovementAndNewAngle.Position); if (HasOutOfBoardCollision(newPositionElement)) { _moveInfoStates[unit] = MoveInfoStates.RejectedBoardCollision; DebugHelper.WriteLine( "Blocked collision of chicken {{{0}}} with game board border.", unit); continue; } ChickenUnit conflictingChicken = null; // ReSharper disable once LoopCanBeConvertedToQuery // ReSharper disable once ForCanBeConvertedToForeach for (var conflictingIndex = 0; conflictingIndex < aliveChickens.Count; conflictingIndex++) { var aliveChicken = aliveChickens[conflictingIndex]; if (aliveChicken == unit) { continue; } if (CollisionDetector.CheckCollision(newPositionElement, aliveChicken.GetElement())) { conflictingChicken = aliveChicken; break; } } if (conflictingChicken != null) { _moveInfoStates[unit] = MoveInfoStates.RejectedOtherUnitCollision; DebugHelper.WriteLine( "Blocked collision of chicken {{{0}}} with {{{1}}}.", unit, conflictingChicken); continue; } unit.SetMovement(movementAndNewPosition.Movement, beakMovementAndNewAngle.Movement); DebugHelper.WriteLine("Chicken {{{0}}} has moved.", unit); } return(true); }