Ejemplo n.º 1
0
        public List <StarSystem> GetSystemsInRange(uint range, bool sort)
        {
            List <StarSystem> returnList = new List <StarSystem>();

            int sectors = (int)((range + 10) * 2 / GalaxyConstants.SectorWidth);

            Sector sector = null;
            Coords coords = null;

            if (this.SpaceShip != null)
            {
                sector = this.SpaceShip.Coords.GetSector();
                coords = this.SpaceShip.Coords;
            }
            else
            {
                sector = this.Location.Sector;
                if (sector != null)
                {
                    coords = sector.Coords;
                }
            }

            if (sector != null && coords != null)
            {
                for (int x = -(sectors >> 1); x < (sectors >> 1) + 1; x++)
                {
                    for (int y = -(sectors >> 1); y < (sectors >> 1) + 1; y++)
                    {
                        Sector sec = new Sector(
                            (uint)(sector.IndexX + x),
                            (uint)(sector.IndexY + y)
                            );

                        for (int s = 0; s < sec.Systems.Count; s++)
                        {
                            if (s > 0 && (s & 0xFFF) == 0)
                            {
                                System.Threading.Thread.Sleep(0);
                            }

                            var distance = coords.CalculateDistance(sec.Systems[s].Coords);
                            if (distance < range)
                            {
                                returnList.Add(sec.Systems[s]);
                            }
                        }
                    }
                }
            }

            if (sort)
            {
                returnList.Sort(
                    delegate(StarSystem a, StarSystem b) {
                    var distA = this.GetDistance(a.Coords);
                    var distB = this.GetDistance(b.Coords);
                    return((distA > distB) ? 1 : (distA < distB) ? -1 : 0);
                }
                    );
            }

            return(returnList);
        }