using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public SpriteRenderer fuelBar; //related HUD element public SpriteRenderer fuelOverfillBar; //related HUD element public float fuelCapacity = 100.0f; public float fuelCostPerFrame = 0.3f; public float fuelForce = 1.5f; public float turnRate = 4.0f; public float fuelOverfillCapacity = 100.0f; public float fuelOverfillCostPerFrame = 0.7f; public float fuelOverfillForce = 2.0f; public float overfillTurnRate = 8.0f; public float fuelFromFood = 25.0f; public float flapForce = 70.0f; private Vector3 fuelScale; private Vector3 fuelOverfillScale; private float currentFuel; private float currentFuelOverfill; private bool paused = false; private GameObject menuObject; void Start() { //initiate all the values currentFuel = fuelCapacity; currentFuelOverfill = 0; menuObject = GameObject.Find ("Menu"); fuelScale = fuelBar.transform.localScale; fuelOverfillScale = fuelOverfillBar.transform.localScale; Vector3 startPos = new Vector3(-10,-10,0); gameObject.rigidbody2D.transform.position = startPos; rigidbody2D.gravityScale = 0; paused = true; } void Update() { //adjust fuel bar HUD graphic scales to match the current fuel amount fuelBar.transform.localScale = new Vector3(fuelScale.x * (currentFuel/fuelCapacity), 1, 1); fuelOverfillBar.transform.localScale = new Vector3(fuelOverfillScale.x * (currentFuelOverfill/fuelCapacity), 1, 1); } void FixedUpdate() { if (paused) { return; } //rotate toward cursor Vector3 cursorPos = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1)); Vector3 targetDir = cursorPos - gameObject.transform.position; targetDir.Normalize(); Vector3 angles = gameObject.transform.eulerAngles; //if overfull, propel at overfill rate if (currentFuelOverfill > 0) { //turn at overfill rate angles.z = Mathf.MoveTowardsAngle(gameObject.transform.eulerAngles.z, Mathf.Atan2(-targetDir.x,targetDir.y)*Mathf.Rad2Deg, overfillTurnRate); gameObject.transform.eulerAngles = angles; //get direction of movement Vector2 propulsionDirection = new Vector2(-Mathf.Sin(angles.z*Mathf.Deg2Rad), Mathf.Cos(angles.z*Mathf.Deg2Rad)); propulsionDirection.Normalize(); //spend fuel gameObject.rigidbody2D.velocity = (propulsionDirection*fuelOverfillForce); currentFuelOverfill -= fuelOverfillCostPerFrame; if (currentFuelOverfill <= 0) { currentFuelOverfill = 0; } } //otherwise, propel at standard speed else if (currentFuel >= 0) { //turn at standard rate, regardless of fuel angles.z = Mathf.MoveTowardsAngle(gameObject.transform.eulerAngles.z, Mathf.Atan2(-targetDir.x,targetDir.y)*Mathf.Rad2Deg, turnRate); gameObject.transform.eulerAngles = angles; if (currentFuel > 0) { //get direction of movement Vector2 propulsionDirection = new Vector2(-Mathf.Sin(angles.z*Mathf.Deg2Rad), Mathf.Cos(angles.z*Mathf.Deg2Rad)); propulsionDirection.Normalize(); //spend fuel gameObject.rigidbody2D.velocity = (propulsionDirection*fuelForce); currentFuel -= fuelCostPerFrame; if (currentFuel <= 0) { currentFuel = 0; rigidbody2D.gravityScale = 1; } } } } public void Reset() { //reset the bird's everything for a fresh game paused = false; currentFuel = fuelCapacity; currentFuelOverfill = 0; Vector3 startPos = new Vector3(0,0,0); Quaternion startRot = new Quaternion(0,0,0,0); transform.position = startPos; transform.rotation = startRot; rigidbody2D.gravityScale = 0; rigidbody2D.fixedAngle = true; } void OnTriggerEnter2D(Collider2D collider) { if (paused) return; //bird eats food! if (collider.tag == "Food") { GameObject.Find ("GetMeat").audio.Play(); currentFuel += fuelFromFood; if (currentFuel > fuelCapacity) { GameObject.Find ("Overfill").audio.Play(); currentFuelOverfill = currentFuel - fuelCapacity; currentFuel = fuelCapacity; } Destroy(collider.gameObject); rigidbody2D.gravityScale = 0; } } void OnCollisionEnter2D(Collision2D collision) { if (paused) return; //bird dies! if (collision.collider.tag == "Obstacle") { //GAME OVER MAN! show that sweet retry menu GameObject.Find ("Hit").audio.Play(); GameObject.Find ("RecordScratch").audio.Play(); GameObject.Find ("Blue_Danube_by_Strauss").audio.Stop(); paused = true; rigidbody2D.gravityScale = 1; rigidbody2D.fixedAngle = false; menuObject.SetActive(true); Menu menu = menuObject.GetComponent