Beispiel #1
0
            public void Update(CtfGameState state, Team yourTeam, IList<Team> enemyTeams)
            {
                var enemy = enemyTeams[0];
                foreach (var ship in yourTeam.Ships)
                {
                    if (Math.Round(m_rand.NextDouble()) == 0)
                    {
                        ship.Fire();
                    }
                    else
                    {
                        ship.StopFiring();
                    }

                    ship.SetThrust((float)m_rand.NextDouble());

                    var t = enemy.Flag.HeldBy == ship.Id ? yourTeam.Flag.Position : yourTeam.Flag.HeldBy != null ? yourTeam.Flag.Position : enemy.Flag.Position;
                    Vector2 target = Vector2.Normalize(t - ship.Position);
                    if (!(float.IsNaN(target.X) && float.IsNaN(target.Y)))
                    {
                        float d = (float)Math.Atan2(target.Y, target.X) - (ship.Rotation + (ship.AngularVelocity * 5));
                        ship.SetTorque(Math.Sign(d));
                    }
                }
            }
Beispiel #2
0
        private void StepTeam(Team team)
        {
            foreach (var ship in team.Ships)
            {
                StepShip(ship);
            }

            foreach (var projectile in team.Projectiles)
            {
                StepProjectile(projectile);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Create a ctf game
        /// </summary>
        /// <param name="rules">The rules of this game</param>
        /// <param name="ais">All AIs that will be competing in this game</param>
        public CtfGame(CtfGameRules rules, IList<ICtfAi> ais)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }

            if (ais == null)
            {
                throw new ArgumentNullException("ais");
            }

            SpawnPositions = new Dictionary<int, Vector2>();
            Rules = rules;
            States = new List<CtfGameState>(Rules.TurnLimit);

            //Calculate spacing of objects for spawn positions
            float teamSpacing = MathHelper.TwoPi / ais.Count;
            float shipSpacing = MathHelper.TwoPi / Rules.ShipsPerTeam;

            float flagRadius = Rules.ArenaRadius - (2 * Rules.ShipRadius) - Rules.FlagRadius;

            CtfGameState initialState = new CtfGameState();

            initialState.Teams = new List<Team>(ais.Count);
            int teamIndex = 0;

            //Initialise all the teams
            foreach (ICtfAi ai in ais)
            {
                var team = new Team()
                {
                    Name = ai.Name ?? "Unnamed AI"
                };

                team.Flag = new Flag()
                {
                    Bounds = new BoundingCircle(Vector2.FromPolar(flagRadius, teamSpacing * teamIndex), Rules.FlagRadius),
                    Owner = team.Id,
                };
                SpawnPositions[team.Flag.Id] = team.Flag.Position;

                m_ais[team.Id] = ai;

                team.Ships = new List<Ship>();

                for (int i = 0; i < rules.ShipsPerTeam; i++)
                {
                    var ship = new Ship()
                    {
                        Bounds = new BoundingCircle(team.Flag.Position + Vector2.FromPolar(Rules.FlagRadius + Rules.ShipRadius, i * shipSpacing), Rules.ShipRadius),
                        Owner = team.Id,
                        Rotation = (i * shipSpacing),
                    };

                    SpawnPositions[ship.Id] = ship.Position;
                    m_lastFireTurns[ship.Id] = int.MinValue + 1;

                    team.Ships.Add(ship);
                }

                team.Projectiles = new List<Projectile>();

                initialState.Teams.Add(team);

                teamIndex++;
            }

            States.Add(initialState);
            m_currentState = initialState;
        }