Ejemplo n.º 1
0
        public void StraightCorridorGeneration()
        {
            int i = 0;

            foreach (RoomConnectionLib rcl in roomPossibleConnections)
            {
                //Corridor corridor = new Corridor();
                //Get one of the door
                if (rcl.connections.Count != 0)
                {
                    int choosenIndex           = Random.Range(0, rcl.connections.Count);
                    RoomConnectionLib.Line con = rcl.connections[choosenIndex];
                    Door       dr1             = new Door();
                    Door       dr2             = new Door();
                    Vector2Int dr1p            = new Vector2Int(Mathf.RoundToInt(con.v1.x), Mathf.RoundToInt(con.v1.y));
                    Vector2Int dr2p            = new Vector2Int(Mathf.RoundToInt(con.v2.x), Mathf.RoundToInt(con.v2.y));

                    CalcDoorLocation(ref dr1, ref dr2, con);

                    if (con.v1 != con.v2)
                    {
                        //Create corridor
                        if (rcl.corridorDir == CorridorDir.Vertical)
                        {
                            dr1.corridor = new Rect(dr1p, new Vector2(corridorWidth, (con.v2.y - con.v1.y)));
                        }
                        else if (rcl.corridorDir == CorridorDir.Horizontal)
                        {
                            dr1.corridor = new Rect(dr1p, new Vector2((con.v2.x - con.v1.x), corridorWidth));
                        }
                    }

                    dr1.corridorDir = rcl.corridorDir;
                    dr2.corridorDir = rcl.corridorDir;

                    rcl.r1.doorPositions.Add(dr1);
                    rcl.r2.doorPositions.Add(dr2);

                    /*
                     * corridor.doorPosition[0] = new Vector2Int(Mathf.RoundToInt(con.v1.x - (con.direction.x < 0 ? 1 : 0)), Mathf.RoundToInt(con.v1.y - (con.direction.y < 0 ? 1 : 0)));
                     * corridor.doorPosition[1] = new Vector2Int(Mathf.RoundToInt(con.v2.x - (con.direction.x < 0 ? 1 : 0)), Mathf.RoundToInt(con.v2.y - (con.direction.y < 0 ? 1 : 0)));
                     *
                     * if (corridor.doorPosition[0] != corridor.doorPosition[1])
                     * {
                     *      //Create way corridor
                     *      if (rcl.corridorDir == RoomConnectionLib.CorridorDir.Vertical)
                     *              corridor.ways.Add(new Rect(corridor.doorPosition[0], new Vector2(corridorWidth, corridor.doorPosition[1].y - corridor.doorPosition[0].y)));
                     *      else if (rcl.corridorDir == RoomConnectionLib.CorridorDir.Horizontal)
                     *              corridor.ways.Add(new Rect(corridor.doorPosition[0], new Vector2(corridor.doorPosition[1].x - corridor.doorPosition[0].x, corridorWidth)));
                     * }*/
                }

                /*if (corridors.Count != roomPossibleConnections.Count)
                 *      corridors.Add(corridor);
                 * else
                 *      corridors[i] = corridor;*/
                i++;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="door1"></param>
        /// <param name="door2"></param>
        /// <param name="width">Corridor width</param>
        /// <param name="l1"></param>
        void CalcDoorLocation(ref Door door1, ref Door door2, RoomConnectionLib.Line l1, RoomConnectionLib.Line l2 = null)
        {
            if (l2 == null)
            {
                l2           = new RoomConnectionLib.Line(l1.v2, l1.v1);
                l2.direction = -l1.direction;
            }

            //setup door position
            door1.position = new Vector2Int(Mathf.RoundToInt(l1.v1.x), Mathf.RoundToInt(l1.v1.y));
            door2.position = new Vector2Int(Mathf.RoundToInt(l2.v1.x), Mathf.RoundToInt(l2.v1.y));
            if (l1.direction.x > 0)
            {
                door1.position -= Vector2Int.right;
            }
            else if (l1.direction.y > 0)
            {
                door1.position -= Vector2Int.up;
            }
            if (l2.direction.x > 0)
            {
                door2.position -= Vector2Int.right;
            }
            else if (l2.direction.y > 0)
            {
                door2.position -= Vector2Int.up;
            }

            //Setup door size
            if (l1.direction.y == 0)
            {
                door1.size = new Vector2Int(1, corridorWidth);
            }
            else if (l1.direction.x == 0)
            {
                door1.size = new Vector2Int(corridorWidth, 1);
            }
            if (l2.direction.y == 0)
            {
                door2.size = new Vector2Int(1, corridorWidth);
            }
            else if (l2.direction.x == 0)
            {
                door2.size = new Vector2Int(corridorWidth, 1);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="line">Resulted line after intersection</param>
        /// <param name="_rooms">List of rooms</param>
        /// <param name="exclude">Dont check this room</param>
        /// <param name="alsoCheckCorridoor"></param>
        /// <param name="reduction">Reduce length of the resulted "line". Use this to handle the corridorWidth.</param>
        /// <returns></returns>
        bool IsLineCollideWithRoomsOrCorridor(Vector2 start, Vector2 end, out RoomConnectionLib.Line line, List <Room> _rooms, Room exclude = null, bool alsoCheckCorridoor = false, float reduction = 0)
        {
            line = new RoomConnectionLib.Line(start, end);
            foreach (Room r in _rooms)
            {
                if (r == exclude)
                {
                    continue;
                }
                Vector2 p;
                if (r.rect.Intersect(start, end, out p))
                {
                    p      -= (end - start).normalized * (reduction - 0.1f);
                    line.v2 = p;
                    return(true);
                }
                foreach (Door d in r.doorPositions)
                {
                    Vector2 _p;
                    if (d.corridor.Intersect(start, end, out _p))
                    {
                        _p     -= (end - start).normalized * (reduction - 0.1f);
                        line.v2 = _p;
                        return(true);
                    }
                }
            }

            /*
             * if (alsoCheckCorridoor) {
             *      foreach (Corridor c in _corridors) {
             *              Vector2 p;
             *              if (c.Intersect(start, end, out p)) {
             *                      line.v2 = p;
             *                      return true;
             *              }
             *      }
             * }*/
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="r1">Room 1</param>
        /// <param name="r2">Room 2</param>
        /// <param name="width">Max Line Width</param>
        /// <param name="height">Max Line Height</param>
        /// <param name="_rooms"></param>
        /// <param name="rcl"></param>
        void CreateLConnectionLib(Room r1, Room r2, float width, float height, List <Room> _rooms, ref RoomConnectionLib rcl)
        {
            int startIndex = rcl.lConnections.Count;

            List <int> sizeEachColumn = new List <int>();

            //loop vertical (height from room 1)
            for (int y = 0; y < r1.rect.height; y++)
            {
                Vector2 start = new Vector2(r1.rect.x, y + r1.rect.y + 0.1f);
                if (width > 0)
                {
                    start.x = r1.rect.x + r1.rect.width - 0.1f;
                }
                Vector2 end = new Vector2(start.x + width + 0.5f * Mathf.Sign(width), start.y);
                RoomConnectionLib.Line l1;
                if (IsLineCollideWithRoomsOrCorridor(start, end, out l1, _rooms, r1, false, corridorWidth))
                {
                    //Make it imposible to do corridor
                    if (l1.Length < corridorWidth)
                    {
                        l1.v2 = l1.v1;
                    }
                    //if length less than 0.3f, just let it go
                    if (l1.Length < 0.3f)
                    {
                        continue;
                    }
                }
                else
                {
                    l1 = new RoomConnectionLib.Line(start, end);
                }
                l1.direction = (end - start).normalized;

                int w = 0;
                //loop horizontal (width from room 2)
                //find connection
                for (int x = 0; x < r2.rect.width; x++)
                {
                    start.x = r2.rect.x + x + 0.1f;
                    start.y = r2.rect.y + 0.1f;
                    if (height > 0)
                    {
                        start.y = r2.rect.y + r2.rect.height - 0.1f;
                    }
                    end.x = start.x;
                    end.y = start.y + height + 0.5f * Mathf.Sign(height);
                    RoomConnectionLib.Line l2;
                    if (IsLineCollideWithRoomsOrCorridor(start, end, out l2, _rooms, r2, false, corridorWidth))
                    {
                        //Make it imposible to do corridor
                        if (l2.Length < corridorWidth)
                        {
                            l2.v2 = l2.v1;
                        }
                        //if length less than 0.3f, just let it go
                        if (l2.Length < 0.3f)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        l2 = new RoomConnectionLib.Line(start, end);
                    }
                    l2.direction = (end - start).normalized;

                    //if ((r1.id == 7 || r1.id == 26) && (r2.id == 7 || r2.id == 26))
                    //{
                    //	debugLine.Add(l1);
                    //	//debugLine.Add(l2);
                    //}

                    //if 2 line intersection, then this will become corridor
                    Vector2 itsc;
                    if (LineSegmentsIntersection.Math2d.LineSegmentsIntersection(l2.v1, l2.v2, l1.v1, l1.v2, out itsc))
                    {
                        //ignore first connection, it will result good connection
                        if (w != 0)
                        {
                            RoomConnectionLib.LConnection lCon = new RoomConnectionLib.LConnection();
                            l1.v1.x = Mathf.Round(l1.v1.x);
                            l1.v1.y = Mathf.Round(l1.v1.y);
                            l2.v1.x = Mathf.Round(l2.v1.x);
                            l2.v1.y = Mathf.Round(l2.v1.y);
                            itsc.x  = Mathf.Round(itsc.x);
                            itsc.y  = Mathf.Round(itsc.y);

                            lCon.l1           = new RoomConnectionLib.Line(l1.v1, itsc);
                            lCon.l2           = new RoomConnectionLib.Line(l2.v1, itsc);
                            lCon.l1.direction = l1.direction;
                            lCon.l2.direction = l2.direction;
                            rcl.lConnections.Add(lCon);
                        }
                        w++;
                    }
                }
                //remove the last possible connection(width)
                for (int i = 0; i < corridorWidth; i++)
                {
                    if (rcl.lConnections.Count == 0 || w <= 1)
                    {
                        break;
                    }
                    rcl.lConnections.RemoveAt(rcl.lConnections.Count - 1);
                    w--;
                }
                if (w != 0)
                {
                    sizeEachColumn.Add(w - 1);
                }
            }

            //remove the last possible connection(height)
            for (int i = 0; i < corridorWidth; i++)
            {
                if (sizeEachColumn.Count - 1 - i < 0)
                {
                    break;
                }
                int del = sizeEachColumn[sizeEachColumn.Count - 1 - i];
                for (int j = 0; j < del; j++)
                {
                    if (rcl.lConnections.Count == 0)
                    {
                        break;
                    }
                    rcl.lConnections.RemoveAt(rcl.lConnections.Count - 1);
                }
            }

            //remove the first row, this will be resulting good connection
            if (sizeEachColumn.Count > 0)
            {
                for (int i = 0; i < sizeEachColumn[0]; i++)
                {
                    if (rcl.lConnections.Count <= startIndex)
                    {
                        break;
                    }
                    rcl.lConnections.RemoveAt(startIndex);
                }
            }
        }