Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Globas : MonoBehaviour {
- private Others.State state = Others.State.Prepare;
- private Ray ray = new Ray();
- private int areacenterx;
- private GameObject mainbox;
- public int timelimit = 0;
- public int steplimit = 0;
- public Vector2 limits;
- public Vector2 areasize;
- public float starttime;
- private float gametime;
- public Vector3 mouseposition;
- public RaycastHit hit = new RaycastHit();
- public int screenwidth;
- public int screenheight;
- //Global flags
- public bool mouseleft; //State of left mouse button
- public bool hitfree = true;
- public bool winner = false;
- //Prefabs
- public GameObject floor; //Floor
- public GameObject arrow; //Arrow
- public GameObject mainboxprefab; //MainBox (Player)
- public GameObject mainboxcakeprefab;
- public GameObject here;
- //Creaters objects
- public GameObject[] arrows = new GameObject[2]; //Arrows
- private GameObject[,] floors; //Floor
- private GameObject[][] fencs;
- public Material fencsHor;
- public Material fencsVert;
- public Material fencsAngH;
- public Material fencsAngV;
- public Vector3 mousecposition;
- private Vector3 winposition;
- public int steps;
- private Texture2D splash;
- private bool cameramove;
- private Vector3 cameraposition;
- private Vector3 newcameraposition;
- void Start() {
- starttime = Time.time;
- limits = new Vector2(areasize.x*0.32f, areasize.y*0.32f);
- floors = new GameObject[(int)areasize.x, (int)areasize.y];
- fencs = new GameObject[5][];
- fencs[0] = new GameObject[(int)areasize.x];
- fencs[1] = new GameObject[(int)areasize.x];
- fencs[2] = new GameObject[(int)areasize.y];
- fencs[3] = new GameObject[(int)areasize.y];
- fencs[4] = new GameObject[4];
- screenwidth = Screen.width;
- screenheight = Screen.height;
- cameraposition = newcameraposition = Camera.main.transform.position;
- for (int i = 0; i < floors.GetLength(0); i++) {
- for (int j = 0; j < floors.GetLength(1); j++) {
- floors[i, j] = Instantiate(floor, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
- floors[i, j].transform.position = new Vector3(0.32f*j, 0, 0.32f*i);
- floors[i, j].transform.Rotate(new Vector3(270, 0, 90));
- }
- }
- for (int i = 0; i < fencs.Length - 1; i++) {
- for (int j = 0; j < fencs[i].Length; j++) {
- if (i == 0) {
- fencs[i][j] = Instantiate(floor, new Vector3(-0.32f, 0f, 0.32f * j), Quaternion.identity) as GameObject;
- fencs[i][j].transform.Rotate(new Vector3(270, 0, 180));
- fencs[i][j].renderer.material = fencsVert;
- }
- else if (i == 1) {
- fencs[i][j] = Instantiate(floor, new Vector3(0.32f * areasize.y, 0f, 0.32f * j), Quaternion.identity) as GameObject;
- fencs[i][j].transform.Rotate(new Vector3(270, 0, 0));
- fencs[i][j].renderer.material = fencsVert;
- }
- else if (i == 2) {
- fencs[i][j] = Instantiate(floor, new Vector3(0.32f * j, 0f, -0.32f), Quaternion.identity) as GameObject;
- fencs[i][j].transform.Rotate(new Vector3(270, 0, 0));
- fencs[i][j].renderer.material = fencsHor;
- }
- else if (i == 3) {
- fencs[i][j] = Instantiate(floor, new Vector3(0.32f * j, 0f, 0.32f * areasize.x), Quaternion.identity) as GameObject;
- fencs[i][j].transform.Rotate(new Vector3(270, 0, 180));
- fencs[i][j].renderer.material = fencsHor;
- }
- }
- }
- fencs[4][0] = Instantiate(floor, new Vector3(-0.32f, 0, -0.32f), Quaternion.identity) as GameObject;
- fencs[4][0].transform.Rotate(new Vector3(270, 0, 180));
- fencs[4][0].renderer.material = fencsAngV;
- fencs[4][1] = Instantiate(floor, new Vector3(-0.32f, 0, 0.32f * areasize.y), Quaternion.identity) as GameObject;
- fencs[4][1].transform.Rotate(new Vector3(270, 0, 180));
- fencs[4][1].renderer.material = fencsAngH;
- fencs[4][2] = Instantiate(floor, new Vector3(0.32f * areasize.x, 0, 0.32f * areasize.y), Quaternion.identity) as GameObject;
- fencs[4][2].transform.Rotate(new Vector3(270, 0, 0));
- fencs[4][2].renderer.material = fencsAngV;
- fencs[4][3] = Instantiate(floor, new Vector3(0.32f * areasize.x, 0, -0.32f), Quaternion.identity) as GameObject;
- fencs[4][3].transform.Rotate(new Vector3(270, 0, 0));
- fencs[4][3].renderer.material = fencsAngH;
- for (int i = 0; i < arrows.Length; i++) {
- arrows[i] = Instantiate(arrow, new Vector3(1000, 1000, 1000), Quaternion.identity) as GameObject;
- arrows[i].transform.rotation = Quaternion.Euler(270, 270, 0);
- }
- areacenterx = (int)Mathf.Round(areasize.x / 2);
- if ((areasize.x % 2) != 0)
- areacenterx++;
- mainbox = Instantiate(mainboxprefab, mainboxprefab.transform.position, Quaternion.identity) as GameObject;
- mainbox.transform.position = new Vector3(0, 0, 0.32f*(areacenterx-1));
- winposition = new Vector3(0.32f*(areasize.y-1),
- mainbox.transform.position.y,
- mainbox.transform.position.z);
- here = Instantiate(here, here.transform.position, Quaternion.identity) as GameObject;
- here.transform.eulerAngles = new Vector3(270f, 270f, 0f);
- here.transform.position = winposition + new Vector3(0f, 0.5f, 0f);
- splash = new Texture2D(screenwidth, screenheight);
- for (int i = 0; i <= splash.width; i++)
- for (int j = 0; j <= splash.height; j++)
- splash.SetPixel(i, j, new Color(0f, 0f, 0f, 0.8f));
- splash.Apply();
- }
- void Update() {
- if (Camera.main.transform.position != newcameraposition && cameramove)
- Camera.main.transform.position = Vector3.MoveTowards(Camera.main.transform.position, newcameraposition, Time.deltaTime);
- switch (state) {
- case Others.State.Loading:
- break;
- case Others.State.Playning:
- Playning();
- break;
- case Others.State.Paused:
- break;
- case Others.State.Winner:
- break;
- }
- }
- void OnGUI() {
- switch (state) {
- case Others.State.Loading:
- Others.GUILoadingScreen();
- break;
- case Others.State.Prepare:
- GUIPrepare();
- break;
- case Others.State.Playning:
- GUIPlayning();
- break;
- case Others.State.Paused:
- break;
- case Others.State.Winner:
- break;
- case Others.State.Loser:
- GUILoser();
- break;
- }
- }
- void OnLevelWasLoaded() {
- state = Others.State.Prepare;
- }
- private void Playning() {
- mouseposition = Input.mousePosition;
- mouseleft = Input.GetMouseButton(0);
- gametime = Time.time - starttime;
- ray = Camera.main.ScreenPointToRay(mouseposition);
- if (Physics.Raycast(ray, out hit) && hitfree && !winner) {
- if (hit.collider.gameObject.tag.Equals("Box")) {
- (hit.collider.gameObject.GetComponent("Box") as Box).MouseTrigger();
- }
- }
- if (!(mainbox.GetComponent("Box") as Box).move) {
- if (mainbox.transform.position == winposition) {
- //Destroy(mainbox);
- //mainbox = Instantiate(mainboxcakeprefab, mainbox.transform.position, Quaternion.identity) as GameObject;
- state = Others.State.Winner;
- }
- else if (timelimit != 0 && gametime >= timelimit)
- state = Others.State.Loser;
- else if (steplimit != 0 && steps >= steplimit)
- state = Others.State.Loser;
- }
- }
- private void GUIPlayning() {
- float time = Mathf.Round(gametime);
- GUI.skin.label.alignment = TextAnchor.MiddleLeft;
- GUI.BeginGroup(new Rect(0, 0, 150, 40));
- GUI.Label(new Rect(0, 0, 150, 20), "Time: "+time.ToString());
- GUI.Label(new Rect(0, 20, 150, 20), "Steps: "+steps.ToString());
- GUI.EndGroup();
- }
- private void GUIPrepare() {
- GUI.DrawTexture(new Rect(0, 0, screenwidth, screenheight), splash);
- if (GUI.Button(new Rect(screenwidth/2 - 50, screenheight/2 - 12, 100, 25), "Play level")) {
- starttime = Time.time;
- state = Others.State.Playning;
- }
- string rules;
- rules = "Complete level in ";
- if (timelimit != 0) {
- rules += timelimit.ToString()+" seconds";
- if (steplimit != 0)
- rules += " and "+steplimit.ToString()+" steps";
- }
- else if (steplimit != 0) {
- rules += steplimit.ToString()+" steps";
- }
- else
- rules = "Complete level in free time";
- GUI.skin.label.alignment = TextAnchor.MiddleCenter;
- GUI.Label(new Rect(screenwidth/2 - 125, screenheight/2-40, 250, 20), rules);
- }
- private void GUILoser() {
- GUI.DrawTexture(new Rect(0, 0, screenwidth, screenheight), splash);
- if (GUI.Button(new Rect(screenwidth/2 - 40, screenheight/2 - 12, 80, 25), "Replay")) {
- state = Others.State.Loading;
- Application.LoadLevel(Application.loadedLevelName);
- }
- GUI.skin.label.alignment = TextAnchor.MiddleCenter;
- GUI.Label(new Rect(screenwidth/2 - 75, screenheight/2 - 40, 150, 20), "You lose this time");
- }
- public void MoveCamera(Vector3 targetposition) {
- }
- }
Add Comment
Please, Sign In to add comment