Skip to content

Commit

Permalink
ECS & Networking Refactoring (#170)
Browse files Browse the repository at this point in the history
* Implements CVar

* Fixes a few things that didnt work

* Adds NetVar

* NetVar framework

* Some messy netvars

* Adds the ability to queue arbitrary actions to run on a system thread

This allows arbitrary actions to be called on a system without the requirement of a signal. This means that we can set the value of a variable on the thread which has ownership of that value.
Note that these will not be networked and cannot be networked unlike signals.
Hopefully this can be abstracted away when we introduce proper CVars and thread ownership management.

* Contents no longer uses this module which doesn't exist

* Cvars

* Implements world to act as a container for static instances

* Implements entity system setup

* Implements processing systems

* Implements world specific events, down to 8 failing tests

* Fixes a failing test case

* Optimises signal call from 30000 => 7000000 (Still only 1% of just calling methods though)

* Fixes network initialisation order issues

* Fixes processing systems constantly firing if control was relinquished

* Fixes entity systems becoming deadlocked

* Processing systems

* Fixes entity system processing

* Disables processing system debug

* Adds in a rendering scaling mode, fixes button test

* Update ExampleRenderCore.cs

* Update NetCVar.cs

* Fixes CVAR network communications. Deletes automap

* Render Core Depth Output

* Adds the ability to fetch the depth texture and render texture from render cores

* Implements proper depth drawing across render core layers

* Removes the depth program

* Fixes the tests not compiling

* Update RenderCore.cs

* Update NetworkingTest.cs
  • Loading branch information
PowerfulBacon authored Sep 13, 2023
1 parent eb22330 commit e8efb77
Show file tree
Hide file tree
Showing 206 changed files with 3,584 additions and 1,031 deletions.
10 changes: 5 additions & 5 deletions AutoMap/AutoMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class AutoMap
[UsingDependency]
private static IIsometricCameraFactory isometricCameraFactory;

[UsingDependency]
private static IEntityFactory EntityFactory;
//[UsingDependency]
//private static IEntityFactory EntityFactory;

public static void Main(string[] args)
{
Expand All @@ -35,11 +35,11 @@ public static void Main(string[] args)
IIsometricCamera camera = isometricCameraFactory.CreateCamera();

//Create the entity to hold and move the camera
EntityFactory.CreateEmptyEntity((mainCameraEntity) => {
mainCameraEntity.AddComponent(new TransformComponent());
//EntityFactory.CreateEmptyEntity((mainCameraEntity) => {
// mainCameraEntity.AddComponent(new TransformComponent());
//mainCameraEntity.AddComponent(new PlayerMovementComponent());
//mainCameraEntity.AddComponent(new CameraComponent(camera));
});
//});

//Set the main camera
CorgEngMain.SetMainCamera(camera);
Expand Down
5 changes: 5 additions & 0 deletions AutoMap/Rendering/AutoMapRenderCore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CorgEng.Core;
using CorgEng.Core.Dependencies;
using CorgEng.Core.Rendering;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using CorgEng.GenericInterfaces.Rendering.Renderers.SpriteRendering;
using System;
using System.Collections.Generic;
Expand All @@ -18,6 +19,10 @@ public class AutoMapRenderCore : RenderCore

private ISpriteRenderer spriteRenderer;

public AutoMapRenderCore(IWorld world) : base(world)
{
}

public override void Initialize()
{
spriteRenderer = SpriteRendererFactory.CreateSpriteRenderer(0);
Expand Down
9 changes: 5 additions & 4 deletions CorgEng.AiBehaviour/BehaviourManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace CorgEng.AiBehaviour
{
internal class BehaviourManager : IBehaviourManager
internal class BehaviourManager : WorldObject, IBehaviourManager
{

//Are we currently processing? Prevents AIs from having multiple AI trees thinking
Expand All @@ -22,7 +22,7 @@ internal class BehaviourManager : IBehaviourManager
private TransformComponent _transform;


public IVector<float> Position => _transform.Position;
public IVector<float> Position => _transform.Position.Value;

/// <summary>
/// The pawn entity we are attached to
Expand Down Expand Up @@ -57,15 +57,16 @@ public IEntity PawnEntity
/// <summary>
/// The root behaviour node
/// </summary>
internal BehaviourRoot root = new BehaviourRoot();
internal BehaviourRoot root;

/// <summary>
/// Setup the behaviour manager
/// </summary>
/// <param name="pawnEntity"></param>
public BehaviourManager(IEntity pawnEntity)
public BehaviourManager(IWorld world, IEntity pawnEntity) : base(world)
{
PawnEntity = pawnEntity;
root = new BehaviourRoot(world);
}

public async Task Process()
Expand Down
5 changes: 3 additions & 2 deletions CorgEng.AiBehaviour/BehaviourNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using CorgEng.Core.Dependencies;
using CorgEng.GenericInterfaces.AiBehaviours;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using CorgEng.GenericInterfaces.Logging;
using CorgEng.GenericInterfaces.UtilityTypes.BinaryLists;
using System;
Expand All @@ -12,7 +13,7 @@

namespace CorgEng.AiBehaviour
{
public abstract class BehaviourNode : IBehaviourNode
public abstract class BehaviourNode : WorldObject, IBehaviourNode
{

[UsingDependency]
Expand All @@ -27,7 +28,7 @@ public abstract class BehaviourNode : IBehaviourNode

public abstract BehaviourContinuationMode ContinuationMode { get; }

public BehaviourNode()
public BehaviourNode(IWorld world) : base(world)
{
Subtasks = BinaryListFactory.CreateEmpty<IBehaviourNode>();
}
Expand Down
4 changes: 4 additions & 0 deletions CorgEng.AiBehaviour/BehaviourRoot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CorgEng.GenericInterfaces.AiBehaviours;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,6 +10,9 @@ namespace CorgEng.AiBehaviour
{
internal class BehaviourRoot : BehaviourNode
{
public BehaviourRoot(IWorld world) : base(world)
{
}

public override BehaviourContinuationMode ContinuationMode => BehaviourContinuationMode.CANCEL_ON_FAIL;

Expand Down
4 changes: 2 additions & 2 deletions CorgEng.AiBehaviour/Factories/BehaviourManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace CorgEng.AiBehaviour.Factories
internal class BehaviourManagerFactory : IBehaviourManagerFactory
{

public IBehaviourManager CreateBehaviourManager(IEntity attachedPawn, params IBehaviourNode[] behaviourNodes)
public IBehaviourManager CreateBehaviourManager(IWorld world, IEntity attachedPawn, params IBehaviourNode[] behaviourNodes)
{
BehaviourManager createdManager = new BehaviourManager(attachedPawn);
BehaviourManager createdManager = new BehaviourManager(world, attachedPawn);

int i = 0;
foreach (IBehaviourNode behaviourNode in behaviourNodes)
Expand Down
2 changes: 1 addition & 1 deletion CorgEng.AiBehaviour/Systems/AiActionProcessingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class AiActionProcessingSystem : ProcessingSystem

protected override int ProcessDelay => 100;

public override void SystemSetup()
public override void SystemSetup(IWorld world)
{
RegisterLocalEvent<AiBehaviourComponent, ComponentAddedEvent>(OnComponentAdded);
}
Expand Down
2 changes: 1 addition & 1 deletion CorgEng.AiBehaviour/Systems/BehaviourProcessingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class BehaviourProcessingSystem : ProcessingSystem

protected override int ProcessDelay => 1000;

public override void SystemSetup()
public override void SystemSetup(IWorld world)
{
RegisterLocalEvent<AiBehaviourComponent, ComponentAddedEvent>(OnComponentAdded);
}
Expand Down
4 changes: 2 additions & 2 deletions CorgEng.Audio/Systems/AudioListenerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class AudioListenerSystem : EntitySystem

public override EntitySystemFlags SystemFlags => EntitySystemFlags.CLIENT_SYSTEM;

public override void SystemSetup()
public override void SystemSetup(IWorld world)
{
RegisterLocalEvent<AudioListenerComponent, MoveEvent>(AudioListenerMoved);
RegisterLocalEvent<AudioListenerComponent, InitialiseEvent>(AudioListenerInitialised);
Expand All @@ -32,7 +32,7 @@ private void AudioListenerMoved(IEntity entity, AudioListenerComponent audioList

private void AudioListenerInitialised(IEntity entity, AudioListenerComponent audioListenerComponent, InitialiseEvent initialiseEvent)
{
IVector<float> position = audioListenerComponent.Transform.Position;
IVector<float> position = audioListenerComponent.Transform.Position.Value;
AudioMaster.UpdateListener(position.X, position.Y, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion CorgEng.Claims/Systems/ClaimSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ClaimSystem : EntitySystem

public override EntitySystemFlags SystemFlags => EntitySystemFlags.HOST_SYSTEM;

public override void SystemSetup()
public override void SystemSetup(IWorld world)
{
//Deletable is common to all entities, so register to that component.
RegisterLocalEvent<DeleteableComponent, RequestClaimEvent>(HandleClaimRequest);
Expand Down
5 changes: 3 additions & 2 deletions CorgEng.ContentLoading/DefinitionNodes/ArrayNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CorgEng.GenericInterfaces.ContentLoading.DefinitionNodes;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -26,15 +27,15 @@ public override void ParseSelf(XmlNode node)
arrayType = EntityLoader.TypePaths[node.Attributes["type"].Value];
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
//Create the array object
Array createdArray = Array.CreateInstance(arrayType, Children.Count);
//Populate the array
int i = 0;
foreach (DefinitionNode child in Children)
{
createdArray.SetValue(child.CreateInstance(createdArray, instanceRefs), i++);
createdArray.SetValue(child.CreateInstance(world, createdArray, instanceRefs), i++);
}
//Add a reference to the created array
if (Key != null)
Expand Down
4 changes: 2 additions & 2 deletions CorgEng.ContentLoading/DefinitionNodes/ComponentNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public ComponentNode(DefinitionNode parent) : base(parent)
Parent = parent as EntityNode;
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
//Create the component
IComponent createdComponent = base.CreateInstance(parent, instanceRefs) as IComponent;
IComponent createdComponent = base.CreateInstance(world, parent, instanceRefs) as IComponent;
//Add the compoennt
IEntity parentEntity = parent as IEntity;
parentEntity.AddComponent(createdComponent);
Expand Down
8 changes: 6 additions & 2 deletions CorgEng.ContentLoading/DefinitionNodes/DefinitionNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -16,8 +17,11 @@ internal abstract class DefinitionNode

public string? ID { get; set; }

protected IWorld world;

public DefinitionNode(DefinitionNode parent)
{
this.world = world;
parent?.Children.Add(this);
}

Expand All @@ -36,7 +40,7 @@ public virtual void ParseSelf(XmlNode node)
/// Called when the instance needs to be created
/// </summary>
/// <returns></returns>
public abstract object CreateInstance(object parent, Dictionary<string, object> instanceRefs);
public abstract object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs);

}
}
5 changes: 3 additions & 2 deletions CorgEng.ContentLoading/DefinitionNodes/DependencyNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CorgEng.Core.Dependencies;
using CorgEng.GenericInterfaces.ContentLoading.DefinitionNodes;
using CorgEng.GenericInterfaces.DependencyInjection;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -85,7 +86,7 @@ public override void ParseSelf(XmlNode node)
}
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
//Set the parameters
if (hasDynamicParams)
Expand All @@ -94,7 +95,7 @@ public override object CreateInstance(object parent, Dictionary<string, object>
{
if (isDynamic[i])
{
parameters[i] = Children[i].CreateInstance(parent, instanceRefs);
parameters[i] = Children[i].CreateInstance(world, parent, instanceRefs);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions CorgEng.ContentLoading/DefinitionNodes/DictionaryNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CorgEng.GenericInterfaces.ContentLoading.DefinitionNodes;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -41,15 +42,15 @@ public override void ParseSelf(XmlNode node)
BindingFlags.Public | BindingFlags.Instance);
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
//Create the array object
object dictionary = Activator.CreateInstance(dictionaryType);
//Populate the dictionary
foreach (DefinitionNode child in Children)
{
addMethod.Invoke(dictionary, new object[] {
child.CreateInstance(dictionary, instanceRefs)
child.CreateInstance(world, dictionary, instanceRefs)
});
}
//Add a reference to the created array
Expand Down
13 changes: 7 additions & 6 deletions CorgEng.ContentLoading/DefinitionNodes/ElementNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CorgEng.GenericInterfaces.ContentLoading.DefinitionNodes;
using CorgEng.GenericInterfaces.EntityComponentSystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -70,9 +71,9 @@ protected void ParseValue(XmlNode node)
}
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
object propertyValue = GetValue(parent, instanceRefs);
object propertyValue = GetValue(world, parent, instanceRefs);
if (Key != null)
{
instanceRefs.Add(Key, propertyValue);
Expand All @@ -82,7 +83,7 @@ public override object CreateInstance(object parent, Dictionary<string, object>

private static MethodInfo cachedMethod = null;

private object GetValue(object parent, Dictionary<string, object> instanceRefs)
private object GetValue(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
//Has 2 children, key value pair
if (Children.Count == 2)
Expand All @@ -99,8 +100,8 @@ private object GetValue(object parent, Dictionary<string, object> instanceRefs)
}
//Make the generic method and call it
return cachedMethod.MakeGenericMethod(dictionaryParentKeyType, dictionaryParentValueType).Invoke(null, new object[] {
childKey.CreateInstance(parent, instanceRefs),
childValue.CreateInstance(parent, instanceRefs)
childKey.CreateInstance(world, parent, instanceRefs),
childValue.CreateInstance(world, parent, instanceRefs)
});

}
Expand All @@ -119,7 +120,7 @@ private object GetValue(object parent, Dictionary<string, object> instanceRefs)
}
//1 Child, return value
// Either object or value node
return Children[0].CreateInstance(null, instanceRefs);
return Children[0].CreateInstance(world, null, instanceRefs);
}

}
Expand Down
17 changes: 7 additions & 10 deletions CorgEng.ContentLoading/DefinitionNodes/EntityNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace CorgEng.GenericInterfaces.ContentLoading.DefinitionNodes
internal class EntityNode : DefinitionNode, IEntityDefinition
{

[UsingDependency]
public static IEntityFactory EntityFactory;

public bool Abstract { get; set; } = false;

/// <summary>
Expand Down Expand Up @@ -50,19 +47,19 @@ public override void ParseSelf(XmlNode node)
/// Create an entity from this node
/// </summary>
/// <returns></returns>
public IEntity CreateEntity(Action<IEntity> setupAction)
public IEntity CreateEntity(IWorld world, Action<IEntity> setupAction)
{
return (IEntity)CreateInstance(null, new Dictionary<string, object>(), setupAction);
return (IEntity)CreateInstance(world, null, new Dictionary<string, object>(), setupAction);
}

public override object CreateInstance(object parent, Dictionary<string, object> instanceRefs)
public override object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs)
{
return CreateInstance(parent, instanceRefs, null);
return CreateInstance(world, parent, instanceRefs, null);
}

public object CreateInstance(object parent, Dictionary<string, object> instanceRefs, Action<IEntity> setupAction)
public object CreateInstance(IWorld world, object parent, Dictionary<string, object> instanceRefs, Action<IEntity> setupAction)
{
IEntity createdEntity = EntityFactory.CreateUninitialisedEntity();
IEntity createdEntity = world.EntityManager.CreateUninitialisedEntity();
//Store the key
if (Key != null)
{
Expand All @@ -71,7 +68,7 @@ public object CreateInstance(object parent, Dictionary<string, object> instanceR
//Add on properties
foreach (DefinitionNode childNode in Children)
{
childNode.CreateInstance(createdEntity, instanceRefs);
childNode.CreateInstance(world, createdEntity, instanceRefs);
}
//Run init event
setupAction?.Invoke(createdEntity);
Expand Down
Loading

0 comments on commit e8efb77

Please sign in to comment.