//  Event handling method for the "Fire" button
    public void FireButtonClicked(object source, EventArgs e)
    {
        //  Get the initial values from the textfield
        double vx0 = Convert.ToDouble(vxTextBox.Text);
        double vy0 = Convert.ToDouble(vyTextBox.Text);
        double vz0 = Convert.ToDouble(vzTextBox.Text);

        distanceToHole = Convert.ToDouble(distanceTextBox.Text);
        double mass    = Convert.ToDouble(massTextBox.Text);
        double area    = Convert.ToDouble(areaTextBox.Text);
        double cd      = Convert.ToDouble(cdTextBox.Text);
        double density = Convert.ToDouble(densityTextBox.Text);
        double windVx  = Convert.ToDouble(windVxTextBox.Text);
        double windVy  = Convert.ToDouble(windVyTextBox.Text);

        //  Create a WindProjectile object representing the golf ball.
        golfball = new WindProjectile(0.0, 0.0, 0.0, vx0, vy0, vz0,
                                      0.0, mass, area, density, cd, windVx, windVy);

        //  Update the display
        UpdateDisplay();

        //  Fire the golf ball using a Timer object
        //  to slow down the action.
        gameTimer.Start();
    }
Пример #2
0
    void spawnProjectile()
    {
        WindProjectile temp = Instantiate(prefab, transform.position, Quaternion.identity).GetComponent <WindProjectile>();

        temp.SetValues(Quaternion.Euler(new Vector3(0, 0, -90 * ts.dir)) * new Vector3(0, 1), ts.damage);
        temp.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, -90 * ts.dir));
        currentAmmo--;
    }
Пример #3
0
 void resetBall()
 {
     shootsTaken++;
     shotsTaken.guiText.text = ("" + (5 - shootsTaken));
     PlayerPrefs.SetInt("shots", shootsTaken);
     score1.guiText.text           = (score + "/15");
     basketBall                    = null;
     gameObject.transform.parent   = Camera.main.transform;
     gameObject.transform.position = Camera.main.transform.TransformPoint(Vector3.forward * 2);
 }
Пример #4
0
    /*
     * Initialize the starting variables again after setting with UI inputs;
     */
    void initialize()
    {
        if (init)
        {
            trailSwitch = true;
            vx0         = tempVx0;
            vy0         = tempVy0;
            vz0         = tempVz0;
            mass        = 20.0f;
            area        = .2f;
            cd          = .4f;
            density     = 1.2f;

            /*
             * if(Application.loadedLevel == 3){
             *      tempWindX = Random.Range(-50, 50);
             *      tempWindY = Random.Range(-50, 50);
             *      windVx = tempWindX;
             *      windVy = tempWindY;
             * }
             * else{
             *      windVx = 0;
             *      windVy = 0;
             * }
             */


            golfBall = new WindProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y,
                                          vx0, vy0, vz0, 0.0, mass, area, density, cd, windVx, windVy);


            gameObject.transform.position = new Vector3((float)golfBall.GetX(), (float)golfBall.GetZ(), (float)golfBall.GetY());

            init = false;
            //	reverseVz = false;
            if (Application.loadedLevel == 3)
            {
                tempWindX = Random.Range(-50, 50);
                tempWindY = Random.Range(-50, 50);
                windVx    = tempWindX;
                windVy    = tempWindY;
            }
            else
            {
                windVx = 0;
                windVy = 0;
            }
        }
    }
