using System; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class WallCreate : MonoBehaviour { public static WallCreate instance; public static float WallHeight = 1f; public float WallThickness = 0.05f; public int Rounding = 1; public bool move = false; public bool BoxColliderEnabled = true; public Dialog Dialog; public GameObject ZonesScroll; public GameObject Player; //public Vector3 targetPos; public enum Mode { Wall, Room, Zone, Pole, Cursor }; public Mode mode = Mode.Cursor; GameObject wall; Wall currentWall; //public Dictionary WallObjects = new Dictionary(); public static Dictionary> Walls = new Dictionary>(); //public static List Rooms = new List(); public static Dictionary> Zones = new Dictionary>(); private Vector3 screenPoint; private Vector3 offset; private Vector3 curScreenPoint; private Vector3 curPosition; private Vector3 start_pos; //Vector3 Point; GameObject canvas; Text text; MeshCollider meshCollider; Ray ray; CompanyController company; private void Awake() { instance = this; } // Start is called before the first frame update void Start() { // gameObject.GetComponent().enabled = BoxColliderEnabled; //cursor_end = transform.GetChild(6).gameObject; //Point = Camera.main.WorldToScreenPoint(gameObject.transform.position); canvas = transform.GetChild(0).gameObject; text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent(); company = CompanyController.instance; } // Update is called once per frame void Update() { meshCollider = company.locations[company.locations_index[company.active_location]].plane.GetComponent(); ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitData; if (meshCollider.Raycast(ray, out hitData, 1000)) { transform.position = hitData.point; } //transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Point.z)); //if (transform.position.y != 0.5f) transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z); } void OnMouseDown() { if (!EventSystem.current.IsPointerOverGameObject()) { //Debug.Log(StagesEditorController.indexCursor); screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); //s_point = Camera.main.WorldToScreenPoint(gameObject.transform.position); start_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); switch (mode) { case Mode.Wall: wall = AddWall(company, Vector3.zero); HistoryController.instance.EventHistory(0, wall); break; case Mode.Room: wall = AddRoom(); break; case Mode.Zone: wall = AddZone(Dialog, company, ZonesScroll); var location_id = company.locations_index[company.active_location]; HistoryController.instance.EventHistory(0, wall, Zones[location_id].Last().buttons); break; } } canvas.SetActive(true); } float scale_x; float scale_z; void OnMouseDrag() { if (!EventSystem.current.IsPointerOverGameObject()) { curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); // e_point = Camera.main.WorldToScreenPoint(gameObject.transform.position); curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset; var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); //Debug.Log("start_pos.x "+start_pos.x); scale_x = (float)Math.Round(end_pos.x - start_pos.x, Rounding); scale_z = (float)Math.Round(end_pos.z - start_pos.z, Rounding); text.text = $"x={scale_x} y={scale_z}"; switch (mode) { case Mode.Wall: if (Math.Abs(scale_x) > Math.Abs(scale_z)/* && x_mod >= 0.4f*/) { wall.transform.localScale = new Vector3(scale_x, WallHeight, WallThickness); wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, WallHeight / 2, start_pos.z); } if (Math.Abs(scale_z) > Math.Abs(scale_x)/* && z_mod >= 0.4f*/) { wall.transform.localScale = new Vector3(WallThickness, WallHeight, scale_z); wall.transform.position = new Vector3(start_pos.x, WallHeight / 2, scale_z / 2 + start_pos.z); } break; case Mode.Room: /*if (scale_x > 0 && scale_z > 0) */ LocationController.Cube(wall, new Vector3(scale_x, scale_z, WallHeight), new Vector2((float)Math.Round(start_pos.x, Rounding), (float)Math.Round(start_pos.z, Rounding)), WallThickness); break; //case Mode.Pole: // AddWall(pos); // break; case Mode.Zone: wall.transform.localScale = new Vector3(scale_x/10, 1, scale_z/10); wall.transform.position = new Vector3(scale_x / 2 + start_pos.x, 0.05f, scale_z / 2 + start_pos.z); break; } float offsetPosY = transform.position.y + 1.5f; // Final position of marker above GO in world space canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z); } } private void OnMouseUp() { //var end_pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); //Debug.Log($"end_pos = {end_pos}"); var location_id = company.locations_index[company.active_location]; if (!Walls.ContainsKey(location_id)) Walls[location_id] = new List(); if (mode == Mode.Room) { for (int i = 0; i < wall.transform.childCount; i++) { var w = wall.transform.GetChild(i); if(w.gameObject.activeSelf == true) { currentWall = new Wall { id = Convert.ToUInt32(4000000000 + Walls[location_id].Count),location_id = location_id, start_width = w.position.x, start_height = w.position.z, end_width = w.localScale.x, end_height = w.localScale.z, go = wall }; Walls[location_id].Add(currentWall); } } } //if (mode == Mode.Room) Rooms.Add(currentWall); if (mode == Mode.Wall) { currentWall = new Wall { id =Convert.ToUInt32(4000000000 + Walls[location_id].Count), start_width = wall.transform.position.x, start_height = wall.transform.position.z, end_width = wall.transform.localScale.x, end_height = wall.transform.localScale.z, go = wall, location_id=location_id }; Debug.Log($"new wall save id {currentWall.id} x {currentWall.start_width} z {currentWall.start_height} x2 {currentWall.end_width} z2 {currentWall.end_height} location_id {currentWall.location_id}"); Walls[location_id].Add(currentWall); } /*if (mode == Mode.Wall || mode == Mode.Room)*/ /*Walls.Add(currentWall);*/ canvas.SetActive(false); if (mode == Mode.Zone && !EventSystem.current.IsPointerOverGameObject()) { Dialog.GetComponent().Text.gameObject.SetActive(false); Dialog.GetComponent().InputField.gameObject.SetActive(true); Dialog.GetComponent().Button.onClick.AddListener(() => { wall.GetComponent().TextMesh.text=Dialog.GetComponent().InputField.text; }); var z = Zones[location_id][last_zone]; z.start_width = wall.transform.position.x; z.start_height = wall.transform.position.z; z.end_width = wall.transform.localScale.x; z.end_height = wall.transform.localScale.z; Debug.Log($"z {z.id} z.start_width {z.start_width} z.start_height {z.start_height}"); //Zones[location_id].Insert(last_zone, z); } } public GameObject AddWall(CompanyController company, Vector3 pos, Vector3 scale = default) { var wall = Instantiate(Resources.Load("GameObjects/Wall", typeof(GameObject))) as GameObject; wall.transform.position = pos; if (scale != null) wall.transform.localScale = scale; var location = company.locations[company.locations_index[company.active_location]].location; wall.transform.SetParent(location.transform.GetChild(1).transform); //WallObjects.Add(wall); return wall; } GameObject AddRoom() { var room = Instantiate(Resources.Load("GameObjects/Cube", typeof(GameObject))) as GameObject; var location = company.locations[company.locations_index[company.active_location]].location; room.transform.SetParent(location.transform.GetChild(1).transform); LocationController.Cube(room, new Vector3(0.1f, 0.1f, 1)); //for (var i = 0; i < room.transform.childCount; i++) // if (room.transform.GetChild(i).gameObject.activeSelf == true) // WallObjects.Add(room.transform.GetChild(i).gameObject); return room; } static int last_zone; public GameObject AddZone(Dialog Dialog, CompanyController company, GameObject ZonesScroll, Zone z = null) { var location = company.locations[company.locations_index[company.active_location]].location; var location_id = company.locations_index[company.active_location]; if (!Zones.ContainsKey(location_id)) Zones[location_id] = new List(); var zone = Instantiate(Resources.Load("GameObjects/Zone", typeof(GameObject))) as GameObject; zone.name = $"Zone_{Zones[location_id].Count + 1}"; var zname = $"Зона {Zones[location_id].Count + 1}"; if (z != null) zname = z.name; //zone.transform.position = pos; zone.transform.SetParent(location.transform.GetChild(2).transform); var panel_button = Instantiate(Resources.Load("GameObjects/Panel_Beacon", typeof(GameObject))) as GameObject; panel_button.transform.SetParent(ZonesScroll.transform); var button = panel_button.transform.GetChild(0).GetComponent