Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Grille committed May 6, 2024
1 parent ca0127d commit 0b7dea2
Show file tree
Hide file tree
Showing 32 changed files with 968 additions and 486 deletions.
13 changes: 12 additions & 1 deletion Assets/Asset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ abstract class Asset
{
public string Name { get; set; } = string.Empty;

public string Description { get; set; } = string.Empty;

public Image Preview { get; set; }

public string SourceFile { get; }

public JsonDictWrapper Object { get; }

public Asset(JsonDictWrapper obj, string source)
{
Object = obj;
SourceFile = source;

if (obj.TryPopValue("preview", out string path))
Expand All @@ -29,14 +34,20 @@ public Asset(JsonDictWrapper obj, string source)
var bitmap = new Bitmap(stream);
Preview = bitmap;
}
catch {
catch
{
Preview = Properties.Resources.InvalidPreview;
}
}
else
{
Preview = Properties.Resources.NoPreview;
}

if (obj.TryPopValue("description", out string desc))
{
Description = desc;
}
}

}
62 changes: 62 additions & 0 deletions Assets/AssetCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LevelTemplateCreator.Assets;

internal class AssetCollection<T> : ICollection<T> where T : Asset
{
Dictionary<string, T> assets = new Dictionary<string, T>();

public int Count => assets.Count;

public bool IsReadOnly => false;

public void Add(T item)
{
assets[item.Name] = item;
}

public void Clear()
{
assets.Clear();
}

public T this[string key] => assets[key];


public bool ConatinsKey(string key) => assets.ContainsKey(key);

public bool TryGetValue(string key, out T value)
{
return assets.TryGetValue(key, out value!);
}

public bool Contains(T item)
{
return assets.ContainsKey(item.Name);
}

public void CopyTo(T[] array, int arrayIndex)
{
assets.Values.CopyTo(array, arrayIndex);
}

public IEnumerator<T> GetEnumerator()
{
return assets.Values.GetEnumerator();
}

public bool Remove(T item)
{
return assets.Remove(item.Name);
}

IEnumerator IEnumerable.GetEnumerator()
{
return assets.Values.GetEnumerator();
}
}
110 changes: 103 additions & 7 deletions Assets/AssetLibary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using LevelTemplateCreator.IO;
using LevelTemplateCreator.IO.Resources;
using LevelTemplateCreator.SceneTree;
using LevelTemplateCreator.SceneTree.Main;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,24 +13,119 @@ namespace LevelTemplateCreator.Assets;

internal class AssetLibary
{
public List<LevelPreset> LevelPresets { get; }
public AssetCollection<LevelPreset> LevelPresets { get; }

public ResourceManager TextureFiles { get; }
public AssetCollection<TerrainPbrMaterialAsset> TerrainMaterials { get; }

public List<TerrainPbrMaterialAsset> TerrainMaterials { get; }
public AssetCollection<ObjectPbrMaterialAsset> ObjectMaterials { get; }

public List<ObjectPbrMaterialAsset> ObjectMaterials { get; }
public AssetCollection<GroundCoverAsset> GroundCoverDefinitions { get; }

public List<GroundCoverAsset> GroundCoverAssets { get; }
public AssetCollection<GroundCoverInstanceAsset> GroundCoverInstances { get; }

public Bitmap? Preview { get; set; }

public int TotalCount => LevelPresets.Count + TerrainMaterials.Count + ObjectMaterials.Count + GroundCoverDefinitions.Count + GroundCoverInstances.Count;

public AssetLibary()
{
LevelPresets = new();
TextureFiles = new();
TerrainMaterials = new();
ObjectMaterials = new();
GroundCoverAssets = new();
GroundCoverDefinitions = new();
GroundCoverInstances = new();
}

public void Add(IEnumerable<Asset> assets)
{
foreach (var asset in assets)
{
Add(asset);
}
}

public void Add(Asset asset)
{
switch (asset)
{
case LevelPreset preset:
LevelPresets.Add(preset);
break;

case TerrainPbrMaterialAsset material:
TerrainMaterials.Add(material);
break;

default:
throw new ArgumentException();
}
}

public void Clear()
{
LevelPresets.Clear();
TerrainMaterials.Clear();
ObjectMaterials.Clear();
GroundCoverDefinitions.Clear();
GroundCoverInstances.Clear();
}

public void SeperateGroundCoverInstances()
{
foreach (var item in GroundCoverDefinitions)
{
AddGroundCoverInstances(GroundCoverInstance.PopFromGroundcover(item.GroundCover), item.SourceFile);
}

foreach (var item in TerrainMaterials) {
AddGroundCoverInstances(GroundCoverInstance.PopFromMaterial(item.Material), item.SourceFile);
}
}

void AddGroundCoverInstances(GroundCoverInstance[] instances, string source)
{
foreach (var item in instances)
{
var asset = new GroundCoverInstanceAsset(item, source);
GroundCoverInstances.Add(asset);
}
}

public void GetGroundCoverInstances(AssetCollection<TerrainPbrMaterialAsset> filter, AssetCollection<GroundCoverInstanceAsset> target)
{
foreach (var item in GroundCoverInstances)
{
if (filter.ConatinsKey(item.Instance.Layer.Value))
{
target.Add(item);
}
}
}

public void CreateGroundCoverObjects(AssetCollection<GroundCoverInstanceAsset> instances)
{

}

public void GetMaterials(AssetCollection<GroundCoverAsset> filter, AssetCollection<ObjectPbrMaterialAsset> target)
{
foreach (var item in GroundCoverInstances)
{
if (filter.ConatinsKey(item.Instance.Layer.Value))
{
//target.Add(item);
}
}
}

public void PrintSumary()
{
Console.WriteLine($"Assets: {TotalCount}");
Console.WriteLine($"- LevelPresets: {LevelPresets.Count}");
Console.WriteLine($"- TerrainMaterials: {TerrainMaterials.Count}");
Console.WriteLine($"- ObjectMaterials: {ObjectMaterials.Count}");
Console.WriteLine($"- GroundCoverDefinitions: {GroundCoverDefinitions.Count}");
Console.WriteLine($"- GroundCoverInstances: {GroundCoverInstances.Count}");
Console.WriteLine();
}
}
25 changes: 24 additions & 1 deletion Assets/AssetLibaryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void ParseObject(JsonDictWrapper item)
case GroundCoverAsset.ClassName:
{
var groundcover = new GroundCoverAsset(item, currentFile);
Libary.GroundCoverAssets.Add(groundcover);
Libary.GroundCoverDefinitions.Add(groundcover);
break;
}
default:
Expand All @@ -130,4 +130,27 @@ void ParseArray(JsonDictWrapper array)
ParseObject(si);
}
}