Пример #5
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            isFiring = true;
            gameObject.transform.parent = null;
            shootingDirection           = GameObject.Find("Main Camera").transform.forward;
            initialVelocity             = shootingDirection * (power);
            windX      = Random.Range(0, 20);    //Randomly generates wind
            windY      = Random.Range(0, 20);    //Randomly generates wind
            basketBall = new WindProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y,
                                            initialVelocity.x, initialVelocity.z, initialVelocity.y, 0.0, mass, area, density, cd, windX, windY);
            gameObject.transform.position = new Vector3((float)basketBall.GetX(), (float)basketBall.GetZ(), (float)basketBall.GetY());
        }
        if (isFiring)
        {
            time += Time.deltaTime / 100;
            basketBall.UpdateLocationAndVelocity(time);

            if (basketBall.GetZ() >= -1)
            {
                gameObject.transform.position = new Vector3((float)basketBall.GetX(), (float)basketBall.GetZ(), (float)basketBall.GetY());
            }
            else
            {
                if (distance(ball.transform.position, target.transform.position) < 1)
                {
                    audio.PlayOneShot(cheer);    //sound
                    score += 3;                  //increases
                }
                isFiring = false;
                time     = 0;
                resetBall();
            }
            if (shootsTaken == 5)
            {
                previousLevel++;
                PlayerPrefs.SetInt("currentScore", currentScore + score);
                PlayerPrefs.SetInt("previousLevel", previousLevel);
                PlayerPrefs.Save();
                Application.LoadLevel("briefing");
            }
        }
    }
    public GolfGame3()
    {
        //  Create a WindProjectile object with default values
        //  so the display can be updated the first time.
        golfball =
            new WindProjectile(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                               0.0459, 0.001432, 0.5, 1.225, 0.0, 0.0);

        //  Set up images
        golferIcon = Image.FromFile("Golfer.jpg");
        flagIcon   = Image.FromFile("Hole_Cartoon.jpg");

        //  Initialize the distanceToHole field.
        distanceToHole = 200.0;

        //  Create a Timer object that will be used
        //  to slow the action down.
        gameTimer          = new Timer();
        gameTimer.Interval = 50; //  delay in milliseconds.
        gameTimer.Tick    += new EventHandler(ActionPerformed);

        //  Create some Labels
        vxLabel       = new Label();
        vxLabel.Text  = "Initial x-velocity, m/s";
        vxLabel.Font  = new Font(vxLabel.Font, FontStyle.Bold);
        vxLabel.Top   = 50;
        vxLabel.Left  = 10;
        vxLabel.Width = 130;

        vyLabel       = new Label();
        vyLabel.Text  = "Initial y-velocity, m/s";
        vyLabel.Font  = new Font(vyLabel.Font, FontStyle.Bold);
        vyLabel.Top   = 80;
        vyLabel.Left  = 10;
        vyLabel.Width = 130;

        vzLabel       = new Label();
        vzLabel.Text  = "Initial z-velocity, m/s";
        vzLabel.Font  = new Font(vzLabel.Font, FontStyle.Bold);
        vzLabel.Top   = 110;
        vzLabel.Left  = 10;
        vzLabel.Width = 130;

        distanceLabel       = new Label();
        distanceLabel.Text  = "Distance to hole, m";
        distanceLabel.Font  = new Font(distanceLabel.Font, FontStyle.Bold);
        distanceLabel.Top   = 140;
        distanceLabel.Left  = 10;
        distanceLabel.Width = 120;

        massLabel       = new Label();
        massLabel.Text  = "mass, kg";
        massLabel.Font  = new Font(massLabel.Font, FontStyle.Bold);
        massLabel.Top   = 50;
        massLabel.Left  = 210;
        massLabel.Width = 110;

        areaLabel       = new Label();
        areaLabel.Text  = "area, m^2";
        areaLabel.Font  = new Font(areaLabel.Font, FontStyle.Bold);
        areaLabel.Top   = 80;
        areaLabel.Left  = 210;
        areaLabel.Width = 110;

        cdLabel       = new Label();
        cdLabel.Text  = "drag coefficient";
        cdLabel.Font  = new Font(cdLabel.Font, FontStyle.Bold);
        cdLabel.Top   = 110;
        cdLabel.Left  = 210;
        cdLabel.Width = 110;

        densityLabel       = new Label();
        densityLabel.Text  = "density, kg/m^3";
        densityLabel.Font  = new Font(densityLabel.Font, FontStyle.Bold);
        densityLabel.Top   = 140;
        densityLabel.Left  = 210;
        densityLabel.Width = 110;

        axesLabel       = new Label();
        axesLabel.Text  = "View axes";
        axesLabel.Font  = new Font(axesLabel.Font, FontStyle.Bold);
        axesLabel.Top   = 170;
        axesLabel.Left  = 10;
        axesLabel.Width = 70;

        windVxLabel       = new Label();
        windVxLabel.Text  = "Wind x-velocity, m/s";
        windVxLabel.Font  = new Font(windVxLabel.Font, FontStyle.Bold);
        windVxLabel.Top   = 170;
        windVxLabel.Left  = 210;
        windVxLabel.Width = 120;

        windVyLabel       = new Label();
        windVyLabel.Text  = "Wind y-velocity, m/s";
        windVyLabel.Font  = new Font(windVyLabel.Font, FontStyle.Bold);
        windVyLabel.Top   = 200;
        windVyLabel.Left  = 210;
        windVyLabel.Width = 120;

        //  Create TextBox objects to display the inputs.
        vxTextBox          = new TextBox();
        vxTextBox.Width    = 50;
        vxTextBox.Text     = "31.0";
        vxTextBox.AutoSize = true;
        vxTextBox.Top      = vxLabel.Top;
        vxTextBox.Left     = 150;

        vyTextBox          = new TextBox();
        vyTextBox.Width    = 50;
        vyTextBox.Text     = "0.0";
        vyTextBox.AutoSize = true;
        vyTextBox.Top      = vyLabel.Top;
        vyTextBox.Left     = 150;

        vzTextBox          = new TextBox();
        vzTextBox.Width    = 50;
        vzTextBox.Text     = "35.0";
        vzTextBox.AutoSize = true;
        vzTextBox.Top      = vzLabel.Top;
        vzTextBox.Left     = 150;

        distanceTextBox          = new TextBox();
        distanceTextBox.Width    = 50;
        distanceTextBox.Text     = "200.0";
        distanceTextBox.AutoSize = true;
        distanceTextBox.Top      = distanceLabel.Top;
        distanceTextBox.Left     = 150;

        massTextBox          = new TextBox();
        massTextBox.Width    = 60;
        massTextBox.Text     = "0.0459";
        massTextBox.AutoSize = true;
        massTextBox.Top      = massLabel.Top;
        massTextBox.Left     = 330;

        areaTextBox          = new TextBox();
        areaTextBox.Width    = 60;
        areaTextBox.Text     = "0.001432";
        areaTextBox.AutoSize = true;
        areaTextBox.Top      = areaLabel.Top;
        areaTextBox.Left     = 330;

        cdTextBox          = new TextBox();
        cdTextBox.Width    = 60;
        cdTextBox.Text     = "0.25";
        cdTextBox.AutoSize = true;
        cdTextBox.Top      = cdLabel.Top;
        cdTextBox.Left     = 330;

        densityTextBox          = new TextBox();
        densityTextBox.Width    = 60;
        densityTextBox.Text     = "1.225";
        densityTextBox.AutoSize = true;
        densityTextBox.Top      = densityLabel.Top;
        densityTextBox.Left     = 330;

        windVxTextBox          = new TextBox();
        windVxTextBox.Width    = 50;
        windVxTextBox.Text     = "10.0";
        windVxTextBox.AutoSize = true;
        windVxTextBox.Top      = windVxLabel.Top;
        windVxTextBox.Left     = 330;

        windVyTextBox          = new TextBox();
        windVyTextBox.Width    = 50;
        windVyTextBox.Text     = "0.0";
        windVyTextBox.AutoSize = true;
        windVyTextBox.Top      = windVyLabel.Top;
        windVyTextBox.Left     = 330;

        //  Create a ComboBox to select the view axes.
        axesComboBox = new ComboBox();
        axesComboBox.Items.Add("XZ");
        axesComboBox.Items.Add("XY");
        axesComboBox.SelectedIndex = 0;
        axesComboBox.Left          = 80;
        axesComboBox.Top           = axesLabel.Top;

        //  Create Button objects
        int buttonHeight = 30;
        int buttonWidth  = 50;
        int buttonLeft   = 20;

        fireButton        = new Button();
        fireButton.Text   = "Fire";
        fireButton.Height = buttonHeight;
        fireButton.Width  = buttonWidth;
        fireButton.Top    = 200;
        fireButton.Left   = buttonLeft;
        fireButton.Click += new EventHandler(FireButtonClicked);

        resetButton        = new Button();
        resetButton.Text   = "Reset";
        resetButton.Height = buttonHeight;
        resetButton.Width  = buttonWidth;
        resetButton.Top    = 250;
        resetButton.Left   = buttonLeft;
        resetButton.Click += new EventHandler(ResetButtonClicked);

        //  Create a drawing panel.
        drawingPanel             = new Panel();
        drawingPanel.Width       = 501;
        drawingPanel.Height      = 201;
        drawingPanel.Left        = 20;
        drawingPanel.Top         = 300;
        drawingPanel.BorderStyle = BorderStyle.FixedSingle;

        //  Add the GUI components to the Form
        this.Controls.Add(vxLabel);
        this.Controls.Add(vyLabel);
        this.Controls.Add(vzLabel);
        this.Controls.Add(distanceLabel);
        this.Controls.Add(axesLabel);
        this.Controls.Add(massLabel);
        this.Controls.Add(areaLabel);
        this.Controls.Add(cdLabel);
        this.Controls.Add(densityLabel);
        this.Controls.Add(windVxLabel);
        this.Controls.Add(windVyLabel);
        this.Controls.Add(vxTextBox);
        this.Controls.Add(vyTextBox);
        this.Controls.Add(vzTextBox);
        this.Controls.Add(distanceTextBox);
        this.Controls.Add(massTextBox);
        this.Controls.Add(areaTextBox);
        this.Controls.Add(cdTextBox);
        this.Controls.Add(densityTextBox);
        this.Controls.Add(windVxTextBox);
        this.Controls.Add(windVyTextBox);
        this.Controls.Add(axesComboBox);
        this.Controls.Add(fireButton);
        this.Controls.Add(resetButton);
        this.Controls.Add(drawingPanel);

        // Set the size and title of the form
        this.Width  = 600;
        this.Height = 550;
        this.Text   = "Golf Game version 3";

        //  Center the form on the screen and make
        //  it visible.
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Visible       = true;

        //  Update the GUI display
        UpdateDisplay();
    }