-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSimpleDungeonGenerator.cs
141 lines (117 loc) · 4.51 KB
/
SimpleDungeonGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
using UnityEngine.Tilemaps;
public class SimpleDungeonGenerator : MonoBehaviour
{
[SerializeField]
private Vector2Int roomSize = new Vector2Int(10, 10);
[SerializeField]
private Tilemap roomMap, colliderMap;
[SerializeField]
private TileBase roomFloorTile, pathFloorTile;
[SerializeField]
private InputActionReference generate;
public UnityEvent OnFinishedRoomGeneration;
public static List<Vector2Int> fourDirections = new()
{
Vector2Int.up,
Vector2Int.right,
Vector2Int.down,
Vector2Int.left
};
private DungeonData dungeonData;
private void Awake()
{
dungeonData = FindObjectOfType<DungeonData>();
if (dungeonData == null)
dungeonData = gameObject.AddComponent<DungeonData>();
generate.action.performed += Generate;
}
private void Generate(InputAction.CallbackContext obj)
{
dungeonData.Reset();
dungeonData.Rooms.Add(
GenerateRectangularRoomAt(Vector2.zero, roomSize));
dungeonData.Rooms.Add(
GenerateRectangularRoomAt(Vector2Int.zero + Vector2Int.right * 15, roomSize));
dungeonData.Rooms.Add(
GenerateRectangularRoomAt(Vector2Int.zero + Vector2Int.down * 15, roomSize));
dungeonData.Path.UnionWith(
CreateStraightCorridor(Vector2Int.zero, Vector2Int.zero + Vector2Int.right * 15));
dungeonData.Path.UnionWith(
CreateStraightCorridor(Vector2Int.zero, Vector2Int.zero + Vector2Int.down * 15));
GenerateDungeonCollider();
OnFinishedRoomGeneration?.Invoke();
}
private void GenerateDungeonCollider()
{
//create a hahset that contains all the tiles that represent the dungeon
HashSet<Vector2Int> dungeonTiles = new HashSet<Vector2Int>();
foreach (Room room in dungeonData.Rooms)
{
dungeonTiles.UnionWith(room.FloorTiles);
}
dungeonTiles.UnionWith(dungeonData.Path);
//Find the outline of the dungeon that will be our walls / collider aound the dungeon
HashSet<Vector2Int> colliderTiles = new HashSet<Vector2Int>();
foreach (Vector2Int tilePosition in dungeonTiles)
{
foreach (Vector2Int direction in fourDirections)
{
Vector2Int newPosition = tilePosition + direction;
if(dungeonTiles.Contains(newPosition) == false)
{
colliderTiles.Add(newPosition);
continue;
}
}
}
foreach (Vector2Int pos in colliderTiles)
{
colliderMap.SetTile((Vector3Int)pos, roomFloorTile);
}
}
private Room GenerateRectangularRoomAt(Vector2 roomCenterPosition, Vector2Int roomSize)
{
Vector2Int half = roomSize / 2;
HashSet<Vector2Int> roomTiles = new();
//Generate the room around the roomCenterposition
for (int x = -half.x; x < half.x; x++)
{
for (int y = -half.y; y < half.y; y++)
{
Vector2 position = roomCenterPosition + new Vector2(x, y);
Vector3Int positionInt = roomMap.WorldToCell(position);
roomTiles.Add((Vector2Int)positionInt);
roomMap.SetTile(positionInt, roomFloorTile);
}
}
return new Room(roomCenterPosition, roomTiles);
}
private HashSet<Vector2Int> CreateStraightCorridor(Vector2Int startPostion,
Vector2Int endPosition)
{
//Create a hashset and add start and end positions to it
HashSet<Vector2Int> corridorTiles = new();
corridorTiles.Add(startPostion);
roomMap.SetTile((Vector3Int)startPostion, pathFloorTile);
corridorTiles.Add(endPosition);
roomMap.SetTile((Vector3Int)endPosition, pathFloorTile);
//Find the direction of the straight line
Vector2Int direction
= Vector2Int.CeilToInt(((Vector2)endPosition - startPostion).normalized);
Vector2Int currentPosition = startPostion;
//Add all tiles until we reach the end position
while (Vector2.Distance(currentPosition, endPosition) > 1)
{
currentPosition += direction;
corridorTiles.Add(currentPosition);
roomMap.SetTile((Vector3Int)currentPosition, pathFloorTile);
}
return corridorTiles;
}
}