Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using Assets.Scripts;
- using System;
- public class PlayerControl : MonoBehaviour {
- public GameObject lineRenderer;
- private bool flying = false;
- private bool enableFlight = false;
- private int enableTicks = 0;
- private float speed = 0.1f;
- public Sprite[] inventory;
- public GameObject[] blocks;
- public String[] names;
- private HotbarElement[] items;
- private GameObject[] hotbarUI = new GameObject[10];
- public GameObject hotbarSlot;
- private GameObject selector;
- public Transform thirdPerson;
- public Camera maincam;
- private int rotX = 0;
- private int rotY = 0;
- private int rotZ = 0;
- private int page = 0;
- private int slot = 0;
- public static List<GameObject> worldBlocks = new List<GameObject>();
- public GameObject physics;
- public static bool staticmode = true;
- public static bool pausephysics = false;
- public GameObject pauseMenu;
- public GameObject options;
- private GameObject ghost;
- public GameObject sunlight;
- private GameObject glueparent;
- private Transform powerprov;
- public AudioClip bump;
- public static AudioClip blockBump;
- public static string handheld = "none";
- void Start () {
- blockBump = bump;
- items = new HotbarElement[inventory.Length];
- setCamState(thirdPerson.gameObject, false);
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- for (int i = 0;i<10;i++)
- {
- Vector3 v = new Vector3((-420+(50*i)),-325,0);
- hotbarUI[i] = (GameObject)Instantiate(hotbarSlot, v, hotbarSlot.transform.rotation);
- hotbarUI[i].transform.localScale = new Vector3(0.5f, 0.5f, 1);
- hotbarUI[i].transform.SetParent(GameObject.FindObjectOfType<Canvas>().transform,false);
- }
- for (int i = 0;i<inventory.Length;i++)
- {
- items[i] = new HotbarElement(blocks[i],inventory[i],names[i]);
- }
- selector = GameObject.FindGameObjectWithTag("hbSelectionItem");
- selector.transform.SetSiblingIndex(GameObject.FindObjectOfType<Canvas>().transform.childCount-1);
- load();
- }
- void Update () {
- //options
- if (options.activeSelf)
- {
- options.transform.GetChild(1).GetChild(4).GetComponent<Text>().text = options.transform.GetChild(1).GetComponent<Slider>().value.ToString();
- int width = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[0]);
- int height = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[1]);
- int refresh = Convert.ToInt32(options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text.Split("x".ToCharArray())[2]);
- Screen.SetResolution(width,height,options.transform.GetChild(2).GetChild(3).GetComponent<Toggle>().isOn,refresh);
- foreach (Camera c in Camera.allCameras)
- {
- c.fieldOfView = options.transform.GetChild(1).GetComponent<Slider>().value;
- }
- }
- //handheld
- handheld = items.Length > 10 * page + slot ? items[10 * page + slot].name : "none";
- //body
- GameObject.FindGameObjectWithTag("Player").transform.rotation = Quaternion.Euler(0, GameObject.FindGameObjectWithTag("Player").transform.rotation.eulerAngles.y, 0);
- //block rotation
- if (Input.GetKeyDown(KeyCode.Home))
- {
- rotX++;
- }
- if (Input.GetKeyDown(KeyCode.End))
- {
- rotX--;
- }
- if (Input.GetKeyDown(KeyCode.Insert))
- {
- rotZ++;
- }
- if (Input.GetKeyDown(KeyCode.PageUp))
- {
- rotZ--;
- }
- if (Input.GetKeyDown(KeyCode.Delete))
- {
- rotY++;
- }
- if (Input.GetKeyDown(KeyCode.PageDown))
- {
- rotY--;
- }
- if (rotX == 4)
- {
- rotX = 0;
- }
- if (rotX == -1)
- {
- rotX = 3;
- }
- if (rotY == 4)
- {
- rotY = 1;
- }
- if (rotY == -1)
- {
- rotY = 3;
- }
- if (rotZ == 4)
- {
- rotZ = 1;
- }
- if (rotZ == -1)
- {
- rotZ = 3;
- }
- if (!pauseMenu.activeSelf && !options.activeSelf)
- {
- //flight
- if (flying)
- {
- gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 0, 0);
- gameObject.GetComponent<Rigidbody>().useGravity = false;
- }
- else
- {
- gameObject.GetComponent<Rigidbody>().useGravity = true;
- }
- if (enableFlight)
- {
- enableTicks++;
- if (enableTicks * Time.smoothDeltaTime >= 0.5)
- {
- enableTicks = 0;
- enableFlight = false;
- }
- }
- if (Input.GetKeyDown(KeyCode.Space))
- {
- if (!flying && isOnGround(10))
- {
- gameObject.GetComponent<Rigidbody>().velocity = new Vector3(0, 10, 0);
- }
- if (enableFlight)
- {
- if (flying)
- {
- enableFlight = false;
- flying = false;
- enableTicks = 0;
- }
- else
- {
- enableFlight = false;
- flying = true;
- enableTicks = 0;
- }
- }
- else
- {
- enableFlight = true;
- }
- }
- //Movement
- if (Input.GetKey(KeyCode.Space) && flying)
- {
- transform.Translate(new Vector3(0, 1, 0) * speed, Space.World);
- }
- if (Input.GetKey(KeyCode.W))
- {
- Vector3 v = transform.forward;
- v.y = 0;
- v.Normalize();
- v *= speed;
- transform.Translate(v, Space.World);
- }
- if (Input.GetKey(KeyCode.A))
- {
- Vector3 v = -transform.right;
- v.y = 0;
- v.Normalize();
- v *= speed;
- transform.Translate(v, Space.World);
- }
- if (Input.GetKey(KeyCode.S))
- {
- Vector3 v = -transform.forward;
- v.y = 0;
- v.Normalize();
- v *= speed;
- transform.Translate(v, Space.World);
- }
- if (Input.GetKey(KeyCode.D))
- {
- Vector3 v = transform.right;
- v.y = 0;
- v.Normalize();
- v *= speed;
- transform.Translate(v, Space.World);
- }
- if (Input.GetKey(KeyCode.LeftShift) && flying)
- {
- if (transform.position.y <= 2)
- {
- flying = false;
- }
- else
- {
- transform.Translate(new Vector3(0, -1, 0) * speed, Space.World);
- }
- }
- if (transform.position.y < Terrain.activeTerrain.SampleHeight(transform.position))
- {
- Vector3 pos = transform.position;
- pos.y = Terrain.activeTerrain.SampleHeight(pos);
- transform.position = pos;
- }
- //Speed
- if (Input.GetKey(KeyCode.LeftControl))
- {
- speed = 10 * Time.deltaTime;
- }
- else
- {
- speed = 5 * Time.deltaTime;
- }
- //Camera
- float vertical = (-Input.GetAxis("Mouse Y") * Time.deltaTime * 100);
- float horizontal = Input.GetAxis("Mouse X") * Time.deltaTime * 100;
- transform.Rotate(vertical, horizontal, 0);
- float x = transform.rotation.eulerAngles.x;
- if (x != 0)
- {
- if (x < 271 && x > 180)
- {
- x = 271;
- }
- else
- {
- if (x > 89 && x < 180)
- {
- x = 89;
- }
- }
- }
- transform.rotation = Quaternion.Euler(x, transform.rotation.eulerAngles.y, 0);
- }
- //Exit
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- if (!pauseMenu.activeSelf && !options.activeSelf)
- {
- pause();
- } else if (!options.activeSelf)
- {
- unpause();
- }
- }
- //block selection
- float scroll = Input.GetAxis("Mouse ScrollWheel");
- int scr = scroll<0?-1:scroll>0?1:0;
- if (scr != 0 && !pauseMenu.activeSelf && !options.activeSelf)
- {
- slot += scr;
- page += slot >= 0 ? Mathf.FloorToInt(slot / 10) : -1;
- slot = slot >= 0 ? slot % 10 : slot + 10;
- slot = Mathf.Clamp(slot, 0, 9);
- page = Mathf.Clamp(page, 0, Mathf.CeilToInt(inventory.Length / 9));
- }
- for (int i = 0; i<hotbarUI.Length;i++)
- {
- if (items.Length <= 10 * page + i)
- {
- hotbarUI[i].GetComponent<Image>().overrideSprite = hotbarUI[i].GetComponent<Image>().sprite;
- } else
- {
- hotbarUI[i].GetComponent<Image>().overrideSprite = items[10 * page + i].image;
- }
- }
- selector.transform.position = hotbarUI[slot].transform.position;
- //third person
- if (Input.GetKeyDown(KeyCode.F5) && !pauseMenu.activeSelf && !options.activeSelf)
- {
- if (thirdPerson.gameObject.GetComponent<Camera>().enabled)
- {
- setCamState(maincam.transform.gameObject, true);
- setCamState(thirdPerson.gameObject, false);
- } else
- {
- setCamState(thirdPerson.gameObject, true);
- setCamState(maincam.transform.gameObject, false);
- }
- }
- //physics
- if (Input.GetKeyDown(KeyCode.O) && !pauseMenu.activeSelf && !options.activeSelf)
- {
- staticmode = !staticmode;
- }
- if (Input.GetKeyDown(KeyCode.P) && !pauseMenu.activeSelf && !options.activeSelf)
- {
- pausephysics = !pausephysics;
- }
- physics.transform.GetChild(0).GetComponent<Text>().text = "Static Building: " + (staticmode ? "on" : "off");
- physics.transform.GetChild(1).GetComponent<Text>().text = "Physics: " + (pausephysics ? "paused" : "running");
- physics.transform.GetChild(2).GetComponent<Text>().text = "Selected: " + (items.Length > 10 * page + slot ? items[10 * page + slot].name : "none");
- //block ghosting
- if (ghost != null)
- {
- Destroy(ghost);
- ghost = null;
- }
- if (items.Length > 10 * page + slot && items[10 * page + slot].prefab != null)
- {
- Ray ray = new Ray(transform.position, transform.forward);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- {
- Vector3 v = new Vector3(0, 0, 0);
- if (hit.transform.gameObject.tag == "block")
- {
- v = hit.transform.position + hit.normal;
- ghost = build(items[10 * page + slot].prefab,Quaternion.Euler(rotX*90,rotY*90,rotZ*90));
- if (ghost == null)
- {
- float xpos = Mathf.RoundToInt(hit.point.x);
- float ypos = Mathf.CeilToInt(hit.point.y);
- float zpos = Mathf.RoundToInt(hit.point.z);
- v = new Vector3(xpos, ypos, zpos);
- ghost = (GameObject)Instantiate(items[10 * page + slot].prefab, v, Quaternion.Euler(rotX * 90, rotY * 90, rotZ * 90));
- }
- }
- else
- {
- float xpos = Mathf.RoundToInt(hit.point.x);
- float ypos = Mathf.CeilToInt(hit.point.y);
- float zpos = Mathf.RoundToInt(hit.point.z);
- if (ypos - 0.5 < Terrain.activeTerrain.SampleHeight(new Vector3(xpos,ypos,zpos)))
- {
- ypos = Mathf.Ceil(Terrain.activeTerrain.SampleHeight(new Vector3(xpos, ypos, zpos))+0.5f);
- }
- v = new Vector3(xpos, ypos, zpos);
- ghost = (GameObject)Instantiate(items[10 * page + slot].prefab, v, Quaternion.Euler(rotX * 90, rotY * 90, rotZ * 90));
- }
- ghost.GetComponent<Block>().stat = true;
- ghost.GetComponent<MeshRenderer>().material.SetFloat("_Mode", 2);
- Color c = ghost.GetComponent<MeshRenderer>().material.color;
- ghost.GetComponent<MeshRenderer>().material.color = new Color(c.r, c.g, c.b, c.a * 0.75f);
- Collider[] col = ghost.GetComponents<Collider>();
- foreach(Collider coll in col)
- {
- coll.enabled = false;
- }
- ghost.tag = "ghost";
- ghost.GetComponent<Block>().enabled = false;
- if (ghost.transform.childCount > 0)
- {
- for (int i = 0;i < ghost.transform.childCount;i++)
- {
- Destroy(ghost.transform.GetChild(i).gameObject);
- }
- }
- }
- }
- //glue
- if (items.Length > 10 * page + slot)
- {
- if (items[10 * page + slot].name == "Glue")
- {
- if (Input.GetMouseButtonDown(2))
- {
- glueparent = null;
- Target t = getTarget(transform);
- if (t.trans != null)
- {
- if (t.trans.gameObject.tag == "block")
- {
- if (!t.trans.gameObject.GetComponent<Block>().stat)
- {
- glueparent = t.trans.gameObject;
- }
- }
- }
- }
- if (Input.GetMouseButtonDown(1))
- {
- if (glueparent != null)
- {
- if (glueparent.GetComponent<Rigidbody>() != null)
- {
- Transform t = getTarget(transform).trans;
- if (t != null)
- {
- if (t.gameObject != glueparent && t.gameObject.tag == "block")
- {
- if (!t.gameObject.GetComponent<Block>().stat)
- {
- if (glueparent.GetComponent<FixedJoint>() != null)
- {
- if (glueparent.GetComponent<FixedJoint>().connectedBody != t.GetComponent<Rigidbody>())
- {
- if (t.GetComponent<FixedJoint>() == null)
- {
- t.gameObject.AddComponent<FixedJoint>();
- }
- t.gameObject.GetComponent<FixedJoint>().connectedBody = glueparent.GetComponent<Rigidbody>();
- }
- } else
- {
- if (t.GetComponent<FixedJoint>() == null)
- {
- t.gameObject.AddComponent<FixedJoint>();
- }
- t.gameObject.GetComponent<FixedJoint>().connectedBody = glueparent.GetComponent<Rigidbody>();
- }
- }
- }
- }
- }
- }
- }
- if (Input.GetMouseButtonDown(0))
- {
- Transform t = getTarget(transform).trans;
- if (t != null)
- {
- if (t.gameObject.tag == "block")
- {
- if (!t.gameObject.GetComponent<Block>().stat)
- {
- if (t.GetComponent<FixedJoint>() != null)
- {
- Destroy(t.GetComponent<FixedJoint>());
- }
- }
- }
- }
- }
- }
- }
- //powerPlug
- if (items.Length > 10 * page + slot)
- {
- if (items[10 * page + slot].name == "PowerPlug")
- {
- if (Input.GetMouseButtonDown(2))
- {
- powerprov = null;
- Target t = getTarget(transform);
- if (t.trans != null)
- {
- if (t.trans.gameObject.tag == "block")
- {
- powerprov = t.trans;
- }
- }
- }
- if (Input.GetMouseButtonDown(1))
- {
- if (powerprov != null)
- {
- Transform t = getTarget(transform).trans;
- if (t != null)
- {
- if (t.gameObject != powerprov && t.gameObject.tag == "block")
- {
- if ((getHighestParent(t.gameObject) == getHighestParent(powerprov.gameObject))||(t.GetComponent<Block>().stat&&powerprov.GetComponent<Block>().stat))
- {
- t.gameObject.GetComponent<Block>().powerProvider = powerprov;
- }
- }
- }
- }
- }
- if (Input.GetMouseButtonDown(0))
- {
- Transform t = getTarget(transform).trans;
- if (t != null)
- {
- if (t.gameObject.tag == "block")
- {
- t.gameObject.GetComponent<Block>().powerProvider = null;
- }
- }
- }
- }
- }
- //block placement
- if (Input.GetMouseButtonDown(1) && !pauseMenu.activeSelf && !options.activeSelf)
- {
- if (ghost != null)
- {
- worldBlocks.Add((GameObject)Instantiate(items[10 * page + slot].prefab, ghost.transform.position, ghost.transform.rotation));
- }
- }
- //block destruction
- bool gluing = false;
- if (items.Length > 10 * page + slot )
- {
- if (items[10 * page + slot].name == "Glue" || items[10 * page + slot].name == "PowerPlug")
- {
- gluing = true;
- }
- }
- if (Input.GetMouseButtonDown(0) && !pauseMenu.activeSelf && !options.activeSelf && !gluing)
- {
- Transform t = getTarget(transform).trans;
- if (t != null)
- {
- if (t.gameObject.tag == "block")
- {
- if (t.parent != null)
- {
- t.SetParent(null);
- }
- worldBlocks.Remove(t.gameObject);
- Destroy(t.gameObject);
- }
- }
- }
- }
- private bool isOnGround(float maxdist)
- {
- return Physics.Raycast(transform.position,Vector3.down, maxdist);
- }
- public static void setCamState(GameObject cam,bool enabled)
- {
- cam.GetComponent<Camera>().enabled = enabled;
- cam.GetComponent<AudioListener>().enabled = enabled;
- cam.GetComponent<FlareLayer>().enabled = enabled;
- cam.GetComponent<GUILayer>().enabled = enabled;
- }
- public void pause()
- {
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- Time.timeScale = 0;
- pauseMenu.SetActive(true);
- save(worldBlocks.ToArray(),transform.position);
- }
- public void unpause()
- {
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- Time.timeScale = 1;
- pauseMenu.SetActive(false);
- }
- public void exit()
- {
- Application.Quit();
- }
- public void loadOptions()
- {
- options.SetActive(true);
- options.transform.GetChild(0).GetComponent<InputField>().text = PlayerPrefs.GetString("PlayerName");
- options.transform.GetChild(1).GetComponent<Slider>().value = PlayerPrefs.GetFloat("FOV");
- Resolution[] resolutions = Screen.resolutions;
- List<Dropdown.OptionData> dropdownOptions = new List<Dropdown.OptionData>();
- int currentres = 0;
- for (int i = 0;i<resolutions.Length;i++)
- {
- Resolution res = resolutions[i];
- dropdownOptions.Add(new Dropdown.OptionData(res.width + "x" + res.height + "x" + res.refreshRate));
- if (res.Equals(Screen.currentResolution))
- {
- currentres = i;
- }
- }
- options.transform.GetChild(2).GetComponent<Dropdown>().options = dropdownOptions;
- options.transform.GetChild(2).GetComponent<Dropdown>().value = currentres;
- pauseMenu.SetActive(false);
- }
- public void saveOptions()
- {
- PlayerPrefs.SetString("PlayerName", options.transform.GetChild(0).GetComponent<InputField>().text);
- PlayerPrefs.SetFloat("FOV", options.transform.GetChild(1).GetComponent<Slider>().value);
- PlayerPrefs.SetString("resolution", options.transform.GetChild(2).GetComponent<Dropdown>().options.ToArray()[options.transform.GetChild(2).GetComponent<Dropdown>().value].text);
- PlayerPrefs.SetString("fullscreen", options.transform.GetChild(2).GetChild(3).GetComponent<Toggle>().isOn.ToString());
- PlayerPrefs.Save();
- options.SetActive(false);
- pauseMenu.SetActive(true);
- }
- public void cancelOptions()
- {
- bool firstload = false;
- if (PlayerPrefs.GetFloat("FOV") < 30 || PlayerPrefs.GetFloat("FOV") > 90)
- {
- firstload = true;
- }
- if (PlayerPrefs.GetString("fullscreen") != "true" && PlayerPrefs.GetString("fullscreen") != "false")
- {
- firstload = true;
- }
- if (PlayerPrefs.GetString("resolution").Split("x".ToCharArray()).Length != 3)
- {
- firstload = true;
- }
- if (!firstload)
- {
- foreach (Camera c in Camera.allCameras)
- {
- c.fieldOfView = PlayerPrefs.GetFloat("FOV");
- }
- int resW = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[0]);
- int resH = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[1]);
- int resR = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[2]);
- bool resF = Convert.ToBoolean(PlayerPrefs.GetString("fullscreen"));
- Screen.SetResolution(resW, resH, resF, resR);
- }
- else
- {
- PlayerPrefs.SetFloat("FOV", 60f);
- PlayerPrefs.SetString("resolution", Screen.currentResolution.width + "x" + Screen.currentResolution.height + "x" + Screen.currentResolution.refreshRate);
- PlayerPrefs.SetString("fullscreen", Screen.fullScreen.ToString());
- PlayerPrefs.Save();
- }
- options.SetActive(false);
- pauseMenu.SetActive(true);
- }
- public GameObject fromString(string s,bool lastpausephysics)
- {
- string[] data = s.Split("/".ToCharArray());
- if (data.Length != 19)
- {
- return null;
- }
- foreach (string str in data)
- {
- if (str == "")
- {
- return null;
- }
- }
- string name = data[0];
- double x = Convert.ToDouble(data[1]);
- double y = Convert.ToDouble(data[2]);
- double z = Convert.ToDouble(data[3]);
- Vector3 pos = new Vector3((float)x, (float)y, (float)z);
- double rx = Convert.ToDouble(data[4]);
- double ry = Convert.ToDouble(data[5]);
- double rz = Convert.ToDouble(data[6]);
- double rw = Convert.ToDouble(data[7]);
- Quaternion rot = new Quaternion((float)rx, (float)ry, (float)rz, (float)rw);
- double lvx = Convert.ToDouble(data[8]);
- double lvy = Convert.ToDouble(data[9]);
- double lvz = Convert.ToDouble(data[10]);
- Vector3 lv = new Vector3((float)lvx, (float)lvy, (float)lvz);
- double gpx = Convert.ToDouble(data[12]);
- double gpy = Convert.ToDouble(data[13]);
- double gpz = Convert.ToDouble(data[14]);
- Vector3 gp = new Vector3((float)gpx, (float)gpy, (float)gpz);
- double ppx = Convert.ToDouble(data[15]);
- double ppy = Convert.ToDouble(data[16]);
- double ppz = Convert.ToDouble(data[17]);
- Vector3 pp = new Vector3((float)ppx, (float)ppy, (float)ppz);
- float eu = (float)Convert.ToDouble(data[18]);
- bool stat = Convert.ToBoolean(data[11]);
- GameObject prefab = null;
- foreach (GameObject go in blocks)
- {
- if (go != null)
- {
- if (name.StartsWith(go.name))
- {
- prefab = go;
- }
- }
- }
- if (prefab == null)
- {
- return null;
- }
- GameObject obj = (GameObject)Instantiate(prefab, pos, rot);
- obj.GetComponent<Block>().stat = stat;
- obj.GetComponent<Block>().oldvec = lv;
- if (!lastpausephysics)
- {
- obj.GetComponent<Block>().loadVelocity = lv;
- }
- obj.GetComponent<Block>().laststat = lastpausephysics;
- obj.GetComponent<Block>().scriptSpawn = true;
- obj.GetComponent<Block>().searchGlueParent = gp;
- obj.GetComponent<Block>().searchPowerProvider = pp;
- obj.GetComponent<Block>().eu = eu;
- return obj;
- }
- public string toString(GameObject go)
- {
- String str = "";
- string name = go.name;
- double x = go.transform.position.x;
- double y = go.transform.position.y;
- double z = go.transform.position.z;
- double rx = go.transform.rotation.x;
- double ry = go.transform.rotation.y;
- double rz = go.transform.rotation.z;
- double rw = go.transform.rotation.w;
- double lvx = go.GetComponent<Block>().oldvec.x;
- double lvy = go.GetComponent<Block>().oldvec.y;
- double lvz = go.GetComponent<Block>().oldvec.z;
- if (!pausephysics && go.GetComponent<Rigidbody>() != null)
- {
- lvx = go.GetComponent<Rigidbody>().velocity.x;
- lvy = go.GetComponent<Rigidbody>().velocity.y;
- lvz = go.GetComponent<Rigidbody>().velocity.z;
- }
- double gpx = 0;
- double gpy = 0;
- double gpz = 0;
- if (go.GetComponent<FixedJoint>() != null)
- {
- gpx = go.GetComponent<FixedJoint>().connectedBody.transform.position.x;
- gpy = go.GetComponent<FixedJoint>().connectedBody.transform.position.y;
- gpz = go.GetComponent<FixedJoint>().connectedBody.transform.position.z;
- }
- double ppx = 0;
- double ppy = 0;
- double ppz = 0;
- if (go.GetComponent<Block>().powerProvider != null)
- {
- ppx = go.GetComponent<Block>().powerProvider.position.x;
- ppy = go.GetComponent<Block>().powerProvider.position.y;
- ppz = go.GetComponent<Block>().powerProvider.position.z;
- }
- bool stat = go.GetComponent<Block>().stat;
- float eu = go.GetComponent<Block>().eu;
- str = name + "/" + x + "/" + y + "/" + z + "/" + rx + "/" + ry + "/" + rz + "/" + rw + "/" + lvx + "/" + lvy + "/" + lvz + "/" + stat + "/" + gpx + "/" + gpy + "/" + gpz + "/" + ppx + "/" + ppy + "/" + ppz + "/" + eu;
- return str;
- }
- private int indexOf<T>(T obj, T[] arr)
- {
- for (int i = 0;i < arr.Length;i++)
- {
- if (arr[i].Equals(obj))
- {
- return i;
- }
- }
- return -1;
- }
- public void save(GameObject[] objects,Vector3 lastpos)
- {
- clearWorld();
- PlayerPrefs.SetInt("worldBlocks", objects.Length);
- for (int i = 0;i< objects.Length;i++)
- {
- PlayerPrefs.SetString("block"+i, toString(objects[i]));
- }
- PlayerPrefs.SetFloat("lastx",lastpos.x);
- PlayerPrefs.SetFloat("lasty", lastpos.y);
- PlayerPrefs.SetFloat("lastz", lastpos.z);
- PlayerPrefs.SetString("stat", "" + staticmode);
- PlayerPrefs.SetString("pp", "" + pausephysics);
- PlayerPrefs.SetFloat("sunrot", sunlight.GetComponent<Sun>().rot);
- PlayerPrefs.Save();
- }
- public static void clearWorld()
- {
- PlayerPrefs.DeleteAll();
- PlayerPrefs.SetFloat("lastx", 746);
- PlayerPrefs.SetFloat("lasty", 480);
- PlayerPrefs.SetFloat("lastz", -124);
- PlayerPrefs.Save();
- }
- public void load()
- {
- bool firstload = false;
- if (PlayerPrefs.GetFloat("FOV") < 30 || PlayerPrefs.GetFloat("FOV") > 90)
- {
- firstload = true;
- }
- if (PlayerPrefs.GetString("fullscreen") != "true" && PlayerPrefs.GetString("fullscreen") != "false")
- {
- firstload = true;
- }
- if (PlayerPrefs.GetString("resolution").Split("x".ToCharArray()).Length != 3)
- {
- firstload = true;
- }
- if (!firstload)
- {
- foreach (Camera c in Camera.allCameras)
- {
- c.fieldOfView = PlayerPrefs.GetFloat("FOV");
- }
- int resW = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[0]);
- int resH = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[1]);
- int resR = Convert.ToInt32(PlayerPrefs.GetString("resolution").Split("x".ToCharArray())[2]);
- bool resF = Convert.ToBoolean(PlayerPrefs.GetString("fullscreen"));
- Screen.SetResolution(resW, resH, resF, resR);
- } else
- {
- PlayerPrefs.SetFloat("FOV",60f);
- PlayerPrefs.SetString("resolution",Screen.currentResolution.width + "x" + Screen.currentResolution.height + "x" + Screen.currentResolution.refreshRate);
- PlayerPrefs.SetString("fullscreen",Screen.fullScreen.ToString());
- PlayerPrefs.Save();
- }
- try
- {
- int blockamount = PlayerPrefs.GetInt("worldBlocks");
- for (int i = 0; i < blockamount; i++)
- {
- string data = PlayerPrefs.GetString("block" + i);
- GameObject go = fromString(data, Convert.ToBoolean(PlayerPrefs.GetString("pp")));
- if (go != null)
- {
- worldBlocks.Add(go);
- }
- }
- transform.position = new Vector3(PlayerPrefs.GetFloat("lastx"), PlayerPrefs.GetFloat("lasty"), PlayerPrefs.GetFloat("lastz"));
- staticmode = Convert.ToBoolean(PlayerPrefs.GetString("stat"));
- pausephysics = Convert.ToBoolean(PlayerPrefs.GetString("pp"));
- sunlight.GetComponent<Sun>().rot = PlayerPrefs.GetFloat("sunrot");
- } catch(Exception e)
- {
- Debug.LogWarning("World loading failed. " + e.Message);
- return;
- }
- }
- public GameObject build(GameObject prefab, Quaternion rot)
- {
- Target t = getTarget(transform);
- if ((t.center == Vector3.zero && t.normal == Vector3.zero))
- {
- return null;
- }
- return (GameObject)Instantiate(prefab, t.center + t.normal, rot);
- }
- public Target getTarget(Transform t, Vector3? point = null, Vector3? normal = null)
- {
- if (point == null || normal == null)
- {
- Ray r = new Ray(t.position, t.forward);
- RaycastHit hit = new RaycastHit();
- if (Physics.Raycast(r, out hit, 100))
- {
- if (hit.transform.gameObject.tag == "block" || hit.transform.gameObject.tag == "blockChild")
- {
- Vector3 target = hit.transform.position;
- if (hit.transform.parent != null)
- {
- target = getTarget(hit.transform, hit.point, hit.normal).center;
- }
- return new Target(target, hit.normal,hit.transform);
- } else
- {
- float x = Mathf.RoundToInt(hit.point.x);
- float y = Mathf.CeilToInt(hit.point.y);
- float z = Mathf.RoundToInt(hit.point.z);
- return new Target(new Vector3(x,y,z),Vector3.zero);
- }
- }
- }
- else
- {
- Transform nearest = t;
- for (int i = 0; i < t.childCount; i++)
- {
- float nm = ((Vector3)(point - nearest.position)).magnitude;
- float cm = ((Vector3)(point - t.GetChild(i).position)).magnitude;
- if (nm > cm)
- {
- nearest = t.GetChild(i);
- }
- }
- return new Target(nearest.position,(Vector3)normal,nearest);
- }
- return new Target(new Vector3(0,0,0),new Vector3(0,0,0));
- }
- public static GameObject getHighestParent(GameObject go)
- {
- if (go.GetComponent<FixedJoint>() == null)
- {
- return go;
- }
- if (go.GetComponent<FixedJoint>().connectedBody == null)
- {
- return go;
- }
- return getHighestParent(go.GetComponent<FixedJoint>().connectedBody.gameObject);
- }
- }
- public class Target
- {
- public Vector3 center;
- public Vector3 normal;
- public Transform trans;
- public Target(Vector3 center,Vector3 normal)
- {
- this.center = center;
- this.normal = normal;
- }
- public Target(Vector3 center, Vector3 normal,Transform trans)
- {
- this.center = center;
- this.normal = normal;
- this.trans = trans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement