Advertisement
Guest User

Unity Game Main Script

a guest
Aug 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 35.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using Assets.Scripts;
  5. using System;
  6.  
  7. public class PlayerControl : MonoBehaviour {
  8.  
  9.     public GameObject lineRenderer;
  10.     private bool flying = false;
  11.     private bool enableFlight = false;
  12.     private int enableTicks = 0;
  13.     private float speed = 0.1f;
  14.     public Sprite[] inventory;
  15.     public GameObject[] blocks;
  16.     public String[] names;
  17.     private HotbarElement[] items;
  18.     private GameObject[] hotbarUI = new GameObject[10];
  19.     public GameObject hotbarSlot;
  20.     private GameObject selector;
  21.     public Transform thirdPerson;
  22.     public Camera maincam;
  23.     private int rotX = 0;
  24.     private int rotY = 0;
  25.     private int rotZ = 0;
  26.     private int page = 0;
  27.     private int slot = 0;
  28.     public static List<GameObject> worldBlocks = new List<GameObject>();
  29.     public GameObject physics;
  30.     public static bool staticmode = true;
  31.     public static bool pausephysics = false;
  32.     public GameObject pauseMenu;
  33.     public GameObject options;
  34.     private GameObject ghost;
  35.     public GameObject sunlight;
  36.     private GameObject glueparent;
  37.     private Transform powerprov;
  38.     public AudioClip bump;
  39.     public static AudioClip blockBump;
  40.     public static string handheld = "none";
  41.     void Start () {
  42.         blockBump = bump;
  43.         items = new HotbarElement[inventory.Length];
  44.         setCamState(thirdPerson.gameObject, false);
  45.         Cursor.lockState = CursorLockMode.Locked;
  46.         Cursor.visible = false;
  47.         for (int i = 0;i<10;i++)
  48.         {
  49.             Vector3 v = new Vector3((-420+(50*i)),-325,0);
  50.             hotbarUI[i] = (GameObject)Instantiate(hotbarSlot, v, hotbarSlot.transform.rotation);
  51.             hotbarUI[i].transform.localScale = new Vector3(0.5f, 0.5f, 1);
  52.             hotbarUI[i].transform.SetParent(GameObject.FindObjectOfType<Canvas>().transform,false);
  53.         }
  54.         for (int i = 0;i<inventory.Length;i++)
  55.         {
  56.             items[i] = new HotbarElement(blocks[i],inventory[i],names[i]);
  57.         }
  58.         selector = GameObject.FindGameObjectWithTag("hbSelectionItem");
  59.         selector.transform.SetSiblingIndex(GameObject.FindObjectOfType<Canvas>().transform.childCount-1);
  60.         load();
  61.     }
  62.  
  63.     void Update () {
  64.         //options
  65.         if (options.activeSelf)
  66.         {
  67.             options.transform.GetChild(1).GetChild(4).GetComponent<Text>().text = options.transform.GetChild(1).GetComponent<Slider>().value.ToString();
  68.             int width = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[0]);
  69.             int height = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[1]);
  70.             int refresh = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[2]);
  71.             Screen.SetResolution(width,height,options.transform.GetChild(2).GetChild(3).GetComponent<Toggle>().isOn,refresh);
  72.             foreach (Camera c in Camera.allCameras)
  73.             {
  74.                 c.fieldOfView = options.transform.GetChild(1).GetComponent<Slider>().value;
  75.             }
  76.         }
  77.         //handheld
  78.         handheld = items.Length > 10 * page + slot ? items[10 * page + slot].name : "none";
  79.         //body
  80.         GameObject.FindGameObjectWithTag("Player").transform.rotation = Quaternion.Euler(0, GameObject.FindGameObjectWithTag("Player").transform.rotation.eulerAngles.y, 0);
  81.         //block rotation
  82.         if (Input.GetKeyDown(KeyCode.Home))
  83.         {
  84.             rotX++;
  85.         }
  86.         if (Input.GetKeyDown(KeyCode.End))
  87.         {
  88.             rotX--;
  89.         }
  90.         if (Input.GetKeyDown(KeyCode.Insert))
  91.         {
  92.             rotZ++;
  93.         }
  94.         if (Input.GetKeyDown(KeyCode.PageUp))
  95.         {
  96.             rotZ--;
  97.         }
  98.         if (Input.GetKeyDown(KeyCode.Delete))
  99.         {
  100.             rotY++;
  101.         }
  102.         if (Input.GetKeyDown(KeyCode.PageDown))
  103.         {
  104.             rotY--;
  105.         }
  106.         if (rotX == 4)
  107.         {
  108.             rotX = 0;
  109.         }
  110.         if (rotX == -1)
  111.         {
  112.             rotX = 3;
  113.         }
  114.  
  115.         if (rotY == 4)
  116.         {
  117.             rotY = 1;
  118.         }
  119.         if (rotY == -1)
  120.         {
  121.             rotY = 3;
  122.         }
  123.  
  124.         if (rotZ == 4)
  125.         {
  126.             rotZ = 1;
  127.         }
  128.         if (rotZ == -1)
  129.         {
  130.             rotZ = 3;
  131.         }
  132.         if (!pauseMenu.activeSelf && !options.activeSelf)
  133.         {
  134.             //flight
  135.             if (flying)
  136.             {
  137.                 gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
  138.                 gameObject.GetComponent<Rigidbody>().useGravity = false;
  139.             }
  140.             else
  141.             {
  142.                 gameObject.GetComponent<Rigidbody>().useGravity = true;
  143.             }
  144.             if (enableFlight)
  145.             {
  146.                 enableTicks++;
  147.                 if (enableTicks * Time.smoothDeltaTime >= 0.5)
  148.                 {
  149.                     enableTicks = 0;
  150.                     enableFlight = false;
  151.                 }
  152.             }
  153.             if (Input.GetKeyDown(KeyCode.Space))
  154.             {
  155.                 if (!flying && isOnGround(10))
  156.                 {
  157.                     gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 10, 0);
  158.                 }
  159.                 if (enableFlight)
  160.                 {
  161.                     if (flying)
  162.                     {
  163.                         enableFlight = false;
  164.                         flying = false;
  165.                         enableTicks = 0;
  166.                     }
  167.                     else
  168.                     {
  169.                         enableFlight = false;
  170.                         flying = true;
  171.                         enableTicks = 0;
  172.                     }
  173.                 }
  174.                 else
  175.                 {
  176.                     enableFlight = true;
  177.                 }
  178.             }
  179.             //Movement
  180.             if (Input.GetKey(KeyCode.Space) && flying)
  181.             {
  182.                 transform.Translate(new Vector3(0, 1, 0) * speed, Space.World);
  183.             }
  184.             if (Input.GetKey(KeyCode.W))
  185.             {
  186.                 Vector3 v = transform.forward;
  187.                 v.y = 0;
  188.                 v.Normalize();
  189.                 v *= speed;
  190.                 transform.Translate(v, Space.World);
  191.             }
  192.             if (Input.GetKey(KeyCode.A))
  193.             {
  194.                 Vector3 v = -transform.right;
  195.                 v.y = 0;
  196.                 v.Normalize();
  197.                 v *= speed;
  198.                 transform.Translate(v, Space.World);
  199.             }
  200.             if (Input.GetKey(KeyCode.S))
  201.             {
  202.                 Vector3 v = -transform.forward;
  203.                 v.y = 0;
  204.                 v.Normalize();
  205.                 v *= speed;
  206.                 transform.Translate(v, Space.World);
  207.             }
  208.             if (Input.GetKey(KeyCode.D))
  209.             {
  210.                 Vector3 v = transform.right;
  211.                 v.y = 0;
  212.                 v.Normalize();
  213.                 v *= speed;
  214.                 transform.Translate(v, Space.World);
  215.             }
  216.             if (Input.GetKey(KeyCode.LeftShift) && flying)
  217.             {
  218.                 if (transform.position.y <= 2)
  219.                 {
  220.                     flying = false;
  221.                 }
  222.                 else
  223.                 {
  224.                     transform.Translate(new Vector3(0, -1, 0) * speed, Space.World);
  225.                 }
  226.             }
  227.             if (transform.position.y < Terrain.activeTerrain.SampleHeight(transform.position))
  228.             {
  229.                 Vector3 pos = transform.position;
  230.                 pos.y = Terrain.activeTerrain.SampleHeight(pos);
  231.                 transform.position = pos;
  232.             }
  233.             //Speed
  234.             if (Input.GetKey(KeyCode.LeftControl))
  235.             {
  236.                 speed = 10 * Time.deltaTime;
  237.             }
  238.             else
  239.             {
  240.                 speed = 5 * Time.deltaTime;
  241.             }
  242.             //Camera
  243.             float vertical = (-Input.GetAxis("Mouse Y") * Time.deltaTime * 100);
  244.             float horizontal = Input.GetAxis("Mouse X") * Time.deltaTime * 100;
  245.             transform.Rotate(vertical, horizontal, 0);
  246.             float x = transform.rotation.eulerAngles.x;
  247.             if (x != 0)
  248.             {
  249.                 if (x < 271 && x > 180)
  250.                 {
  251.                     x = 271;
  252.                 }
  253.                 else
  254.                 {
  255.                     if (x > 89 && x < 180)
  256.                     {
  257.                         x = 89;
  258.                     }
  259.                 }
  260.             }
  261.             transform.rotation = Quaternion.Euler(x, transform.rotation.eulerAngles.y, 0);
  262.         }
  263.         //Exit
  264.         if (Input.GetKeyDown(KeyCode.Escape))
  265.         {
  266.             if (!pauseMenu.activeSelf && !options.activeSelf)
  267.             {
  268.                 pause();
  269.             } else if (!options.activeSelf)
  270.             {
  271.                 unpause();
  272.             }
  273.         }
  274.         //block selection
  275.         float scroll = Input.GetAxis("Mouse ScrollWheel");
  276.         int scr = scroll<0?-1:scroll>0?1:0;
  277.         if (scr != 0 && !pauseMenu.activeSelf && !options.activeSelf)
  278.         {
  279.             slot += scr;
  280.             page += slot >= 0 ? Mathf.FloorToInt(slot / 10) : -1;
  281.             slot = slot >= 0 ? slot % 10 : slot + 10;
  282.             slot = Mathf.Clamp(slot, 0, 9);
  283.             page = Mathf.Clamp(page, 0, Mathf.CeilToInt(inventory.Length / 9));
  284.         }
  285.         for (int i = 0; i<hotbarUI.Length;i++)
  286.         {
  287.             if (items.Length <= 10 * page + i)
  288.             {
  289.                 hotbarUI[i].GetComponent<Image>().overrideSprite = hotbarUI[i].GetComponent<Image>().sprite;
  290.             } else
  291.             {
  292.                 hotbarUI[i].GetComponent<Image>().overrideSprite = items[10 * page + i].image;
  293.             }
  294.         }
  295.         selector.transform.position = hotbarUI[slot].transform.position;
  296.         //third person
  297.         if (Input.GetKeyDown(KeyCode.F5) && !pauseMenu.activeSelf && !options.activeSelf)
  298.         {
  299.             if (thirdPerson.gameObject.GetComponent<Camera>().enabled)
  300.             {
  301.                 setCamState(maincam.transform.gameObject, true);
  302.                 setCamState(thirdPerson.gameObject, false);
  303.             } else
  304.             {
  305.                 setCamState(thirdPerson.gameObject, true);
  306.                 setCamState(maincam.transform.gameObject, false);
  307.             }
  308.         }
  309.         //physics
  310.         if (Input.GetKeyDown(KeyCode.O) && !pauseMenu.activeSelf && !options.activeSelf)
  311.         {
  312.             staticmode = !staticmode;
  313.         }
  314.         if (Input.GetKeyDown(KeyCode.P) && !pauseMenu.activeSelf && !options.activeSelf)
  315.         {
  316.             pausephysics = !pausephysics;
  317.         }
  318.         physics.transform.GetChild(0).GetComponent<Text>().text = "Static Building: " + (staticmode ? "on" : "off");
  319.         physics.transform.GetChild(1).GetComponent<Text>().text = "Physics: " + (pausephysics ? "paused" : "running");
  320.         physics.transform.GetChild(2).GetComponent<Text>().text = "Selected: " + (items.Length > 10 * page + slot ? items[10 * page + slot].name : "none");
  321.         //block ghosting
  322.         if (ghost != null)
  323.         {
  324.             Destroy(ghost);
  325.             ghost = null;
  326.         }
  327.         if (items.Length > 10 * page + slot && items[10 * page + slot].prefab != null)
  328.         {
  329.             Ray ray = new Ray(transform.position, transform.forward);
  330.             RaycastHit hit;
  331.             if (Physics.Raycast(ray, out hit, 100))
  332.             {
  333.                 Vector3 v = new Vector3(0, 0, 0);
  334.                 if (hit.transform.gameObject.tag == "block")
  335.                 {
  336.                     v = hit.transform.position + hit.normal;
  337.                     ghost = build(items[10 * page + slot].prefab,Quaternion.Euler(rotX*90,rotY*90,rotZ*90));
  338.                     if (ghost == null)
  339.                     {
  340.                         float xpos = Mathf.RoundToInt(hit.point.x);
  341.                         float ypos = Mathf.CeilToInt(hit.point.y);
  342.                         float zpos = Mathf.RoundToInt(hit.point.z);
  343.                         v = new Vector3(xpos, ypos, zpos);
  344.                         ghost = (GameObject)Instantiate(items[10 * page + slot].prefab, v, Quaternion.Euler(rotX * 90, rotY * 90, rotZ * 90));
  345.                     }
  346.                 }
  347.                 else
  348.                 {
  349.                     float xpos = Mathf.RoundToInt(hit.point.x);
  350.                     float ypos = Mathf.CeilToInt(hit.point.y);
  351.                     float zpos = Mathf.RoundToInt(hit.point.z);
  352.                     if (ypos - 0.5 < Terrain.activeTerrain.SampleHeight(new Vector3(xpos,ypos,zpos)))
  353.                     {
  354.                         ypos = Mathf.Ceil(Terrain.activeTerrain.SampleHeight(new Vector3(xpos, ypos, zpos))+0.5f);
  355.                     }
  356.                     v = new Vector3(xpos, ypos, zpos);
  357.                     ghost = (GameObject)Instantiate(items[10 * page + slot].prefab, v, Quaternion.Euler(rotX * 90, rotY * 90, rotZ * 90));
  358.                 }
  359.                 ghost.GetComponent<Block>().stat = true;
  360.                 ghost.GetComponent<MeshRenderer>().material.SetFloat("_Mode", 2);
  361.                 Color c = ghost.GetComponent<MeshRenderer>().material.color;
  362.                 ghost.GetComponent<MeshRenderer>().material.color = new Color(c.r, c.g, c.b, c.a * 0.75f);
  363.                 Collider[] col = ghost.GetComponents<Collider>();
  364.                 foreach(Collider coll in col)
  365.                 {
  366.                     coll.enabled = false;
  367.                 }
  368.                 ghost.tag = "ghost";
  369.                 ghost.GetComponent<Block>().enabled = false;
  370.                 if (ghost.transform.childCount > 0)
  371.                 {
  372.                     for (int i = 0;i < ghost.transform.childCount;i++)
  373.                     {
  374.                         Destroy(ghost.transform.GetChild(i).gameObject);
  375.                     }
  376.                 }
  377.             }
  378.         }
  379.         //glue
  380.         if (items.Length > 10 * page + slot)
  381.         {
  382.             if (items[10 * page + slot].name == "Glue")
  383.             {
  384.                 if (Input.GetMouseButtonDown(2))
  385.                 {
  386.                     glueparent = null;
  387.                     Target t = getTarget(transform);
  388.                     if (t.trans != null)
  389.                     {
  390.                         if (t.trans.gameObject.tag == "block")
  391.                         {
  392.                             if (!t.trans.gameObject.GetComponent<Block>().stat)
  393.                             {
  394.                                 glueparent = t.trans.gameObject;
  395.                             }
  396.                         }
  397.                     }
  398.                 }
  399.                 if (Input.GetMouseButtonDown(1))
  400.                 {
  401.                     if (glueparent != null)
  402.                     {
  403.                         if (glueparent.GetComponent<Rigidbody>() != null)
  404.                         {
  405.                             Transform t = getTarget(transform).trans;
  406.                             if (t != null)
  407.                             {
  408.                                 if (t.gameObject != glueparent && t.gameObject.tag == "block")
  409.                                 {
  410.                                     if (!t.gameObject.GetComponent<Block>().stat)
  411.                                     {
  412.                                         if (glueparent.GetComponent<FixedJoint>() != null)
  413.                                         {
  414.                                             if (glueparent.GetComponent<FixedJoint>().connectedBody != t.GetComponent<Rigidbody>())
  415.                                             {
  416.                                                 if (t.GetComponent<FixedJoint>() == null)
  417.                                                 {
  418.                                                     t.gameObject.AddComponent<FixedJoint>();
  419.                                                 }
  420.                                                 t.gameObject.GetComponent<FixedJoint>().connectedBody = glueparent.GetComponent<Rigidbody>();
  421.                                             }
  422.                                         } else
  423.                                         {
  424.                                             if (t.GetComponent<FixedJoint>() == null)
  425.                                             {
  426.                                                 t.gameObject.AddComponent<FixedJoint>();
  427.                                             }
  428.                                             t.gameObject.GetComponent<FixedJoint>().connectedBody = glueparent.GetComponent<Rigidbody>();
  429.                                         }
  430.                                     }
  431.                                 }
  432.                             }
  433.                         }
  434.                     }
  435.                 }
  436.                 if (Input.GetMouseButtonDown(0))
  437.                 {
  438.                     Transform t = getTarget(transform).trans;
  439.                     if (t != null)
  440.                     {
  441.                         if (t.gameObject.tag == "block")
  442.                         {
  443.                             if (!t.gameObject.GetComponent<Block>().stat)
  444.                             {
  445.                                 if (t.GetComponent<FixedJoint>() != null)
  446.                                 {
  447.                                     Destroy(t.GetComponent<FixedJoint>());
  448.                                 }
  449.                             }
  450.                         }
  451.                     }
  452.                 }
  453.             }
  454.         }
  455.         //powerPlug
  456.         if (items.Length > 10 * page + slot)
  457.         {
  458.             if (items[10 * page + slot].name == "PowerPlug")
  459.             {
  460.                 if (Input.GetMouseButtonDown(2))
  461.                 {
  462.                     powerprov = null;
  463.                     Target t = getTarget(transform);
  464.                     if (t.trans != null)
  465.                     {
  466.                         if (t.trans.gameObject.tag == "block")
  467.                         {
  468.                             powerprov = t.trans;
  469.                         }
  470.                     }
  471.                 }
  472.                 if (Input.GetMouseButtonDown(1))
  473.                 {
  474.                     if (powerprov != null)
  475.                     {
  476.                         Transform t = getTarget(transform).trans;
  477.                         if (t != null)
  478.                         {
  479.                             if (t.gameObject != powerprov && t.gameObject.tag == "block")
  480.                             {
  481.                                 if ((getHighestParent(t.gameObject) == getHighestParent(powerprov.gameObject))||(t.GetComponent<Block>().stat&&powerprov.GetComponent<Block>().stat))
  482.                                 {
  483.                                     t.gameObject.GetComponent<Block>().powerProvider = powerprov;
  484.                                 }
  485.                             }
  486.                         }
  487.                     }
  488.                 }
  489.                 if (Input.GetMouseButtonDown(0))
  490.                 {
  491.                     Transform t = getTarget(transform).trans;
  492.                     if (t != null)
  493.                     {
  494.                         if (t.gameObject.tag == "block")
  495.                         {
  496.                             t.gameObject.GetComponent<Block>().powerProvider = null;
  497.                         }
  498.                     }
  499.                 }
  500.             }
  501.         }
  502.         //block placement
  503.         if (Input.GetMouseButtonDown(1) && !pauseMenu.activeSelf && !options.activeSelf)
  504.         {
  505.             if (ghost != null)
  506.             {
  507.                 worldBlocks.Add((GameObject)Instantiate(items[10 * page + slot].prefab, ghost.transform.position, ghost.transform.rotation));
  508.             }
  509.         }
  510.         //block destruction
  511.         bool gluing = false;
  512.         if (items.Length > 10 * page + slot )
  513.         {
  514.             if (items[10 * page + slot].name == "Glue" || items[10 * page + slot].name == "PowerPlug")
  515.             {
  516.                 gluing = true;
  517.             }
  518.         }
  519.         if (Input.GetMouseButtonDown(0) && !pauseMenu.activeSelf && !options.activeSelf && !gluing)
  520.         {
  521.  
  522.             Transform t = getTarget(transform).trans;
  523.             if (t != null)
  524.             {
  525.                 if (t.gameObject.tag == "block")
  526.                 {
  527.                     if (t.parent != null)
  528.                     {
  529.                         t.SetParent(null);
  530.                     }
  531.                     worldBlocks.Remove(t.gameObject);
  532.                     Destroy(t.gameObject);
  533.                 }
  534.             }
  535.         }
  536.     }
  537.  
  538.     private bool isOnGround(float maxdist)
  539.     {
  540.         return Physics.Raycast(transform.position,Vector3.down, maxdist);
  541.     }
  542.  
  543.     public static void setCamState(GameObject cam,bool enabled)
  544.     {
  545.         cam.GetComponent<Camera>().enabled = enabled;
  546.         cam.GetComponent<AudioListener>().enabled = enabled;
  547.         cam.GetComponent<FlareLayer>().enabled = enabled;
  548.         cam.GetComponent<GUILayer>().enabled = enabled;
  549.     }
  550.     public void pause()
  551.     {
  552.         Cursor.lockState = CursorLockMode.None;
  553.         Cursor.visible = true;
  554.         Time.timeScale = 0;
  555.         pauseMenu.SetActive(true);
  556.         save(worldBlocks.ToArray(),transform.position);
  557.     }
  558.     public void unpause()
  559.     {
  560.         Cursor.lockState = CursorLockMode.Locked;
  561.         Cursor.visible = false;
  562.         Time.timeScale = 1;
  563.         pauseMenu.SetActive(false);
  564.     }
  565.     public void exit()
  566.     {
  567.         Application.Quit();
  568.     }
  569.     public void loadOptions()
  570.     {
  571.         options.SetActive(true);
  572.         options.transform.GetChild(0).GetComponent<InputField>().text = PlayerPrefs.GetString("PlayerName");
  573.         options.transform.GetChild(1).GetComponent<Slider>().value = PlayerPrefs.GetFloat("FOV");
  574.         Resolution[] resolutions = Screen.resolutions;
  575.         List<Dropdown.OptionData> dropdownOptions = new List<Dropdown.OptionData>();
  576.         int currentres = 0;
  577.         for (int i = 0;i<resolutions.Length;i++)
  578.         {
  579.             Resolution res = resolutions[i];
  580.             dropdownOptions.Add(new Dropdown.OptionData(res.width + "x" + res.height + "x" + res.refreshRate));
  581.             if (res.Equals(Screen.currentResolution))
  582.             {
  583.                 currentres = i;
  584.             }
  585.         }
  586.         options.transform.GetChild(2).GetComponent<Dropdown>().options = dropdownOptions;
  587.         options.transform.GetChild(2).GetComponent<Dropdown>().value = currentres;
  588.         pauseMenu.SetActive(false);
  589.     }
  590.     public void saveOptions()
  591.     {
  592.         PlayerPrefs.SetString("PlayerName", options.transform.GetChild(0).GetComponent<InputField>().text);
  593.         PlayerPrefs.SetFloat("FOV", options.transform.GetChild(1).GetComponent<Slider>().value);
  594.         PlayerPrefs.SetString("resolution", options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text);
  595.         PlayerPrefs.SetString("fullscreen", options.transform.GetChild(2).GetChild(3).GetComponent<Toggle>().isOn.ToString());
  596.         PlayerPrefs.Save();
  597.         options.SetActive(false);
  598.         pauseMenu.SetActive(true);
  599.     }
  600.     public void cancelOptions()
  601.     {
  602.         bool firstload = false;
  603.         if (PlayerPrefs.GetFloat("FOV") < 30 || PlayerPrefs.GetFloat("FOV") > 90)
  604.         {
  605.             firstload = true;
  606.         }
  607.         if (PlayerPrefs.GetString("fullscreen") != "true" && PlayerPrefs.GetString("fullscreen") != "false")
  608.         {
  609.             firstload = true;
  610.         }
  611.         if (PlayerPrefs.GetString("resolution").Split("x".ToCharArray()).Length != 3)
  612.         {
  613.             firstload = true;
  614.         }
  615.         if (!firstload)
  616.         {
  617.             foreach (Camera c in Camera.allCameras)
  618.             {
  619.                 c.fieldOfView = PlayerPrefs.GetFloat("FOV");
  620.             }
  621.             int resW = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[0]);
  622.             int resH = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[1]);
  623.             int resR = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[2]);
  624.             bool resF = Convert.ToBoolean(PlayerPrefs.GetString("fullscreen"));
  625.             Screen.SetResolution(resW, resH, resF, resR);
  626.         }
  627.         else
  628.         {
  629.             PlayerPrefs.SetFloat("FOV", 60f);
  630.             PlayerPrefs.SetString("resolution", Screen.currentResolution.width + "x" + Screen.currentResolution.height + "x" + Screen.currentResolution.refreshRate);
  631.             PlayerPrefs.SetString("fullscreen", Screen.fullScreen.ToString());
  632.             PlayerPrefs.Save();
  633.         }
  634.         options.SetActive(false);
  635.         pauseMenu.SetActive(true);
  636.     }
  637.     public GameObject fromString(string s,bool lastpausephysics)
  638.     {
  639.         string[] data = s.Split("/".ToCharArray());
  640.         if (data.Length != 19)
  641.         {
  642.             return null;
  643.         }
  644.         foreach (string str in data)
  645.         {
  646.             if (str == "")
  647.             {
  648.                 return null;
  649.             }
  650.         }
  651.         string name = data[0];
  652.             double x = Convert.ToDouble(data[1]);
  653.             double y = Convert.ToDouble(data[2]);
  654.             double z = Convert.ToDouble(data[3]);
  655.         Vector3 pos = new Vector3((float)x, (float)y, (float)z);
  656.             double rx = Convert.ToDouble(data[4]);
  657.             double ry = Convert.ToDouble(data[5]);
  658.             double rz = Convert.ToDouble(data[6]);
  659.             double rw = Convert.ToDouble(data[7]);
  660.         Quaternion rot = new Quaternion((float)rx, (float)ry, (float)rz, (float)rw);
  661.             double lvx = Convert.ToDouble(data[8]);
  662.             double lvy = Convert.ToDouble(data[9]);
  663.             double lvz = Convert.ToDouble(data[10]);
  664.         Vector3 lv = new Vector3((float)lvx, (float)lvy, (float)lvz);
  665.             double gpx = Convert.ToDouble(data[12]);
  666.             double gpy = Convert.ToDouble(data[13]);
  667.             double gpz = Convert.ToDouble(data[14]);
  668.         Vector3 gp = new Vector3((float)gpx, (float)gpy, (float)gpz);
  669.             double ppx = Convert.ToDouble(data[15]);
  670.             double ppy = Convert.ToDouble(data[16]);
  671.             double ppz = Convert.ToDouble(data[17]);
  672.         Vector3 pp = new Vector3((float)ppx, (float)ppy, (float)ppz);
  673.         float eu = (float)Convert.ToDouble(data[18]);
  674.         bool stat = Convert.ToBoolean(data[11]);
  675.         GameObject prefab = null;
  676.         foreach (GameObject go in blocks)
  677.         {
  678.             if (go != null)
  679.             {
  680.                 if (name.StartsWith(go.name))
  681.                 {
  682.                     prefab = go;
  683.                 }
  684.             }
  685.         }
  686.         if (prefab == null)
  687.         {
  688.             return null;
  689.         }
  690.         GameObject obj = (GameObject)Instantiate(prefab, pos, rot);
  691.         obj.GetComponent<Block>().stat = stat;
  692.         obj.GetComponent<Block>().oldvec = lv;
  693.         if (!lastpausephysics)
  694.         {
  695.             obj.GetComponent<Block>().loadVelocity = lv;
  696.         }
  697.         obj.GetComponent<Block>().laststat = lastpausephysics;
  698.         obj.GetComponent<Block>().scriptSpawn = true;
  699.         obj.GetComponent<Block>().searchGlueParent = gp;
  700.         obj.GetComponent<Block>().searchPowerProvider = pp;
  701.         obj.GetComponent<Block>().eu = eu;
  702.         return obj;
  703.     }
  704.     public string toString(GameObject go)
  705.     {
  706.         String str = "";
  707.         string name = go.name;
  708.         double x = go.transform.position.x;
  709.         double y = go.transform.position.y;
  710.         double z = go.transform.position.z;
  711.         double rx = go.transform.rotation.x;
  712.         double ry = go.transform.rotation.y;
  713.         double rz = go.transform.rotation.z;
  714.         double rw = go.transform.rotation.w;
  715.         double lvx = go.GetComponent<Block>().oldvec.x;
  716.         double lvy = go.GetComponent<Block>().oldvec.y;
  717.         double lvz = go.GetComponent<Block>().oldvec.z;
  718.         if (!pausephysics && go.GetComponent<Rigidbody>() != null)
  719.         {
  720.             lvx = go.GetComponent<Rigidbody>().velocity.x;
  721.             lvy = go.GetComponent<Rigidbody>().velocity.y;
  722.             lvz = go.GetComponent<Rigidbody>().velocity.z;
  723.         }
  724.         double gpx = 0;
  725.         double gpy = 0;
  726.         double gpz = 0;
  727.         if (go.GetComponent<FixedJoint>() != null)
  728.         {
  729.             gpx = go.GetComponent<FixedJoint>().connectedBody.transform.position.x;
  730.             gpy = go.GetComponent<FixedJoint>().connectedBody.transform.position.y;
  731.             gpz = go.GetComponent<FixedJoint>().connectedBody.transform.position.z;
  732.         }
  733.         double ppx = 0;
  734.         double ppy = 0;
  735.         double ppz = 0;
  736.         if (go.GetComponent<Block>().powerProvider != null)
  737.         {
  738.             ppx = go.GetComponent<Block>().powerProvider.position.x;
  739.             ppy = go.GetComponent<Block>().powerProvider.position.y;
  740.             ppz = go.GetComponent<Block>().powerProvider.position.z;
  741.         }
  742.         bool stat = go.GetComponent<Block>().stat;
  743.         float eu = go.GetComponent<Block>().eu;
  744.         str = name + "/" + x + "/" + y + "/" + z + "/" + rx + "/" + ry + "/" + rz + "/" + rw + "/" + lvx + "/" + lvy + "/" + lvz + "/" + stat + "/" + gpx + "/" + gpy + "/" + gpz + "/" + ppx + "/" + ppy + "/" + ppz + "/" + eu;
  745.         return str;
  746.     }
  747.     private int indexOf<T>(T obj, T[] arr)
  748.     {
  749.         for (int i = 0;i < arr.Length;i++)
  750.         {
  751.             if (arr[i].Equals(obj))
  752.             {
  753.                 return i;
  754.             }
  755.         }
  756.         return -1;
  757.     }
  758.     public void save(GameObject[] objects,Vector3 lastpos)
  759.     {
  760.         clearWorld();
  761.         PlayerPrefs.SetInt("worldBlocks", objects.Length);
  762.         for (int i = 0;i< objects.Length;i++)
  763.         {
  764.             PlayerPrefs.SetString("block"+i, toString(objects[i]));
  765.         }
  766.         PlayerPrefs.SetFloat("lastx",lastpos.x);
  767.         PlayerPrefs.SetFloat("lasty", lastpos.y);
  768.         PlayerPrefs.SetFloat("lastz", lastpos.z);
  769.         PlayerPrefs.SetString("stat", "" + staticmode);
  770.         PlayerPrefs.SetString("pp", "" + pausephysics);
  771.         PlayerPrefs.SetFloat("sunrot", sunlight.GetComponent<Sun>().rot);
  772.         PlayerPrefs.Save();
  773.     }
  774.  
  775.     public static void clearWorld()
  776.     {
  777.         PlayerPrefs.DeleteAll();
  778.         PlayerPrefs.SetFloat("lastx", 746);
  779.         PlayerPrefs.SetFloat("lasty", 480);
  780.         PlayerPrefs.SetFloat("lastz", -124);
  781.         PlayerPrefs.Save();
  782.     }
  783.  
  784.     public void load()
  785.     {
  786.        
  787.         bool firstload = false;
  788.         if (PlayerPrefs.GetFloat("FOV") < 30 || PlayerPrefs.GetFloat("FOV") > 90)
  789.         {
  790.             firstload = true;
  791.         }
  792.         if (PlayerPrefs.GetString("fullscreen") != "true" && PlayerPrefs.GetString("fullscreen") != "false")
  793.         {
  794.             firstload = true;
  795.         }
  796.         if (PlayerPrefs.GetString("resolution").Split("x".ToCharArray()).Length != 3)
  797.         {
  798.             firstload = true;
  799.         }
  800.         if (!firstload)
  801.         {
  802.             foreach (Camera c in Camera.allCameras)
  803.             {
  804.                 c.fieldOfView = PlayerPrefs.GetFloat("FOV");
  805.             }
  806.             int resW = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[0]);
  807.             int resH = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[1]);
  808.             int resR = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[2]);
  809.             bool resF = Convert.ToBoolean(PlayerPrefs.GetString("fullscreen"));
  810.             Screen.SetResolution(resW, resH, resF, resR);
  811.         } else
  812.         {
  813.             PlayerPrefs.SetFloat("FOV",60f);
  814.             PlayerPrefs.SetString("resolution",Screen.currentResolution.width + "x" + Screen.currentResolution.height + "x" + Screen.currentResolution.refreshRate);
  815.             PlayerPrefs.SetString("fullscreen",Screen.fullScreen.ToString());
  816.             PlayerPrefs.Save();
  817.         }
  818.         try
  819.         {
  820.             int blockamount = PlayerPrefs.GetInt("worldBlocks");
  821.             for (int i = 0; i < blockamount; i++)
  822.             {
  823.                 string data = PlayerPrefs.GetString("block" + i);
  824.                 GameObject go = fromString(data, Convert.ToBoolean(PlayerPrefs.GetString("pp")));
  825.                 if (go != null)
  826.                 {
  827.                     worldBlocks.Add(go);
  828.                 }
  829.             }
  830.             transform.position = new Vector3(PlayerPrefs.GetFloat("lastx"), PlayerPrefs.GetFloat("lasty"), PlayerPrefs.GetFloat("lastz"));
  831.             staticmode = Convert.ToBoolean(PlayerPrefs.GetString("stat"));
  832.             pausephysics = Convert.ToBoolean(PlayerPrefs.GetString("pp"));
  833.             sunlight.GetComponent<Sun>().rot = PlayerPrefs.GetFloat("sunrot");
  834.         } catch(Exception e)
  835.         {
  836.             Debug.LogWarning("World loading failed. " + e.Message);
  837.             return;
  838.         }
  839.     }
  840.     public GameObject build(GameObject prefab, Quaternion rot)
  841.     {
  842.         Target t = getTarget(transform);
  843.         if ((t.center == Vector3.zero && t.normal == Vector3.zero))
  844.         {
  845.             return null;
  846.         }
  847.         return (GameObject)Instantiate(prefab, t.center + t.normal, rot);
  848.     }
  849.     public Target getTarget(Transform t, Vector3? point = null, Vector3? normal = null)
  850.     {
  851.         if (point == null || normal == null)
  852.         {
  853.             Ray r = new Ray(t.position, t.forward);
  854.             RaycastHit hit = new RaycastHit();
  855.             if (Physics.Raycast(r, out hit, 100))
  856.             {
  857.                 if (hit.transform.gameObject.tag == "block" || hit.transform.gameObject.tag == "blockChild")
  858.                 {
  859.                     Vector3 target = hit.transform.position;
  860.                     if (hit.transform.parent != null)
  861.                     {
  862.                         target = getTarget(hit.transform, hit.point, hit.normal).center;
  863.                     }
  864.                     return new Target(target, hit.normal,hit.transform);
  865.                 } else
  866.                 {
  867.                     float x = Mathf.RoundToInt(hit.point.x);
  868.                     float y = Mathf.CeilToInt(hit.point.y);
  869.                     float z = Mathf.RoundToInt(hit.point.z);
  870.                     return new Target(new Vector3(x,y,z),Vector3.zero);
  871.                 }
  872.             }
  873.         }
  874.         else
  875.         {
  876.             Transform nearest = t;
  877.             for (int i = 0; i < t.childCount; i++)
  878.             {
  879.                 float nm = ((Vector3)(point - nearest.position)).magnitude;
  880.                 float cm = ((Vector3)(point - t.GetChild(i).position)).magnitude;
  881.                 if (nm > cm)
  882.                 {
  883.                     nearest = t.GetChild(i);
  884.                 }
  885.             }
  886.             return new Target(nearest.position,(Vector3)normal,nearest);
  887.         }
  888.         return new Target(new Vector3(0,0,0),new Vector3(0,0,0));
  889.     }
  890.  
  891.     public static GameObject getHighestParent(GameObject go)
  892.     {
  893.         if (go.GetComponent<FixedJoint>() == null)
  894.         {
  895.             return go;
  896.         }
  897.         if (go.GetComponent<FixedJoint>().connectedBody == null)
  898.         {
  899.             return go;
  900.         }
  901.         return getHighestParent(go.GetComponent<FixedJoint>().connectedBody.gameObject);
  902.     }
  903. }
  904.  
  905. public class Target
  906. {
  907.     public Vector3 center;
  908.     public Vector3 normal;
  909.     public Transform trans;
  910.     public Target(Vector3 center,Vector3 normal)
  911.     {
  912.         this.center = center;
  913.         this.normal = normal;
  914.     }
  915.     public Target(Vector3 center, Vector3 normal,Transform trans)
  916.     {
  917.         this.center = center;
  918.         this.normal = normal;
  919.         this.trans = trans;
  920.     }
  921. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement