Felix Boucher ef8a70aba4 finish to hijack update loop + serialization
problem : tiles dont have an update loop, neither do they have a start / destroy method

solution : finished working on a way to create a custom start/update/destroy pipeline for the projects custom tiles. it's using LevelManagerScript as to Update.

note : also created a database and a start of serialization system so we can save and load stuff.
2023-05-27 20:38:42 -04:00

45 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using BindingFlags = System.Reflection.BindingFlags;
using UnityEngine;
public static class Extensions
{
public enum LevelObjectType { GameObject, Tile }
public static bool Approximately(this Vector3 vect, Vector3 other)
{
return Mathf.Approximately(Vector3.Distance(vect, other), 0);
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class LevelSerializeAttribute : Attribute { }
/// <summary>
/// turns an object into a serializable dictionary
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static Dictionary<string, object> ToDictionary<T>(this T obj) where T : ILevelObject
{
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var toReturn = new Dictionary<string, object>();
var type = obj.GetType();
foreach (var field in type.GetFields(flags))
{
if (!Attribute.IsDefined(field, typeof(LevelSerializeAttribute))) continue;
toReturn[field.Name] = field.GetValue(obj);
}
foreach (var property in type.GetProperties(flags))
{
if (!Attribute.IsDefined(property, typeof(LevelSerializeAttribute))) continue;
toReturn[property.Name] = property.GetValue(obj);
}
if (obj is LevelObject) toReturn[nameof(LevelObjectType)] = LevelObjectType.GameObject;
else toReturn[nameof(LevelObjectType)] = LevelObjectType.Tile;
return toReturn;
}
}