Implemented loading dungeon map json into Unity.
This commit is contained in:
parent
0ff1e92fb9
commit
ec466ee6cd
82 changed files with 252177 additions and 1068 deletions
|
|
@ -0,0 +1,80 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ae0fded700e8a14439ad0c8179b3e7f5
|
||||
Loading…
Add table
Add a link
Reference in a new issue