Empeld
Empeld plugin documentation.
essentials.Prefabs.Prefab Class Reference

A subset of blocks within a world, that creates an existing entity Able to save and load from disk, clone pieces of the world, etc More...

Inheritance diagram for essentials.Prefabs.Prefab:
essentials.Prefabs.IPrefab essentials.Prefabs.IReadonlyPrefab

Public Member Functions

 Prefab (int width, int length, int height, Vector3i center)
 Creates a new instance of a prefab with the given width, length, and height More...
 
 Prefab (int width, int length, int height)
 Creates a new prefab More...
 
 Prefab (Size3i size, Vector3i center)
 Creates a new prefab More...
 
 Prefab (Size3i size)
 Creates a new prefab More...
 
void Save (Stream stream)
 Save prefab data to a stream (eg file) More...
 
void Clear (ushort id)
 Clear the prefab space with a specific block id More...
 
void Clear ()
 Clear the prefab space with empty block (id 0) More...
 
void Merge (IReadonlyPrefab src, Vector3i pos)
 Merge one prefab into self More...
 
- Public Member Functions inherited from essentials.Prefabs.IReadonlyPrefab
bool InBounds (int x, int y, int z)
 Returns whether or not a point is within the bounds of the prefab More...
 
bool InBounds (Vector3i pt)
 Returns whether or not a point is within the bounds of the prefab More...
 

Static Public Member Functions

static Prefab Load (Stream stream)
 Load prefab data from a data stream More...
 

Properties

int Width [get]
 Gets the width of the prefab More...
 
int Length [get]
 Gets the lenght of the prefab More...
 
int Height [get]
 Gets the height of the prefab More...
 
Size3i Size [get]
 Gets the size of the prefab (width, length, height) More...
 
Vector3i Center [get]
 Gets the relative center coordinate of the prefab (Not world coord) More...
 
- Properties inherited from essentials.Prefabs.IReadonlyPrefab
int Width [get]
 The width of the prefab More...
 
int Length [get]
 The length of the prefab More...
 
int Height [get]
 The height of the prefab More...
 
Size3i Size [get]
 The size of the prefab (width, length, height) More...
 
Vector3i Center [get]
 The relative center coordinate of the prefab More...
 

Detailed Description

A subset of blocks within a world, that creates an existing entity Able to save and load from disk, clone pieces of the world, etc

Exists in in its own space (Like a 3D Bitmap)

Constructor & Destructor Documentation

◆ Prefab() [1/4]

essentials.Prefabs.Prefab.Prefab ( int  width,
int  length,
int  height,
Vector3i  center 
)

Creates a new instance of a prefab with the given width, length, and height

Parameters
widthWidth.
lengthLength.
heightHeight.
centerCenter.

Attribute: width*length*height

;

81  {
82  this.InjectDependencies();
83 
84  _width = width;
85  _length = length;
86  _height = height;
87  _center = center;
88  _blocks = new ushort[width*length*height];
89  }

◆ Prefab() [2/4]

essentials.Prefabs.Prefab.Prefab ( int  width,
int  length,
int  height 
)

Creates a new prefab

Parameters
widthWidth.
lengthLength.
heightHeight.
98  :this(width, length, height, Vector3i.Zero)
99  {}
A class representing a 3D coordinate of integers
Definition: Vector3i.cs:13
static readonly Vector3i Zero
Vector (0,0,0)
Definition: Vector3i.cs:59

◆ Prefab() [3/4]

essentials.Prefabs.Prefab.Prefab ( Size3i  size,
Vector3i  center 
)

Creates a new prefab

Parameters
sizeSize.
centerCenter.
107  :this(size.Width, size.Length, size.Height, center)
108  {}
int Height
Definition: Size3i.cs:8
int Width
Definition: Size3i.cs:8
int Length
Definition: Size3i.cs:8

◆ Prefab() [4/4]

essentials.Prefabs.Prefab.Prefab ( Size3i  size)

Creates a new prefab

Parameters
sizeSize.
115  :this(size.Width, size.Length, size.Height, Vector3i.Zero)
116  {}
int Height
Definition: Size3i.cs:8
int Width
Definition: Size3i.cs:8
int Length
Definition: Size3i.cs:8
A class representing a 3D coordinate of integers
Definition: Vector3i.cs:13
static readonly Vector3i Zero
Vector (0,0,0)
Definition: Vector3i.cs:59

Member Function Documentation

◆ Clear() [1/2]

void essentials.Prefabs.Prefab.Clear ( ushort  id)

Clear the prefab space with a specific block id

Parameters
idIdentifier.

Attribute: i

= id;

210  {
211  for (int i=0; i<_blocks.Length; ++i)
212  {
213  _blocks[i] = id;
214  }
215  }

◆ Clear() [2/2]

void essentials.Prefabs.Prefab.Clear ( )

Clear the prefab space with empty block (id 0)

221  {
222  Clear(0);
223  }
void Clear()
Clear the prefab space with empty block (id 0)
Definition: Prefab.cs:220

◆ Load()

