コード例 #1
0
        public System(Vector2 position)
        {
            this.Position = position;

            GameRandom rnd = GameRandom.Instance;

            float rads = (float)(Math.Atan2(position.Y, position.X) + Math.PI);

            rads += rnd.NextFloat() * 0.5f;
            rads -= rnd.NextFloat() * 0.5f;

            if (rads > Math.PI * 1.5f && rads < Math.PI * 2)
            {
                Color = new Color(255, 50, 50);
            }
            else if (rads > Math.PI && rads < Math.PI * 2)
            {
                Color = new Color(255, 255, 50);
            }
            else if (rads > Math.PI * 0.5f && rads < Math.PI * 2)
            {
                Color = new Color(50, 255, 255);
            }
            else     // A
            {
                Color = new Color(255, 50, 255);
            }

            float planetRnd = (float)rnd.NextDouble();

            if (planetRnd < 0.1)
            {
                AddPlanets(2);
            }
            else if (planetRnd < 0.3)
            {
                AddPlanets(3);
            }
            else if (planetRnd < 0.6)
            {
                AddPlanets(4);
            }
            else if (planetRnd < 0.8)
            {
                AddPlanets(5 + (int)(3 * rnd.NextDouble()));
            }
        }
コード例 #2
0
        public GalaxyPane()
        {
            GameRandom rnd = GameRandom.Instance;

            float randomFloat(float range, bool centered = false)
            {
                float val = (float)rnd.NextDouble() * range;

                if (centered)
                {
                    val = 2 * val - range;
                }
                return(val);
            }

            System[,] systemsArray = new System[(int)WorldHeight, (int)WorldWidth];

            for (int x = 0; x < WorldWidth; x++)
            {
                for (int y = 0; y < WorldHeight; y++)
                {
                    const float HalfWorldWidth  = WorldWidth * 0.5f;
                    const float HalfWorldHeight = WorldHeight * 0.5f;
                    Vector2     initPos         = new Vector2(x + 0.5f - HalfWorldWidth, y + 0.5f - HalfWorldHeight);

                    // TODO: fix this
                    initPos += new Vector2(randomFloat(0.3f, true), randomFloat(0.3f, true));
                    if (randomFloat(1) > 0.3f)
                    {
                        System system = new System(initPos);
                        system.Difficulty = initPos.Length() / new Vector2(HalfWorldWidth, HalfWorldHeight).Length();
                        System.Systems.Add(system);

                        systemsArray[y, x] = system;
                    }
                }
            }

            const int radius = 1;

            for (int x = 0; x < WorldWidth; x++)
            {
                for (int y = 0; y < WorldHeight; y++)
                {
                    var system = systemsArray[y, x];
                    if (system != null)
                    {
                        for (int neighborX = -radius + x; neighborX <= x + radius; neighborX++)
                        {
                            for (int neighborY = -radius + y; neighborY <= y + radius; neighborY++)
                            {
                                if (neighborX >= 0 && neighborX < WorldWidth && neighborY >= 0 && neighborY < WorldHeight && (neighborX != x || neighborY != y))
                                {
                                    var neighborSystem = systemsArray[neighborY, neighborX];
                                    if (neighborSystem != null)
                                    {
                                        system.Neighbors.Add(neighborSystem);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // generate planet names
            {
                String[] SYSTEM_NAMES =
                {
                    "Fremulon",
                    "Erakis",
                    "Hyporayon",
                    "Kreeptan",
                    "Ohlderahn",
                    "Tarkalis",
                    "Ooban"
                };
                int   closestSystemId = -1;
                float closestD        = 1000000;
                for (int systemId = 0; systemId < System.Systems.Count; systemId++)
                {
                    System s        = System.Systems[systemId];
                    float  distance = s.Position.Length();
                    if (distance < closestD)
                    {
                        closestD        = distance;
                        closestSystemId = systemId;
                    }
                }

                System.Systems[closestSystemId].SetHomeSystem();
                currentSystem = System.Systems[closestSystemId];

                int count = 0;
                foreach (System s in System.Systems)
                {
                    if (s.Name == null)
                    {
                        s.Name = SYSTEM_NAMES[count % SYSTEM_NAMES.Length];
                        count++;
                    }
                }
            }
        }