Example #1
0
    /*
     * //returns arraylist size 4 of neighbors to room
     * ArrayList findNeighborz(room room)
     * {
     *  Vector2 mid = new Vector2(room.x + room.width / 2, room.y + room.height / 2);
     *
     *
     *  ArrayList neighbors = new ArrayList(4);
     *  int[] closest_dist = new int[4];
     *  int count = 0;
     *  int closest = 1000;
     *  //ArrayList neighborz = new ArrayList(4);
     *  //neighborz.Add(new room());
     *
     *  for (int i = 0; i < rooms.Length; i++)
     *  {
     *      if (count >= 4)
     *      {
     *          break;
     *      }
     *      room check = rooms[i];
     *      if (check.index == room.index)
     *      {
     *          continue;
     *      }
     *
     *      Vector2 check_mid = new Vector2(check.x + check.width / 2, check.y + check.height / 2);
     *      int distance = (int)(Mathf.Abs(mid.x - check_mid.x) + Mathf.Abs(mid.y - check_mid.y));
     *
     *      if (distance < closest && check.neighbor != i ) //hard coded
     *      {
     *
     *          rooms[room.index].neighbor = i;
     *          rooms[i].neighbor = room.index;
     *          neighbors.Add(check);
     *          count++;
     *
     *          closest = distance;
     *          //closest = check;
     *      }
     *
     *  }
     *  return neighbors;
     * }
     */

    void createMap()
    {
        for (int i = 0; i < numRooms; i++) //for each room
        {
            room currentRoom = rooms[i];
            currentRoom.updateCenter();

            //create empty game object room to put tiles in
            GameObject room = new GameObject("room " + i);
            room.transform.position = currentRoom.center;
            room.transform.parent   = transform;

            currentRoom.roomObject = room;

            for (int x = currentRoom.x; x < currentRoom.x + currentRoom.width; x++)
            {
                for (int y = currentRoom.y; y < currentRoom.y + currentRoom.height; y++)
                {
                    //add tiles to empty game object
                    if (map[x, y] != 1)
                    {
                        map[x, y] = 1;
                        GameObject tile = (GameObject)Instantiate(ground, new Vector3(x, y, 5), Quaternion.identity);
                        tile.transform.parent = room.transform;
                    }
                }
            }
        }

        //draw wall tiles around rooms
        //can instantiate here and add to room gameobject if want to
        for (int x = 1; x < mapSize; x++)
        {
            for (int y = 1; y < mapSize; y++)
            {
                if (map[x, y] == 1)
                {
                    for (int xx = x - 1; xx <= x + 1; xx++)
                    {
                        for (int yy = y - 1; yy <= y + 1; yy++)
                        {
                            if (map[xx, yy] == 0 && map[xx, yy] != 3)
                            {
                                map[xx, yy] = 2;
                            }
                        }
                    }
                }
            }
        }
    }