public ContainerItem(Item item) { if (item == null) { item = ItemCache.Get <Item>("winecrash:air"); } this.Item = item; }
public ContainerItem(string identifier) { if (identifier == null) { identifier = "winecrash:air"; } this.Item = ItemCache.Get <Item>(identifier); }
public virtual void Harvest(Chunk chunk, int x, int y, int z) { ItemAmount[] items = NextHarvestDrop(); // no custom chance for now for (int i = 0; i < items.Length; i++) { ItemEntity item = ItemEntity.FromItem(ItemCache.Get(items[i].Identifier), items[i].Amount); item.WObject.Position = (Vector3D)World.LocalToGlobal(chunk.Coordinates, new Vector3I(x, y, z)) + Vector3D.One * 0.5D; // apply force to item Vector2D dir = Winecrash.Random.NextDirection2D(); Vector3D force = new Vector3D(dir.X, 0.0, dir.Y) * Item.ItemDigSpawnForce; item.RigidBody.Velocity += force; } }
private void ConstructInternal() { if (this.Deleted) { return; } if (this.Blocks == null) { BuildEndFrame = true; return; } Stopwatch sw = new Stopwatch(); sw.Start(); cwest = WestNeighbor?.Blocks != null; if (cwest) { bwest = new ushort[Chunk.TotalBlocks]; WestNeighbor.Blocks.CopyTo(bwest, 0); } ceast = (EastNeighbor?.Blocks != null); if (ceast) { beast = new ushort[Chunk.TotalBlocks]; EastNeighbor.Blocks.CopyTo(beast, 0); } cnorth = (NorthNeighbor?.Blocks != null); if (cnorth) { bnorth = new ushort[Chunk.TotalBlocks]; NorthNeighbor.Blocks.CopyTo(bnorth, 0); } csouth = (SouthNeighbor?.Blocks != null); if (csouth) { bsouth = new ushort[Chunk.TotalBlocks]; SouthNeighbor.Blocks.CopyTo(bsouth, 0); } List <Vector3F> svertices = new List <Vector3F>(1 << 18); List <Vector2F> suv = new List <Vector2F>(1 << 18); List <Vector3F> snormals = new List <Vector3F>(1 << 18); List <uint> striangles = new List <uint>(1 << 18); uint striangle = 0; List <Vector3F> tvertices = new List <Vector3F>(1 << 18); List <Vector2F> tuv = new List <Vector2F>(1 << 18); List <Vector3F> tnormals = new List <Vector3F>(1 << 18); List <uint> ttriangles = new List <uint>(1 << 18); uint ttriangle = 0; Stack <BlockFaces> faces = new Stack <BlockFaces>(6); Block block = null; ushort index = 0; if (this.Deleted) { return; } Vector3F thisWP = this.WObject.Position; for (int z = 0; z < Depth; z++) { for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { if (this.Deleted) { return; } index = this.Blocks[x + Width * y + Width * Height * z]; if (!constructblocks.TryGetValue(index, out block)) { block = ItemCache.Get <Block>(index); constructblocks.Add(index, block); } if (block.Identifier == "winecrash:air") { continue; // ignore if air } bool transparent = block.Transparent; if (block.DrawAllSides) { faces.Push(BlockFaces.Up); // up faces.Push(BlockFaces.Down); // down faces.Push(BlockFaces.West); // west faces.Push(BlockFaces.East); // east faces.Push(BlockFaces.North); // north faces.Push(BlockFaces.South); // south } else { if (IsTransparent(block, x, y + 1, z, constructblocks)) { faces.Push(BlockFaces.Up); // up } if (IsTransparent(block, x, y - 1, z, constructblocks)) { faces.Push(BlockFaces.Down); // down } if (IsTransparent(block, x - 1, y, z, constructblocks)) { faces.Push(BlockFaces.West); // west } if (IsTransparent(block, x + 1, y, z, constructblocks)) { faces.Push(BlockFaces.East); // east } if (IsTransparent(block, x, y, z + 1, constructblocks)) { faces.Push(BlockFaces.North); // north } if (IsTransparent(block, x, y, z - 1, constructblocks)) { faces.Push(BlockFaces.South); // south } } foreach (BlockFaces face in faces) { CreateVerticesCube(x, y, z, face, transparent ? tvertices : svertices); /*int count = usedvertices.Count; * for (int i = count - 6; i < count; i++) * { * Vector3F worldPos = thisWP + usedvertices[i] - new Vector3F(572, 0, 459); * * Vector3F final = worldPos.RotateAround(new Vector3F(0,-360, 0), new Quaternion(worldPos.X * 0.1,0,worldPos.Z * 0.1)); * * * final += new Vector3F(572, 0, 459); * usedvertices[i] = final - thisWP; * }*/ CreateUVsCube(face, transparent ? tuv : suv, index); CreateNormalsCube(face, transparent ? tnormals : snormals); if (transparent) { ttriangles.AddRange(new uint[6] { ttriangle, ttriangle + 1, ttriangle + 2, ttriangle + 3, ttriangle + 4, ttriangle + 5 }); ttriangle += 6; } else { striangles.AddRange(new uint[6] { striangle, striangle + 1, striangle + 2, striangle + 3, striangle + 4, striangle + 5 }); striangle += 6; } } faces.Clear(); } } } sw.Stop(); Debug.Log("Chunk build time: " + sw.Elapsed.TotalMilliseconds.ToString("F2") + "ms"); if (svertices.Count != 0) { if (this.SolidRenderer.Mesh == null) { this.SolidRenderer.Mesh = new Mesh("Chunk Mesh"); } this.SolidRenderer.Mesh.Vertices = svertices.ToArray(); this.SolidRenderer.Mesh.Triangles = striangles.ToArray(); this.SolidRenderer.Mesh.UVs = suv.ToArray(); this.SolidRenderer.Mesh.Normals = snormals.ToArray(); this.SolidRenderer.Mesh.Tangents = new Vector4F[svertices.Count]; this.SolidRenderer.Mesh.Apply(true); } else { this.SolidRenderer.Mesh?.Delete(); this.SolidRenderer.Mesh = null; } if (tvertices.Count != 0) { if (this.TransparentRenderer.Mesh == null) { this.TransparentRenderer.Mesh = new Mesh("Chunk Transparent Mesh"); } this.TransparentRenderer.Mesh.Vertices = tvertices.ToArray(); this.TransparentRenderer.Mesh.Triangles = ttriangles.ToArray(); this.TransparentRenderer.Mesh.UVs = tuv.ToArray(); this.TransparentRenderer.Mesh.Normals = tnormals.ToArray(); this.TransparentRenderer.Mesh.Tangents = new Vector4F[tvertices.Count]; this.TransparentRenderer.Mesh.Apply(true); } else { this.TransparentRenderer.Mesh?.Delete(); this.TransparentRenderer.Mesh = null; } svertices = null; striangles = null; suv = null; snormals = null; tvertices = null; ttriangles = null; tuv = null; tnormals = null; cwest = ceast = cnorth = csouth = false; bsouth = null; bnorth = null; beast = null; bwest = null; ConstructedOnce = true; }
private bool IsTransparent(Block currentBlock, int x, int y, int z, Dictionary <ushort, Block> cache) { //if outside world, yes if (y < 0 || y > 255) { return(true); } if (x < 0) // check west neighbor { if (cwest) { transpid = this.bwest[15 + Width * y + Width * Height * z]; } } else if (x > 15) // check east neighbor { if (ceast) { transpid = this.beast[0 + Width * y + Width * Height * z]; } } else if (z > 15) //check south neighbor { if (cnorth) { transpid = this.bnorth[x + Width * y + Width * Height * 0]; } } else if (z < 0) { if (csouth) { transpid = this.bsouth[x + Width * y + Width * Height * 15]; } } else { transpid = this.GetBlockIndex(x, y, z); } if (!cache.TryGetValue(transpid, out transblock)) { transblock = ItemCache.Get <Block>(transpid); cache.Add(transpid, transblock); } if (currentBlock == transblock) { return(transblock.Transparent && transblock.DrawInternalFaces); } else { return(transblock.Transparent); } }