Guest User

Untitled

a guest
May 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Globas : MonoBehaviour {
  5.        
  6.     private Others.State state = Others.State.Prepare;
  7.    
  8.     private Ray ray = new Ray();
  9.     private int areacenterx;
  10.     private GameObject mainbox;
  11.    
  12.     public int timelimit = 0;
  13.     public int steplimit = 0;
  14.    
  15.     public Vector2 limits;
  16.     public Vector2 areasize;
  17.     public float starttime;
  18.     private float gametime;
  19.     public Vector3 mouseposition;
  20.     public RaycastHit hit = new RaycastHit();
  21.     public int screenwidth;
  22.     public int screenheight;
  23.    
  24.     //Global flags
  25.     public bool mouseleft; //State of left mouse button
  26.     public bool hitfree = true;
  27.     public bool winner = false;
  28.    
  29.     //Prefabs
  30.     public GameObject floor; //Floor
  31.     public GameObject arrow; //Arrow
  32.     public GameObject mainboxprefab; //MainBox (Player)
  33.     public GameObject mainboxcakeprefab;
  34.     public GameObject here;
  35.    
  36.     //Creaters objects
  37.     public GameObject[] arrows = new GameObject[2]; //Arrows
  38.     private GameObject[,] floors; //Floor
  39.     private GameObject[][] fencs;
  40.    
  41.     public Material fencsHor;
  42.     public Material fencsVert;
  43.     public Material fencsAngH;
  44.     public Material fencsAngV;
  45.    
  46.     public Vector3 mousecposition;
  47.     private Vector3 winposition;
  48.    
  49.     public int steps;
  50.     private Texture2D splash;
  51.     private bool cameramove;
  52.     private Vector3 cameraposition;
  53.     private Vector3 newcameraposition;
  54.    
  55.     void Start() {
  56.         starttime = Time.time;
  57.         limits = new Vector2(areasize.x*0.32f, areasize.y*0.32f);
  58.         floors = new GameObject[(int)areasize.x, (int)areasize.y];
  59.         fencs = new GameObject[5][];
  60.         fencs[0] = new GameObject[(int)areasize.x];
  61.         fencs[1] = new GameObject[(int)areasize.x];
  62.         fencs[2] = new GameObject[(int)areasize.y];
  63.         fencs[3] = new GameObject[(int)areasize.y];
  64.         fencs[4] = new GameObject[4];
  65.         screenwidth = Screen.width;
  66.         screenheight = Screen.height;
  67.         cameraposition = newcameraposition = Camera.main.transform.position;
  68.        
  69.         for (int i = 0; i < floors.GetLength(0); i++) {
  70.             for (int j = 0; j < floors.GetLength(1); j++) {
  71.                 floors[i, j] = Instantiate(floor, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
  72.                 floors[i, j].transform.position = new Vector3(0.32f*j, 0, 0.32f*i);
  73.                 floors[i, j].transform.Rotate(new Vector3(270, 0, 90));
  74.             }
  75.         }  
  76.        
  77.         for (int i = 0; i < fencs.Length - 1; i++) {
  78.             for (int j = 0; j < fencs[i].Length; j++) {
  79.                 if (i == 0) {
  80.                     fencs[i][j] = Instantiate(floor, new Vector3(-0.32f, 0f, 0.32f * j), Quaternion.identity) as GameObject;
  81.                     fencs[i][j].transform.Rotate(new Vector3(270, 0, 180));
  82.                     fencs[i][j].renderer.material = fencsVert;
  83.                 }
  84.                 else if (i == 1) {
  85.                     fencs[i][j] = Instantiate(floor, new Vector3(0.32f * areasize.y, 0f, 0.32f * j), Quaternion.identity) as GameObject;
  86.                     fencs[i][j].transform.Rotate(new Vector3(270, 0, 0));
  87.                     fencs[i][j].renderer.material = fencsVert;
  88.                 }
  89.                 else if (i == 2) {
  90.                     fencs[i][j] = Instantiate(floor, new Vector3(0.32f * j, 0f, -0.32f), Quaternion.identity) as GameObject;
  91.                     fencs[i][j].transform.Rotate(new Vector3(270, 0, 0));
  92.                     fencs[i][j].renderer.material = fencsHor;
  93.                 }
  94.                 else if (i == 3) {
  95.                     fencs[i][j] = Instantiate(floor, new Vector3(0.32f * j, 0f, 0.32f * areasize.x), Quaternion.identity) as GameObject;
  96.                     fencs[i][j].transform.Rotate(new Vector3(270, 0, 180));
  97.                     fencs[i][j].renderer.material = fencsHor;
  98.                 }
  99.             }
  100.         }
  101.        
  102.         fencs[4][0] = Instantiate(floor, new Vector3(-0.32f, 0, -0.32f), Quaternion.identity) as GameObject;
  103.         fencs[4][0].transform.Rotate(new Vector3(270, 0, 180));
  104.         fencs[4][0].renderer.material = fencsAngV;
  105.        
  106.         fencs[4][1] = Instantiate(floor, new Vector3(-0.32f, 0, 0.32f * areasize.y), Quaternion.identity) as GameObject;
  107.         fencs[4][1].transform.Rotate(new Vector3(270, 0, 180));
  108.         fencs[4][1].renderer.material = fencsAngH;
  109.        
  110.         fencs[4][2] = Instantiate(floor, new Vector3(0.32f * areasize.x, 0, 0.32f * areasize.y), Quaternion.identity) as GameObject;
  111.         fencs[4][2].transform.Rotate(new Vector3(270, 0, 0));
  112.         fencs[4][2].renderer.material = fencsAngV;
  113.        
  114.         fencs[4][3] = Instantiate(floor, new Vector3(0.32f * areasize.x, 0, -0.32f), Quaternion.identity) as GameObject;
  115.         fencs[4][3].transform.Rotate(new Vector3(270, 0, 0));
  116.         fencs[4][3].renderer.material = fencsAngH;
  117.                
  118.         for (int i = 0; i < arrows.Length; i++) {
  119.             arrows[i] = Instantiate(arrow, new Vector3(1000, 1000, 1000), Quaternion.identity) as GameObject;
  120.             arrows[i].transform.rotation = Quaternion.Euler(270, 270, 0);
  121.         }
  122.        
  123.         areacenterx = (int)Mathf.Round(areasize.x / 2);
  124.         if ((areasize.x % 2) != 0)
  125.             areacenterx++;
  126.        
  127.         mainbox = Instantiate(mainboxprefab, mainboxprefab.transform.position, Quaternion.identity) as GameObject;
  128.         mainbox.transform.position = new Vector3(0, 0, 0.32f*(areacenterx-1));
  129.        
  130.         winposition = new Vector3(0.32f*(areasize.y-1),
  131.                                   mainbox.transform.position.y,
  132.                                   mainbox.transform.position.z);   
  133.        
  134.         here = Instantiate(here, here.transform.position, Quaternion.identity) as GameObject;
  135.         here.transform.eulerAngles = new Vector3(270f, 270f, 0f);
  136.         here.transform.position = winposition + new Vector3(0f, 0.5f, 0f);
  137.        
  138.         splash = new Texture2D(screenwidth, screenheight);
  139.         for (int i = 0; i <= splash.width; i++)
  140.             for (int j = 0; j <= splash.height; j++)
  141.                  splash.SetPixel(i, j, new Color(0f, 0f, 0f, 0.8f));
  142.         splash.Apply();
  143.     }
  144.  
  145.     void Update() {
  146.        
  147.         if (Camera.main.transform.position != newcameraposition && cameramove)
  148.             Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, newcameraposition, Time.deltaTime);
  149.        
  150.         switch (state) {
  151.         case Others.State.Loading:
  152.             break;
  153.            
  154.         case Others.State.Playning:
  155.             Playning();
  156.             break;
  157.            
  158.         case Others.State.Paused:
  159.             break;
  160.            
  161.         case Others.State.Winner:
  162.             break;
  163.         }
  164.     }
  165.    
  166.     void OnGUI() {
  167.         switch (state) {
  168.         case Others.State.Loading:
  169.             Others.GUILoadingScreen();
  170.             break;
  171.            
  172.         case Others.State.Prepare:
  173.             GUIPrepare();
  174.             break;
  175.            
  176.         case Others.State.Playning:
  177.             GUIPlayning();
  178.             break;
  179.            
  180.         case Others.State.Paused:
  181.             break;
  182.            
  183.         case Others.State.Winner:
  184.             break;
  185.            
  186.         case Others.State.Loser:
  187.             GUILoser();
  188.             break;
  189.         }
  190.     }
  191.    
  192.     void OnLevelWasLoaded() {
  193.         state = Others.State.Prepare;
  194.     }
  195.    
  196.     private void Playning() {
  197.         mouseposition = Input.mousePosition;
  198.         mouseleft = Input.GetMouseButton(0);
  199.        
  200.         gametime = Time.time - starttime;
  201.        
  202.         ray = Camera.main.ScreenPointToRay(mouseposition);
  203.         if (Physics.Raycast(ray, out hit) && hitfree && !winner) {
  204.             if (hit.collider.gameObject.tag.Equals("Box")) {
  205.                 (hit.collider.gameObject.GetComponent("Box") as Box).MouseTrigger();
  206.             }          
  207.         }
  208.        
  209.         if (!(mainbox.GetComponent("Box") as Box).move) {
  210.             if (mainbox.transform.position == winposition) {
  211.                 //Destroy(mainbox);
  212.                 //mainbox = Instantiate(mainboxcakeprefab, mainbox.transform.position, Quaternion.identity) as GameObject;
  213.                 state = Others.State.Winner;
  214.             }
  215.             else if (timelimit != 0 && gametime >= timelimit)
  216.                 state = Others.State.Loser;
  217.             else if (steplimit != 0 && steps >= steplimit)
  218.                 state = Others.State.Loser;
  219.         }
  220.     }
  221.    
  222.     private void GUIPlayning() {
  223.         float time = Mathf.Round(gametime);
  224.        
  225.         GUI.skin.label.alignment = TextAnchor.MiddleLeft;
  226.         GUI.BeginGroup(new Rect(0, 0, 150, 40));
  227.         GUI.Label(new Rect(0, 0, 150, 20), "Time: "+time.ToString());
  228.         GUI.Label(new Rect(0, 20, 150, 20), "Steps: "+steps.ToString());
  229.         GUI.EndGroup();
  230.     }
  231.    
  232.     private void GUIPrepare() {    
  233.         GUI.DrawTexture(new Rect(0, 0, screenwidth, screenheight), splash);
  234.         if (GUI.Button(new Rect(screenwidth/2 - 50, screenheight/2 - 12, 100, 25), "Play level")) {
  235.             starttime = Time.time;
  236.             state = Others.State.Playning;
  237.         }
  238.        
  239.         string rules;
  240.         rules = "Complete level in ";
  241.        
  242.         if (timelimit != 0) {
  243.             rules += timelimit.ToString()+" seconds";
  244.             if (steplimit != 0)
  245.                 rules += " and "+steplimit.ToString()+" steps";
  246.         }
  247.         else if (steplimit != 0) {
  248.             rules += steplimit.ToString()+" steps";
  249.         }
  250.         else
  251.             rules = "Complete level in free time";
  252.        
  253.         GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  254.         GUI.Label(new Rect(screenwidth/2 - 125, screenheight/2-40, 250, 20), rules);
  255.     }
  256.    
  257.     private void GUILoser() {
  258.         GUI.DrawTexture(new Rect(0, 0, screenwidth, screenheight), splash);
  259.        
  260.         if (GUI.Button(new Rect(screenwidth/2 - 40, screenheight/2 - 12, 80, 25), "Replay")) {
  261.             state = Others.State.Loading;
  262.             Application.LoadLevel(Application.loadedLevelName);
  263.         }
  264.        
  265.         GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  266.        
  267.         GUI.Label(new Rect(screenwidth/2 - 75, screenheight/2 - 40, 150, 20), "You lose this time");
  268.     }
  269.    
  270.     public void MoveCamera(Vector3 targetposition) {
  271.     }
  272. }
Add Comment
Please, Sign In to add comment