static Prefab essentials.Prefabs.Prefab.Load ( Stream  stream)
static

Load prefab data from a data stream

Parameters
streamStream.

Attribute: i

= reader.ReadUInt16();

Attribute: pos

= data;

125  {
126  var reader = new BinaryReader(stream);
127  string magic = reader.ReadString();
128  if (magic != MAGIC)
129  {
130  throw new FileLoadException("Invalid magic");
131  }
132 
133  byte version = reader.ReadByte();
134  if (version != 0)
135  {
136  throw new FileLoadException("Invalid version");
137  }
138 
139  int width = reader.ReadInt32();
140  int length = reader.ReadInt32();
141  int height = reader.ReadInt32();
142  int cX = reader.ReadInt32();
143  int cY = reader.ReadInt32();
144  int cZ = reader.ReadInt32();
145 
146  var prefab = new Prefab(width, length, height, new Vector3i(cX,cY,cZ));
147  for (int i=0; i<width*length*height; ++i)
148  {
149  prefab._blocks[i] = reader.ReadUInt16();
150  }
151 
152  if (stream.Length > stream.Position) //Backwards compat
153  {
154  //There's metadata!
155  int labelCount = reader.ReadInt32();
156  for (int i=0; i<labelCount; ++i)
157  {
158  Vector3i pos;
159  pos.X = reader.ReadInt32();
160  pos.Y = reader.ReadInt32();
161  pos.Z = reader.ReadInt32();
162  string data = reader.ReadString();
163  prefab._labels[pos] = data;
164  }
165  }
166 
167  return prefab;
168  }
static readonly Vector3i
All directions, including diagnols
Definition: Vector3i.cs:111
Prefab(int width, int length, int height, Vector3i center)
Creates a new instance of a prefab with the given width, length, and height
Definition: Prefab.cs:80
int Y
The Y coordinate
Definition: Vector3i.cs:23
A class representing a 3D coordinate of integers
Definition: Vector3i.cs:13
int X
The X coordinate
Definition: Vector3i.cs:18
int Z
The Z coordinate
Definition: Vector3i.cs:28

◆ Merge()

void essentials.Prefabs.Prefab.Merge ( IReadonlyPrefab  src,
Vector3i  pos 
)

Merge one prefab into self

Parameters
srcSource.
posPosition.

Attribute: myPos] = src[x,y,z

;

231  {
232  for (int x=0; x<src.Width; ++x)
233  {
234  for (int y=0; y<src.Length; ++y)
235  {
236  for (int z=0; z<src.Height; ++z)
237  {
238  Vector3i myPos = new Vector3i(x,y,z) - src.Center + pos;
239  if (myPos.X >= 0 && myPos.X < _width
240  && myPos.Y >= 0 && myPos.Y < _length
241  && myPos.Z >= 0 && myPos.Z < _height)
242  {
243  this[myPos] = src[x,y,z];
244  }
245  }
246  }
247  }
248  }
static readonly Vector3i
All directions, including diagnols
Definition: Vector3i.cs:111
int Y
The Y coordinate
Definition: Vector3i.cs:23
A class representing a 3D coordinate of integers
Definition: Vector3i.cs:13
int X
The X coordinate
Definition: Vector3i.cs:18
int Z
The Z coordinate
Definition: Vector3i.cs:28

◆ Save()

void essentials.Prefabs.Prefab.Save ( Stream  stream)

Save prefab data to a stream (eg file)

Parameters
streamStream.

Attribute: i

);

175  {
176  var writer = new BinaryWriter(stream);
177  writer.Write(MAGIC);
178  writer.Write((byte)0);
179 
180  writer.Write(_width);
181  writer.Write(_length);
182  writer.Write(_height);
183 
184  writer.Write(_center.X);
185  writer.Write(_center.Y);
186  writer.Write(_center.Z);
187 
188  for (int i=0; i<_width*_length*_height; ++i)
189  writer.Write(_blocks[i]);
190 
191  writer.Write((int)this._labels.Count);
192  foreach(var label in this._labels)
193  {
194  writer.Write(label.Key.X);
195  writer.Write(label.Key.Y);
196  writer.Write(label.Key.Z);
197  writer.Write(label.Value);
198  }
199  }
int Y
The Y coordinate
Definition: Vector3i.cs:23
int X
The X coordinate
Definition: Vector3i.cs:18
int Z
The Z coordinate
Definition: Vector3i.cs:28

Property Documentation

◆ Center

Vector3i essentials.Prefabs.Prefab.Center
get

Gets the relative center coordinate of the prefab (Not world coord)

The center.

◆ Height

int essentials.Prefabs.Prefab.Height
get

Gets the height of the prefab

The height.

◆ Length

int essentials.Prefabs.Prefab.Length
get

Gets the lenght of the prefab

The length.

◆ Size

Size3i essentials.Prefabs.Prefab.Size
get

Gets the size of the prefab (width, length, height)

The size.

◆ Width

int essentials.Prefabs.Prefab.Width
get

Gets the width of the prefab

The width.


The documentation for this class was generated from the following file: