using UnityEngine; using System.Collections; public class ChaseBarrelGenerator : BarrelGenerator { // The burning ship prefab public GameObject burningShip; ChaseLevelManager chaseLevelManager; // How the chase-ee moves from side to side float sideFactor; Vector3 nextVector; float shipZ; float temp; float wideBarrelDistance = 30.0f; // See MoveShip for a discussion of this field float shipTime; Vector3 originalPosition; Quaternion originalRotation; // Multiply shipZ when hit by an acorn (Dan) int hitMultiplier; protected Material shipMaterial; public override void Awake() { base.Awake(); chaseLevelManager = levelManager.GetComponent(); } public override void Start () { // In chase mode, the barrels spawn at the back of the ship, so they don't use a very large // xDistance. But that looks silly for the initial barrels. So, spread the initial barrels out // over a large xDistance and then switch back to the narrow xDistance temp = xDistance; xDistance = wideBarrelDistance; base.Start(); xDistance = temp; // How far the ship wanders from side-to-side sideFactor = 0.5f; shipTime = StartingShipTime(); // Remember where we started originalPosition = transform.position; originalRotation = transform.rotation; // No hits yet. (Dan) hitMultiplier = 1; shipMaterial = GameObject.Find("ship").renderer.material; } protected void FixedUpdate () { // Generate the barrels base.Update(); // The chase-ee does some kind of movement unless we've paused the game if (Time.timeScale > 0f) MoveGenerator(); if (Input.GetKeyDown("space")) transform.Translate(Vector3.forward * -30, Space.World); } void MoveGenerator() { // The generator starts in the center and moves side to side based on a cosine function. If it doesn't move // equally from side to side, the ship runs the risk of risk of moving, or even sinking, off-screen. To // prevent that, base the cosine on time by way of a field, shipTime, that starts at 0 or pi * (4/3) so that // Mathf.Cos(shipTime * 0.75f) = 1 or -1. We reset ship time each time we reset the generator float tempOffset = Mathf.Cos(shipTime * 0.75f); shipTime += Time.deltaTime; // Always sway the ship transform.eulerAngles = new Vector3(0,0, Mathf.Sin(Time.time * 0.75f) * 10f); // Don't move along z if the player ship is sick shipZ = (playerShip.Health != 0) ? -((chaseLevelManager.GeneratorSpeed + playerShip.BarrelBoost) * hitMultiplier * Time.deltaTime) : 0; // Find where the ship goes next and move there nextVector = new Vector3(tempOffset * sideFactor, 0, shipZ); TimeManager.Translate(transform, nextVector, Space.World); // ************************************************************************************************* // This next code, commented out, came from a time when the barrel generator itself would sink, // and I wanted its side-to-side movement to narrow down as it sank. The barrel generator no // longer sinks, but I didn't want to lose this code just in case. // // Narrow the ship's side-to-side movement when burning - try to catch the cosine at 0 to minimize // the suddenness of the movement change // if ( Mathf.Abs(tempOffset) <= 0.01f ) { // sideFactor = 0.24f; // } // TimeManager.Translate(transform, // new Vector3(tempOffset * sideFactor, 0, 0), // Space.World); // ************************************************************************************************* } public void OnTriggerEnter(Collider other) { // Deal with the chase ship overtaking the barrel generator ShipControls sc = other.GetComponent(); if (sc != null) { // Create the burning ship and make it sink StartCoroutine(SinkPrefab()); // Move the barrel generator back out to the horizon ResetGenerator(); // Let the level manager know so it can update the level levelManager.GetComponent().ShipCaught(); } } IEnumerator SinkPrefab() { // Create the prefab on top of the barrel generator GameObject go = (GameObject)Instantiate(burningShip, transform.position, transform.rotation); // This one second wait seems to allow time for the burning ship Start() to complete yield return new WaitForSeconds(1.5f); go.GetComponent().Sink(); // go.GetComponent().Sink(); } // Called when an Acorn hits the ship, adds to the shipZMultiplier causing the ship // to move toward the player's ship more quickly (or move forward more slowly). (Dan) public int AcornHit() { hitMultiplier++; shipMaterial.SetFloat("_Cutoff", 1.0f - (float)(1.0f/hitMultiplier)); return hitMultiplier; } // (Dan) public float GetShipZ() { return transform.position.z; } // See MoveShip for a discussion of shipTime. StartingShipTime sets (resets) shipTime. Setting shipTime to 0 starts // the generator moving to the right. Setting shipTime to pi * (4 / 3) starts the generator moving to the left. float StartingShipTime() { int n = Random.Range(0, 10); if (n < 5) { return 0; } else { return Mathf.PI * (4 / 3.0f); } } void ResetGenerator() { // Move the generator back to it's starting location transform.position = originalPosition; transform.rotation = originalRotation; // re-center the generator's side to side movement shipTime = StartingShipTime(); // Create some inital barrels over a wide distance again, just as in start, but create one fewer just // so things don't get too croweded too fast temp = xDistance; xDistance = wideBarrelDistance; base.CreateInitalBarrels(initialBarrels - 1); xDistance = temp; // Reset the Ship's hit multiplier (Dan) hitMultiplier = 1; shipMaterial.SetFloat("_Cutoff", 0.0f); } // void OnGUI() { // // GUI.Label(new Rect(Screen.width * 0.10f, Screen.height * 0.01f, // Screen.width * 0.20f, Screen.height * 0.20f), // "Real cbg speed\n" + (chaseLevelManager.GeneratorSpeed + playerShip.BarrelBoost)); // // GUI.Label(new Rect(Screen.width * 0.25f, Screen.height * 0.01f, // Screen.width * 0.20f, Screen.height * 0.20f), // "percentGoodBarrels\n" + percentGoodBarrels); // // GUI.Label(new Rect(Screen.width * 0.45f, Screen.height * 0.01f, // Screen.width * 0.20f, Screen.height * 0.20f), // "percentBoostBarrels\n" + percentBoostBarrels); // // } }