Example #1
0
 public void DeleteSelectedWall()
 {
     selectedWallFace.RelatedLine.Destroy();
     lines.Remove(selectedWallFace.RelatedLine);
     selectedWallFace = null;
     regeneratePath(true);
 }
    private Mesh WallFaceBuilder(WallFace wallFace)
    {
        Mesh           mesh      = new Mesh();
        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();

        var w = Utils.Round(wallFace.width);
        var h = wallFace.height;

        if (Utils.IsEqual(w, 0))
        {
            return(mesh);
        }

        PolygonPoint[] shape =
        {
            new PolygonPoint(0, 0),
            new PolygonPoint(h, 0),
            new PolygonPoint(h, w),
            new PolygonPoint(0, w),
        };

        Polygon p = new Polygon(shape);

        foreach (var polygonPoint in shape)
        {
            var vertex = new Vector3(0.002f, polygonPoint.Xf, polygonPoint.Yf);
            vertices.Add(vertex);
        }

        P2T.Triangulate(p);

        foreach (var triangle in p.Triangles)
        {
            var points = triangle.Points;
            var f1v1   = new Vector3(0.002f, points._0.Xf, points._0.Yf);
            var f1v2   = new Vector3(0.002f, points._1.Xf, points._1.Yf);
            var f1v3   = new Vector3(0.002f, points._2.Xf, points._2.Yf);
            triangles.Add(vertices.IndexOf(f1v1));
            triangles.Add(vertices.IndexOf(f1v2));
            triangles.Add(vertices.IndexOf(f1v3));
        }

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.RecalculateNormals();
        return(mesh);
    }
Example #3
0
    public void SetFace(WallFace wallFace)
    {
        face = wallFace;
        switch (face)
        {
        case WallFace.Bottom:
            bouncingDirection = Vector3.down;
            break;

        case WallFace.Left:
            bouncingDirection = new Vector3(-Mathf.Cos(Mathf.PI / 6f), Mathf.Sin(Mathf.PI / 6f));
            break;

        case WallFace.Right:
            bouncingDirection = new Vector3(Mathf.Cos(Mathf.PI / 6f), Mathf.Sin(Mathf.PI / 6f));
            break;
        }
    }
Example #4
0
    WallFace getSelectedWallFace()
    {
        float    dst          = float.MaxValue;
        WallFace selectedFace = null;

        for (int i = 0; i < wallFaces.Count; i++)
        {
            float det;
            if (wallFaces[i].IsMouseOver(out det))
            {
                if (det < dst)
                {
                    dst          = det;
                    selectedFace = wallFaces[i];
                }
            }
        }

        return(selectedFace);
    }
