108 lines
4.7 KiB
C#
108 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DungeonMapGenerator;
|
|
using Unity.VisualScripting;
|
|
using UnityEditor;
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
|
namespace DungeonGenerator
|
|
{
|
|
public class DungeonMapLoader
|
|
{
|
|
private const string ROOMS_PARENT = "RoomsParent";
|
|
private const string MONSTER_ROOM = "Rooms\\MonsterRoom";
|
|
private const string BOSS_ROOM = "Rooms\\BossRoom";
|
|
private const string NORMAL_ROOM = "Rooms\\Room";
|
|
|
|
|
|
private Dictionary<int, GameObject> _roomIdToGameObject = new Dictionary<int, GameObject>();
|
|
|
|
public void AddDungeonRoomsToGameObject(GameObject gameObject, string pathToDungeonFile)
|
|
{
|
|
DungeonMap map = DungeonMapSerializer.DeserializeFromFile(pathToDungeonFile);
|
|
GameObject bossRoomPrefab = Resources.Load<GameObject>(BOSS_ROOM);
|
|
GameObject monsterRoomPrefab = Resources.Load<GameObject>(MONSTER_ROOM);
|
|
GameObject normalRoomPrefab = Resources.Load<GameObject>(NORMAL_ROOM);
|
|
|
|
GameObject bossRoomGO = PrefabUtility.InstantiatePrefab(bossRoomPrefab, gameObject.transform) as GameObject;
|
|
bossRoomGO.transform.position = ConvertToUnityPosition(map.GetBossRoom().GetCenterOfRoom(), map.Width, map.Height);
|
|
AddLockToRoomObject(bossRoomGO, map.GetBossRoom().Lock.GetLock());
|
|
_roomIdToGameObject[map.GetBossRoom().Id] = bossRoomGO;
|
|
|
|
foreach (var monsterRoom in map.GetMonsterRooms())
|
|
{
|
|
GameObject monsterRoomGO = PrefabUtility.InstantiatePrefab(monsterRoomPrefab, gameObject.transform) as GameObject;
|
|
monsterRoomGO.transform.position = ConvertToUnityPosition(monsterRoom.GetCenterOfRoom(), map.Width, map.Height);
|
|
AddLockToRoomObject(monsterRoomGO, monsterRoom.Lock.GetLock());
|
|
_roomIdToGameObject[monsterRoom.Id] = monsterRoomGO;
|
|
}
|
|
|
|
foreach (var normalRoom in map.GetNormalRooms())
|
|
{
|
|
GameObject normalRoomGO = PrefabUtility.InstantiatePrefab(normalRoomPrefab, gameObject.transform) as GameObject;
|
|
normalRoomGO.transform.position = ConvertToUnityPosition(normalRoom.GetCenterOfRoom(), map.Width, map.Height);
|
|
AddLockToRoomObject(normalRoomGO, normalRoom.Lock.GetLock());
|
|
_roomIdToGameObject[normalRoom.Id] = normalRoomGO;
|
|
}
|
|
|
|
foreach (var entranceRoom in map.GetEntranceRooms())
|
|
{
|
|
GameObject entranceRoomGO = PrefabUtility.InstantiatePrefab(normalRoomPrefab, gameObject.transform) as GameObject;
|
|
entranceRoomGO.transform.position = ConvertToUnityPosition(entranceRoom.GetCenterOfRoom(), map.Width, map.Height);
|
|
entranceRoomGO.GetComponent<Room>().IsEntrance = true;
|
|
AddLockToRoomObject(entranceRoomGO, entranceRoom.Lock.GetLock());
|
|
_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);
|
|
}
|
|
|
|
private void AddLockToRoomObject(GameObject room, string lockString)
|
|
{
|
|
NumberLock _lock;
|
|
switch (lockString)
|
|
{
|
|
case "2":
|
|
case "3":
|
|
case "4":
|
|
case "5":
|
|
case "6":
|
|
case "7":
|
|
case "8":
|
|
case "9":
|
|
case "10":
|
|
case "11":
|
|
case "12":
|
|
_lock = room.AddComponent<NumberLock>();
|
|
_lock.SetNumber(Int32.Parse(lockString));
|
|
break;
|
|
case "=":
|
|
room.AddComponent<MatchingDiceLock>();
|
|
break;
|
|
case null:
|
|
Debug.Log("Lock for room was null when imported. Using 1 as lock");
|
|
_lock = room.AddComponent<NumberLock>();
|
|
_lock.SetNumber(1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|