// Creates a Room given the specification of tiles. // The floor tile will be what covers the ground. // The walls tile will be what surrounds the room across the cardinal directions. // The door tile will be used in case this room is connected to other rooms. private void CreateGenericRoom(Vector3 unit, Vector3 topLeft, Vector3 bottomRight, GameObject room, string floor, string walls, string doors) { // Cache resources for later use. Object floorPrefab = Resources.Load(TILES_FOLDER + floor, typeof(GameObject)); Object wallPrefab = Resources.Load(TILES_FOLDER + walls, typeof(GameObject)); Object doorPrefab = Resources.Load(doors, typeof(GameObject)); Transform roomTransform = this.transform.Find(room.name); // Add a room script to this room. roomTransform.gameObject.AddComponent <Room>(); MapNode node = mapNodes[room.name] as MapNode; int columns = this.numberTileColums; int rows = this.numberTileRows; for (int i = 0; i < columns; i++) { float x = topLeft.x + (unit.x / 2) + (i * unit.x); float y = topLeft.y - (unit.y / 2); float z = topLeft.z; Vector3 position = new Vector3(x, y, z); GameObject instantiatedTile; // -------------------------------- // ---- Top Row Tile Placement ---- // -------------------------------- // if there is no room above, place a wall if (node.Up.Equals("")) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, else { bool hasDoorUp = stateManager.DoorBetween(room.name, node.Up); // if the room above has no door connecting to it, if (!hasDoorUp) { // if we're placing tiles in columns other than the center ones, place a wall tile. if (i < (columns / 2) - 1 || i > columns / 2 + 1) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, because there is no door, place a floor tile and make it a doorway else { instantiatedTile = Instantiate(floorPrefab, position, Quaternion.identity) as GameObject; instantiatedTile.AddComponent <Doorway>().Setup(Direction.North, node.Up); } } // otherwise, if the room above has a door connecting to it, else { // if we're placing tiles in columns other than the center ones, place a wall tile. if (i < (columns / 2) - 1 || i > columns / 2 + 1) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, we're adding a door in this location. else { instantiatedTile = Instantiate(doorPrefab, position, Quaternion.identity) as GameObject; if (i + 1 > columns / 2 + 1) { instantiatedTile.transform.Rotate(0, 180, 0); } instantiatedTile.name = stateManager.DoorName(room.name, node.Up); // Set where the door leads to. instantiatedTile.GetComponent <Door>().Setup(Direction.North, node.Up); stateManager.AddObjectToLocation(instantiatedTile, room.name); } } } // set the parent of the instantiated tile to be the room transform instantiatedTile.transform.parent = roomTransform; // set the tile's script and register it in the room roomTransform.GetComponent <Room>().SetTile(0, i, instantiatedTile); // -------------------------------- // -- Middle Rows Tile Placement -- // -------------------------------- bool hasDoorRight = stateManager.DoorBetween(room.name, node.Right); bool hasDoorLeft = stateManager.DoorBetween(room.name, node.Left); for (int j = 1; j < rows - 1; j++) { // Update the y coordinate of the tile, and the corresponding position. y = topLeft.y - (unit.y / 2) - (j * unit.y); position = new Vector3(x, y, z); instantiatedTile = null; // if we're at the first column and there is no room to the left, or // if we're at the last column and there is no room to the right, place a wall tile if ((i == 0 && node.Left.Equals("")) || (i > columns - 1.5 && node.Right.Equals(""))) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, if we're at the first column and there is a room on the left, or // if we're at the last column and there is a room on the right... else if ((i == 0 && !node.Left.Equals("")) || (i > columns - 1.5 && !node.Right.Equals(""))) { // if we're placing tiles in rows other than the center rows, place a wall tile if (j < (rows / 2) - 1 || j > rows / 2 + 1) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, else { // if we're at the first column and there is a door on the left, or // we're at the last column and there is a door on the right, // the tile to instantiate is a door if ((i == 0 && hasDoorLeft || (i > columns - 1.5 && hasDoorRight))) { instantiatedTile = Instantiate(doorPrefab, position, Quaternion.identity) as GameObject; // Rotate it, depending on whether we're placing a left door or a right door if (j + 1 > rows / 2 + 1) { instantiatedTile.transform.Rotate(0, 0, 90); } else { instantiatedTile.transform.Rotate(0, 0, 270); } // Set the name, depending on whether it is a left door or a right door if (i == 0) { // Set where the door leads to. instantiatedTile.name = stateManager.DoorName(room.name, node.Left); instantiatedTile.GetComponent <Door>().Setup(Direction.West, node.Left); } else { // Set where the door leads to. instantiatedTile.name = stateManager.DoorName(room.name, node.Right); instantiatedTile.GetComponent <Door>().Setup(Direction.East, node.Right); } // Add it as an object to the given location stateManager.AddObjectToLocation(instantiatedTile, room.name); } // otherwise, place a floor tile else { instantiatedTile = Instantiate(floorPrefab, position, Quaternion.identity) as GameObject; instantiatedTile.AddComponent <Doorway>(); if (i == 0) { instantiatedTile.GetComponent <Doorway>().Setup(Direction.West, node.Left); } else { instantiatedTile.GetComponent <Doorway>().Setup(Direction.East, node.Right); } } } } // otherwise, place a floor tile else { instantiatedTile = Instantiate(floorPrefab, position, Quaternion.identity) as GameObject; Destroy(instantiatedTile.GetComponent <BoxCollider2D>()); // remove colliders in the middle of the room } // set the parent of the instantiated tile to be the room transform instantiatedTile.transform.parent = roomTransform; // set the tile's script and register it in the room roomTransform.GetComponent <Room>().SetTile(j, i, instantiatedTile); } // -------------------------------- // -- Bottom Row Tile Placement --- // -------------------------------- // Once again, update the y position and the corresponding position y = bottomRight.y + (unit.y / 2); position = new Vector3(x, y, z); instantiatedTile = null; // if there is no room below, if (node.Down.Equals("")) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, if there is a room below, else { bool hasDoorDown = stateManager.DoorBetween(room.name, node.Down); // if there is no door between this room and the room below, if (!hasDoorDown) { // if we're placing tiles in columns other than the center ones, place a wall tile. if (i < (columns / 2) - 1 || i > columns / 2 + 1) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, place a floor else { instantiatedTile = Instantiate(floorPrefab, position, Quaternion.identity) as GameObject; instantiatedTile.AddComponent <Doorway>().Setup(Direction.South, node.Down); } } // otherwise, if there is a door between this room and the room below, else { // if we are not placing a tile in the middle columns, place a wall if (i < (columns / 2) - 1 || i > columns / 2 + 1) { instantiatedTile = Instantiate(wallPrefab, position, Quaternion.identity) as GameObject; } // otherwise, place a door else { instantiatedTile = Instantiate(doorPrefab, position, Quaternion.identity) as GameObject; if (i + 1 > columns / 2 + 1) { instantiatedTile.transform.Rotate(0, 180, 0); } instantiatedTile.name = stateManager.DoorName(room.name, node.Down); instantiatedTile.GetComponent <Door>().Setup(Direction.South, node.Down); stateManager.AddObjectToLocation(instantiatedTile, room.name); } } } // set the parent of the instantiated tile to be the room transform instantiatedTile.transform.parent = roomTransform; // set the tile's script and register it in the room roomTransform.GetComponent <Room>().SetTile(rows - 1, i, instantiatedTile); } }
private void CreateGeneric (Vector3 unit, Vector3 topLeft, Vector3 bottomRight, GameObject room, string floor, string walls, string doors) { MapNode node = map[room.name] as MapNode; float max = Mathf.Abs(topLeft.x - bottomRight.x) / unit.x; float maxY = Mathf.Abs(topLeft.y - bottomRight.y) / unit.y; GameObject thingGO = null; for (int i = 0; i < max; i++) { if (node.Up.Equals("")) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { if (!stateManager.DoorBetween(room.name, node.Up)) { if (i == 0 || i > (Mathf.Abs(topLeft.x - bottomRight.x) / unit.x) - 1.5) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { thingGO = Instantiate(Resources.Load(floor, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } } else { if (i < (max / 2) - 1 || i > max / 2 + 1 || ((i + 1) + 1 == max / 2 + 1)) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { thingGO = Instantiate(Resources.Load(doors, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; if (i + 1 > max / 2 + 1) thingGO.transform.Rotate(0, 180, 0); thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = stateManager.DoorName(room.name, node.Up); stateManager.AddObjectToLocation(thingGO, room.name); } } } bool doorRight = stateManager.DoorBetween(room.name, node.Right); bool doorLeft = stateManager.DoorBetween(room.name, node.Left); for (int j = 1; j < (Mathf.Abs(topLeft.y - bottomRight.y) / unit.y) - 1; j++) { if ((i == 0 && node.Left.Equals("")) || (i > (Mathf.Abs(topLeft.x - bottomRight.x) / unit.x) - 1.5 && node.Right.Equals(""))) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2) - (j * unit.y), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else if ((i == 0 && doorLeft) || (i > (Mathf.Abs(topLeft.x - bottomRight.x) / unit.x) - 1.5 && doorRight)) { if (j < (maxY / 2) - 1 || j > maxY / 2 + 1) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2) - (j * unit.y), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { thingGO = Instantiate(Resources.Load(doors, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2) - (j * unit.y), topLeft.z), Quaternion.identity) as GameObject; if (j + 1 > maxY / 2 + 1) thingGO.transform.Rotate (0, 0, 90); else thingGO.transform.Rotate (0, 0, 270); thingGO.transform.parent = this.transform.FindChild(room.name); if (i == 0) thingGO.name = stateManager.DoorName(room.name, node.Left); else thingGO.name = stateManager.DoorName(room.name, node.Right); stateManager.AddObjectToLocation(thingGO, room.name); } } else { thingGO = Instantiate(Resources.Load(floor, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), topLeft.y - (unit.y / 2) - (j * unit.y), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } } if (node.Down.Equals("")) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), bottomRight.y + (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { if (!stateManager.DoorBetween(room.name, node.Down)) { if (i == 0 || i > (Mathf.Abs(topLeft.x - bottomRight.x) / unit.x) - 1.5) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), bottomRight.y + (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { thingGO = Instantiate(Resources.Load(floor, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), bottomRight.y + (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } } else { if (i < (max / 2) - 1 || i > max / 2 + 1 || ((i + 1) + 1 == max / 2 + 1)) { thingGO = Instantiate(Resources.Load(walls, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), bottomRight.y + (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = thingGO.name + tileCount++; } else { thingGO = Instantiate(Resources.Load(doors, typeof(GameObject)), new Vector3 (topLeft.x + (unit.x / 2) + (i * unit.x), bottomRight.y + (unit.y / 2), topLeft.z), Quaternion.identity) as GameObject; if (i + 1 > max / 2 + 1) thingGO.transform.Rotate(0, 180, 0); thingGO.transform.parent = this.transform.FindChild(room.name); thingGO.name = stateManager.DoorName(room.name, node.Down); stateManager.AddObjectToLocation(thingGO, room.name); } } } } }