2using System.Collections.Generic;
4using UnityEngine.Tilemaps;
11 [CreateAssetMenu(menuName =
"Mania Map/Map Tile Set")]
20 private int _pixelsPerUnit = 16;
24 public int PixelsPerUnit {
get => _pixelsPerUnit;
set => _pixelsPerUnit = value; }
27 private Vector2Int _tileSize =
new Vector2Int(16, 16);
31 public Vector2Int
TileSize {
get => _tileSize;
set => _tileSize = value; }
43 private Texture2D _northWall;
47 public Texture2D
NorthWall {
get => _northWall;
set => _northWall = value; }
50 private Texture2D _southWall;
54 public Texture2D
SouthWall {
get => _southWall;
set => _southWall = value; }
57 private Texture2D _westWall;
61 public Texture2D
WestWall {
get => _westWall;
set => _westWall = value; }
64 private Texture2D _eastWall;
68 public Texture2D
EastWall {
get => _eastWall;
set => _eastWall = value; }
73 private Texture2D _northDoor;
77 public Texture2D
NorthDoor {
get => _northDoor;
set => _northDoor = value; }
80 private Texture2D _southDoor;
84 public Texture2D
SouthDoor {
get => _southDoor;
set => _southDoor = value; }
87 private Texture2D _westDoor;
91 public Texture2D
WestDoor {
get => _westDoor;
set => _westDoor = value; }
94 private Texture2D _eastDoor;
98 public Texture2D
EastDoor {
get => _eastDoor;
set => _eastDoor = value; }
101 private Texture2D _topDoor;
105 public Texture2D
TopDoor {
get => _topDoor;
set => _topDoor = value; }
108 private Texture2D _bottomDoor;
112 public Texture2D
BottomDoor {
get => _bottomDoor;
set => _bottomDoor = value; }
117 private Texture2D _grid;
121 public Texture2D
Grid {
get => _grid;
set => _grid = value; }
126 private Texture2D _savePoint;
130 public Texture2D
SavePoint {
get => _savePoint;
set => _savePoint = value; }
133 private List<FeatureMapTile> _featureTiles =
new List<FeatureMapTile>();
137 public List<FeatureMapTile>
FeatureTiles {
get => _featureTiles;
set => _featureTiles = value; }
142 private Dictionary<string, Texture2D>
Textures {
get; } =
new Dictionary<string, Texture2D>();
147 private Dictionary<string, long>
FeatureFlags {
get; } =
new Dictionary<string, long>();
152 private Dictionary<long, string>
FeatureNames {
get; } =
new Dictionary<long, string>();
157 private Dictionary<MapTileKey, Tile>
Tiles {
get; } =
new Dictionary<MapTileKey, Tile>();
162 public bool IsDirty {
get;
private set; } =
true;
164 private void OnValidate()
212 Textures.Add(feature.Feature, feature.Tile);
250 if (
string.IsNullOrWhiteSpace(name))
251 throw new System.ArgumentException($
"Invalid feature name: {name}.");
257 throw new System.ArgumentException($
"Feature count exceeded. Cannot add feature: {name}.");
274 if (
string.IsNullOrWhiteSpace(name))
276 if (
Textures.TryGetValue(name, out Texture2D tile))
290 if (
string.IsNullOrWhiteSpace(name))
317 public Tile
GetTile(
long flags, Color32 color)
321 if (!
Tiles.TryGetValue(key, out Tile tile))
324 Tiles.Add(key, tile);
337 var tile = CreateInstance<Tile>();
339 sprite.name =
"Mania Map Tile Sprite";
340 tile.sprite = sprite;
341 tile.name =
"Mania Map Tile";
351 var pivot =
new Vector2(0.5f, 0.5f);
352 var rect =
new Rect(1, 1, texture.width - 2, texture.height - 2);
353 var sprite = Sprite.Create(texture, rect, pivot,
PixelsPerUnit);
354 sprite.name =
"Mania Map Tile Sprite";
382 for (
long i = flags; i != 0; i &= i - 1)
384 var flag = ~(i - 1) & i;
Contains the map tiles used to draw maps for a Layout.
string GetFeatureName(long flag)
Returns the feature name corresponding to the specified flag if it exists. Returns null otherwise.
Dictionary< MapTileKey, Tile > Tiles
A dictionary of map tiles by map tile keys.
long GetFeatureFlag(string name)
Returns the feature flag corresponding to the feature name if it exists. Returns zero otherwise.
Dictionary< string, long > FeatureFlags
A dictionary of feature flags by feature name.
void PopulateFeatureFlags()
Populates the feature flag dictionaries.
Tile CreateTile(long flags, Color32 color)
Returns a new tile with the specified feature flags and background color.
const int MaxFeatureCount
The maximum number of features that can be managed by the set.
Texture2D Grid
The tile used for the LayoutMap grid (optional).
Texture2D NorthWall
The superinposed tile when a north wall exists.
Texture2D BottomDoor
The superimposed tile when a bottom door exists.
Texture2D SavePoint
The tile used for save point features (optional).
Texture2D CreateFeatureTexture(long flags, Color32 color)
Creates a new texture with the specified features and background color. The returned texture has 1 pi...
void PopulateIfDirty()
Populates the map tile set textures and feature flags if it is dirty.
Sprite CreateSprite(Texture2D texture)
Creates a new sprite with the specified texture. The texture should include 1 pixel of padding.
Dictionary< long, string > FeatureNames
A dictionary of feature names by flag.
Texture2D WestWall
The superimposed tile when a west wall exists.
Texture2D NorthDoor
The superimposed tile when a north door exists.
Dictionary< string, Texture2D > Textures
A dictionary of referenced tiles.
Texture2D TopDoor
The superimposed tile when a top door exists.
Texture2D GetTexture(string name)
Returns the texture corresponding to the feature name if it exists. Returns null otherwise.
Texture2D EastDoor
The superimposed tile when an east door exists.
List< FeatureMapTile > FeatureTiles
A list of cell feature tiles.
void DrawFeatures(Texture2D texture, long flags)
Draws the feature tiles for the specified flags onto a texture.
int PixelsPerUnit
The number of pixels per unit for each tile.
void PopulateTextures()
Populates the textures dictionary.
Texture2D SouthWall
The superinposed tile when a south wall exists.
Vector2Int TileSize
The width and height of each tile.
Tile GetTile(long flags, Color32 color)
Returns the map tile with the specified feature flags and background color. Creates the tile if it do...
Texture2D SouthDoor
The superimposed tile when a south door exists.
Texture2D EastWall
The superimposed tile when an east wall exists.
FilterMode FilterMode
The map tile texture filter mode.
bool AddFeatureFlag(string name)
Adds the specified feature to the feature dictionaries. Returns false if the feature already exists.
void MarkDirty()
Sets the map tile set as dirty.
Texture2D WestDoor
The superimposed tile when a west door exists.
bool IsDirty
True if the map tile set is dirty and requires update.
Contains methods for manipulating textures.
static void Fill(Texture2D texture, Color32 color)
Fills the texture with the specified color.
static void FillBorder(Texture2D texture)
Fills the 1 pixel border around the texture with the colors at a 1 pixel inset.
static void DrawImage(Texture2D texture, Texture2D brush, Vector2Int point)
Draws the brush texture at the specified point.
A structure containing map tile type flags and an associated color.