Example #5
0
    void Update()
    {
        if (viewingMode == ViewingMode.Interior)
        {
            for (int i = 0; i < wallFaces.Count; i++)
            {
                if (!wallFaces[i].IsFacingCamera || wallFaces[i].WallFaceType == WallFaceType.Outer)
                {
                    wallFaces[i].Wireframe = true;
                    wallFaces[i].Solid     = false;
                    for (int j = 0; j < wallFaces[i].RelatedLine.Doors.Count; j++)
                    {
                        wallFaces [i].RelatedLine.Doors [j].Door.SetActive(false);
                    }
                    for (int j = 0; j < wallFaces[i].RelatedLine.Windows.Count; j++)
                    {
                        wallFaces [i].RelatedLine.Windows [j].Window.SetActive(false);
                    }
                    wallFaces [i].gameObject.GetComponent <Collider> ().enabled = false;
                }
                else
                {
                    wallFaces[i].Wireframe = false;
                    wallFaces[i].Solid     = true;
                    for (int j = 0; j < wallFaces[i].RelatedLine.Doors.Count; j++)
                    {
                        wallFaces [i].RelatedLine.Doors [j].Door.SetActive(true);
                    }
                    for (int j = 0; j < wallFaces[i].RelatedLine.Windows.Count; j++)
                    {
                        wallFaces [i].RelatedLine.Windows [j].Window.SetActive(true);
                    }
                    wallFaces [i].gameObject.GetComponent <Collider> ().enabled = true;
                }
            }
        }
        else
        {
            for (int i = 0; i < wallFaces.Count; i++)
            {
                wallFaces[i].Wireframe = false;
                wallFaces[i].Solid     = true;
                for (int j = 0; j < wallFaces[i].RelatedLine.Doors.Count; j++)
                {
                    wallFaces [i].RelatedLine.Doors [j].Door.SetActive(true);
                }
                for (int j = 0; j < wallFaces[i].RelatedLine.Windows.Count; j++)
                {
                    wallFaces [i].RelatedLine.Windows [j].Window.SetActive(true);
                }
                wallFaces [i].gameObject.GetComponent <Collider> ().enabled = true;
            }
        }

        //if (!planningMode)
        {
            switch (Mode)
            {
            case BuildingEditMode.None:
            {
                if (Input.GetMouseButtonUp(0))
                {
                    if (SelectedItem == null)
                    {
                        WallFace wallface = getSelectedWallFace();
                        if (wallface != null)
                        {
                            selectedWallFace = wallface;
                            Mode             = BuildingEditMode.WallFaceSelected;
                        }
                    }
                    else
                    {
                        for (int i = 0; i < floorColliders.Count; i++)
                        {
                            floorColliders [i].enabled = true;
                        }

                        if (SelectedItem.itemType == type.Window || SelectedItem.itemType == type.Door)
                        {
                            WallFace wallface = getSelectedWallFace();
                            if (wallface != null)
                            {
                                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                                RaycastHit hit;
                                if (Physics.Raycast(ray, out hit, float.MaxValue) && !EventSystem.current.IsPointerOverGameObject())
                                {
                                    Vector2 location;
                                    Vector2?correctedLocation;
                                    if (wallface.RelatedLine.LocateItemInWall(hit.point, SelectedItem, out location, 100, out correctedLocation))
                                    {
                                        if (SelectedItem.itemType == type.Window)
                                        {
                                            wallface.RelatedLine.Windows.Add(new WallWindow(wallface.RelatedLine, location, SelectedItem.prefabItem.Size.z, SelectedItem.prefabItem.Size.y, Instantiate(SelectedItem.prefabItem.gameObject)));
                                            regeneratePath(false);
                                        }
                                        else if (SelectedItem.itemType == type.Door)
                                        {
                                            wallface.RelatedLine.Doors.Add(new WallDoor(wallface.RelatedLine, location.x, SelectedItem.prefabItem.Size.z, SelectedItem.prefabItem.Size.y, Instantiate(SelectedItem.prefabItem.gameObject)));
                                            regeneratePath(false);
                                        }
                                    }
                                    else if (correctedLocation.HasValue)
                                    {
                                        if (SelectedItem.itemType == type.Window)
                                        {
                                            wallface.RelatedLine.Windows.Add(new WallWindow(wallface.RelatedLine, correctedLocation.Value, SelectedItem.prefabItem.Size.z, SelectedItem.prefabItem.Size.y, Instantiate(SelectedItem.prefabItem.gameObject)));
                                            regeneratePath(false);
                                        }
                                        else if (SelectedItem.itemType == type.Door)
                                        {
                                            wallface.RelatedLine.Doors.Add(new WallDoor(wallface.RelatedLine, correctedLocation.Value.x, SelectedItem.prefabItem.Size.z, SelectedItem.prefabItem.Size.y, Instantiate(SelectedItem.prefabItem.gameObject)));
                                            regeneratePath(false);
                                        }
                                    }
                                }
                            }
                        }
                        else                                     // not window and not door

                        {
                            Vector3    location;
                            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                            RaycastHit hit;
                            if (Physics.Raycast(ray, out hit, float.MaxValue) && !EventSystem.current.IsPointerOverGameObject())
                            {
                                location = hit.point - ray.direction * SelectedItem.prefabItem.Size.z * 0.5f;
                                if (SelectedItem.alignToFloor)
                                {
                                    RaycastHit floorHit;
                                    if (hit.collider.Raycast(new Ray(location, Vector3.down), out floorHit, float.MaxValue))
                                    {
                                        Bounds aabb = new Bounds(floorHit.point + Vector3.up * SelectedItem.prefabItem.Size.y, SelectedItem.prefabItem.Size);

                                        Bounds?nAABB = alignToFloor(aabb, 10);
                                        if (nAABB.HasValue)
                                        {
                                            GameObject go        = Instantiate(SelectedItem.prefabItem.gameObject);
                                            PrefabItem pItem     = go.GetComponent <PrefabItem> ();
                                            Draggable  draggable = go.AddComponent <Draggable> ();
                                            draggable.XEnabled      = true;
                                            draggable.YEnabled      = false;
                                            draggable.ZEnabled      = true;
                                            draggable.XSnapDistance = 0;
                                            draggable.ZSnapDistance = 0;
                                            draggable.Enabled       = true;
                                            draggable.StartMoving  += delegate(GameObject sender, Vector3 oldPosition, Vector3 newPosition) {
                                                Bounds _aabb = new Bounds(newPosition + Vector3.up * pItem.Size.y, pItem.Size);
                                                Debug.Log("start " + newPosition);
                                                Bounds?_nAABB = alignToFloor(_aabb, 10);
                                                if (_nAABB != null)
                                                {
                                                    sender.transform.position = newPosition;
                                                }
                                            };
                                            draggable.Moving += delegate(GameObject sender, Vector3 oldPosition, Vector3 newPosition) {
                                                Bounds _aabb = new Bounds(newPosition + Vector3.up * pItem.Size.y, pItem.Size);

                                                Bounds?_nAABB = alignToFloor(_aabb, 10);
                                                if (_nAABB != null)
                                                {
                                                    sender.transform.position = newPosition;
                                                }
                                            };
                                            draggable.EndMoving += delegate(GameObject sender, Vector3 oldPosition, Vector3 newPosition) {
                                                Bounds _aabb = new Bounds(newPosition + Vector3.up * pItem.Size.y, pItem.Size);
                                                Debug.Log("end " + newPosition);

                                                Bounds?_nAABB = alignToFloor(_aabb, 10);
                                                if (_nAABB != null)
                                                {
                                                    sender.transform.position = newPosition;
                                                }
                                            };


                                            go.transform.position = nAABB.Value.center;
                                            items.Add(go);
                                        }
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < floorColliders.Count; i++)
                        {
                            floorColliders [i].enabled = false;
                        }
                    }
                }
            }
            break;

            case BuildingEditMode.WallFaceSelected:
            {
                if (Input.GetMouseButtonUp(0) && getSelectedWallFace() != null)
                {
                    selectedWallFace = null;
                    Mode             = BuildingEditMode.None;
                }
            }
            break;

            case BuildingEditMode.WallFaceMoving:
            {
            }
            break;
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (selectedWallFace != null)
                {
                    cameraTarget = (selectedWallFace.a + selectedWallFace.b) * 0.5f + Vector3.up * selectedWallFace.Height * 0.5f;
                }
                if (Time.time - lastClickTime < DoubleClickCatchTime)
                {
                    gameCamera.TargetObject = cameraTarget;
                }
                lastClickTime = Time.time;
            }
        }
        //else
        {
            if ((Mode == BuildingEditMode.None || Mode == BuildingEditMode.Drawing) && SelectedItem == null)
            {
                Ray        ray  = Camera.main.ScreenPointToRay(Input.mousePosition);
                Collider   coll = GetComponent <MeshCollider>();
                RaycastHit hit;
                snapObject.SetActive(false);
                if (Physics.Raycast(ray, out hit, float.MaxValue) && hit.collider == coll && !EventSystem.current.IsPointerOverGameObject())
                {
                    //if (coll.Raycast (ray, out hit, float.MaxValue)) {
                    if (snapEnabled)
                    {
                        hit.point = snapToGrid(hit.point);
                        //                      Debug.Log (hit.point);
                    }

                    if (verticesSelected.Count != 0)
                    {
                        for (int i = 0; i < verticesSelected.Count; i++)
                        {
                            if (verticesSelected[i] % 2 == 0)
                            {
                                lines[verticesSelected[i] / 2].a = hit.point;
                            }
                            else
                            {
                                lines[verticesSelected[i] / 2].b = hit.point;
                            }
                        }
                    }



                    if (DraggedLine != null)
                    {
                        DraggedLine.b = hit.point;
                    }

                    snapObject.SetActive(true);
                    snapObject.transform.position = hit.point;
                    if (Input.GetMouseButtonDown(0) && verticesSelected.Count == 0)
                    {
                        if (!pointASelected)
                        {
                            DraggedLine.Enabled = true;
                            //DraggedLine = new Line (hit.point, hit.point, DraggedLineMaterial);
                            pointA         = hit.point;
                            DraggedLine.a  = hit.point;
                            DraggedLine.b  = hit.point;
                            pointASelected = true;
                        }
                        else
                        {
                            Vector3 pointB = hit.point;
                            lines = Line.Split(lines, pointA);
                            lines = Line.Split(lines, pointB);
                            int id1 = lineVertices.IndexOf(pointA);
                            int id2 = lineVertices.IndexOf(pointB);
                            if (id1 == -1)
                            {
                                id1 = lineVertices.Count;
                                lineVertices.Add(pointA);
                            }
                            if (id2 == -1)
                            {
                                id2 = lineVertices.Count;
                                lineVertices.Add(pointB);
                            }

                            lines.Add(new Line(lineVertices, id1, id2, 0.2f, LineMaterial, DefaultInnerWallMaterial, DefaultOuterWallMaterial, DefaultSideMaterial));
                            lines[lines.Count - 1].Parent = this.transform;
                            pointASelected      = false;
                            DraggedLine.Enabled = false;
                            //                          DraggedLine.Destroy ();
                            //                          DraggedLine = null;

                            for (int i = 0; i < lines.Count; i++)
                            {
                                lines[i].Enabled = false;
                            }
                            regeneratePath(true);
                        }
                    }
                    else if (Input.GetMouseButtonDown(1) || (Input.GetMouseButton(0) && verticesSelected.Count != 0))
                    {
                        if (verticesSelected.Count == 0)
                        {
                            for (int i = 0; i < lines.Count; i++)
                            {
                                if (hit.point == lines[i].a)
                                {
                                    verticesSelected.Add(i * 2);
                                }
                                if (hit.point == lines[i].b)
                                {
                                    verticesSelected.Add(i * 2 + 1);
                                }
                            }
                        }
                        else
                        {
                            verticesSelected.Clear();
                        }
                    }
                }
            }

            if (Input.GetKeyDown(KeyCode.K))
            {
                //              List<Vector3> verts = new List<Vector3> ();
                //              for (int i = 0; i < lines.Count; i++)
                //              {
                //                  if (!verts.Contains (lines [i].a))
                //                      verts.Add (lines [i].a);
                //
                //                  if (!verts.Contains (lines [i].b))
                //                      verts.Add (lines [i].b);
                //              }

                //              List<Line> nlines = new List<Line> ();
                //              {
                //                  List<Vector3> vvv = new List<Vector3> ();
                //                  vvv.Add (new Vector3 (0, 0, 0));
                //                  vvv.Add (new Vector3 (0, 0, -1));
                //                  vvv.Add (new Vector3 (-1, 0, -1));
                //                  vvv.Add (new Vector3 (-1, 0, 0));
                //                  nlines.Add (new Line (vvv, 0, 1, 0.1f, LineMaterial, null, null, null));
                //                  nlines.Add (new Line (vvv, 1, 2, 0.1f, LineMaterial, null, null, null));
                //                  nlines.Add (new Line (vvv, 2, 3, 0.1f, LineMaterial, null, null, null));
                //                  nlines.Add (new Line (vvv, 3, 0, 0.1f, LineMaterial, null, null, null));
                //              }
                //



                //
                //              List<int> triangles;
                //              List<Vector3> vs;
                //              List<Vector2> uvs;
                //              List<Vector3> normals;
                //
                //              Line.FillCap (nlines, out triangles, out vs, out uvs, out normals);
                ////
                //              Mesh m = new Mesh ();
                //              m.vertices = vs.ToArray ();
                //              m.uv = uvs.ToArray ();
                //              m.triangles = triangles.ToArray ();
                //              m.normals = normals.ToArray ();

                //              GameObject go = new GameObject ("wal");
                //              go.AddComponent<MeshFilter> ().mesh = m;
                //              go.AddComponent<UpperWallFace> ().CreateFromLines (nlines);
                //              MeshRenderer mr = go.AddComponent<MeshRenderer> ();
                //              mr.material = LineMaterial;
            }
        }
    }
Example #6
0
    void regeneratePath(bool optimize)
    {
        Vector3 _selectedWallFaceA = Vector3.zero, _selectedWallFaceB = Vector3.zero;

        if (_selectedWallFace != null)
        {
            _selectedWallFaceA = _selectedWallFace.a;
            _selectedWallFaceB = _selectedWallFace.b;
        }

        for (int i = 0; i < wallFaces.Count; i++)
        {
            wallFaces[i].Destroy();
        }
        wallFaces.Clear();
        GameObject.Destroy(upperWallFace);
        upperWallFace = null;

        List <WallFace> outerWall;
        List <WallFace> doorSides;
        List <WallFace> innerWall;


        List <Mesh> floors;


        if (optimize)
        {
            Line.OptimizePath(ref lines);
        }

        Line.Generate3DWallFacesFromLines(lines, WallWireframeMaterial, WallSelectedMaterial, out outerWall, out doorSides, out innerWall, out upperWallFace, out floors);

        try{
            //		gggg (lines, WallWireframeMaterial, WallSelectedMaterial, out outerWall, out doorSides, out innerWall, out upperWallFace, out floors);


            for (int i = 0; i < this.floors.Count; i++)
            {
                GameObject.Destroy(this.floors[i]);
            }

            this.floors.Clear();
            this.floorColliders.Clear();

            for (int i = 0; i < floors.Count; i++)
            {
                GameObject floor = new GameObject("Room" + i.ToString() + "Floor");
                floor.transform.position += Vector3.up * 0.001f;
                floor.AddComponent <MeshFilter>().mesh       = floors[i];
                floor.AddComponent <MeshRenderer>().material = DefaultFloorMaterial;
                floorColliders.Add(floor.AddComponent <MeshCollider>());
                floorColliders[i].enabled = false;
                floor.transform.parent    = this.transform;
                this.floors.Add(floor);
            }

            if (Roof != null)
            {
                GameObject.Destroy(Roof);
                Roof = null;
            }

            Roof = new GameObject("roof");
            Roof.AddComponent <Roof>().CreateFromLines(lines, 0.4f, 0.4f);
            Roof.transform.parent = transform;
            Roof.GetComponent <MeshRenderer>().material = DefaultRoofMaterial;
            if (_viewingMode == ViewingMode.Interior)
            {
                Roof.SetActive(false);
            }
        }
        catch {
        }

        for (int i = 0; i < outerWall.Count; i++)
        {
            wallFaces.Add(outerWall[i]);
            wallFaces[wallFaces.Count - 1].Parent    = transform;
            wallFaces[wallFaces.Count - 1].Wireframe = !wallFaces[wallFaces.Count - 1].IsFacingCamera;
        }

        for (int i = 0; i < innerWall.Count; i++)
        {
            wallFaces.Add(innerWall[i]);
            wallFaces[wallFaces.Count - 1].Parent    = transform;
            wallFaces[wallFaces.Count - 1].Wireframe = !wallFaces[wallFaces.Count - 1].IsFacingCamera;
        }

        for (int i = 0; i < doorSides.Count; i++)
        {
            wallFaces.Add(doorSides[i]);
            wallFaces[wallFaces.Count - 1].Parent    = transform;
            wallFaces[wallFaces.Count - 1].Wireframe = !wallFaces[wallFaces.Count - 1].IsFacingCamera;
        }

        if (_selectedWallFace != null)
        {
            float dst = float.MaxValue;
            for (int i = 0; i < wallFaces.Count; i++)
            {
                float det1 = (wallFaces[i].a - _selectedWallFaceA).sqrMagnitude + (wallFaces[i].b - _selectedWallFaceB).sqrMagnitude;
                float det2 = (wallFaces[i].b - _selectedWallFaceA).sqrMagnitude + (wallFaces[i].a - _selectedWallFaceB).sqrMagnitude;
                if (det1 < dst)
                {
                    _selectedWallFace = wallFaces[i];
                    dst = det1;
                }
                if (det2 < dst)
                {
                    _selectedWallFace = wallFaces[i];
                    dst = det2;
                }
            }
            _selectedWallFace.Selected = true;
        }
    }