Use linq instead of JsonConvert - makes for less nast code, but this still needs work.
This commit is contained in:
@@ -58,8 +58,8 @@
|
||||
<Compile Include="Level\Level.cs" />
|
||||
<Compile Include="Level\TileData.cs" />
|
||||
<Compile Include="Level\TileGroup.cs" />
|
||||
<Compile Include="Level\TileInfo.cs" />
|
||||
<Compile Include="Mesh.cs" />
|
||||
<Compile Include="ObjVertex.cs" />
|
||||
<Compile Include="Shaders\Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Shaders\Shader.cs" />
|
||||
|
||||
@@ -6,18 +6,14 @@ using System.Linq;
|
||||
using Diamond.Buffers;
|
||||
using Diamond.Shaders;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
|
||||
namespace Diamond.Level
|
||||
{
|
||||
public class Level
|
||||
{
|
||||
[JsonProperty("models")]
|
||||
private string[] MeshNames { get; set; }
|
||||
|
||||
[JsonProperty("tiles")]
|
||||
private TileInfo[] TileInfos { get; set; }
|
||||
|
||||
private TileData[] _allTiles;
|
||||
private ObjVertex[] _allVertices;
|
||||
|
||||
@@ -38,55 +34,50 @@ namespace Diamond.Level
|
||||
|
||||
public static Level LoadLevel(string file)
|
||||
{
|
||||
var level = JsonConvert.DeserializeObject<Level>(File.ReadAllText(file));
|
||||
var levelData = JObject.Parse(File.ReadAllText(file));
|
||||
|
||||
var dir = Path.GetDirectoryName(file);
|
||||
|
||||
// this is horrendous, but not as bad as trying to directly deserialize it.
|
||||
|
||||
var meshes = levelData["models"]
|
||||
.Select(path => Mesh.FromObj(Path.Combine(dir, (string) path)))
|
||||
.SelectMany(objects => objects)
|
||||
.ToArray();
|
||||
|
||||
// region assemble mesh map
|
||||
var meshes = new Dictionary<string, Mesh<ObjVertex>>();
|
||||
var meshDict = meshes
|
||||
.ToDictionary(mesh => mesh.Name, mesh => mesh);
|
||||
|
||||
foreach (var meshPath in level.MeshNames)
|
||||
{
|
||||
var objects = Mesh.FromObj(Path.Combine(dir, meshPath));
|
||||
Debug.WriteLine(string.Join("\n", objects.Select(o => o.Name)));
|
||||
foreach (var mesh in objects)
|
||||
var allVertices = Mesh.Join(meshes);
|
||||
|
||||
var tilegroups = levelData["tiles"]
|
||||
.Select(tile => new
|
||||
{
|
||||
meshes[mesh.Name] = mesh;
|
||||
}
|
||||
}
|
||||
info = new
|
||||
{
|
||||
mesh = meshDict[(string) tile["mesh"]]
|
||||
},
|
||||
pos = tile["pos"].ToObject<Vector3>()
|
||||
})
|
||||
.GroupBy(tile => tile.info)
|
||||
.Select(group => new TileGroup(new SubArray<TileData>(
|
||||
group.Select(data => new TileData(data.pos))
|
||||
.ToArray()),
|
||||
group.Key.mesh))
|
||||
.ToArray();
|
||||
|
||||
// region store all used meshes
|
||||
level._meshes = meshes.Values.ToArray();
|
||||
// join meshes
|
||||
level._allVertices = Mesh.Join(level._meshes);
|
||||
Debug.WriteLine(level._allVertices.Length);
|
||||
Debug.WriteLine(level._meshes[1].Vertices.Length);
|
||||
Debug.WriteLine(level._meshes[1].Vertices.Offset);
|
||||
var tileArrays = tilegroups
|
||||
.Select(group => group.Tiles);
|
||||
|
||||
var groupDict = new Dictionary<string, List<TileData>>();
|
||||
var allTiles = SubArray.Join(tileArrays);
|
||||
|
||||
foreach (var tileInfo in level.TileInfos)
|
||||
var level = new Level
|
||||
{
|
||||
var meshName = tileInfo.Mesh;
|
||||
if (!groupDict.ContainsKey(meshName))
|
||||
groupDict[meshName] = new List<TileData>();
|
||||
groupDict[meshName].Add(tileInfo.TileData);
|
||||
}
|
||||
|
||||
var groupList = new List<TileGroup>();
|
||||
var tileSubArrayList = new List<SubArray<TileData>>();
|
||||
|
||||
foreach (var kvp in groupDict)
|
||||
{
|
||||
var sa = new SubArray<TileData>(kvp.Value.ToArray());
|
||||
groupList.Add(new TileGroup(sa, meshes[kvp.Key]));
|
||||
tileSubArrayList.Add(sa);
|
||||
}
|
||||
|
||||
level._tileGroups = groupList.ToArray();
|
||||
|
||||
level._allTiles = SubArray.Join(tileSubArrayList);
|
||||
_allTiles = allTiles,
|
||||
_allVertices = allVertices,
|
||||
_meshes = meshes,
|
||||
_tileGroups = tilegroups
|
||||
};
|
||||
|
||||
level.InitializeBuffers();
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace Diamond.Level
|
||||
[VertexData(Divisor = 1)]
|
||||
public struct TileData
|
||||
{
|
||||
[JsonProperty("pos")]
|
||||
[VertexPointer("glbpos", 3)]
|
||||
public Vector3 Position;
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using OpenTK;
|
||||
|
||||
namespace Diamond.Level
|
||||
{
|
||||
internal class TileInfo
|
||||
{
|
||||
[JsonProperty("mesh")]
|
||||
public string Mesh { get; set; }
|
||||
|
||||
[JsonProperty("pos")]
|
||||
public Vector3 Position { get; set; }
|
||||
|
||||
public TileData TileData => new TileData(Position);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Mesh: {Mesh}, Position: {Position}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,34 +59,8 @@ namespace Diamond
|
||||
}
|
||||
}
|
||||
|
||||
[VertexData]
|
||||
public struct ObjVertex
|
||||
{
|
||||
[VertexPointer("position", 3)]
|
||||
public Vector3 Position;
|
||||
|
||||
[VertexPointer("uv", 2)]
|
||||
public Vector2 UV;
|
||||
|
||||
[VertexPointer("normal", 3)]
|
||||
public Vector3 Normal;
|
||||
|
||||
public ObjVertex(Vector3 position, Vector2 uv, Vector3 normal)
|
||||
{
|
||||
Position = position;
|
||||
UV = uv;
|
||||
Normal = normal;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Mesh
|
||||
{
|
||||
public static Mesh<T> FromJson<T>(string file) where T : struct
|
||||
{
|
||||
var vertices = JsonConvert.DeserializeObject<T[]>(File.ReadAllText(file));
|
||||
return new Mesh<T>(vertices);
|
||||
}
|
||||
|
||||
public static Mesh<ObjVertex>[] FromObj(string file)
|
||||
{
|
||||
var lines = File.ReadAllLines(file);
|
||||
|
||||
25
Diamond/ObjVertex.cs
Normal file
25
Diamond/ObjVertex.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Diamond.Buffers;
|
||||
using OpenTK;
|
||||
|
||||
namespace Diamond
|
||||
{
|
||||
[VertexData]
|
||||
public struct ObjVertex
|
||||
{
|
||||
[VertexPointer("position", 3)]
|
||||
public Vector3 Position;
|
||||
|
||||
[VertexPointer("uv", 2)]
|
||||
public Vector2 UV;
|
||||
|
||||
[VertexPointer("normal", 3)]
|
||||
public Vector3 Normal;
|
||||
|
||||
public ObjVertex(Vector3 position, Vector2 uv, Vector3 normal)
|
||||
{
|
||||
Position = position;
|
||||
UV = uv;
|
||||
Normal = normal;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ namespace hexworld
|
||||
|
||||
|
||||
public HexRender(int width, int height)
|
||||
: base(width, height, new GraphicsMode(32, 24, 0, 0))
|
||||
: base(width, height, new GraphicsMode(32, 24, 0, 8))
|
||||
{
|
||||
Width = width;
|
||||
Height = Height;
|
||||
|
||||
Reference in New Issue
Block a user