ecs and tranforms
added the moonworks ecs library. testing it by setting up a transform and parent system.
This commit is contained in:
62
Nerfed.Runtime/Systems/ParentSystem.cs
Normal file
62
Nerfed.Runtime/Systems/ParentSystem.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
|
||||
namespace Nerfed.Runtime.Systems
|
||||
{
|
||||
public class ParentSystem : MoonTools.ECS.System
|
||||
{
|
||||
private readonly Filter parentsAddedFilter;
|
||||
private readonly Filter parentsRemovedFilter;
|
||||
private readonly Filter parentsFilter;
|
||||
|
||||
public ParentSystem(World world) : base(world)
|
||||
{
|
||||
parentsAddedFilter = FilterBuilder.Include<Parent>().Exclude<PreviousParent>().Build();
|
||||
parentsRemovedFilter = FilterBuilder.Include<PreviousParent>().Exclude<Parent>().Build();
|
||||
parentsFilter = FilterBuilder.Include<Parent>().Include<PreviousParent>().Build();
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
// Update removed parents.
|
||||
foreach (Entity entity in parentsRemovedFilter.Entities)
|
||||
{
|
||||
// Do stuff here to update/remove child relations etc.
|
||||
PreviousParent previousParent = Get<PreviousParent>(entity);
|
||||
World.Unrelate<ChildRelation>(previousParent.parentEntity, entity);
|
||||
Remove<PreviousParent>(entity);
|
||||
}
|
||||
|
||||
// Update added parents.
|
||||
foreach (Entity entity in parentsAddedFilter.Entities)
|
||||
{
|
||||
Parent parent = Get<Parent>(entity);
|
||||
|
||||
if (Has<Parent>(parent.parentEntity) && Get<Parent>(parent.parentEntity).parentEntity == entity)
|
||||
{
|
||||
Log.Warning($"Entity {entity} cannot be a parent of entity {parent.parentEntity}, because {parent.parentEntity} is the parent of {entity}");
|
||||
Remove<Parent>(entity);
|
||||
continue;
|
||||
}
|
||||
|
||||
PreviousParent previousParent = new(parent.parentEntity);
|
||||
Set(entity, previousParent);
|
||||
World.Relate(parent.parentEntity, entity, new ChildRelation());
|
||||
}
|
||||
|
||||
// Update relations if the parent has changed.
|
||||
foreach (Entity entity in parentsFilter.Entities)
|
||||
{
|
||||
Parent parent = Get<Parent>(entity);
|
||||
PreviousParent previousParent = Get<PreviousParent>(entity);
|
||||
|
||||
if(parent.parentEntity != previousParent.parentEntity)
|
||||
{
|
||||
World.Unrelate<ChildRelation>(previousParent.parentEntity, entity);
|
||||
Set(entity, new PreviousParent(parent.parentEntity));
|
||||
World.Relate(parent.parentEntity, entity, new ChildRelation());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
47
Nerfed.Runtime/Systems/TransformSystem.cs
Normal file
47
Nerfed.Runtime/Systems/TransformSystem.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using MoonTools.ECS;
|
||||
using Nerfed.Runtime.Components;
|
||||
using Nerfed.Runtime.Util;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Nerfed.Runtime.Systems
|
||||
{
|
||||
public class TransformSystem : MoonTools.ECS.System
|
||||
{
|
||||
private readonly Filter rootEntitiesFilter;
|
||||
|
||||
public TransformSystem(World world) : base(world)
|
||||
{
|
||||
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Parent>().Build();
|
||||
}
|
||||
|
||||
public override void Update(TimeSpan delta)
|
||||
{
|
||||
Matrix4x4 rootMatrix = Matrix4x4.Identity;
|
||||
|
||||
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||
{
|
||||
UpdateWorldTransform(entity, rootMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateWorldTransform(in Entity entity, in Matrix4x4 parentLocalToWorld)
|
||||
{
|
||||
// TODO: Only update dirty transforms.
|
||||
// Maybe store the local transform matrix.
|
||||
// If something is dirty all the children need to update their localToWorld matrix.
|
||||
|
||||
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||
Matrix4x4 localToWorldMatrix = Matrix4x4.Multiply(parentLocalToWorld, localTransform.TRS());
|
||||
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||
Set(entity, localToWorld);
|
||||
|
||||
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||
|
||||
ReverseSpanEnumerator<Entity> childEntities = World.OutRelations<ChildRelation>(entity);
|
||||
foreach (Entity childEntity in childEntities)
|
||||
{
|
||||
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user