-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunks.cs
172 lines (158 loc) · 5.93 KB
/
Chunks.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Filler;
namespace ROQWE
{
class Map
{
public List<Chunk> ModifiedChunks = new List<Chunk>();
public Map()
{
}
public void RemoveAt(IntVector3D pos)
{
RemoveAt(pos.X, pos.Y, pos.Z);
}
public void RemoveAt(IntVector pos, int layer)
{
RemoveAt(pos.X, pos.Y, layer);
}
public void RemoveAt(int x, int y, int z)
{
IntVector CC = new IntVector(x / Chunk.Size, y / Chunk.Size); //chunk in map coordinate
IntVector CR = new IntVector(x, y) % (Chunk.Size, Chunk.Size); //chunk relative to itself coordinate
int index = ModifiedChunks.FindIndex(X => X.ChunkCoordinate == CC);
if (index != -1)
{
ModifiedChunks[index].Write((CR.X, CR.Y, z), null);
}
}
public void Write(IntVector pos, int layer, char type, Cube pic, Stats stats)
{
Entity entity = new Entity(pos.X, pos.Y,layer, type, Guid.NewGuid(), pic, stats);
Write(entity);
}
public void Write(int x, int y, int z, char type, Cube pic, Stats stats)
{
Entity entity = new Entity(x, y,z, type, Guid.NewGuid(), pic, stats);
Write(entity);
}
public void Write(Entity entity)
{
IntVector CC = new IntVector(entity.X / Chunk.Size, entity.Y / Chunk.Size); //chunk in map coordinate
IntVector CR = entity.Position.XY % (Chunk.Size, Chunk.Size); //chunk relative to itself coordinate
int index = ModifiedChunks.FindIndex(X => X.ChunkCoordinate == CC);
if (index == -1)
{
Chunk Written = new Chunk(CC);
Written.Write((CR.X, CR.Y, entity.Z), entity);
ModifiedChunks.Add(Written);
}
else
{
ModifiedChunks[index].Write((CR.X,CR.Y, entity.Z), entity);
}
}
public char FindChar(IntVector3D position)
{
return FindChar(position.X, position.Y, position.Z);
}
public char FindChar(int x,int y, int z)
{
IntVector CC = new IntVector(x / Chunk.Size, y / Chunk.Size); //chunk in map coordinate
IntVector CR = new IntVector(x, y) % (Chunk.Size, Chunk.Size); //chunk relative to itself coordinate
Chunk Read = ModifiedChunks.Find(X => X.ChunkCoordinate == CC);
if(Read == null || Read.Read((CR.X, CR.Y, z)) == null)
{
return ' ';
}
else
{
char character = Read.Read((CR.X, CR.Y, z)).Type;
return character;
}
}
/// <summary>
/// returns a string of characters from the spcified XY coordinate,
/// </summary>
/// <param name="position"></param>
/// <returns></returns>
public string FindStr(IntVector position)
{
return FindStr(position.X, position.Y);
}
public string FindStr(int x, int y)
{
IntVector CC = new IntVector(x / Chunk.Size, y / Chunk.Size); //chunk in map coordinate
IntVector CR = new IntVector(x, y) % (Chunk.Size, Chunk.Size); //chunk relative to itself coordinate
Chunk Read = ModifiedChunks.Find(C => C.ChunkCoordinate == CC);
string returned = "";
if (Read == null)
{
return " ";
}
for (int z = 0; z < Chunk.Depth; z++)
{
if (Read.Read((CR.X, CR.Y, z)) == null)
{
returned += " ";
}
else
{
char character = Read.Read((CR.X, CR.Y, z)).Type;
returned += character;
}
}
return returned;
}
public Entity Find(IntVector3D position)
{
return Find(position.X, position.Y, position.Z);
}
public Entity Find(int x, int y, int z)
{
IntVector CC = new IntVector(x / Chunk.Size, y / Chunk.Size); //chunk in map coordinate
IntVector CR = new IntVector(x, y) % (Chunk.Size, Chunk.Size); //chunk relative to itself coordinate
Chunk Read = ModifiedChunks.Find(X => X.ChunkCoordinate == CC);
if (Read == null)
{
return new Entity(CR.X, CR.Y, z, ' ', Guid.NewGuid(), new Cube(), new Stats(1));
}
else
{
if(Read.Read((CR.X, CR.Y, z)) == null)
{
return new Entity(CR.X, CR.Y,z, ' ', Guid.NewGuid(), new Cube(), new Stats(1));
}
Entity character = Read.Read((CR.X, CR.Y, z));
return character;
}
}
}
class Chunk
{
public IntVector ChunkCoordinate { get; set; }
public static int Size = 16;
public static int Depth = 4;
public Entity[,,] ChunkData = new Entity[Size,Size,Depth];
public Chunk(IntVector chunkCoordinate)
{
ChunkCoordinate = chunkCoordinate;
}
public Chunk(int x,int y)
{
ChunkCoordinate = new IntVector(x,y);
}
public void Write(IntVector3D c, Entity entity)
{
ChunkData[c.X, c.Y, c.Z] = entity;
}
public Entity Read(IntVector3D c)
{
return ChunkData[c.X, c.Y, c.Z];
}
}
}