80 lines
3.1 KiB
C#
80 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DungeonMapGenerator;
|
|
using Unity.VisualScripting;
|
|
|
|
|
|
namespace DungeonGenerator
|
|
{
|
|
public class DungeonMapLoader : MonoBehaviour
|
|
{
|
|
[SerializeField] private string pathToDungeonMap;
|
|
[SerializeField] private GameObject roomsParent;
|
|
[SerializeField] private GameObject monsterRoomPrefab;
|
|
[SerializeField] private GameObject bossRoomPrefab;
|
|
[SerializeField] private GameObject normalRoomPrefab;
|
|
|
|
private Dictionary<int, GameObject> roomIdToGameObject = new Dictionary<int, GameObject>();
|
|
|
|
private void Start()
|
|
{
|
|
DungeonMap map = DungeonMapSerializer.DeserializeFromFile(pathToDungeonMap);
|
|
|
|
GameObject bossRoomGO = Instantiate(bossRoomPrefab,
|
|
ConvertToUnityPosition(map.GetBossRoom().GetCenterOfRoom(), map.Width, map.Height),
|
|
Quaternion.identity,
|
|
roomsParent.transform);
|
|
roomIdToGameObject[map.GetBossRoom().Id] = bossRoomGO;
|
|
|
|
foreach (var monsterRoom in map.GetMonsterRooms())
|
|
{
|
|
GameObject monsterRoomGO = Instantiate(
|
|
monsterRoomPrefab,
|
|
ConvertToUnityPosition(monsterRoom.GetCenterOfRoom(), map.Width, map.Height),
|
|
Quaternion.identity,
|
|
roomsParent.transform);
|
|
roomIdToGameObject[monsterRoom.Id] = monsterRoomGO;
|
|
}
|
|
|
|
foreach (var normalRoom in map.GetNormalRooms())
|
|
{
|
|
GameObject normalRoomGO = Instantiate(
|
|
normalRoomPrefab,
|
|
ConvertToUnityPosition(normalRoom.GetCenterOfRoom(), map.Width, map.Height),
|
|
Quaternion.identity,
|
|
roomsParent.transform);
|
|
roomIdToGameObject[normalRoom.Id] = normalRoomGO;
|
|
}
|
|
|
|
foreach (var entranceRoom in map.GetEntranceRooms())
|
|
{
|
|
GameObject entranceRoomGO = Instantiate(
|
|
normalRoomPrefab,
|
|
ConvertToUnityPosition(entranceRoom.GetCenterOfRoom(), map.Width, map.Height),
|
|
Quaternion.identity,
|
|
roomsParent.transform);
|
|
entranceRoomGO.GetComponent<Room>().IsEntrance = true;
|
|
roomIdToGameObject[entranceRoom.Id] = entranceRoomGO;
|
|
}
|
|
|
|
foreach (var mapRoom in map.GetAllRooms())
|
|
{
|
|
HashSet<int> adjacentRoomsIds = mapRoom.GetAdjacentRoomIds();
|
|
Room roomComponent = roomIdToGameObject[mapRoom.Id].GetComponent<Room>();
|
|
foreach (var id in adjacentRoomsIds)
|
|
{
|
|
roomComponent.AdjacentRooms.Add(roomIdToGameObject[id]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private Vector3 ConvertToUnityPosition(Point dungeonPosition, int mapWidth, int mapHeight)
|
|
{
|
|
float newX = (dungeonPosition.X - (mapWidth / 2f)) / 2;
|
|
float newY = -(dungeonPosition.Y - (mapHeight / 2f)) / 2;
|
|
|
|
return new Vector3(newX, newY, 0f);
|
|
}
|
|
}
|
|
}
|