private void SpawnDoorTile(Vector2 position, int roomId) { GameObject tileGO = Instantiate(doorTileHolder, position, Quaternion.identity, initialRoomList[roomId].root.transform); tileGO.GetComponent <DungeonTile>().roomId = roomId; DungeonRoomData.Tile tile = new DungeonRoomData.Tile(tileGO); Collider2D collider2d = tileGO.AddComponent <BoxCollider2D>(); tile.collider2d = collider2d; initialRoomList[roomId].doorTileList.Add(tile); }
private void SpawnRoomGO() { for (int id = 0; id < initialRoomList.Count; id++) { GameObject roomRoot = new GameObject("roomRoot"); roomRoot.transform.position = initialRoomList[id].center; roomRoot.transform.SetParent(roomHolder.transform); // Spawn floor and wall for (int m = 0; m < initialRoomList[id].width; m += Constants.MapInfo.GridSize) { for (int n = 0; n < initialRoomList[id].height; n += Constants.MapInfo.GridSize) { Vector2 position = new Vector2(initialRoomList[id].center.x + (initialRoomList[id].width / 2 - m) * Constants.MapInfo.GridSize, initialRoomList[id].center.y + (initialRoomList[id].height / 2 - n) * Constants.MapInfo.GridSize); // If on edge int right = initialRoomList[id].width - Constants.MapInfo.GridSize; int bottom = initialRoomList[id].height - Constants.MapInfo.GridSize; if (m == 0 || m == right || n == 0 || n == bottom) { // If not the corners if (!((m == 0 && n == 0) || (m == 0 && n == bottom) || (m == right && n == 0) || (m == right && n == bottom))) { GameObject wallGO = Instantiate(wallTileHolder, position, Quaternion.identity, roomRoot.transform); wallGO.GetComponent <DungeonTile>().roomId = id; DungeonRoomData.Tile wallTile = new DungeonRoomData.Tile(wallGO); initialRoomList[id].wallTileList.Add(wallTile); } } else { GameObject floorGO = Instantiate(floorTileHolder, position, Quaternion.identity, roomRoot.transform); floorGO.GetComponent <DungeonTile>().roomId = id; DungeonRoomData.Tile floorTile = new DungeonRoomData.Tile(floorGO); initialRoomList[id].floorTileList.Add(floorTile); } } } BoxCollider2D collider2d = roomRoot.AddComponent <BoxCollider2D>(); collider2d.size = new Vector2(initialRoomList[id].width + 4 * Constants.MapInfo.GridSize, initialRoomList[id].height + 4 * Constants.MapInfo.GridSize); Rigidbody2D rigidbody2d = roomRoot.AddComponent <Rigidbody2D>(); rigidbody2d.gravityScale = 0; rigidbody2d.constraints = RigidbodyConstraints2D.FreezeRotation; initialRoomList[id].root = roomRoot; DungeonRoom dungeonRoom = roomRoot.AddComponent <DungeonRoom>(); dungeonRoom.roomId = initialRoomList[id].id; } }
private void ChangeDoorTileGO(int index, DungeonRoomData.Tile doorTile, DungeonRoomData room, int id) { if (index == 12) { index = 0; } else { index = 1; } GameObject doorTileGO = Instantiate(doorTileSO.tilePrefabList[index], doorTile.go.transform.position, Quaternion.identity, doorTile.go.transform.parent); doorTileGO.GetComponent <DungeonDoor>().roomId = room.doorTileList[id].go.GetComponent <DungeonDoor>().roomId; Destroy(room.doorTileList[id].go); room.doorTileList[id].go = doorTileGO; room.doorTileList[id].spriteRenderer = null; room.doorTileList[id].collider2d = doorTileGO.GetComponent <Collider2D>(); }
private void SpawnCorridorTile(Vector2 position, bool isWall, ref GameObject root, DungeonRoomData corridorRoom) { GameObject tileGO; Collider2D collider2d; if (isWall) { tileGO = Instantiate(wallTileHolder, position, Quaternion.identity, root.transform); tileGO.GetComponent <DungeonTile>().roomId = corridorRoom.id; DungeonRoomData.Tile tile = new DungeonRoomData.Tile(tileGO); collider2d = tileGO.AddComponent <BoxCollider2D>(); tile.collider2d = collider2d; corridorRoom.wallTileList.Add(tile); } else { tileGO = Instantiate(floorTileHolder, position, Quaternion.identity, root.transform); tileGO.GetComponent <DungeonTile>().roomId = corridorRoom.id; DungeonRoomData.Tile tile = new DungeonRoomData.Tile(tileGO); collider2d = tileGO.AddComponent <BoxCollider2D>(); tile.collider2d = collider2d; corridorRoom.floorTileList.Add(tile); } }
private void SetDoorTile() { RaycastHit2D[] hits = new RaycastHit2D[1]; int index = 0; int hitNum = 0; Physics2D.queriesStartInColliders = false; foreach (var room in dungeonGenerator.mainRoomList) { for (int i = 0; i < room.doorTileList.Count; i++) { DungeonRoomData.Tile doorTile = room.doorTileList[i]; int north = 0, south = 0, east = 0, west = 0; index = 0; hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.up, hits, Constants.MapInfo.GridSize); if (hitNum > 0) { if (hits[0].collider.GetComponent <DungeonWall>()) { north = 1; index += 1; } else if (hits[0].collider.GetComponent <DungeonDoor>()) { north = 2; index += 2; } } hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.left, hits, Constants.MapInfo.GridSize); if (hitNum > 0) { if (hits[0].collider.GetComponent <DungeonWall>()) { west = 1; index += 3; } else if (hits[0].collider.GetComponent <DungeonDoor>()) { west = 2; index += 6; } } hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.right, hits, Constants.MapInfo.GridSize); if (hitNum > 0) { if (hits[0].collider.GetComponent <DungeonWall>()) { east = 1; index += 9; } else if (hits[0].collider.GetComponent <DungeonDoor>()) { east = 2; index += 18; } } hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.down, hits, Constants.MapInfo.GridSize); if (hitNum > 0) { if (hits[0].collider.GetComponent <DungeonWall>()) { south = 1; index += 27; } else if (hits[0].collider.GetComponent <DungeonDoor>()) { south = 2; index += 54; } } // Destroy the floor tile underneath this door tile. Physics2D.queriesHitTriggers = false; Physics2D.queriesStartInColliders = true; hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.zero, hits); if (hitNum > 0 && hits[0].collider.GetComponent <DungeonFloor>()) { room.floorTileList.Remove(room.floorTileList.Find((obj) => obj.go == hits[0].collider.gameObject)); Destroy(hits[0].collider.gameObject); } Physics2D.queriesHitTriggers = true; Physics2D.queriesStartInColliders = false; // Exceptions switch (index) { case 2: hitNum = Physics2D.RaycastNonAlloc(doorTile.go.transform.position, Vector2.down, hits, Constants.MapInfo.GridSize); if (hitNum > 0) { index = 4; } break; case 12: case 56: ChangeDoorTileGO(index, doorTile, room, i); continue; } if (doorTileSO.tileSpriteList[index] == null) { Debug.LogFormat("No door sprites! north {0}, west {1}, east {2}, south {3}, index {4}, position {5}", north, west, east, south, index, doorTile.go.transform.position); continue; } doorTile.spriteRenderer.sprite = doorTileSO.tileSpriteList[index]; doorTile.collider2d.isTrigger = false; } } Physics2D.queriesStartInColliders = true; }