Created
March 17, 2018 08:04
-
-
Save cosmo-naut/4c7b2092e67dc8fb3b28923bff3ded35 to your computer and use it in GitHub Desktop.
Anvil.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Anvil : MonoBehaviour { | |
public float forceMax; //maximum force applied to the anvil, furthest you can throw | |
public float forceMin; //absolute minimum force applied | |
public float movementQueueLength = 10; | |
public GameManager gameManager; | |
public float timeOut = 5; // Number of seconds to wait until we aassume the anvil is lost | |
private Rigidbody rigidBody; | |
private Vector3 start = new Vector3(); | |
private Vector3 end = new Vector3(); | |
private Vector3 direction = new Vector3(); //the normalised direction the player has flung the anvil | |
private float force; // the strength with which the player has flung the anvil | |
private bool drag; | |
private Queue<Vector3> movement = new Queue<Vector3>(); | |
private float cameraDistanceFromGame = 8f; | |
private bool landed = false; | |
private bool launched = false; | |
// Use this for initialization | |
void Start () { | |
drag = false; | |
rigidBody = GetComponent<Rigidbody>(); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (drag) | |
{ | |
if (movement.Count >= movementQueueLength) | |
{ | |
movement.Dequeue(); | |
} | |
movement.Enqueue(Input.mousePosition); | |
} | |
} | |
private void OnCollisionEnter(Collision collision) | |
{ | |
//Debug.Log("Anvil.OnCollisionEnter: " + landed); | |
if (!landed) // Check that this is the first collision made | |
{ | |
if (collision.gameObject.GetComponent<Target>() != null) | |
{ | |
StopCoroutine("LostAnvilCollection"); | |
gameManager.Success(50); | |
landed = true; | |
} | |
else if (collision.gameObject.tag == "Ground") | |
{ | |
gameManager.Fail(this); | |
landed = true; | |
} | |
} | |
} | |
private void OnMouseDown() | |
{ | |
if (launched) { return; } | |
start = getPos(Input.mousePosition); | |
Debug.Log("Anvil.OnMouseUp: " + start); | |
drag = true; | |
} | |
private void OnMouseUp() | |
{ | |
if (drag) | |
{ | |
end = getPos(Input.mousePosition); | |
Debug.Log("Anvil.OnMouseUp: " + end); | |
drag = false; | |
Launch(); | |
} | |
} | |
void Launch() | |
{ | |
// calculations | |
if (movement.Count < 2) | |
{ | |
Debug.Log("Anvil.Launch: Not enough values in movement queue"); | |
return; | |
} | |
int i = movement.Count - 2; | |
Debug.Log("Anvil.Launch: Removing " + i + " results from queue"); | |
for(; i > 0; i--) //Remove all but the last two results | |
{ | |
movement.Dequeue(); | |
} | |
Vector3 temp = movement.Dequeue(); | |
direction = (movement.Dequeue() - temp).normalized; | |
Debug.Log("Anvil.Launch: Direction: " + direction); | |
force = (end - start).magnitude; | |
Debug.Log("Anvil.Launch: Force: " + force); | |
//apply force to the anvil here | |
rigidBody.AddForce(direction * force * 100); | |
StartCoroutine("LostAnvilCollection"); | |
launched = true; | |
landed = false; | |
//cleanup | |
movement.Clear(); | |
} | |
Vector3 getPos(Vector3 mousePos) | |
{ | |
return Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cameraDistanceFromGame)); | |
} | |
IEnumerable LostAnvilCollection() | |
{ | |
Debug.Log("Anvil.LostAnvilCollection: Starting Routine"); | |
yield return null; | |
yield return new WaitForSeconds(5); | |
Debug.Log("Anvil.LostAnvilCollection: Resetting anvil"); | |
gameManager.Fail(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment