forked from RonenNess/UnityUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileType.cs
215 lines (189 loc) · 7.82 KB
/
TileType.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Represent a tile type you can attach to tiles.
* Mostly contain metadata about a tile and how to build it.
*
* Author: Ronen Ness.
* Since: 2017.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
namespace NesScripts.Tilemap
{
/// <summary>
/// Different tile categories.
/// </summary>
public enum TileCategory
{
/// <summary>
/// Regular, walkable, floor tiles.
/// </summary>
Floor,
/// <summary>
/// Walls / blocking tiles.
/// </summary>
Wall,
/// <summary>
/// Water / other liquid tiles you can swim in.
/// </summary>
Water,
/// <summary>
/// Endless pit tiles.
/// </summary>
Pit,
/// <summary>
/// 'Null' tiles, eg nothing there. Use this for internal stuff and editor.
/// </summary>
Null,
}
/// <summary>
/// Define the API for a tile type.
/// </summary>
[System.Serializable]
public class TileType
{
// a dictionary with all tile type instances
static internal Dictionary<string, TileType> _types = new Dictionary<string, TileType>();
/// <summary>
/// Get tile type by identifier (must match class name).
/// </summary>
/// <param name="typeName">Tile type identifier.</param>
public static TileType GetType(string typeName) {
// try to get if already loaded
TileType instance;
if (_types.TryGetValue (typeName, out instance))
{
return instance;
}
// if got here it means this tile type is not loaded yet. get type from name.
var assembly = Assembly.GetExecutingAssembly();
var type = Array.Find(assembly.GetTypes(), t => t.Name == typeName);
// instanciate it
instance = Activator.CreateInstance(type) as TileType;
// add to dictionary and return
_types[typeName] = instance;
return instance;
}
/// <summary>
/// Gets the tile category.
/// </summary>
/// <value>The tile type category.</value>
virtual public TileCategory TileCategory { get {return TileCategory.Null;} }
/// <summary>
/// Only if true will try to build other tile parts, using the GetXxxPart() functions.
/// </summary>
/// <value><c>true</c> if use dynamic parts build; otherwise, <c>false</c>.</value>
virtual public bool UseDynamicPartsBuild { get { return false; } }
/// <summary>
/// When a tile is built and its front (positive Z) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "front part" of this tile.
/// For example, if your tile is a wall, this will be its front.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The front part for this tile. Note: will be cloned, not used directly.</value>
virtual public GameObject GetFrontPart(Tile self, Tile neighbor) {
return null;
}
/// <summary>
/// When a tile is built and its back (negative Z) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "back part" of this tile.
/// For example, if your tile is a wall, this will be its back.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The back part for this tile. Note: will be cloned, not used directly.</value>
virtual public GameObject GetBackPart(Tile self, Tile neighbor) {
return null;
}
/// <summary>
/// When a tile is built and its left (negative X) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "left part" of this tile.
/// For example, if your tile is a wall, this will be its left size.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The left part for this tile. Note: will be cloned, not used directly.</value>
virtual public GameObject GetLeftPart(Tile self, Tile neighbor) {
return null;
}
/// <summary>
/// When a tile is built and its right (positive X) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "right part" of this tile.
/// For example, if your tile is a wall, this will be its right size.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The right part for this tile. Note: will be cloned, not used directly.</value>
virtual public GameObject GetRightPart(Tile self, Tile neighbor) {
return null;
}
}
/// <summary>
/// Define a basic null tile type for testing.
/// </summary>
public class TileType_Null : TileType
{
}
/// <summary>
/// An basic tile with a constant type of walls to merge.
/// </summary>
public class TileType_WallsExample : TileType
{
/// <summary>
/// Only if true will try to build other tile parts, using the GetXxxPart() functions.
/// </summary>
/// <value>true</value>
override public bool UseDynamicPartsBuild { get { return true; } }
/// <summary>
/// The path of the prefab we use for walls for this tile type (under Resources folder).
/// </summary>
virtual protected string WallResourcePath { get { return "World/Prefabs/Tiles/Wall"; } }
/// <summary>
/// When a tile is built and its front (positive Z) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "front part" of this tile.
/// For example, if your tile is a wall, this will be its front.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The front part for this tile. Note: will be cloned, not used directly.</value>
override public GameObject GetFrontPart(Tile self, Tile neighbor) {
return (GameObject)Resources.Load(WallResourcePath, typeof(GameObject));
}
/// <summary>
/// When a tile is built and its back (negative Z) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "back part" of this tile.
/// For example, if your tile is a wall, this will be its back.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The back part for this tile. Note: will be cloned, not used directly.</value>
override public GameObject GetBackPart(Tile self, Tile neighbor) {
return (GameObject)Resources.Load(WallResourcePath, typeof(GameObject));
}
/// <summary>
/// When a tile is built and its left (negative X) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "left part" of this tile.
/// For example, if your tile is a wall, this will be its left size.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The left part for this tile. Note: will be cloned, not used directly.</value>
override public GameObject GetLeftPart(Tile self, Tile neighbor) {
return (GameObject)Resources.Load(WallResourcePath, typeof(GameObject));
}
/// <summary>
/// When a tile is built and its right (positive X) neighbor is of a different type, this function will be called.
/// If returns a GameObject and not null, will use this prefab as the "right part" of this tile.
/// For example, if your tile is a wall, this will be its right size.
///
/// Note: handle positioning and rotation. Your prefab is assumed to be facing forward.
/// </summary>
/// <value>The right part for this tile. Note: will be cloned, not used directly.</value>
override public GameObject GetRightPart(Tile self, Tile neighbor) {
return (GameObject)Resources.Load(WallResourcePath, typeof(GameObject));
}
}
}