/
TilemapVisualizer.cs
151 lines (134 loc) · 5.01 KB
/
TilemapVisualizer.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
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using ProceduralGeneration;
public class TilemapVisualizer : MonoBehaviour
{
[SerializeField]
private Tilemap floorTilemap, wallTilemap;
// DThe different tile sprites from the tile map
[SerializeField]
private TileBase floorTile, wallTop, wallSideRight, wallSideLeft, wallBottom, wallFull,
wallInnerCornerDownLeft, wallInnerCornerDownRight,
wallDiagonalCornerDownRight, wallDiagonalCornerDownLeft,
wallDiagonalCornerUpRight, wallDiagonalCornerUpLeft;
public void Start()
{
Clear();
}
// Paint a single floor tile at a specified position
public void PaintFloor(Vector2Int position)
{
PaintSingleTile(floorTilemap, floorTile, position);
}
// iterates through all tiles and paint each one on the screen
internal void PaintTiles(List<DungeonTile> tiles)
{
foreach(DungeonTile tile in tiles)
{
if (tile.Type == TileType.Floor)
PaintFloor(tile.Position);
else
PaintWall(tile.Position, tile.Type);
}
}
// renders the correct wall tile from the tilemap based on the tile type
internal void PaintWall(Vector2Int position, TileType type)
{
TileBase tile = null; // the type of tile to be painted
switch(type)
{
case TileType.Top: tile = wallTop;
break;
case TileType.SideLeft: tile = wallSideLeft;
break;
case TileType.SideRight: tile = wallSideRight;
break;
case TileType.Bottom: tile = wallBottom;
break;
case TileType.InnerCornerDownLeft: tile = wallInnerCornerDownLeft;
break;
case TileType.InnerCornerDownRight: tile = wallInnerCornerDownRight;
break;
case TileType.DiagonalCornerDownLeft: tile = wallDiagonalCornerDownLeft;
break;
case TileType.DiagonalCornerDownRight: tile = wallDiagonalCornerDownRight;
break;
case TileType.DiagonalCornerUpLeft: tile = wallDiagonalCornerUpLeft;
break;
case TileType.DiagonalCornerUpRight: tile = wallDiagonalCornerUpRight;
break;
case TileType.WallFull: tile = wallFull;
break;
}
// if the tile has been assigned paint it
if (tile != null)
PaintSingleTile(wallTilemap, tile, position);
}
/*
// Renders all tiles in a given list of positions
private void PaintTiles(IEnumerable<Vector2Int> positions, Tilemap tilemap, TileBase tile)
{
foreach (var position in positions)
{
PaintSingleTile(tilemap, tile, position);
}
}*/
// Adds a single tile to the tilemap using specified parameters
private void PaintSingleTile(Tilemap tilemap, TileBase tile, Vector2Int position)
{
var tilePosition = tilemap.WorldToCell((Vector3Int)position);
tilemap.SetTile(tilePosition, tile);
}
// removes all tiles previous generated in the tilemap
public void Clear()
{
floorTilemap.ClearAllTiles();
wallTilemap.ClearAllTiles();
}
/*
internal void PaintSingleCornerWall(Vector2Int position, string binaryType)
{
int typeAsInt = Convert.ToInt32(binaryType, 2); // converts string of binary numbers into an integer
TileBase tile = null; // the type of tile to be painted
// checks if the matching binary number is in any of the lists in the wall type helper and
// if it is, sets the tile to that wall type
if (WallTypesHelper.wallInnerCornerDownLeft.Contains(typeAsInt))
{
tile = wallInnerCornerDownLeft;
}
else if (WallTypesHelper.wallInnerCornerDownRight.Contains(typeAsInt))
{
tile = wallInnerCornerDownRight;
}
else if (WallTypesHelper.wallDiagonalCornerDownLeft.Contains(typeAsInt))
{
tile = wallDiagonalCornerDownLeft;
}
else if (WallTypesHelper.wallDiagonalCornerDownRight.Contains(typeAsInt))
{
tile = wallDiagonalCornerDownRight;
}
else if (WallTypesHelper.wallDiagonalCornerUpRight.Contains(typeAsInt))
{
tile = wallDiagonalCornerUpRight;
}
else if (WallTypesHelper.wallDiagonalCornerUpLeft.Contains(typeAsInt))
{
tile = wallDiagonalCornerUpLeft;
}
else if (WallTypesHelper.wallFullEightDirections.Contains(typeAsInt))
{
tile = wallFull;
}
else if (WallTypesHelper.wallBottomEightDirections.Contains(typeAsInt))
{
tile = wallBottom;
}
// if the tile has been assigned paint it
if (tile != null)
PaintSingleTile(wallTilemap, tile, position);
}*/
}