public void PrintErrors()
{
if (Errors.Count == 0)
return;

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{Errors.Count} errors while loading assets.");
Console.ForegroundColor = ConsoleColor.Gray;

Console.WriteLine();

for (int i = 0; i < Errors.Count; i++)
{
var error = Errors[i];

Console.WriteLine($"At {error.File}");
Console.WriteLine(error.Exception.Message);

Console.WriteLine();
}

}
}
9 changes: 7 additions & 2 deletions Assets/GroundCoverAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ internal class GroundCoverAsset : Asset
{
public const string ClassName = "GroundCover";

public GroundCoverDefinition Definition { get; }
public GroundCover GroundCover { get; }

public GroundCoverAsset(JsonDictWrapper item, string file) : base(item, file)
{
Definition = new GroundCoverDefinition(item.Dict);
GroundCover = new GroundCover(item.Dict);
}

public void CreateNew(AssetCollection<GroundCoverInstanceAsset> instances)
{

}
}
19 changes: 19 additions & 0 deletions Assets/GroundCoverInstanceAsset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using LevelTemplateCreator.SceneTree;
using LevelTemplateCreator.SceneTree.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LevelTemplateCreator.Assets;

internal class GroundCoverInstanceAsset : Asset
{
public GroundCoverInstance Instance { get; }

public GroundCoverInstanceAsset(JsonDictWrapper obj, string source) : base(obj, source)
{
Instance = new GroundCoverInstance(obj.Dict);
}
}
File renamed without changes.
19 changes: 7 additions & 12 deletions Assets/TerrainPbrMaterialAsset.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LevelTemplateCreator.SceneTree;
using LevelTemplateCreator.IO.Resources;
using LevelTemplateCreator.SceneTree;
using LevelTemplateCreator.SceneTree.Art;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -31,17 +32,11 @@ internal class TerrainPbrMaterialAsset : MaterialAsset

Name = Material.InternalName.Value;

/*
if (material.TryPopValue("groundcover", out Dictionary<string, object>[] groundcover))
{
}
*/

SetLayerIfEmpty(Material.AmbientOcclusion.Base, "#ffffff");
SetLayerIfEmpty(Material.Normal.Base, "#7f7fff");
SetLayerIfEmpty(Material.Height.Base, "#000000");
SetLayerIfEmpty(Material.BaseColor.Base, "#515151");
SetLayerIfEmpty(Material.AmbientOcclusion.Base, SolidColorNames.BaseAmbientOcclusion);
SetLayerIfEmpty(Material.Normal.Base, SolidColorNames.BaseNormal);
SetLayerIfEmpty(Material.Height.Base, SolidColorNames.BaseHeight);
SetLayerIfEmpty(Material.BaseColor.Base, SolidColorNames.BaseColor);
SetLayerIfEmpty(Material.Roughness.Base, SolidColorNames.BaseRoughness);
}

void SetLayerIfEmpty(TerrainMaterialTextureLayer layer, string value)
Expand Down
5 changes: 5 additions & 0 deletions EnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,9 @@ static public void Save()
using var stream = new FileStream(ConfigFilePath, FileMode.Create);
JsonDictSerializer.Serialize(stream, dict, true);
}

public static void PrintSumary()
{
Console.WriteLine();
}
}
Loading

0 comments on commit 0b7dea2

Please sign in to comment.