public Fleet ByID(uint fleetID) { return AllVisibleFleets.FirstOrDefault(f => f.ID == fleetID); }
public void Sense() { var newFleets = Robot.Bodies .Where(b => b.Group?.Type == GroupTypes.Fleet) // check the sprite .GroupBy(b => b.Group) .Select(g => new Fleet { ID = g.Key.ID, Name = g.Key.Caption, Sprite = g.Select(b => b.Sprite).FirstOrDefault(), Color = g.Key.Color, Ships = g.Select(b => new Ship { ID = b.ID, Angle = b.Angle, Momentum = b.Momentum, Position = b.Position, Size = b.Size }).ToList() }) .ToList(); foreach (var fleet in AllVisibleFleets) fleet.PendingDestruction = true; foreach (var fleet in newFleets) { var existing = AllVisibleFleets.FirstOrDefault(f => f.ID == fleet.ID); if (existing != null) { existing.Name = fleet.Name; existing.Sprite = fleet.Sprite; existing.Color = fleet.Color; existing.PendingDestruction = false; foreach (var ship in existing.Ships) ship.PendingDestruction = true; foreach (var ship in fleet.Ships) { var existingShip = existing.Ships.FirstOrDefault(s => s.ID == ship.ID); if (existingShip != null) { existingShip.Position = ship.Position; existingShip.Momentum = ship.Momentum; existingShip.Size = ship.Size; existingShip.Angle = ship.Angle; ship.PendingDestruction = false; } else existing.Ships.Add(ship); existing.Ships = existing.Ships.Where(s => !s.PendingDestruction).ToList(); } } else AllVisibleFleets.Add(fleet); } AllVisibleFleets = AllVisibleFleets.Where(f => !f.PendingDestruction).ToList(); MyFleet = AllVisibleFleets.FirstOrDefault(f => f.ID == Robot.FleetID); }
public void Sense() { foreach (var fleet in AllVisibleFleets) { fleet.PendingDestruction = true; } var fleetGroups = Robot.Bodies .Where(b => b.Group?.Type == GroupTypes.Fleet) // check the sprite .GroupBy(keySelector: b => b.Group); foreach (var ships in fleetGroups) { var fleetGroup = ships.Key; var fleet = AllVisibleFleets.FirstOrDefault(f => f.ID == fleetGroup.ID); if (fleet == null) { fleet = new Fleet { ID = fleetGroup.ID }; AllVisibleFleets.Add(fleet); } fleet.Name = fleetGroup.Caption; fleet.Color = fleetGroup.Color; fleet.PendingDestruction = false; foreach (var ship in fleet.Ships) { ship.PendingDestruction = true; } foreach (var shipBody in ships) { var ship = fleet.Ships.FirstOrDefault(s => s.ID == shipBody.ID); if (ship == null) { ship = new Ship(); ship.ID = shipBody.ID; fleet.Ships.Add(ship); } ship.Position = shipBody.Position; ship.Momentum = shipBody.Velocity; ship.Size = shipBody.Size; ship.Angle = shipBody.Angle; ship.PendingDestruction = false; } // destruction is nigh fleet.Ships = fleet.Ships.Where(s => !s.PendingDestruction).ToList(); } // destruction is nigh AllVisibleFleets = AllVisibleFleets.Where(f => !f.PendingDestruction).ToList(); foreach (var fleet in AllVisibleFleets) { fleet.CacheCenter(); } MyFleet = AllVisibleFleets.FirstOrDefault(f => f.ID == Robot.FleetID); if (AllVisibleFleets.Count(f => f.ID == Robot.FleetID) > 1) { Console.WriteLine($"Multiple fleets with ID: {Robot.FleetID}"); } if (MyFleet != null) { LastKnownCenter = MyFleet.Center; } Others = MyFleet != null ? AllVisibleFleets.Where(v => v != MyFleet).ToArray() : AllVisibleFleets.ToArray(); }