示例#1
0
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            throw new Exception("[" + GetType().Name + "] Trying to instanciate a second instance");
        }

        base.Awake();

        rigidbody.mass = mass;
    }
示例#2
0
        // creation function that generates planets or gravity centers, only operates if one of the radio buttons is selected
        private void PlanetScreen_MouseDown(object sender, MouseEventArgs e)
        {
            Graphics g = PlanetScreen.CreateGraphics(); ;
            int w = Convert.ToInt32(Change_Radius.Value);
            // create a gravity center
            if (CenterOfGravity.Checked == true)
            {
                GravityCenter gravityCenter = new GravityCenter { };
                gravityCenter.x = (e.X-5);
                gravityCenter.y = (e.Y-5);
                gravityCenter.radius = w;
                gravityCenters.Add (gravityCenter); // adds the center of black circle to list of gravity centers

                g.FillEllipse (Brushes.LightGray, e.X-w, e.Y-w, w*2, w*2); // Draws gray and black circles
                g.FillEllipse(Brushes.Black, e.X-5, e.Y-5, 10, 10); // Since static no need to add them to time interval
            }
            // create a planet
            if (CreateAPlanet.Checked == true)
            {
                Planet planet = new Planet { };
                planet.x = e.X - 5;
                planet.y = e.Y - 5;
                foreach (GravityCenter gravity in gravityCenters)
                {
                    if (gravity.x != planet.x || gravity.y != planet.y){ // if statement needed so program doesnt try to divide by zero and make the program crash
                        if (Math.Sqrt(Math.Pow((planet.x - gravity.x), 2) + Math.Pow((planet.y - gravity.y), 2)) < gravity.radius)
                        {
                            planet.nearest_X = gravity.x;
                            planet.nearest_Y = gravity.y;
                            planet.gravitationalRadius = Convert.ToInt32(Math.Sqrt(Math.Pow((planet.x - gravity.x), 2) + Math.Pow((planet.y - gravity.y), 2)));
                            if (planet.x < gravity.x)
                                planet.angle = (float)(Math.Atan((planet.y - gravity.y) / (planet.x - gravity.x)) - Math.PI); // exception needed if planets is clicked ontop of a gravity center
                            else
                                planet.angle = (float)Math.Atan((planet.y - gravity.y) / (planet.x - gravity.x));
                            planetList.Add(planet);
                            g.FillEllipse(Brushes.HotPink, planet.x, planet.y, 10, 10);
                            break;
                        }// if planet x,y within radius of gravity circle set gravity circle x,y to planet
                    planetList.Add(planet);
                    g.FillEllipse(Brushes.HotPink, planet.x, planet.y, 10, 10);
                    }
                }
            }
        }