public Ch0nk(Dimension dimension, Vector3i position, IMaterial material) { _dimension = dimension; _position = position; _eightFoldTree = new EightFoldTree(Size, material); //_eightFoldTree[0, 0, 0] = new SandMaterial(); }
public List<Block> GetRandomTestingBlocks(Vector3i startingPosition) { List<Block> blocks = new List<Block>(); PerlinNoise noise = new PerlinNoise(99); Ch0nk c = new Ch0nk(this, startingPosition, new GrassMaterial()); for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { for (int k = 0; k < 64; k++) { double d = noise.Noise(i + 0.5, j + 0.5, k + 0.5); //Console.WriteLine(d); IMaterial material = new GrassMaterial(); if (d > 0.5) material = new GrassMaterial(); else if (d > 0.0) material = new SandMaterial(); else if (d > -0.5) material = new StoneMaterial(); blocks.Add(new Block(c, new Vector3b(i, j, k), material, 1)); } } } return blocks; }
/// <summary> /// Loads a chunk from a certain location /// </summary> /// <param name="location"></param> /// <returns></returns> public static Ch0nk LoadCh0nk(Vector3i location) { byte[] arrBytes = File.ReadAllBytes(GetFileName(location)); MemoryStream memStream = new MemoryStream(); BinaryFormatter binForm = new BinaryFormatter(); memStream.Write(arrBytes, 0, arrBytes.Length); memStream.Seek(0, SeekOrigin.Begin); return (Ch0nk)binForm.Deserialize(memStream); }
private static bool DoesCubeIntersectSphere(Vector3i min, Vector3i max, Vector3i center, float radius) { float dist_squared = radius * radius; /* assume C1 and C2 are element-wise sorted, if not, do that now */ if (center.X < min.X) dist_squared -= Squared(center.X - min.X); else if (center.X > max.X) dist_squared -= Squared(center.X - max.X); if (center.Y < min.Y) dist_squared -= Squared(center.Y - min.Y); else if (center.Y > max.Y) dist_squared -= Squared(center.Y - max.Y); if (center.Z < min.Z) dist_squared -= Squared(center.Z - min.Z); else if (center.Z > max.Z) dist_squared -= Squared(center.Z - max.Z); return dist_squared > 0; }
protected override void GenerateBlock(Vector3i startLocation) { if (startLocation.X + startLocation.Y > 5 && startLocation.Z <= 0) { _chonks.Add(startLocation, new Ch0nk(this, startLocation, new StoneMaterial())); } else if(startLocation.Z <= 0) { _chonks.Add(startLocation, new Ch0nk(this, startLocation, new GrassMaterial())); } else { _chonks.Add(startLocation, new Ch0nk(this, startLocation, new AirMaterial())); } }
public void GenerateAt(Vector3i center) { Vector3i centerChonkLocation = GetLowestEnclosingCh0nkLocation(center); //Vector3i centerChonkLocation = new Vector3i(center.X / Ch0nk.Size, center.Y / Ch0nk.Size, center.Y / Ch0nk.Size); for(int i=-1; i <= 1;i++) { for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { Vector3i neighbourCh0Nk = new Vector3i(centerChonkLocation.X + i * Ch0nk.Size, centerChonkLocation.Y + j * Ch0nk.Size, centerChonkLocation.Z + k * Ch0nk.Size); if (!_chonks.ContainsKey(neighbourCh0Nk)) GenerateBlock(neighbourCh0Nk); } } } }
public IMaterial this[Vector3i vectorLocation] { get { if (_children == null || _middle == 0) return _material; EightFoldTree childTree = _children[vectorLocation.X / _middle, vectorLocation.Y / _middle, vectorLocation.Z / _middle]; if (childTree == null) return _material; return childTree[vectorLocation.X % _middle, vectorLocation.Y % _middle, vectorLocation.Z % _middle]; //Vector3i[] vectors = GetIndexAndLocation(vectorLocation); //return _children[vectors[0].X, vectors[0].Y, vectors[0].Z][vectors[1]]; } set { if (Size == 1) _material = value; else { //if there are no children, expand now if (_children == null) //Expand(); _children = new EightFoldTree[2, 2, 2]; //determine the child tree index Vector3i location = new Vector3i(vectorLocation.X / _middle, vectorLocation.Y / _middle, vectorLocation.Z / _middle); //if that index has not yet been allocated, do it now if (_children[location.X, location.Y, location.Z] == null) _children[location.X, location.Y, location.Z] = new EightFoldTree(_middle, _material); _children[location.X, location.Y, location.Z][vectorLocation.X % _middle, vectorLocation.Y % _middle, vectorLocation.Z % _middle] = value; //Vector3i[] vectors = GetIndexAndLocation(vectorLocation); //_children[vectors[0].X, vectors[0].Y, vectors[0].Z][vectors[1]] = value; } } }
private Vector3i GetLowestEnclosingCh0nkLocation(Vector3i center) { int x = ((center.X / Ch0nk.Size)) * Ch0nk.Size; int y = ((center.Y / Ch0nk.Size)) * Ch0nk.Size; int z = ((center.Z / Ch0nk.Size)) * Ch0nk.Size; return new Vector3i(x <= center.X ? x : x - Ch0nk.Size, y <= center.Y ? y : y - Ch0nk.Size, z <= center.Z ? z : z - Ch0nk.Size); }
protected abstract void GenerateBlock(Vector3i startLocation);
public BoundingBox(Vector3i min, int size) { _min = min; _max = min + size; }
public float DistanceTo(Vector3i b) { float dx = _x - b._x; float dy = _y - b._y; float dz = _z - b._z; return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz); }
private static String GetFileName(Vector3i location) { return Ch0nksFolder + "Ch0nk(" + location.X + "," + location.Y + "," + location.Z + ").ch0nk"; }
public LocalizedEightFoldTree(Vector3i location, EightFoldTree tree) { Location = location; Tree = tree; }
/// <summary> /// Returns all trees with a certain size /// </summary> /// <param name="startLocation"></param> /// <param name="size"></param> /// <returns></returns> public List<LocalizedEightFoldTree> GetLocalizedTreesOfSize(Vector3i startLocation, short size) { if (Size == size || _children == null) return new List<LocalizedEightFoldTree>(new[] { new LocalizedEightFoldTree(startLocation, this) }); //otherwise, iterate over all the children List<LocalizedEightFoldTree> eightFoldTrees = new List<LocalizedEightFoldTree>(); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) eightFoldTrees.AddRange(_children[i, j, k].GetLocalizedTreesOfSize(startLocation + new Vector3i(i * _middle, j * _middle, k * _middle), size)); return eightFoldTrees; }
public BoundingSphere(Vector3i center, int radius) { _center = center; _radius = radius; }
public static float Distance(Vector3i a, Vector3i b) { float dx = a._x - b._x; float dy = a._y - b._y; float dz = a._z - b._z; return (float)Math.Sqrt(dx * dx + dy * dy + dz * dz); }
public BoundingBox(Vector3i min, Vector3i max) { _min = min; _max = max; }
public bool Equals(Vector3i other) { return other._x == _x && other._y == _y && other._z == _z; }
public BoundingCube(Vector3i min, int size) : base(min, size) { }