-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cs
162 lines (147 loc) · 3.91 KB
/
Block.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
using UnityEngine;
using System.Collections.Generic;
namespace Uzu
{
/// <summary>
/// All system block types.
///
/// To customize in the application:
///
/// 1). For applications that only need two block types
/// (empty and solid), just use Uzu.BlockType as-is,
///
/// 2). For applications that have more than two block types,
/// new block types can be defined like so:
///
/// public enum MyBlockType : byte
/// {
/// MY_BLOCK_TYPE_0 = Uzu.BlockType.USER_START_VALUE,
/// MY_BLOCK_TYPE_1,
/// ...
/// }
///
/// The EMPTY block type will always be defined by the system.
/// If user block types are defined, the default system SOLID
/// block type will be overwritten by the first user block type.
/// </summary>
public enum BlockType : byte
{
/// <summary>
/// An empty block.
/// </summary>
EMPTY = 0,
/// <summary>
/// A basic non-empty block.
/// </summary>
SOLID,
/// <summary>
/// The number of blocks used by the system by
/// default if no user block types are defined.
/// </summary>
SYSTEM_DEFAULT_COUNT,
/// <summary>
/// The first user-defined block type start value.
/// </summary>
USER_START_VALUE = EMPTY + 1,
};
/// <summary>
/// A block description.
/// One description is stored for each block type.
/// </summary>
public class BlockDesc
{
public Material Material { get; set; }
/// <summary>
/// Allows user-specified ignore faces to be set on a per-block-type basis.
/// For example, if BlockFaceFlag.Back is specified, all blocks of this
/// type will have their back faces (z-axis) ignored.
/// Depending on the type of game, this is useful if the movement directions
/// of the player are limited.
/// </summary>
public BlockFaceFlag IgnoreFaces { get; set; }
}
/// <summary>
/// A single block as represented in the block world.
/// Beward of adding more members to this, as memory usage will explode ^3.
/// </summary>
public class Block
{
public BlockType Type { get; set; }
public Color32 Color { get; set; }
/// <summary>
/// Is this block an "EMPTY" block type?
/// </summary>
public bool IsEmpty { get { return Type == BlockType.EMPTY; } }
}
/// <summary>
/// Helper class for managing configurations of blocks.
/// </summary>
public class BlockContainer
{
public BlockContainer (VectorI3 countXYZ)
{
_countXYZ = countXYZ;
_count = VectorI3.ElementProduct (_countXYZ);
_countYZ = _countXYZ.y * _countXYZ.z;
_blocks = new Block[_count];
// Initialize.
for (int i = 0; i < _count; i++) {
_blocks [i] = new Block ();
}
}
public VectorI3 CountXYZ {
get { return _countXYZ; }
}
public int Count {
get { return _count; }
}
public Block this [int i] {
get { return _blocks [i]; }
set { _blocks [i] = value; }
}
/// <summary>
/// Allows indexing into the container using (x,y,z) tuple.
/// Indexing via a tuple requires the calculation of the index into
/// the buffer. Because of this overhead, use flat indexing (blocks[i])
/// whenever possible.
/// </summary>
public Block this [int x, int y, int z] {
get {
int index = x * _countYZ + y * _countXYZ.z + z;
return _blocks [index];
}
set {
int index = x * _countYZ + y * _countXYZ.z + z;
_blocks [index] = value;
}
}
/// <summary>
/// Resets all blocks in this container
/// to their 'empty' state.
/// </summary>
public void ResetAllBlocks ()
{
for (int i = 0; i < _count; i++) {
_blocks [i].Type = BlockType.EMPTY;
}
}
private Block[] _blocks;
private VectorI3 _countXYZ;
private int _count;
private int _countYZ;
}
/// <summary>
/// Used for ignoring block faces, and other
/// operation the require definition of block (cube) faces.
/// </summary>
[System.Flags]
public enum BlockFaceFlag : byte
{
Front = 1 << 0,
Back = 1 << 1,
Right = 1 << 2,
Left = 1 << 3,
Top = 1 << 4,
Bottom = 1 << 5,
}
}