/
AbstractDungeonGenerator.cs
97 lines (75 loc) · 2.23 KB
/
AbstractDungeonGenerator.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ProceduralGeneration;
public struct DungeonTile
{
public DungeonTile(int x, int y, TileType type)
{
position = new Vector2Int(x, y);
this.type = type;
}
// properties
public Vector2Int Position { get => position; }
public TileType Type { get => type; }
Vector2Int position;
TileType type;
}
public abstract class AbstractDungeonGenerator : MonoBehaviour
{
[SerializeField]
protected TilemapVisualizer tilemapVisualizer = null;
[SerializeField]
protected Vector2Int startPosition = Vector2Int.zero;
[SerializeField]
protected int dungeonWidth = 200;
[SerializeField]
protected int dungeonHeight = 200;
protected DungeonGenerator dungeon;
protected HashSet<Vector2Int> floorPositions;
protected HashSet<Vector2Int> wallPositions;
private void Awake()
{
floorPositions = new HashSet<Vector2Int>();
wallPositions = new HashSet<Vector2Int>();
}
// entry point
public virtual void GenerateDungeon()
{
tilemapVisualizer.Clear();
if (floorPositions != null)
floorPositions.Clear();
else
floorPositions = new HashSet<Vector2Int>();
if(wallPositions != null)
wallPositions.Clear();
else
wallPositions = new HashSet<Vector2Int>();
Generate();
}
public void ClearDungeon()
{
tilemapVisualizer.Clear();
}
public abstract void Generate();
protected List<DungeonTile> GetTileData()
{
TileType[,] mapValues = dungeon.GetMap();
List<DungeonTile> tiles = new List<DungeonTile>();
for (int y = 0; y < mapValues.GetLength(0); y++)
{
for (int x = 0; x < mapValues.GetLength(1); x++)
{
if (mapValues[y, x] != TileType.Empty)
tiles.Add(new DungeonTile(x, y, mapValues[y, x]));
}
}
return tiles;
}
public void GenerateTiles()
{
List<DungeonTile> tiles = GetTileData(); // get the tile positions and types
tilemapVisualizer.Clear();
tilemapVisualizer.PaintTiles(tiles);
}
}