// Update is called once per frame void Update() { if (theBall == null) { //hint: if your game UI never displays, you might want to check this condition somehow... return; } switch (theGameState) { case GameStates.GS_INITIALIZATION: //calculate information about the hill HILL_HALF_WIDTH = HILL_UNSCALED_HALF_WIDTH * theHill.transform.localScale.x; Restart(); //needed to avoid a race condition with the ball's Start() function break; case GameStates.GS_USER_INPUT: //increase or decreate the ballista's elevation by a rate of 10 degrees per second if (Input.GetKey(KeyCode.UpArrow)) { theBallista.localElevationAngle = Mathf.Min(90.0f, theBallista.localElevationAngle + 10.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.DownArrow)) { theBallista.localElevationAngle = Mathf.Max(0.0f, theBallista.localElevationAngle - 10.0f * Time.deltaTime); } //increase or decreate the ballista's angle by a rate of 10 degrees per second if (Input.GetKey(KeyCode.RightArrow)) { theBallista.localRotationAngle = Mathf.Min(60.0f, theBallista.localRotationAngle + 10.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.LeftArrow)) { theBallista.localRotationAngle = Mathf.Max(-60.0f, theBallista.localRotationAngle - 10.0f * Time.deltaTime); } //increase or decreate the ballista's displacement by a rate of 50 centimeters per second if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) { theBallista.displacement = Mathf.Min(2.0f, theBallista.displacement + 0.5f * Time.deltaTime); } if (Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.LeftControl)) { theBallista.displacement = Mathf.Max(0.0f, theBallista.displacement - 0.5f * Time.deltaTime); } theBall.SimulateFlight(theBallista); //check for game start if (Input.GetKeyDown(KeyCode.Space)) { Vector3 spawnPos = new Vector3(0, 0, 0); theBallista.GetSpawnBallPosition(out spawnPos); theBall.ApplyMovement(spawnPos); theBall.LaunchBall(theBallista.localElevationAngle, theBallista.localRotationAngle); theGameState = GameStates.GS_GAME_IN_PROGRESS; } break; case GameStates.GS_GAME_IN_PROGRESS: break; case GameStates.GS_ENDGAME_VICTORY: //check for new game if (Input.GetKeyDown(KeyCode.Space)) { Restart(); } break; case GameStates.GS_ENDGAME_DEFEAT: //check for new game if (Input.GetKeyDown(KeyCode.Space)) { Restart(); } break; } }
public void SimulateFlight(BallistaScript theBallista) { Vector3 startPos = new Vector3(0, 0, 0); Vector3 endPos = new Vector3(0, 0, 0); Vector3 moveDelta = new Vector3(0, 0, 0); Vector3 simVelocity = new Vector3(0, 0, 0); Vector3 hitSurfaceNormal = new Vector3(0, 1, 0); bool hitTarget = false; bool hitGround = false; int sanity = 0; if (theReticule == null) { return; } if (theBallista != null) { theBallista.GetSpawnBallPosition(out startPos); GetLaunchVelocity(theBallista.localElevationAngle, theBallista.localRotationAngle, out simVelocity); //print("StartPos = " + startPos + " simVelocity = " + simVelocity); //simulate the flight in 1/5 second increments while (!hitTarget && !hitGround) { DoMotion(startPos, 0.1f, ref simVelocity, out moveDelta); DoCollisionDetection(startPos, moveDelta, out endPos, out hitSurfaceNormal, out hitTarget, out hitGround); //set our start position to be equal to the end position and try again startPos = endPos; //print("simVelocity = " + simVelocity); sanity++; if (sanity > 1000) { break; } } } if (hitTarget) { theReticule.renderer.material = greenReticuleMaterial; } else { theReticule.renderer.material = redReticuleMaterial; } //print("hitTarget - " + hitTarget + " endPos = " + endPos + " sanity = " + sanity); //where should the reticule go, and what direction should it face? theReticule.newPos = endPos; theReticule.newOrientationInEulerAngles = Vector3.zero; float angleFromVertical = Vector3.Angle(hitSurfaceNormal, Vector3.up); if ((angleFromVertical > 1.0f) && (angleFromVertical < 45.0f)) { // bit of a hack... I know the orientation of the hill, // so if the reticule is hitting anything other than flat ground, // set it to the orientation of the hill theReticule.newOrientationInEulerAngles = theGameManager.theHill.transform.eulerAngles; } }
public void SimulateFlight(BallistaScript theBallista) { Vector3 startPos = new Vector3(0,0,0); Vector3 endPos = new Vector3(0,0,0); Vector3 moveDelta = new Vector3(0,0,0); Vector3 simVelocity = new Vector3(0,0,0); Vector3 hitSurfaceNormal = new Vector3(0,1,0); bool hitTarget = false; bool hitGround = false; int sanity = 0; if (theReticule == null) { return; } if (theBallista != null) { theBallista.GetSpawnBallPosition(out startPos); GetLaunchVelocity(theBallista.localElevationAngle, theBallista.localRotationAngle, out simVelocity); //print("StartPos = " + startPos + " simVelocity = " + simVelocity); //simulate the flight in 1/5 second increments while (!hitTarget && !hitGround) { DoMotion(startPos, 0.1f, ref simVelocity, out moveDelta); DoCollisionDetection(startPos, moveDelta, out endPos, out hitSurfaceNormal, out hitTarget, out hitGround); //set our start position to be equal to the end position and try again startPos = endPos; //print("simVelocity = " + simVelocity); sanity++; if (sanity > 1000) { break; } } } if (hitTarget) { theReticule.renderer.material = greenReticuleMaterial; } else { theReticule.renderer.material = redReticuleMaterial; } //print("hitTarget - " + hitTarget + " endPos = " + endPos + " sanity = " + sanity); //where should the reticule go, and what direction should it face? theReticule.newPos = endPos; theReticule.newOrientationInEulerAngles = Vector3.zero; float angleFromVertical = Vector3.Angle(hitSurfaceNormal, Vector3.up); if ((angleFromVertical > 1.0f) && (angleFromVertical < 45.0f)) { // bit of a hack... I know the orientation of the hill, // so if the reticule is hitting anything other than flat ground, // set it to the orientation of the hill theReticule.newOrientationInEulerAngles = theGameManager.theHill.transform.eulerAngles; } }