public void QueueAddChunks(int loadDistance) { chunkLoadQueue.Clear(); float ld2 = loadDistance * loadDistance; ChunkIndex playerLoc = world.Player.ChunkID; for (int y = -loadDistance; y < loadDistance; y++) { for (int x = -loadDistance; x < loadDistance; x++) { for (int z = -loadDistance; z < loadDistance; z++) { float d = x * x + y * y + z * z; if (d < ld2) { ChunkIndex index = new ChunkIndex(playerLoc.X + x, playerLoc.Y + y, playerLoc.Z + z, world.Size); lock (chunkLoadingList) { if (!Chunks.ContainsKey(index) && !chunkLoadingList.Contains(index)) { chunkLoadQueue.Enqueue(index, d); } } } } } } }
private void addMeshDataForExposedRanges(List <Range1D> exposedRanges, Direction camFacingDir, ref int starting_tri_i, int xx, int zz) { ChunkIndex targetBlockIndex; foreach (Range1D rng in exposedRanges) { int blockY = rng.start; // while(blockY < rng.extent()) { targetBlockIndex = new ChunkIndex(xx, blockY, zz); if (blockY == Range1D.theErsatzNullRange().start) { throw new Exception("this range was ersatz nullish " + rng.toString()); } Block b = m_noisePatch.blockAtChunkCoordOffset(chunkCoord, new Coord(xx, blockY, zz)); // addYFaceAtChunkIndex(targetBlockIndex, b.type, camFacingDir, starting_tri_i); // old way with the while loop (now each range is one quad) addYFaceAtChunkIndex(targetBlockIndex, b.type, camFacingDir, starting_tri_i, rng.range); starting_tri_i += 4; // blockY++; // } } }
public void GenerateChunkNonThreaded(Chunk chunk, ChunkIndex chunkIndex, NoiseSettings noiseSettings, bool perlin) { var generated = GenerateNew(chunk, chunkIndex, noiseSettings); chunk.ApplyBlocks(generated.blocks); chunk.ApplyMesh(generated.vertices, generated.uvs, generated.triangles); }
public void UnloadChunks(int n) { for (int i = 0; i < Math.Min(n, chunkUnloadQueue.Count); i++) { ChunkIndex ci = chunkUnloadQueue.Dequeue(); if (!Chunks.ContainsKey(ci)) { i--; } else { if (Chunks.TryRemove(ci, out Chunk chunk)) { if (!chunkPool.PutObject(chunk)) { chunk.CleanUp(); } } else { Console.WriteLine("Failed to remove chunk"); } } } }
public RequestedChunk(ChunkIndex chunkIndex, NoiseSettings noiseSettings, Action <ChunkIndex, Chunk> onInstantiateCallback, bool perlin) { this.chunkIndex = chunkIndex; this.noiseSettings = noiseSettings; OnInstantiateCallback = onInstantiateCallback; this.perlin = perlin; }
public EnemyManager( float xPosition, float yPosition, EnemyType enemyType, EnemyAI aI, float moveSpeed, float aoe, int maxHealth, float currentHealth, ChunkIndex currentChunk, float damage, float agroDistance, float deAgroDistance, float minAgroDuration) { XPosition = xPosition; YPosition = yPosition; EnemyType = enemyType; AI = aI; MoveSpeed = moveSpeed; Aoe = aoe; MaxHealth = maxHealth; CurrentHealth = currentHealth; CurrentChunk = currentChunk; Damage = damage; AgroDistance = agroDistance; DeAgroDistance = deAgroDistance; MinAgroDuration = minAgroDuration; Team = 1; }
void addYFaceAtChunkIndex(ChunkIndex ci, BlockType bType, Direction dir, int tri_index, int height) { Vector3[] verts = new Vector3[] {}; int[] tris = new int[] {}; Vector2[] uvs; int shift = (int)dir % 2 == 1 ? -1 : 1; Direction oppositeDir = (Direction)((int)dir + shift); uvs = uvCoordsForBlockType(bType, oppositeDir); int[] posTriangles = new int[] { 0, 2, 3, 0, 1, 2 }; // clockwise when looking from pos towards neg int[] negTriangles = new int[] { 0, 3, 2, 0, 2, 1 }; // the opposite tris = (int)(oppositeDir) % 2 == 0 ? posTriangles : negTriangles; for (int ii = 0; ii < tris.Length; ++ii) { tris [ii] += tri_index; } verts = faceMesh(oppositeDir, ci, (float)(height - 1)); //third param = extra hieght. TODO: change this silly implementation // vertices_list.AddRange (verts); // // 6 triangles (index_of_so_far + 0/1/2, 0/2/3 <-- depending on the dir!) // triangles_list.AddRange (tris); // // 4 uv coords // uvcoords_list.AddRange (uvs); }
Vector3[] faceMesh(Direction d, ChunkIndex ci, float extra_height) // Vector3[] verts, int[] triangles) { float x0, x1, x2, x3, y0, y1, y2, y3, z0, z1, z2, z3; int dir = (int)d; float halfunit = .5f; bool negDir = dir % 2 == 1; float shift = halfunit; if (negDir) { shift = -halfunit; } // Vector3 f = new Vector3 (0, 0, 0); // Vector3[] ff = new Vector3[] { f, f, f, f, }; //fake if (d <= Direction.xneg) { //return ff; x0 = x1 = x2 = x3 = ci.x + shift; y1 = y2 = ci.y + halfunit + extra_height; y0 = y3 = ci.y - halfunit; z0 = z1 = ci.z - halfunit; z2 = z3 = ci.z + halfunit; } else if (d <= Direction.yneg) { y0 = y1 = y2 = y3 = ci.y + shift; x0 = x3 = ci.x + halfunit; x1 = x2 = ci.x - halfunit; z0 = z1 = ci.z - halfunit; z2 = z3 = ci.z + halfunit; } else { //return ff; z0 = z1 = z2 = z3 = ci.z + shift; x0 = x1 = ci.x + halfunit; x2 = x3 = ci.x - halfunit; y1 = y2 = ci.y + halfunit + extra_height; y0 = y3 = ci.y - halfunit; } // Vector3 v0 = new Vector3 (x0, y0, z0) * VERTEXSCALE; // Vector3 v1 = new Vector3 (x1, y1, z1) * VERTEXSCALE; // Vector3 v2 = new Vector3 (x2, y2, z2) * VERTEXSCALE; // Vector3 v3 = new Vector3 (x3, y3, z3) * VERTEXSCALE; return(new Vector3[] { new Vector3(x0, y0, z0) * VERTEXSCALE, new Vector3(x1, y1, z1) * VERTEXSCALE, new Vector3(x2, y2, z2) * VERTEXSCALE, new Vector3(x3, y3, z3) * VERTEXSCALE }); // v0, v1, v2, v3 }; }
} //TEST FUNC. public Block blockAt(ChunkIndex ci) { Coord offset = new Coord(ci.x, ci.y, ci.z); return(m_noisePatch.blockAtChunkCoordOffset(chunkCoord, offset)); // return blocks [ci.x, ci.y, ci.z]; }
/// <summary> /// Spawns a new chunk, populates it with blocks and adds its mesh /// </summary> /// <param name="chunkIndex">Chunk index.</param> /// <param name="noiseSettings">Noise settings.</param> public void RequestChunkGeneration(ChunkIndex chunkIndex, NoiseSettings noiseSettings, Action <ChunkIndex, Chunk> callback, bool perlin) { lock (Loading) { Loading.Add(chunkIndex); } requestedChunks.Enqueue(new RequestedChunk(chunkIndex, MainNoiseSettings, callback, perlin)); }
public void SetTile(Vector2 location, Tile tile) { ChunkIndex index = new ChunkIndex(location); Chunk chunk = GetOrCreateChunk(index); chunk.m_tiles[index.m_tileX, index.m_tileY] = tile; }
Chunk Create(ChunkIndex index) { var chunk = director.Construct(index); Chunks[index] = chunk; chunk.SetVisible(true); return(chunk); }
public void EnsureDeleted(ChunkIndex index) { if (Chunks.TryGetValue(index, out var chunk)) { chunk.Dispose(); Chunks.Remove(index); } }
float DistanceToPlayer(ChunkIndex index) { var chunkPosition = GetChunkCenter(index); var playerPos = player.position; chunkPosition.y = 0; playerPos.y = 0; return(Vector3.Distance(playerPos, chunkPosition)); }
public void EnsureHidden(ChunkIndex index) { if (!Chunks.TryGetValue(index, out var chunk)) { chunk = Create(index); } chunk.SetVisible(false); }
public void AddSorted(Chunk chunk) { if (Contains(chunk)) { return; } if (Count == 0 || chunk == null) { Enqueue(chunk); return; } for (int i = 0; i < Count; i++) { Chunk tempChunk; try { tempChunk = this[i]; } catch (Exception e) { try { tempChunk = this[i]; } catch (Exception e2) { Popup.Post("EXCEPTION222222222!!!!! " + e2.Message); Enqueue(chunk); return; } Popup.Post("EXCEPTION!!!!! " + e.Message); Enqueue(chunk); return; } ChunkIndex modified = chunk.Index * new ChunkIndex(1, 1, 1); if (tempChunk != null) { int val = ((modified.Position - Constants.Engines.Physics.Player.Position).LengthSquared).CompareTo((tempChunk.Index.Position - Constants.Engines.Physics.Player.Position).LengthSquared); if (val > 0) { ChunkList.Insert(i, chunk); return; } else if (val <= 0) { continue; } } } Enqueue(chunk); }
/// <summary> /// Finds the chunk at the given position /// </summary> /// <returns>The chunk.</returns> /// <param name="worldPosition">World position.</param> public static Chunk FindChunk(Vector3 worldPosition) { ChunkIndex index = FindChunkIndex(worldPosition); if (Chunk.WorldChunks.TryGetValue(index, out Chunk chunk)) { return(chunk); } return(null); }
Vector3 GetPositionForRidgedNoise(ChunkIndex chunkIndex, int x, int y, int z, int smoothness) { Vector3 chunkPos = chunkIndex.WorldPosition; float newX = x + chunkPos.x; float newY = y + chunkPos.y; float newZ = z + chunkPos.z; return(new Vector3(newX, newY, newZ)); }
public Chunk GenerateChunkNonThreaded(ChunkIndex chunkIndex, NoiseSettings noiseSettings, bool perlin) { Chunk chunkObject = Instantiate(ChunkPrefab, chunkIndex.WorldPosition, Quaternion.identity, transform).GetComponent <Chunk>(); chunkObject.Visible = false; GenerateChunkNonThreaded(chunkObject, chunkIndex, noiseSettings, perlin); return(chunkObject); }
bool CheckEnable(ChunkIndex index) { var distance = DistanceToPlayer(index); if (distance < minRadius * ChunkSize) { return(memoryManager.EnsureActive(index)); } return(false); }
public void PlaceVoxel(float3 worldPosition, Voxel voxel) { var world = m_voxelWorld; if (world == null) { Debug.LogError("Could not PlaceVoxel, no active ECS World found!"); return; } // Create a Chunk position ( ChunkIndex ) from the worldPosition. Vector3Int chunkIndex = new Vector3Int( Mathf.FloorToInt(worldPosition.x / m_chunkWidth), Mathf.FloorToInt(worldPosition.y / m_chunkHeight), Mathf.FloorToInt(worldPosition.z / m_chunkDepth) ); ChunkIndex cIndex = new ChunkIndex { X = chunkIndex.x, Y = chunkIndex.y, Z = chunkIndex.z }; if (!m_createdChunks.ContainsKey(cIndex)) { // If a chunk doesn't exist. Create it. CreateChunk(cIndex); } // Create a Voxel position ( Chunk voxel buffer index ) from world position and chunk position. // Remove chunk position from world position making it a position local to that chunk. float3 localPos = new float3 { x = worldPosition.x - (chunkIndex.x * m_chunkWidth), y = worldPosition.y - (chunkIndex.y * m_chunkHeight), z = worldPosition.z - (chunkIndex.z * m_chunkDepth) }; Vector3Int voxelIndex = new Vector3Int( Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z) ); // Set the supplied voxel at the voxel index of the chunk. int flatVoxelIndex = IndexUtils.ToFlatIndex(voxelIndex.x, voxelIndex.y, voxelIndex.z, m_chunkWidth, m_chunkDepth); Entity chunkEntity = m_createdChunks[cIndex]; var voxelBuffer = world.EntityManager.GetBuffer <ChunkModification>(chunkEntity); voxelBuffer.Add(new ChunkModification { FlatVoxelIndex = flatVoxelIndex, Voxel = voxel }); }
private void LoadChunk(ChunkIndex index, Chunk c) { BasicGenerator.GenerateChunkAt(index, ref c); c.Compile(); c.RenderChunk = true; AddChunk(c); lock (chunkLoadingList) { chunkLoadingList.Remove(index); } }
public Tile GetTile(Vector2 location) { ChunkIndex index = new ChunkIndex(location); Chunk chunk = GetChunk(index); if (chunk == null) { return(new Tile()); // empty space tile } return(chunk.m_tiles[index.m_tileX, index.m_tileY]); }
public bool EnsureActive(ChunkIndex index) { bool res = false; if (!Chunks.TryGetValue(index, out var chunk)) { chunk = Create(index); res = true; } chunk.SetVisible(true); return(res); }
void Awake() { Debug.Assert(size > 1); Debug.Assert((size - 1) % 2 == 0); // make sure it's odd CenterIndex = ChunkAndBlockSearch.FindChunkIndex(Player.position); Chunks = new Chunk[size, size]; Center = new TwoDIndex((size - 1) / 2, (size - 1) / 2); FillArray(); }
public Chunk Construct(ChunkIndex index) { currentBuilder.SetChunkIndex(index); currentBuilder.CreateMaterial(); currentBuilder.CreateGameObject(); currentBuilder.CreateRenderingComponents(); currentBuilder.InitializeModelMatrix(); currentBuilder.CreateCollider(); currentBuilder.CreateChunkComponent(); currentBuilder.PrepareGeometryGeneration(); currentBuilder.GenerateGeometry(); return(currentBuilder.GetChunk()); }
void Update() { ChunkIndex playerChunk = ChunkAndBlockSearch.FindChunkIndex(Player.position); if (CenterIndex != playerChunk) { int xDir = playerChunk.ChunkX - CenterIndex.ChunkX; int zDir = playerChunk.ChunkZ - CenterIndex.ChunkZ; CenterIndex = playerChunk; ShiftArray(xDir, zDir); } }
bool CheckSideMirrored(int x, int y, ChunkIndex currentChunkIndex) { // operates in each quadrant var generates = false; var direction = new ChunkIndex(x, y);//quadrant + + generates |= CheckEnable(currentChunkIndex + direction); direction.x = -direction.x; //quadrant - + generates |= CheckEnable(currentChunkIndex + direction); direction.z = -direction.z; //quadrant - - generates |= CheckEnable(currentChunkIndex + direction); direction.x = -direction.x; //quadrant + - generates |= CheckEnable(currentChunkIndex + direction); return(generates); }
public void QueueRemoveChunks(int loadDistance) { chunkUnloadQueue.Clear(); ChunkIndex playerLoc = world.Player.ChunkID; foreach (Chunk c in Chunks.Values.ToList()) { float d = playerLoc.Distance(world.Size, c.ChunkID); if (d > loadDistance) { chunkUnloadQueue.Enqueue(c.ChunkID, 64 / (d + 1)); } } }
public void UpdateChunks(int n) { for (int i = 0; i < Math.Min(n, chunkUpdateQueue.Count); i++) { ChunkIndex ci = chunkUpdateQueue.Dequeue(); if (Chunks.TryGetValue(ci, out Chunk chunk)) { chunk.Update(); } else { i--; } } }
public override void Load(BinaryReaderEx reader) { base.Load(reader); Float1 = reader.ReadSingle(); AnimationLength = reader.ReadSingle(); Bones = reader.ReadInt16(); NumIndices = reader.ReadInt16(); Flags = reader.ReadByte(); reader.BaseStream.Seek(3, System.IO.SeekOrigin.Current); for (var i = 0; i < NumIndices; i++) { int val = reader.ReadInt32(); ChunkIndex idx = new ChunkIndex(); idx.Index = val & 0x7FFFFFFF; idx.Flag = (val & 0x80000000) == 0x80000000; this.Offsets.Add(idx); } }