// This method redraws the GUI display. private void UpdateDisplay() { Graphics g = drawingPanel.CreateGraphics(); int width = drawingPanel.Width - 1; int height = drawingPanel.Height - 1; // Clear the current display. g.Clear(Color.White); // Update the position of the golfball on the screen. SolidBrush brush = new SolidBrush(Color.Black); Pen blackPen = new Pen(Color.Black, 1); // Draw picture based on whether the XZ or // XY axes are selected. string axes = (string)axesComboBox.SelectedItem; if (String.Equals(axes, "XZ")) { // Draw the golfer. int zLocation = height - 50; g.DrawImage(golferIcon, 0, zLocation, 34, 50); // Draw the flag zLocation = height - 62; g.DrawImage(flagIcon, (int)(2.0 * distanceToHole), zLocation, 55, 62); // Update the position of the golfball // on the screen. int xPosition = (int)(2.0 * golfball.GetX() + 14); int zPosition = (int)(height - 5 - 2.0 * golfball.GetZ()); g.FillEllipse(brush, xPosition, zPosition, 5, 5); } else { // Draw location of green. g.DrawEllipse(blackPen, (int)(2.0 * distanceToHole - 20), 80, 40, 40); g.FillEllipse(brush, (int)(2.0 * distanceToHole - 4), 96, 8, 8); // Update the position of the golfball // on the screen. int xPosition = (int)(2.0 * golfball.GetX()); int yPosition = (int)(100 - 2 - 2.0 * golfball.GetY()); g.FillEllipse(brush, xPosition, yPosition, 5, 5); } // Clean up the Graphics object. g.Dispose(); }
void Start() { double vx0 = 5.2f; double vy0 = 5.2f; double vz0 = 10.0f; double mass = 20.0f; double area = .2f; double cd = .4f; double density = 1.2f; // golfBall = new DragProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y, // vx0, vy0, vz0, 0.0, mass, area, density, cd); gameObject.transform.position = new Vector3((float)golfBall.GetX(), (float)golfBall.GetZ(), (float)golfBall.GetY()); // }
/* * 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; golfBall = new DragProjectile(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; } }
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { isFiring = true; shootingDirection = GameObject.Find("Main Camera").transform.forward; initialVelocity = shootingDirection * (power); //sets strengths basketBall = new DragProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y, initialVelocity.x, initialVelocity.z, initialVelocity.y, 0.0, mass, area, density, cd); //creates projectile gameObject.transform.position = new Vector3((float)basketBall.GetX(), (float)basketBall.GetZ(), (float)basketBall.GetY()); } if (isFiring) { time += Time.deltaTime / 100; //slows down time basketBall.UpdateLocationAndVelocity(time); //updates position and speed 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) //hits { audio.PlayOneShot(cheer); //sound score += 2; //increases } isFiring = false; //false time = 0; //sets resetBall(); //reset ball } } if (shootsTaken == 5) //load next level { previousLevel++; PlayerPrefs.SetInt("currentScore", currentScore + score); PlayerPrefs.SetInt("previousLevel", previousLevel); PlayerPrefs.Save(); Application.LoadLevel("briefing"); } }
/* * Only hit the golfball and simulate movement based on input from the UI button in the UIController class * */ void HitBall() { if (hitBall) { // inialize again, But only once initialize(); time += Time.deltaTime; time /= 10; golfBall.UpdateLocationAndVelocity(time); if (golfBall.GetZ() >= 0.0) { gameObject.transform.position = new Vector3((float)golfBall.GetX(), (float)golfBall.GetZ(), (float)golfBall.GetY()); //print ("x="+golfBall.GetX() +" y="+golfBall.GetZ()); } else { hitBall = false; trailSwitch = false; //print ("x="+golfBall.GetX() +" y="+golfBall.GetZ()); } } }