Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.04 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class StateMachine : MonoBehaviour {
  5.  
  6.     // Static variables are always constant
  7.     public static Vector3 mousePosition;
  8.     public static Vector3 playerPosition;
  9.     public static GameObject mainCamera;
  10.     public static GameObject player;
  11.     public static Transform selectedPlatform;
  12.     public static Transform selectedPanel;
  13.     public static Transform selectedPanel2;
  14.     public static int selectedPanelNum;
  15.     public static int selectedPanelNum2;
  16.     public static int score;
  17.     public static Transform targetPlatform;
  18.     public static Ray ray;
  19.     public static int state;
  20.     public static GameObject[] allPlatforms;
  21.     //State 0 is gameplay state
  22.     //State 1 is menu state
  23.     //State 2 is level completestate
  24.     //State 3 is Cutscene state
  25.     // Use this for initialization
  26.     void Start () {
  27.         player =  GameObject.FindWithTag("Player");
  28.         resetPanels();
  29.     }
  30.    
  31.     // Update is called once per frame
  32.     void Update () {
  33.         ray = Camera.main.ScreenPointToRay(new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
  34.         playerPosition = Camera.main.WorldToScreenPoint (new Vector3 (player.transform.position.x, player.transform.position.y, 10));
  35.         mousePosition = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10));
  36.         if(state == 0){
  37.         checkPanels();
  38.         CheckforMouseClick();
  39.         }
  40.         /*if(state == 2){
  41.             // Run level completed sequence and stop camera
  42.             StartCoroutine()
  43.         }*/
  44.     }
  45.     public void resetPanels(){
  46.         selectedPanel = null;
  47.         selectedPanel2 = null;
  48.         selectedPanelNum = 400;
  49.         selectedPanelNum2 = 400;
  50.  
  51.     }
  52.     void CheckforMouseClick(){
  53.         RaycastHit hit;
  54.         if (Physics.Raycast(ray, out hit)) {
  55.         if(hit.transform.tag == "Platform" &&  hit.transform != selectedPlatform){
  56.                 targetPlatform = hit.transform;
  57.             }
  58.         }
  59.     }
  60.  
  61.     void checkPanels(){
  62.         RaycastHit hit;
  63.         if(selectedPlatform){
  64.             if (Physics.Raycast(ray, out hit)) {
  65.                 if(hit.transform.tag == "numberPanel"){
  66.                     if(Input.GetMouseButtonDown (0)){
  67.                     //If the first panel is selected and the eqation is above nine select another panel
  68.                     if(selectedPlatform.transform.GetComponent<Equation>().resultAbove9 && selectedPanel){
  69.                         hit.transform.GetComponent<numberPanel>().selected = true;
  70.                         selectedPanelNum2 = hit.transform.GetComponent<numberPanel>().num;
  71.                         selectedPanel2 = hit.transform;
  72.                         checkResult();
  73.                     }else{
  74.                         hit.transform.GetComponent<numberPanel>().selected = true;
  75.                         selectedPanelNum = hit.transform.GetComponent<numberPanel>().num;
  76.                         selectedPanel = hit.transform;
  77.                         checkResult();
  78.                         }
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.     }
  84.     //Check if asnwer is incorrect in coresspondens with the platform, therefore removing one life from the platform
  85.     void checkResult(){
  86.         int Result = selectedPlatform.GetComponent<Equation>().Result();
  87.         bool above9 = selectedPlatform.GetComponent<Equation>().resultAbove9;
  88.         if(panelResult() != Result && above9 && selectedPanelNum2 < 200){
  89.             selectedPlatform.GetComponent<Equation>().life -= 1;
  90.             resetPanels();
  91.         } else if( selectedPanelNum != Result && !above9){
  92.             selectedPlatform.GetComponent<Equation>().life -= 1;
  93.             resetPanels();
  94.         }
  95.     }
  96.  
  97.     public static int panelResult(){
  98.         if(selectedPanel2){
  99.         return  int.Parse( ""+selectedPanelNum +""+ selectedPanelNum2);
  100.         }else{
  101.             return  selectedPanelNum;
  102.         }
  103.     }
  104. }
  105.     public class levelGenerator : StateMachine {
  106.     public static GameObject friend;
  107.     public GameObject platform;
  108.     //public GameObject[] allPlatforms;
  109.     public int LimitX;
  110.     public int LimitY;
  111.     public int spaceBetween;
  112.     float posX;
  113.     float posY = 10.79f;
  114.     // Use this for initialization
  115.     void Start () {
  116.         SpawnLevels();
  117.    
  118.     }
  119.     void SpawnLevels(){
  120.         friend = GameObject.FindGameObjectWithTag("Friend");
  121.         for (float i = 6.79f; i< LimitY; i+= spaceBetween) {
  122.             GameObject clone;
  123.             GameObject clone2;
  124.             float randomXPos =Random.Range(-LimitX,LimitX);
  125.             posY = Random.Range(i - 1f, i+1f);
  126.             Vector3 position = new Vector3(randomXPos,posY);
  127.             clone = Instantiate(platform,position,Quaternion.identity) as GameObject;
  128.             clone.transform.name = "platform "+i;
  129.             clone.transform.parent = transform;
  130.             if(randomXPos < -1 || randomXPos > 1 ){
  131.                 posY = Random.Range(i - 1f, i+1f);
  132.                 position = new Vector3(randomXPos*-1,posY);
  133.                 clone2 = Instantiate(platform,position,Quaternion.identity) as GameObject;
  134.                 clone2.transform.name = "platform "+i + "second";
  135.                 clone2.transform.parent = transform;
  136.             }
  137.         }
  138.    
  139.         allPlatforms = GameObject.FindGameObjectsWithTag("Platform");
  140.         for(int i = 0; i < allPlatforms.Length; i++){
  141.             allPlatforms[i].GetComponent<Equation>().GenerateEquation();
  142.         }
  143.         allPlatforms[allPlatforms.Length-1].transform.position = new Vector3(0, allPlatforms[allPlatforms.Length-1].transform.position.y +2,0);
  144.         friend.transform.position = new Vector3(allPlatforms[allPlatforms.Length-1].transform.position.x + 2, allPlatforms[allPlatforms.Length-1].transform.position.y + 1.5f,0);
  145.         targetPlatform = allPlatforms[1].transform;
  146.  
  147.     }
  148.  
  149.     void rngFlip(){
  150.         int flip;
  151.         flip = Random.Range(0,2);
  152.         if(flip == 0){
  153.             transform.rotation = new Quaternion(0,180,0,0);
  154.         }else{
  155.             transform.rotation = new Quaternion(0,0,0,0);
  156.         }
  157.     }
  158.  
  159. }
  160. public class playerMove : StateMachine
  161. {
  162.     public Sprite[] States = new Sprite[3];
  163.     public float minForceX = 50;
  164.     public float minForceY = 50;
  165.     float distanceX;
  166.     float distanceY ;
  167.     Equation equation;
  168.     public int result;
  169.     public int answer;
  170.     // Use this for initialization
  171.         void Start (){
  172.  
  173.         }
  174.         void flip (float distanceX){
  175.             if(distanceX>0.01f){
  176.                 transform.rotation = new Quaternion(0,0,0,0);
  177.             }else if(distanceX<0.01f){
  178.                 transform.rotation = new Quaternion(0,180,0,0);
  179.             }
  180.         }
  181.  
  182.         float forceY (float distanceY){
  183.             return minForceY;
  184.         }
  185.        
  186.     void playerState(){
  187.         //Switch between diffrent sprites depending on the players upward velocity
  188.         if(rigidbody.velocity.y >0.1f)
  189.         {
  190.             GetComponent<SpriteRenderer>().sprite = States[1];
  191.         }else{
  192.             GetComponent<SpriteRenderer>().sprite = States[0];
  193.         }
  194.     }
  195.        
  196.     // Update is called once per frame
  197.         //public GameObject particle;
  198.         void Update (){
  199.  
  200.         if(state == 0){
  201.             if(selectedPlatform){
  202.                 equation =   selectedPlatform.GetComponent<Equation>();
  203.             }
  204.             playerState();
  205.             if (Input.GetMouseButtonDown (0)&& Grounded() && selectedPlatform){
  206.                 //Only add force if equation solved
  207.                 result = equation.Result();
  208.             }
  209.             answer = selectedPanelNum;
  210.             if(targetPlatform != null){
  211.             distanceX = targetPlatform.transform.position.x - transform.position.x;
  212.             distanceY = targetPlatform.transform.position.y - transform.position.y;
  213.             }
  214.         }
  215.     }
  216.     void FixedUpdate(){
  217.         if(state == 0){
  218.             if(Input.GetMouseButton (0)&& equation.solved && Grounded() && targetPlatform != selectedPlatform){
  219.                 flip(distanceX);
  220.                 Jump();
  221.             }
  222.         }
  223.     }
  224.     //Create a bool which only returns true if the user is done inputting the answer
  225.     public bool solvedEquation(){
  226.         if(result== panelResult()){
  227.             score += 10;
  228.             return true;
  229.         }else if(result ==200){
  230.             return true;
  231.         }else{
  232.             return false;
  233.         }
  234.     }
  235.  
  236.     void Jump(){
  237.         //Remove previous velocity
  238.         rigidbody.velocity.Normalize();
  239.         //If the equation has been solved and the platform actually contained an equation add score
  240.         if(equation.solved && !equation.empty){
  241.             score += 10;
  242.         }
  243.         //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
  244.         rigidbody.AddForce (new Vector3 (distanceX*0.75f, minForceY + distanceY*0.75f, 0),  ForceMode.Impulse);
  245.         resetPanels();
  246.         //Reset result to a value which cannot equal the panel selection by default
  247.         result = 600;
  248.     }
  249.  
  250.     bool Grounded(){
  251.         RaycastHit hit;
  252.         Ray downRay = new Ray(transform.position, -Vector3.up);
  253.         if (Physics.Raycast(downRay, out hit)) {
  254.             if (Vector3.Distance(hit.transform.position,transform.position) <0.1f || rigidbody.velocity.y < 0.01f && rigidbody.velocity.y > -0.01f){
  255.                 return true;
  256.             }
  257.             return false;
  258.         }else{
  259.             return false;
  260.         }
  261.     }
  262.     void OnCollisionEnter(Collision col){
  263.         if(col.transform.tag == "Platform"){
  264.             selectedPlatform = col.transform;
  265.  
  266.         }
  267.         if(col.transform.tag == "Friend"){
  268.             state = 2;
  269.         }
  270.     }
  271.     void OnTriggerEnter(Collider col){
  272.         if(col.transform.tag == "Friend"){
  273.             col.transform.GetComponentInChildren<Animator>().SetBool("Broken", true);
  274.             state = 2;
  275.         }
  276.     }
  277. }
  278. public class Equation : StateMachine {
  279.     public GameObject equation;
  280.     public GameObject detail;
  281.     public bool empty = false;
  282.     public bool solved = false;
  283.     public bool resultAbove9 = false;
  284.     int number1;
  285.     int number2;
  286.     public int mathState;
  287.     public int life = 2;
  288.     public int Result(){
  289.         if(mathState == 0){
  290.             return number1 + number2;
  291.         }else if(mathState == 1){
  292.             return number1 - number2;
  293.         }else{
  294.             return 200;
  295.         }
  296.     }
  297.     Transform numberPlate;
  298.     public static int num;
  299.  
  300.     public void resetNum(){
  301.         num = 0;
  302.     }
  303.  
  304.     // Use this for initialization
  305.     public void GenerateEquation(){
  306.         //Increment num to determine where in the array the current platform is
  307.         num ++;
  308.         solved = false;
  309.         //Randomly decide if its addition or substraction or empty
  310.         mathState = Mathf.RoundToInt(Random.Range(0,3));
  311.         //Progressively increase difficulty throughout the level higher number ranges
  312.         //depending on what number num is in relation to the length of allPlatforms array
  313.         if(num < allPlatforms.Length/4){
  314.             number1 = Random.Range (3, 6);
  315.             number2 = Random.Range (0, 3);
  316.         }else if (num < allPlatforms.Length/2){
  317.             number1 = Random.Range (6, 10);
  318.             number2 = Random.Range (4, 6);
  319.         }else{
  320.             number1 = Random.Range (7, 16);
  321.             number2 = Random.Range (5, 7);
  322.         }
  323.         if(allPlatforms[0].name != transform.name&& allPlatforms[allPlatforms.Length-1].name != transform.name){
  324.             if(mathState == 0){
  325.                 Addition();
  326.             }else if(mathState == 1){
  327.                 Substraction();
  328.             }else if(mathState == 2){
  329.                 Empty ();
  330.             }
  331.         }else{
  332.             iTween.ScaleTo(gameObject, new Vector3(1,0.688f,1), 1);
  333.             Empty ();
  334.         }
  335.  
  336.         //Check if the result of this equation is above nine if so set resultAbove9 to true
  337.         if(Result()> 9){
  338.             resultAbove9 = true;
  339.         }
  340.         //Place the detail at a random position on the platform
  341.         detail.transform.localPosition = new Vector3(Random.Range(-1,2),0.7219478f,0);
  342.     }
  343.     public GameObject solvedEffect;
  344.     // Update is called once per frame
  345.     void Update () {
  346.         ColliderControl();
  347.         if(selectedPlatform && !solved){
  348.             if(transform.name == selectedPlatform.name && panelResult() < 400 && !resultAbove9){
  349.                 equation.GetComponent<TextMesh> ().text = equation.GetComponent<TextMesh> ().text.Replace("?", panelResult().ToString());
  350.             } else if(resultAbove9 && transform.name == selectedPlatform.name && selectedPanelNum2 < 400){
  351.                 equation.GetComponent<TextMesh> ().text = equation.GetComponent<TextMesh> ().text.Replace("?", panelResult().ToString());
  352.             }
  353.             if(transform.name == selectedPlatform.name){
  354.                 if(Result() == panelResult()){
  355.                     solved = true;
  356.                     Instantiate(solvedEffect, transform.position, Quaternion.identity);
  357.                 }
  358.                 GetComponent<SpriteRenderer>().color = Color.yellow;
  359.             }
  360.         }else{
  361.             GetComponent<SpriteRenderer>().color = Color.white;
  362.         }
  363.         Crumble ();
  364.     }
  365.     void Addition(){
  366.         numberPlate = transform.FindChild("numberPlate");
  367.         numberPlate.renderer.enabled = true;
  368.         equation.GetComponent<TextMesh> ().text = number1 + " + " + number2 +" = ?";
  369.     }
  370.     void Substraction(){
  371.         numberPlate = transform.FindChild("numberPlate");
  372.         numberPlate.renderer.enabled = true;
  373.         equation.GetComponent<TextMesh> ().text = number1 + " - " + number2 +" = ?";
  374.     }
  375.     void Empty(){
  376.         empty = true;
  377.         solved = true;
  378.         numberPlate = transform.FindChild("numberPlate");
  379.         numberPlate.renderer.enabled = false;
  380.         equation.GetComponent<TextMesh> ().text = " ";
  381.     }
  382.     void ColliderControl(){
  383.         if(player.rigidbody.velocity.y > 0){
  384.             collider.isTrigger = true;
  385.         }else{
  386.             collider.isTrigger = false;
  387.         }
  388.     }
  389.     void Crumble(){
  390.         if(life == 1){
  391.             gameObject.transform.GetComponent<iTweenEvent>().enabled = true;
  392.         }
  393.         if(life <= 0){
  394.             gameObject.SetActive(false);
  395.         }
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement