public static bool GenerateBlankLevelBlock(Level level, Vector3 coordinate, string defaultTextureName) { if (level.Contains(coordinate)) { return false; } string[] detailTextureNames = new string[5] { defaultTextureName, null, null, null, null }; Vector2[] uvOffsets = new Vector2[5] { Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero, Vector2.Zero }; Vector2[] uvScales = new Vector2[5] { Vector2.One, Vector2.One, Vector2.One, Vector2.One, Vector2.One }; Color[] blankHeightMap = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES]; for (int i = 0; i < blankHeightMap.Length; i++) { blankHeightMap[i] = new Color(0, 0, 0, 0); } Texture2D blankHeightMapTexture = new Texture2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES); Color[] blankAlphaMap = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD]; for (int i = 0; i < blankAlphaMap.Length; i++) { blankAlphaMap[i] = new Color(0, 0, 0, 0); } Texture2D blankAlphaMapTexture = new Texture2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD); return AddNewLevelBlock(level, coordinate, blankHeightMapTexture, blankAlphaMapTexture, detailTextureNames, uvOffsets, uvScales); }
public static bool AddNewLevelBlock(Level level, Vector3 coordinate, Texture2D heightMap, Texture2D alphaMap, string[] detailTextureNames, Vector2[] uvOffsets, Vector2[] uvScales) { if (level.Contains(coordinate)) { return false; } Color[] heightColors = new Color[HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_VERTICES]; if (heightMap.Width != HeightMapMesh.NUM_SIDE_VERTICES || heightMap.Height != HeightMapMesh.NUM_SIDE_VERTICES) { RenderTarget2D resizedHeightMap = new RenderTarget2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES); GraphicsManager.Device.SetRenderTarget(resizedHeightMap); GraphicsManager.SpriteBatch.Begin(); GraphicsManager.SpriteBatch.Draw(heightMap, new Rectangle(0, 0, HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES), Color.White); GraphicsManager.SpriteBatch.End(); GraphicsManager.Device.SetRenderTarget(null); resizedHeightMap.GetData(heightColors); } else { heightMap.GetData(heightColors); } float[,] heights = new float[HeightMapMesh.NUM_SIDE_VERTICES, HeightMapMesh.NUM_SIDE_VERTICES]; for (int i = 0; i < heightColors.Length; i++) { heights[i % HeightMapMesh.NUM_SIDE_VERTICES, i / HeightMapMesh.NUM_SIDE_VERTICES] = ConvertColorToFloat(heightColors[i]); } if (alphaMap.Height != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD || alphaMap.Width != HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD) { RenderTarget2D resizedAlphaMap = new RenderTarget2D(GraphicsManager.Device, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD); GraphicsManager.Device.SetRenderTarget(resizedAlphaMap); GraphicsManager.SpriteBatch.Begin(); GraphicsManager.SpriteBatch.Draw(alphaMap, new Rectangle(0, 0, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD, HeightMapMesh.NUM_SIDE_VERTICES * HeightMapMesh.NUM_SIDE_TEXELS_PER_QUAD), Color.White); GraphicsManager.SpriteBatch.End(); GraphicsManager.Device.SetRenderTarget(null); alphaMap = resizedAlphaMap; } HeightMapMesh heightMapMesh = new HeightMapMesh(heights, alphaMap, detailTextureNames, uvOffsets, uvScales); AssetLibrary.AddHeightMap(level.Name + coordinate.ToString(), heightMapMesh); level.AddNewBlock(coordinate); return true; }
public ModifiableLevel(Level level, Space space) : base(level.Name) { mSpace = space; foreach (Vector3 block in level.Blocks.Keys) { AddNewBlock(block); } }
public static void WriteLevel(Level level, List<DummyObject> objects, FileInfo filePathAndName) { if (!filePathAndName.Name.Contains(".lvl")) { filePathAndName = new FileInfo(filePathAndName.Name + ".lvl"); } if (File.Exists(filePathAndName.FullName)) { File.Delete(filePathAndName.FullName); } FileStream output = File.Create(filePathAndName.FullName); ZipOutputStream zipStream = new ZipOutputStream(output); List<Tuple<string, MemoryStream>> serializedLevel = level.Serialize(); serializedLevel.Add(SerializedDummies(objects)); zipStream.SetLevel(5); foreach (Tuple<string, MemoryStream> file in serializedLevel) { ZipEntry newEntry = new ZipEntry(file.Item1); newEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(newEntry); StreamUtils.Copy(file.Item2, zipStream, new byte[4096]); zipStream.CloseEntry(); } zipStream.IsStreamOwner = true; zipStream.Close(); }
/// <summary> /// /// </summary> /// <param name="filePathAndName"></param> public static Tuple<Level, List<DummyObject>> LoadLevelFromFile(FileInfo filePathAndName) { Tuple<Level, List<DummyObject>> result; ZipFile zipFile = null; try { FileStream fileStream = File.OpenRead(filePathAndName.FullName); zipFile = new ZipFile(fileStream); Level level = new Level(filePathAndName.Name); Dictionary<Vector3, Texture2D> levelHeightMaps = new Dictionary<Vector3, Texture2D>(); Dictionary<Vector3, Texture2D> levelAlphaMaps = new Dictionary<Vector3, Texture2D>(); Dictionary<Vector3, string[]> levelTextureNames = new Dictionary<Vector3, string[]>(); Dictionary<Vector3, Vector2[]> levelUVOffset = new Dictionary<Vector3, Vector2[]>(); Dictionary<Vector3, Vector2[]> levelUVScale = new Dictionary<Vector3, Vector2[]>(); HashSet<Vector3> coordinates = new HashSet<Vector3>(); List<DummyObject> dummies = new List<DummyObject>(); foreach (ZipEntry zipEntry in zipFile) { if (!zipEntry.IsFile) { continue; } string entryFileName = Path.GetFileName(zipEntry.Name); Stream zipStream = zipFile.GetInputStream(zipEntry); MemoryStream memoryStream = new MemoryStream(); zipStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); if (entryFileName.Contains("Objects")) { dummies = LoadDummyObjects(memoryStream); continue; } string[] nameSegments = entryFileName.Split(new char[] { '_' }); Vector3 coordinate = new Vector3(float.Parse(nameSegments[1]), float.Parse(nameSegments[2]), float.Parse(nameSegments[3])); if (!coordinates.Contains(coordinate)) { coordinates.Add(coordinate); } if (entryFileName.Contains("AlphaMap")) { levelAlphaMaps.Add(coordinate, Texture2D.FromStream(GraphicsManager.Device, memoryStream)); } else if (entryFileName.Contains("HeightMap")) { levelHeightMaps.Add(coordinate, Texture2D.FromStream(GraphicsManager.Device, memoryStream)); } else if (entryFileName.Contains("DetailTextures")) { string[] textureNames; Vector2[] uvOffsets; Vector2[] uvScales; LoadDetailTextureNames(memoryStream, out textureNames, out uvOffsets, out uvScales); levelTextureNames.Add(coordinate, textureNames); levelUVOffset.Add(coordinate, uvOffsets); levelUVScale.Add(coordinate, uvScales); } zipStream.Close(); } foreach (Vector3 coordinate in coordinates) { AddNewLevelBlock(level, coordinate, levelHeightMaps[coordinate], levelAlphaMaps[coordinate], levelTextureNames[coordinate], levelUVOffset[coordinate], levelUVScale[coordinate]); } result = new Tuple<Level, List<DummyObject>>(level, dummies); } finally { if (zipFile != null) { zipFile.IsStreamOwner = true; zipFile.Close(); } } return result; }
public static Level GenerateNewLevel(string levelName, string defaultTexture) { Level level = new Level(levelName); GenerateBlankLevelBlock(level, Vector3.Zero, defaultTexture); return level; }