Skip to content

Instantly share code, notes, and snippets.

@MrMjauh
Created November 25, 2019 22:19
Show Gist options
  • Save MrMjauh/02edb8a7f70365d1863c423277eaa0cf to your computer and use it in GitHub Desktop.
Save MrMjauh/02edb8a7f70365d1863c423277eaa0cf to your computer and use it in GitHub Desktop.
Asset
public class Transform
{
private ulong instanceId = ulong.MaxValue;
public Transform(ulong InstanceId)
{
this.instanceId = InstanceId;
}
public Transform()
{
}
public bool IsInitiated()
{
return instanceId != ulong.MaxValue;
}
public void SetInstanceId(ulong instanceId)
{
this.instanceId = instanceId;
}
public Vector3 GetPosition()
{
if (this.IsInitiated())
{
// return CellboxEngine.GetEntityPosition(InstanceId);
}
return Vector3.Zero;
}
}
public abstract class Component
{
}
public class VoxelDataComponent : Component
{
public string Path { get; set; }
}
public abstract class BaseEntity
{
private Transform transform;
private ulong instanceId;
private Dictionary<Type, Component> Components { get; set; } = new Dictionary<Type, Component>();
public BaseEntity()
{
transform = new Transform();
}
public bool IsInitiated()
{
return this.instanceId != ulong.MaxValue;
}
public Dictionary<Type, Component> GetComponents()
{
return Components;
}
public abstract bool SupportsComponent(Component component);
public void SetInstanceId(ulong instanceId)
{
this.instanceId = instanceId;
this.transform.SetInstanceId(instanceId);
}
public Transform GetTransform()
{
return transform;
}
public void AddComponent(Component component) {
if (this.SupportsComponent(component))
{
Components.Add(component.GetType(), component);
} else
{
throw new DoesNotSupportComponentException();
}
}
public T GetComponent<T>() where T : Component
{
if (Components.ContainsKey(typeof(T)))
{
return (T) Components[typeof(T)];
}
return null;
}
public class DoesNotSupportComponentException : Exception { }
// Functions we can override
public virtual void OnInit()
{
}
}
public class BaseConfig
{
public Vector3 Pos { get; set; }
public Vector3 Rot { get; set; }
public Vector3 Scale { get; set; }
}
public class Asset<T> : BaseEntity where T : BaseConfig
{
private T config;
public override bool SupportsComponent(Component component)
{
return true;
}
public void ApplyConfig(T config)
{
this.config = config;
}
public T GetConfig()
{
return config;
}
}
public class MyCoolVoxelAsset : Asset<BaseConfig>
{
public override void OnInit()
{
VoxelDataComponent voxelDataComponent = new VoxelDataComponent();
voxelDataComponent.Path = "/cool/path/vox";
this.AddComponent(voxelDataComponent);
}
}
public class Instansiator
{
public void InstansiateAsset(Asset<BaseConfig> asset)
{
ulong instanceId = 12;
BaseConfig config = null;
asset.ApplyConfig(config);
// If we can get a transform right away
// instanceId = Cellbox.Instansiate(..., config)
// Set pos things right away
asset.SetInstanceId(instanceId);
// We want to do init after positions are set and we have an instanceId
// So we can query for position and so on
asset.OnInit();
foreach (KeyValuePair<Type, Component> entry in asset.GetComponents())
{
if (entry.Value.GetType() is VoxelDataComponent)
{
// Add render template to object
// Render template instansation
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment