private StarOnMap FindStarOnMap(SolarSystem ss)
 {
     foreach (IDraw obj in ((Map)screens["GalaxyMap"].Objects["Map"]).Objects)
     {
         if (obj is StarOnMap)
         {
             if (((StarOnMap)obj).SS == ss)
             {
                 return (StarOnMap)obj;
             }
         }
     }
     //Another epic crutch
     return null;
 }
        //Maybe there will be gravity simulation code.
        //And maybe there being collision controller code.
        private void SolarSystemCollisions(SolarSystem ss)
        {
            List<IMoveble> except = new List<IMoveble>();
            List<IDraw> forDelete = new List<IDraw>();
            foreach (IMoveble obj in ss.Collision)
            {
                foreach (IMoveble obj2 in ss.Collision)
                {
                    bool isExcept = false;
                    foreach (IMoveble ex in except)
                    {
                        if (System.Object.ReferenceEquals(ex, obj2))
                        {
                            isExcept = true;
                        }
                    }
                    if (isExcept)
                    {
                        continue;
                    }
                    if (!System.Object.ReferenceEquals(obj, obj2))
                    {
                        float width;
                        float height;
                        if ((obj is Asteroid && obj2 is Star) || (obj is Star && obj2 is Asteroid)
                            || (obj is Asteroid && obj2 is Planet) || (obj is Planet && obj2 is Asteroid))
                        {
                            width = obj2.X < obj.X ? obj2.Width * 0.6f : obj.Width * 0.6f;
                            height = obj2.Y < obj.Y ? obj2.Height * 0.6f : obj.Height * 0.6f;
                        }
                        else if (obj is Asteroid && obj2 is Asteroid)
                        {
                            width = obj2.X < obj.X ? obj2.Width: obj.Width;
                            height = obj2.Y < obj.Y ? obj2.Height: obj.Height;
                        }
                        else
                        {
                            width = obj2.X < obj.X ? obj2.Width * 0.6f : obj.Width * 0.6f;
                            height = obj2.Y < obj.Y ? obj2.Height * 0.6f : obj.Height * 0.6f;
                        }
                        if (Math.Abs(obj.X - obj2.X) <= width && Math.Abs(obj.Y - obj2.Y) <= height)
                        {
                            //Debug.Assert(false, "Collision!");
                            if (obj is Asteroid && obj2 is Asteroid)
                            {
                                //Debug.Assert(false, "Asteroid Collision!");
                                ((Asteroid)obj).Velocity = -((Asteroid)obj).Velocity;
                                ((Asteroid)obj2).Velocity = -((Asteroid)obj).Velocity;
                            }

                            if ((obj is Asteroid && obj2 is Star) || (obj is Star && obj2 is Asteroid)
                            || (obj is Asteroid && obj2 is Planet) || (obj is Planet && obj2 is Asteroid))
                            {
                                if (obj is Asteroid)
                                {
                                    forDelete.Add((IDraw)obj);
                                }
                                else
                                {
                                    forDelete.Add((IDraw)obj2);
                                }
                                //Debug.Assert(false, "Asteroid with Object collision!");
                            }
                        }
                    }
                }
                except.Add(obj);
            }
            //Remove destroyed objects
            if (forDelete.Count != 0)
            {
                foreach (IDraw del in forDelete)
                {
                    ss.Objects.Remove(del);
                }
            }
        }
 private Planet FindPlanet(SolarSystem ss)
 {
     foreach (IDraw obj in ss.Objects)
     {
         if (obj is Planet)
         {
             if (((Planet)obj).Name == tempPlanetName)
             {
                 return (Planet)obj;
             }
         }
     }
     return null;
 }
        //------------------------------------------------------------
        //SOLAR SYSTEM CREATE
        //------------------------------------------------------------
        private void SolarSystem(SolarSystem ss)
        {
            if (!screens.ContainsKey("SolarSystem"))
            {
                Screen SolarSystemScreen = new Screen();
                BackGround solarSystemBack = CreateBackground("Temp/SolarBackTest", Scales.None, 255f, false);
                SolarSystemScreen.Objects.Add("Back", solarSystemBack);

                //info popup
                BackGround infoPopupBack = CreateBackground("UI/SoftPopup", Scales.Quarter, 255f);
                Popup infoPopup = CreatePopup(infoPopupBack, Scales.None, 600, 500);
                GameString infoString = CreateGameString("SpriteFont1", "", 30, 20, Color.Azure);
                infoPopup.Objects.Add("InfoString", infoString);
                //end info popup

                TextBox nameTextBox = CreateTextBox("Square", "SpriteFont1", 332, 40, Scales.ThreeWithHalfTenth);

                Popup moreInfoPopup = CreateInfoPopup();
                //for buttons position
                BackGround moreInfoPopupBack = (BackGround)moreInfoPopup.Objects["Back"];

                GameString name = new GameString(Content.Load<SpriteFont>("Fonts/SpriteFont1"),
                                "Name", Shift(262, 40), Color.Azure);
                ((GameString)nameTextBox.ContentSource).Position = Shift(new Vector2(moreInfoPopup.Position.X + nameTextBox.Position.X + 10,
                    moreInfoPopup.Position.Y + nameTextBox.Position.Y + 5));

                BackGround PlanetStateFrame = CreateIcon("PlanetState/Frame", Scales.SevenTenth, 255f, new Vector2(100, 80));

                Button colonize = CreateStrButton("blue", 654, 487, Scales.SevenTenth, locals.Strings["Colonize"]);
                Button terraform = CreateStrButton("blue", 400, 487, Scales.SevenTenth, locals.Strings["Terraform"]);
                Button stationBuilder = CreateStrButton("blue", 400, 487, Scales.SevenTenth, locals.Strings["StationBuilder"]);
                Button createShip = CreateStrButton("blue", 654, 487, Scales.SevenTenth, locals.Strings["CreateShip"]);

                ProgressBar progress = CreateProgressBar("FillingBar", 262, 380, Scales.TwoTenth);//new ProgressBar(Content.Load<Texture2D>("UI/FillingBar"), /*HERE*/Shift(262, 380), /*HERE*/Resize(/*0.2f*/Scales.TwoTenth.Value));
                moreInfoPopup.Objects.Add("Progress", progress);

                moreInfoPopup.Objects.Add("NameString", name);

                moreInfoPopup.Objects.Add("PlanetStateFrame", PlanetStateFrame);

                moreInfoPopup.Objects.Add("NameTextBox", nameTextBox);
                moreInfoPopup.Objects.Add("ColonizeBtn", colonize);
                moreInfoPopup.Objects.Add("TerraformBtn", terraform);
                moreInfoPopup.Objects.Add("CreateShipBtn", createShip);
                moreInfoPopup.Objects.Add("StationBuilderBtn", stationBuilder);
                //end more info popup

                SolarSystemScreen.Objects.Add("SolarSystem", ss);
                SolarSystemScreen.Objects.Add("InfoPopup", infoPopup);
                CreateHUD(SolarSystemScreen);
                SolarSystemScreen.Objects.Add("MoreInfoPopup", moreInfoPopup);
                SolarSystemScreen.Objects.Add("EscPopup", CreateEscMenu());

                CreateDebugInfoWindow(SolarSystemScreen);

                screens.Add("SolarSystem", SolarSystemScreen);
            }
            else
            {
                screens["SolarSystem"].Objects["SolarSystem"] = ss;
            }
        }
        private void SolarSystemControls(Screen solarSystem, SolarSystem ss)
        {
            KeyboardState state = Keyboard.GetState();
            if ((state.IsKeyDown(Keys.Right) || state.IsKeyDown(Keys.D))
                && Math.Abs(((BackGround)solarSystem.Objects["Back"]).X)
                < /*((BackGround)solarSystem.Objects["Back"]).Width*/ /*HERE*/SystemSize.X - graphics.PreferredBackBufferWidth)
            {
                ((IMoveble)solarSystem.Objects["Back"]).X -= MOVEMENT;
                foreach (IDraw obj in ss.Objects)
                {
                    ((IMoveble)obj).X -= MOVEMENT;
                    if (obj is StationOnBuilding)
                    {
                        obj.ToStationOnBuilding().Progress.X -= MOVEMENT;
                    }
                }
            }
            if ((state.IsKeyDown(Keys.Left) || state.IsKeyDown(Keys.A))
                && ((BackGround)solarSystem.Objects["Back"]).X < 0)
            {
                ((IMoveble)solarSystem.Objects["Back"]).X += MOVEMENT;
                foreach (IDraw obj in ss.Objects)
                {
                    ((IMoveble)obj).X += MOVEMENT;
                    if (obj is StationOnBuilding)
                    {
                        obj.ToStationOnBuilding().Progress.X += MOVEMENT;
                    }
                }
            }
            if ((state.IsKeyDown(Keys.Up) || state.IsKeyDown(Keys.W))
                && ((BackGround)solarSystem.Objects["Back"]).Y < 0)
            {
                ((IMoveble)solarSystem.Objects["Back"]).Y += MOVEMENT;
                foreach (IDraw obj in ss.Objects)
                {
                    ((IMoveble)obj).Y += MOVEMENT;
                    if (obj is StationOnBuilding)
                    {
                        obj.ToStationOnBuilding().Progress.Y += MOVEMENT;
                    }
                }
            }
            if ((state.IsKeyDown(Keys.Down) || state.IsKeyDown(Keys.S))
                && Math.Abs(((BackGround)solarSystem.Objects["Back"]).Y)
                < /*((BackGround)solarSystem.Objects["Back"]).Height*//*HERE*/SystemSize.Y - graphics.PreferredBackBufferHeight)
            {
                ((IMoveble)solarSystem.Objects["Back"]).Y -= MOVEMENT;
                foreach (IDraw obj in ss.Objects)
                {
                    ((IMoveble)obj).Y -= MOVEMENT;
                    if (obj is StationOnBuilding)
                    {
                        obj.ToStationOnBuilding().Progress.Y -= MOVEMENT;
                    }
                }
            }

            if (state.IsKeyDown(Keys.M))
            {
                SolarSystemClose(solarSystem);
                GalaxyMapOpen(screens["GalaxyMap"].Objects["Map"].ToMap());
            }

            if (state.IsKeyDown(Keys.Escape) && !isEscDown)
            {
                isEscDown = true;
                EscEvents(solarSystem);
            }

            if (state.IsKeyUp(Keys.Escape))
            {
                isEscDown = false;
            }
        }
        private void GeneratingPlanets(SolarSystem system)
        {
            #region LocalVariables
            Star star = system.Objects[0].ToStar();
            int width = (int)star.Width / 2;
            Random count = new Random();
            Random temperature = new Random();
            Random textureRnd = new Random();
            Random radiusRnd = new Random();
            Random planetInterval = new Random();
            Random planetDegrees = new Random();
            int planetCount = count.Next(1, 11);
            #endregion

            for (int i = 0; i < planetCount; i++)
            {
                int degrees = planetDegrees.Next(0, 360);
                int interval = planetInterval.Next(50, 120);
                int planetSize = new Random().Next(2000, 5000);
                float planetScale = (float)(5000 - planetSize) / 3000f;
                planetScale = 0.05f * planetScale;
                planetScale = 0.1f - planetScale;
                int radius = radiusRnd.Next(WGInfoHelper[i].MinRadius + width, WGInfoHelper[i].MaxRadius + width);

                Planet tempPlanet = new Planet(
                    Content.Load<Texture2D>(WGInfoHelper[i].Textures[textureRnd.Next(0, WGInfoHelper[i].Textures.Length)]),
                    new Vector2(planetScale), radius, interval, degrees, star, WGInfoHelper[i].GenTerraform,
                    Content.Load<Texture2D>("Textures/AlphaPlanet"));

                //tempPlanet.Circle = CreateCircle(radius);

                tempPlanet.Name = ConstructName();

                //Planet Properties
                tempPlanet.PlanetSize = planetSize;
                tempPlanet.MaxTemperature = temperature.Next(WGInfoHelper[i].MinTemperature, WGInfoHelper[i].MaxTemperature);
                tempPlanet.MinTemperature = temperature.Next(WGInfoHelper[i].MinTemperature, tempPlanet.MaxTemperature);
                tempPlanet.MaxTerraform = WGInfoHelper[i].MaxTerraform(WGInfoHelper[i], tempPlanet.MaxTemperature);

                tempPlanet.IsAborigens = new Random().Next(0, 4) == 1 ? true : false;
                tempPlanet.Mass = (tempPlanet.PlanetSize * tempPlanet.PlanetSize) / 10;
                tempPlanet.Gravity = tempPlanet.Mass / 1000000;
                tempPlanet.Climat = "Unknown";

                #region Shit Properties
                int temp = new Random().Next(1, 100);
                int temp2;
                tempPlanet.Stability = (float)temp;
                bool isEqual;
                do
                {
                    temp2 = new Random().Next(1, 100);
                    if (temp == temp2)
                    {
                        isEqual = true;
                    }
                    else
                    {
                        isEqual = false;
                    }
                } while (isEqual);
                tempPlanet.Fertility = (float)temp2;
                do
                {
                    temp = new Random().Next(1, 100);
                    if (temp == temp2)
                    {
                        isEqual = true;
                    }
                    else
                    {
                        isEqual = false;
                    }
                } while (isEqual);
                tempPlanet.Radioactivity = (float)temp;
                #endregion
                //End Planet Properties

                system.Objects.Add(tempPlanet);
            }
        }
        //--------------------------------------------------------------
        //GENERATE SOLAR SYSTEM
        //--------------------------------------------------------------
        private void GenerateSolarSystem(StarOnMap star)
        {
            //Construct star name
            string tempName = ConstructName();
            star.Name = tempName;
            //End name construction
            //Generating Solar System
            Random starType = new Random();

            string starTextureStr = starsTextures[starType.Next(0, starsTextures.Length)];
            Texture2D starTexture = Content.Load<Texture2D>
                ("Stars/"+starTextureStr);

            //Here take a starcolor'
            string color = GetColor(starTextureStr);

            Star tempStar = new Star(starTexture, CenterPoint, new Vector2(Scales.None), color);
            //Copy star name to solar star name
            tempStar.Name = tempName;
            SolarSystem ss = new SolarSystem(tempStar);
            ss.BackGround = CreateBackground("SystemBacks/"+
                systemBackTextures[new Random().Next(0, systemBackTextures.Length)], Scales.None, 255f);

            //NEW PLANET GENERATING
            GeneratingPlanets(ss);
            //END NEW PLANET GENERATING
            GeneratingAsteroids(ss);
            //End generating solar system
            star.SS = ss;
        }