Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class StateMachine : MonoBehaviour {
- // Static variables are always constant
- public static Vector3 mousePosition;
- public static Vector3 playerPosition;
- public static GameObject mainCamera;
- public static GameObject player;
- public static Transform selectedPlatform;
- public static Transform selectedPanel;
- public static Transform selectedPanel2;
- public static int selectedPanelNum;
- public static int selectedPanelNum2;
- public static int score;
- public static Transform targetPlatform;
- public static Ray ray;
- public static int state;
- public static GameObject[] allPlatforms;
- //State 0 is gameplay state
- //State 1 is menu state
- //State 2 is level completestate
- //State 3 is Cutscene state
- // Use this for initialization
- void Start () {
- player = GameObject.FindWithTag("Player");
- resetPanels();
- }
- // Update is called once per frame
- void Update () {
- ray = Camera.main.ScreenPointToRay(new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
- playerPosition = Camera.main.WorldToScreenPoint (new Vector3 (player.transform.position.x, player.transform.position.y, 10));
- mousePosition = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
- if(state == 0){
- checkPanels();
- CheckforMouseClick();
- }
- /*if(state == 2){
- // Run level completed sequence and stop camera
- StartCoroutine()
- }*/
- }
- public void resetPanels(){
- selectedPanel = null;
- selectedPanel2 = null;
- selectedPanelNum = 400;
- selectedPanelNum2 = 400;
- }
- void CheckforMouseClick(){
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- if(hit.transform.tag == "Platform" && hit.transform != selectedPlatform){
- targetPlatform = hit.transform;
- }
- }
- }
- void checkPanels(){
- RaycastHit hit;
- if(selectedPlatform){
- if (Physics.Raycast(ray, out hit)) {
- if(hit.transform.tag == "numberPanel"){
- if(Input.GetMouseButtonDown (0)){
- //If the first panel is selected and the eqation is above nine select another panel
- if(selectedPlatform.transform.GetComponent<Equation>().resultAbove9 && selectedPanel){
- hit.transform.GetComponent<numberPanel>().selected = true;
- selectedPanelNum2 = hit.transform.GetComponent<numberPanel>().num;
- selectedPanel2 = hit.transform;
- checkResult();
- }else{
- hit.transform.GetComponent<numberPanel>().selected = true;
- selectedPanelNum = hit.transform.GetComponent<numberPanel>().num;
- selectedPanel = hit.transform;
- checkResult();
- }
- }
- }
- }
- }
- }
- //Check if asnwer is incorrect in coresspondens with the platform, therefore removing one life from the platform
- void checkResult(){
- int Result = selectedPlatform.GetComponent<Equation>().Result();
- bool above9 = selectedPlatform.GetComponent<Equation>().resultAbove9;
- if(panelResult() != Result && above9 && selectedPanelNum2 < 200){
- selectedPlatform.GetComponent<Equation>().life -= 1;
- resetPanels();
- } else if( selectedPanelNum != Result && !above9){
- selectedPlatform.GetComponent<Equation>().life -= 1;
- resetPanels();
- }
- }
- public static int panelResult(){
- if(selectedPanel2){
- return int.Parse( ""+selectedPanelNum +""+ selectedPanelNum2);
- }else{
- return selectedPanelNum;
- }
- }
- }
- public class levelGenerator : StateMachine {
- public static GameObject friend;
- public GameObject platform;
- //public GameObject[] allPlatforms;
- public int LimitX;
- public int LimitY;
- public int spaceBetween;
- float posX;
- float posY = 10.79f;
- // Use this for initialization
- void Start () {
- SpawnLevels();
- }
- void SpawnLevels(){
- friend = GameObject.FindGameObjectWithTag("Friend");
- for (float i = 6.79f; i< LimitY; i+= spaceBetween) {
- GameObject clone;
- GameObject clone2;
- float randomXPos =Random.Range(-LimitX,LimitX);
- posY = Random.Range(i - 1f, i+1f);
- Vector3 position = new Vector3(randomXPos,posY);
- clone = Instantiate(platform,position,Quaternion.identity) as GameObject;
- clone.transform.name = "platform "+i;
- clone.transform.parent = transform;
- if(randomXPos < -1 || randomXPos > 1 ){
- posY = Random.Range(i - 1f, i+1f);
- position = new Vector3(randomXPos*-1,posY);
- clone2 = Instantiate(platform,position,Quaternion.identity) as GameObject;
- clone2.transform.name = "platform "+i + "second";
- clone2.transform.parent = transform;
- }
- }
- allPlatforms = GameObject.FindGameObjectsWithTag("Platform");
- for(int i = 0; i < allPlatforms.Length; i++){
- allPlatforms[i].GetComponent<Equation>().GenerateEquation();
- }
- allPlatforms[allPlatforms.Length-1].transform.position = new Vector3(0, allPlatforms[allPlatforms.Length-1].transform.position.y +2,0);
- friend.transform.position = new Vector3(allPlatforms[allPlatforms.Length-1].transform.position.x + 2, allPlatforms[allPlatforms.Length-1].transform.position.y + 1.5f,0);
- targetPlatform = allPlatforms[1].transform;
- }
- void rngFlip(){
- int flip;
- flip = Random.Range(0,2);
- if(flip == 0){
- transform.rotation = new Quaternion(0,180,0,0);
- }else{
- transform.rotation = new Quaternion(0,0,0,0);
- }
- }
- }
- public class playerMove : StateMachine
- {
- public Sprite[] States = new Sprite[3];
- public float minForceX = 50;
- public float minForceY = 50;
- float distanceX;
- float distanceY ;
- Equation equation;
- public int result;
- public int answer;
- // Use this for initialization
- void Start (){
- }
- void flip (float distanceX){
- if(distanceX>0.01f){
- transform.rotation = new Quaternion(0,0,0,0);
- }else if(distanceX<0.01f){
- transform.rotation = new Quaternion(0,180,0,0);
- }
- }
- float forceY (float distanceY){
- return minForceY;
- }
- void playerState(){
- //Switch between diffrent sprites depending on the players upward velocity
- if(rigidbody.velocity.y >0.1f)
- {
- GetComponent<SpriteRenderer>().sprite = States[1];
- }else{
- GetComponent<SpriteRenderer>().sprite = States[0];
- }
- }
- // Update is called once per frame
- //public GameObject particle;
- void Update (){
- if(state == 0){
- if(selectedPlatform){
- equation = selectedPlatform.GetComponent<Equation>();
- }
- playerState();
- if (Input.GetMouseButtonDown (0)&& Grounded() && selectedPlatform){
- //Only add force if equation solved
- result = equation.Result();
- }
- answer = selectedPanelNum;
- if(targetPlatform != null){
- distanceX = targetPlatform.transform.position.x - transform.position.x;
- distanceY = targetPlatform.transform.position.y - transform.position.y;
- }
- }
- }
- void FixedUpdate(){
- if(state == 0){
- if(Input.GetMouseButton (0)&& equation.solved && Grounded() && targetPlatform != selectedPlatform){
- flip(distanceX);
- Jump();
- }
- }
- }
- //Create a bool which only returns true if the user is done inputting the answer
- public bool solvedEquation(){
- if(result== panelResult()){
- score += 10;
- return true;
- }else if(result ==200){
- return true;
- }else{
- return false;
- }
- }
- void Jump(){
- //Remove previous velocity
- rigidbody.velocity.Normalize();
- //If the equation has been solved and the platform actually contained an equation add score
- if(equation.solved && !equation.empty){
- score += 10;
- }
- //The built in method AddForce applies a force to the rigidbody in a direction, this direction is determines by distance X and Y and minForceY
- rigidbody.AddForce (new Vector3 (distanceX*0.75f, minForceY + distanceY*0.75f, 0), ForceMode.Impulse);
- resetPanels();
- //Reset result to a value which cannot equal the panel selection by default
- result = 600;
- }
- bool Grounded(){
- RaycastHit hit;
- Ray downRay = new Ray(transform.position, -Vector3.up);
- if (Physics.Raycast(downRay, out hit)) {
- if (Vector3.Distance(hit.transform.position,transform.position) <0.1f || rigidbody.velocity.y < 0.01f && rigidbody.velocity.y > -0.01f){
- return true;
- }
- return false;
- }else{
- return false;
- }
- }
- void OnCollisionEnter(Collision col){
- if(col.transform.tag == "Platform"){
- selectedPlatform = col.transform;
- }
- if(col.transform.tag == "Friend"){
- state = 2;
- }
- }
- void OnTriggerEnter(Collider col){
- if(col.transform.tag == "Friend"){
- col.transform.GetComponentInChildren<Animator>().SetBool("Broken", true);
- state = 2;
- }
- }
- }
- public class Equation : StateMachine {
- public GameObject equation;
- public GameObject detail;
- public bool empty = false;
- public bool solved = false;
- public bool resultAbove9 = false;
- int number1;
- int number2;
- public int mathState;
- public int life = 2;
- public int Result(){
- if(mathState == 0){
- return number1 + number2;
- }else if(mathState == 1){
- return number1 - number2;
- }else{
- return 200;
- }
- }
- Transform numberPlate;
- public static int num;
- public void resetNum(){
- num = 0;
- }
- // Use this for initialization
- public void GenerateEquation(){
- //Increment num to determine where in the array the current platform is
- num ++;
- solved = false;
- //Randomly decide if its addition or substraction or empty
- mathState = Mathf.RoundToInt(Random.Range(0,3));
- //Progressively increase difficulty throughout the level higher number ranges
- //depending on what number num is in relation to the length of allPlatforms array
- if(num < allPlatforms.Length/4){
- number1 = Random.Range (3, 6);
- number2 = Random.Range (0, 3);
- }else if (num < allPlatforms.Length/2){
- number1 = Random.Range (6, 10);
- number2 = Random.Range (4, 6);
- }else{
- number1 = Random.Range (7, 16);
- number2 = Random.Range (5, 7);
- }
- if(allPlatforms[0].name != transform.name&& allPlatforms[allPlatforms.Length-1].name != transform.name){
- if(mathState == 0){
- Addition();
- }else if(mathState == 1){
- Substraction();
- }else if(mathState == 2){
- Empty ();
- }
- }else{
- iTween.ScaleTo(gameObject, new Vector3(1,0.688f,1), 1);
- Empty ();
- }
- //Check if the result of this equation is above nine if so set resultAbove9 to true
- if(Result()> 9){
- resultAbove9 = true;
- }
- //Place the detail at a random position on the platform
- detail.transform.localPosition = new Vector3(Random.Range(-1,2),0.7219478f,0);
- }
- public GameObject solvedEffect;
- // Update is called once per frame
- void Update () {
- ColliderControl();
- if(selectedPlatform && !solved){
- if(transform.name == selectedPlatform.name && panelResult() < 400 && !resultAbove9){
- equation.GetComponent<TextMesh> ().text = equation.GetComponent<TextMesh> ().text.Replace("?", panelResult().ToString());
- } else if(resultAbove9 && transform.name == selectedPlatform.name && selectedPanelNum2 < 400){
- equation.GetComponent<TextMesh> ().text = equation.GetComponent<TextMesh> ().text.Replace("?", panelResult().ToString());
- }
- if(transform.name == selectedPlatform.name){
- if(Result() == panelResult()){
- solved = true;
- Instantiate(solvedEffect, transform.position, Quaternion.identity);
- }
- GetComponent<SpriteRenderer>().color = Color.yellow;
- }
- }else{
- GetComponent<SpriteRenderer>().color = Color.white;
- }
- Crumble ();
- }
- void Addition(){
- numberPlate = transform.FindChild("numberPlate");
- numberPlate.renderer.enabled = true;
- equation.GetComponent<TextMesh> ().text = number1 + " + " + number2 +" = ?";
- }
- void Substraction(){
- numberPlate = transform.FindChild("numberPlate");
- numberPlate.renderer.enabled = true;
- equation.GetComponent<TextMesh> ().text = number1 + " - " + number2 +" = ?";
- }
- void Empty(){
- empty = true;
- solved = true;
- numberPlate = transform.FindChild("numberPlate");
- numberPlate.renderer.enabled = false;
- equation.GetComponent<TextMesh> ().text = " ";
- }
- void ColliderControl(){
- if(player.rigidbody.velocity.y > 0){
- collider.isTrigger = true;
- }else{
- collider.isTrigger = false;
- }
- }
- void Crumble(){
- if(life == 1){
- gameObject.transform.GetComponent<iTweenEvent>().enabled = true;
- }
- if(life <= 0){
- gameObject.SetActive(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement