public void PerformAction(World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { PerformAction(Position, gameWorld, prefab, selectedSpawn, spawnRot); if (MirrorMode) { PerformAction(new Vector3(((gameWorld.X_SIZE) - Position.X), Position.Y, Position.Z), gameWorld, prefab, selectedSpawn, spawnRot); } }
internal void CutPrefabSapce(World gameWorld, PrefabChunk prefab) { int startX = (int)Position.X - (prefab.X_SIZE / 2); int startY = (int)Position.Y - (prefab.Y_SIZE / 2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { gameWorld.SetVoxelActive(startX + x, startY + y, startZ + z, false); // destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); if (MirrorMode) { gameWorld.SetVoxelActive(((gameWorld.X_SIZE) - startX - prefab.X_SIZE) + x, startY + y, startZ + z, false); } } } } }
public void PerformAction(Vector3 position, World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { // Need to make it so voxels have a type and then we can check if tree so we don't put trees on top of trees yo Vector2 center = new Vector2(position.X, position.Y); switch (Mode) { case CursorMode.LandScape: for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if (Voxels[x, y, 0].Active) { gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - Height, 0, VoxelType.Ground, gameWorld.ThemeTopColor(Theme), gameWorld.ThemeSideColor(Theme)); } } } } break; case CursorMode.Trees: int numTrees = 1 + (Size / 2); for (int i = 0; i < numTrees; i++) { Vector2 pos = Helper.RandomPointInCircle(new Vector2(position.X, position.Y), 0f, Size); //int z = 0; //for (int zz = 0; zz < Chunk.Z_SIZE; zz++) if (gameWorld.GetVoxel((int)Position.X, (int)Position.Y, zz).Active) { z = zz - 1; break; } gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)position.Z); } //for (float a = 0f; a < MathHelper.TwoPi; a += 0.05f) //{ // for (int r = 0; r < Size; r++) // { // Vector3 pos = new Vector3(Helper.PointOnCircle(ref center, r, a), 0f); // gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)Position.Z); // } //} break; case CursorMode.Water: for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if (Voxels[x, y, 0].Active) { gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - (Height - 1), 0, VoxelType.Water, new Color(0.1f, 0.1f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f, new Color(0.3f, 0.3f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f); } } } } break; case CursorMode.Prefab: int startX = (int)position.X - (prefab.X_SIZE / 2); int startY = (int)position.Y - (prefab.Y_SIZE / 2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { if (prefab.Voxels[x, y, z].Active) { gameWorld.SetVoxel(startX + x, startY + y, startZ + z, true, destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); } } } } break; case CursorMode.Spawn: bool found = false; for (int i = gameWorld.Spawns.Count - 1; i >= 0; i--) { if (gameWorld.Spawns[i].Position == position) { found = true; gameWorld.Spawns.RemoveAt(i); } } if (!found) { gameWorld.Spawns.Add(new Spawn() { Position = position, Rotation = (byte)spawnRot, Type = (SpawnType)selectedSpawn, }); } break; } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.DepthStencilState = DepthStencilState.Default; //GraphicsDevice.RasterizerState = RasterizerState.CullNone; //GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; GraphicsDevice.BlendState = BlendState.AlphaBlend; foreach (EffectPass pass in drawEffect.CurrentTechnique.Passes) { pass.Apply(); for (int y = 0; y < gameWorld.Y_CHUNKS; y++) { for (int x = 0; x < gameWorld.X_CHUNKS; x++) { Chunk c = gameWorld.Chunks[x, y, 0]; if (!c.Visible) { continue; } if (c == null || c.VertexArray == null || c.VertexArray.Length == 0) { continue; } if (!gameCamera.boundingFrustum.Intersects(c.boundingSphere.Transform(Matrix.CreateTranslation(gameCamera.Position)))) { continue; } GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } foreach (Spawn s in gameWorld.Spawns) { drawEffect.World = Matrix.CreateRotationZ(-(MathHelper.PiOver2 * s.Rotation)) * Matrix.CreateTranslation(s.Position * Voxel.SIZE) * gameCamera.worldMatrix; foreach (EffectPass pass in drawEffect.CurrentTechnique.Passes) { pass.Apply(); AnimChunk c = spawnSprites.AnimChunks[(int)s.Type]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } if (cursor.Mode == CursorMode.Prefab) { if (Prefabs.Count > 0) { cursorEffect.Alpha = 0.5f; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); PrefabChunk c = Prefabs[selectedPrefab]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } } else if (cursor.Mode == CursorMode.Spawn) { cursorEffect.Alpha = 0.5f; cursorEffect.World = Matrix.CreateRotationZ(-(MathHelper.PiOver2 * spawnRot)) * Matrix.CreateTranslation(cursor.Position * Voxel.SIZE) * gameCamera.worldMatrix; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); AnimChunk c = spawnSprites.AnimChunks[selectedSpawn]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } else { cursorEffect.Alpha = 1f; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); if (cursor.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, cursor.VertexArray, 0, cursor.VertexArray.Length, cursor.IndexArray, 0, cursor.VertexArray.Length / 2); } } } spriteBatch.Begin(); spriteBatch.DrawString(font, cursor.Mode.ToString(), Vector2.One * 20, Color.White); if (cursor.Mode == CursorMode.LandScape || cursor.Mode == CursorMode.Water) { spriteBatch.DrawString(font, "Brush height: " + cursor.Height, new Vector2(20, 40), Color.White); spriteBatch.DrawString(font, "Theme: " + cursor.Theme.ToString(), new Vector2(20, 60), Color.White); } if (cursor.Mode == CursorMode.Prefab) { spriteBatch.DrawString(font, "Brush height: " + cursor.Height, new Vector2(20, 40), Color.White); spriteBatch.DrawString(font, "Destructable level: " + cursor.destructable, new Vector2(20, 60), Color.White); } if (cursor.Mode == CursorMode.Spawn) { spriteBatch.DrawString(font, "Spawn height: " + cursor.Height, new Vector2(20, 40), Color.White); } if (cursor.MirrorMode) { spriteBatch.DrawString(font, "Mirror mode", new Vector2(300, 40), Color.White); } spriteBatch.End(); base.Draw(gameTime); }
public static PrefabChunk LoadPrefab(string fn) { PrefabChunk prefab; byte[] buffer; using (FileStream gstr = new FileStream(fn, FileMode.Open)) { byte[] lb = new byte[4]; gstr.Position = gstr.Length - 4; gstr.Read(lb, 0, 4); int msgLength = BitConverter.ToInt32(lb, 0); buffer = new byte[msgLength]; gstr.Position = 0; using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress)) { str.Read(buffer, 0, msgLength); } } int pos = 0; int xs = buffer[0]; int ys = buffer[1]; int zs = buffer[2]; int frames = buffer[3]; prefab = new PrefabChunk(xs, ys, zs); pos = 4; for (int i = 0; i < 10; i++) { pos += 3; } while (pos < buffer.Length) { if (Convert.ToChar(buffer[pos]) != 'c') { int vx = buffer[pos]; int vy = buffer[pos + 1]; int vz = buffer[pos + 2]; Color top = new Color(buffer[pos + 3], buffer[pos + 4], buffer[pos + 5]); prefab.SetVoxel(vx, (zs-1)-vz, vy, true, VoxelType.Prefab, top); pos += 6; } else { pos++; break; } } prefab.UpdateMesh(); GC.Collect(); return prefab; }
public void PerformAction(Vector3 position, World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { // Need to make it so voxels have a type and then we can check if tree so we don't put trees on top of trees yo Vector2 center = new Vector2(position.X, position.Y); switch (Mode) { case CursorMode.LandScape: for (int x = 0; x < 32; x++) for (int y = 0; y < 32; y++) for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if(Voxels[x,y,0].Active) gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - Height, 0, VoxelType.Ground, gameWorld.ThemeTopColor(Theme), gameWorld.ThemeSideColor(Theme)); } break; case CursorMode.Trees: int numTrees = 1 +(Size / 2); for (int i = 0; i < numTrees; i++) { Vector2 pos = Helper.RandomPointInCircle(new Vector2(position.X,position.Y), 0f, Size); //int z = 0; //for (int zz = 0; zz < Chunk.Z_SIZE; zz++) if (gameWorld.GetVoxel((int)Position.X, (int)Position.Y, zz).Active) { z = zz - 1; break; } gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)position.Z); } //for (float a = 0f; a < MathHelper.TwoPi; a += 0.05f) //{ // for (int r = 0; r < Size; r++) // { // Vector3 pos = new Vector3(Helper.PointOnCircle(ref center, r, a), 0f); // gameWorld.MakeTree((int)pos.X, (int)pos.Y, (int)Position.Z); // } //} break; case CursorMode.Water: for (int x = 0; x < 32; x++) for (int y = 0; y < 32; y++) for (int z = Chunk.Z_SIZE - 1; z >= 0; z--) { if(Voxels[x,y,0].Active) gameWorld.SetVoxel(((int)center.X - 16) + x, ((int)center.Y - 16) + y, z, z >= Chunk.Z_SIZE - (Height - 1), 0, VoxelType.Water, new Color(0.1f, 0.1f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f, new Color(0.3f, 0.3f, 0.8f + ((float)Helper.Random.NextDouble() * 0.1f)) * 0.8f); } break; case CursorMode.Prefab: int startX = (int)position.X - (prefab.X_SIZE/2); int startY = (int)position.Y - (prefab.Y_SIZE/2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { if (prefab.Voxels[x, y, z].Active) { gameWorld.SetVoxel(startX + x, startY + y, startZ + z, true, destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); } } } } break; case CursorMode.Spawn: bool found = false; for (int i = gameWorld.Spawns.Count - 1; i >= 0; i--) { if (gameWorld.Spawns[i].Position == position) { found = true; gameWorld.Spawns.RemoveAt(i); } } if (!found) { gameWorld.Spawns.Add(new Spawn() { Position = position, Rotation = (byte)spawnRot, Type = (SpawnType)selectedSpawn, }); } break; } }
public void PerformAction(World gameWorld, PrefabChunk prefab, int selectedSpawn, int spawnRot) { PerformAction(Position, gameWorld, prefab, selectedSpawn, spawnRot); if (MirrorMode) PerformAction(new Vector3(((gameWorld.X_SIZE)-Position.X),Position.Y,Position.Z), gameWorld, prefab, selectedSpawn, spawnRot); }
internal void CutPrefabSapce(World gameWorld, PrefabChunk prefab) { int startX = (int)Position.X - (prefab.X_SIZE / 2); int startY = (int)Position.Y - (prefab.Y_SIZE / 2); int startZ = (Chunk.Z_SIZE - Height) - (prefab.Z_SIZE); for (int x = 0; x < prefab.X_SIZE; x++) { for (int y = 0; y < prefab.Y_SIZE; y++) { for (int z = 0; z < prefab.Z_SIZE; z++) { gameWorld.SetVoxelActive(startX + x, startY + y, startZ + z, false); // destructable, VoxelType.Prefab, new Color(prefab.Voxels[x, y, z].TR, prefab.Voxels[x, y, z].TG, prefab.Voxels[x, y, z].TB), new Color(prefab.Voxels[x, y, z].SR, prefab.Voxels[x, y, z].SG, prefab.Voxels[x, y, z].SB)); if (MirrorMode) gameWorld.SetVoxelActive(((gameWorld.X_SIZE) - startX-prefab.X_SIZE)+x, startY+y, startZ + z, false); } } } }
public static PrefabChunk LoadPrefab(string fn) { PrefabChunk prefab; byte[] buffer; using (FileStream gstr = new FileStream(fn, FileMode.Open)) { byte[] lb = new byte[4]; gstr.Position = gstr.Length - 4; gstr.Read(lb, 0, 4); int msgLength = BitConverter.ToInt32(lb, 0); buffer = new byte[msgLength]; gstr.Position = 0; using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress)) { str.Read(buffer, 0, msgLength); } } int pos = 0; int xs = buffer[0]; int ys = buffer[1]; int zs = buffer[2]; int frames = buffer[3]; prefab = new PrefabChunk(xs, ys, zs); pos = 4; for (int i = 0; i < 10; i++) { pos += 3; } while (pos < buffer.Length) { if (Convert.ToChar(buffer[pos]) != 'c') { int vx = buffer[pos]; int vy = buffer[pos + 1]; int vz = buffer[pos + 2]; Color top = new Color(buffer[pos + 3], buffer[pos + 4], buffer[pos + 5]); prefab.SetVoxel(vx, (zs - 1) - vz, vy, true, VoxelType.Prefab, top); pos += 6; } else { pos++; break; } } prefab.UpdateMesh(); GC.Collect(); return(prefab); }