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
8
PuzzleGameProject/Assets/Scripts/DungeonGenerator.meta
Normal file
8
PuzzleGameProject/Assets/Scripts/DungeonGenerator.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b2976da120e9418ab6437c973c915dd7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -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
|
||||
|
|
@ -4,12 +4,13 @@ using TMPro;
|
|||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
|
||||
public abstract class Room : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private List<GameObject> adjacentRooms;
|
||||
[SerializeField] private bool isEntrance;
|
||||
[FormerlySerializedAs("adjacentRooms")] [SerializeField] public List<GameObject> AdjacentRooms;
|
||||
[SerializeField] public bool IsEntrance { get; set; }
|
||||
[SerializeField] protected RoomReward roomReward;
|
||||
public event EventHandler<Room> RoomExploredByDice;
|
||||
public static event Action<Room> RoomExploredByTorch;
|
||||
|
|
@ -40,7 +41,7 @@ public abstract class Room : MonoBehaviour
|
|||
InitializeRoom();
|
||||
_roomNumberOriginalColor = gameObject.GetComponentInChildren<TextMeshProUGUI>().color;
|
||||
}
|
||||
|
||||
|
||||
public bool GetRoomExplored()
|
||||
{
|
||||
return _isExplored;
|
||||
|
|
@ -49,7 +50,7 @@ public abstract class Room : MonoBehaviour
|
|||
public abstract void SetRoomExplored();
|
||||
|
||||
protected virtual void InitializeRoom() {
|
||||
if (isEntrance) {
|
||||
if (IsEntrance) {
|
||||
SetPropertiesOfEntrance();
|
||||
}
|
||||
_locks = gameObject.GetComponents<Lock>();
|
||||
|
|
@ -67,7 +68,7 @@ public abstract class Room : MonoBehaviour
|
|||
|
||||
void SetPropertiesOfEntrance() {
|
||||
gameObject.GetComponent<SpriteRenderer>().color = Color.green;
|
||||
isEntrance = true;
|
||||
IsEntrance = true;
|
||||
}
|
||||
|
||||
void OnMouseDown() {
|
||||
|
|
@ -113,7 +114,7 @@ public abstract class Room : MonoBehaviour
|
|||
return false;
|
||||
}
|
||||
|
||||
if (isEntrance) {
|
||||
if (IsEntrance) {
|
||||
// All entrance rooms are valid to be explored.
|
||||
return true;
|
||||
}
|
||||
|
|
@ -125,7 +126,7 @@ public abstract class Room : MonoBehaviour
|
|||
}
|
||||
|
||||
bool HasExploredAdjacentRooms() {
|
||||
foreach (GameObject adjacentRoom in adjacentRooms) {
|
||||
foreach (GameObject adjacentRoom in AdjacentRooms) {
|
||||
if (adjacentRoom.GetComponent<Room>()._isExplored)
|
||||
{
|
||||
return true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue